KB9IQX AIS Receiver & APRS.fi Reporter using RTL-SDR
I saw that APRS.fi had ship locations on the map! So I wanted to add AIS receive function to my RX-only iGate station. Both AIS and APRS are using an SDR, one for each.
My personal preference is to start with the Pi Zero, so as to have the lowest power Pi thrown at the problem. No sense in using the 3B+ or 4 if you don't have to. The Zero and the 3A+ have much better power management anyway. They are cheaper too. Start there.
Hardware
Raspberry Pi Zero W - TESTED. WORKS.
Raspberry Pi 3A+ - TESTED. WORKS.
Raspberry Pi 3B/3B+ - TESTED. WORKS.
USB RTL-SDR dongle. Any should work, I've used the cheapy cheapest to "better" ones. I've used the Nooelec NESDR Nano 3 RTL-SDR USB - it's tiny! it's metal! it has a heat sink and gets hot! All 4 Pi USB ports can be filled with these. I've used NESDR SMART; it works fatnatically well as well.
an Antenna - wire gets excite gets bzzzzts sends to computer
References
AIS uses 9600 bps GMSK on 161.975 and 162.025 MHz [source].
Other sites with installation instructions that I referenced before wandering off on my own.
This was the key to bridging the decoder part to the send-to-APRS.fi part [source] :
After fiddling around for some days, I found rtl-ais which is a modified version of rtl-sdr, with direct support for AIS decoding. It worked! Just specifying the tuner error, and it was outputting AIS messages.
However, the decoder was not that elegant, and GNUAIS had interesting features, like the ability to feed data directly to mysql.
AIS is transmitted at 161.975 and 162.025MHz, which is within the 2MHz bandwidth of the RTL-SDR sticks, so both channels can be received at once. The rtl_ais software is a modified version of rtl_fm, which can do exactly this. Feeding the output audio to a named pipe, and setting gnuais up to read from that named pipe, instead of audio card, worked just perfectly!
1. Install an image. I used a general Raspbian Stretch (9) because that's what I had lying around on my desktop.
2. Log into or SSH into your machine.
3. Install and build Osmocom RTL-SDR At this other page you will find great,detailed instructions... don't worry that it's ADS-B not AIS, the basic drivers are the same.
4. Install and build DGiardini's RTL-AIS. Whether you installed the libsdr libraries via apt repository (apt-get install librtlsdr-dev librtlsdr0) or you installed the OsmoCom RTL-SDR libraries, the default paths in the Makefile are incorrect. There is some futzing you'll have to do with the Makefile to point libraries to the correct paths changes in red bold. Here is what worked for me:
When this is configured, compile rtl_ais and you'll see messages from the compilation. Type:
When it compiles, install the compiled binary. (typing sudo make -n install will tell you what commands it'll run without actually doing them) To actually install, type:
TESTING rtl_ais
You can test whether your installation is receiving AIS data thsu far. Make sure your SDR is plugged into the USB, attach an antenna, and type:
rtl_ais -l 161.975M -r 162.025M -n
This will run the receiver application on the SDR, tuned to both AIS frequencies. Let it sit for a while and you should received actual AIS messages that look like the following output. The lines begining with AIVDM indicate an actual received packet from an actual boat.
5. Create a named pipe.
More info about named pipes is here. It seems that gnuais can read a file for input but not take raw input from stdin/command line. It is designed to take raw audio input from a soundcard, back when the only way to receive AIS was to tap into the discriminator audio output of a radio. This does not work for SDR.
But!
There is a test feature which allows gnuais to read sound input from a test file. In this case, our "test file" is the actual audio that is being dumped into a file by rtl_ais. But audio can eat up a lot of room when dumped into a file...
So!
we will tell rtl_ais to dump received SDR audio into the named pipe -- basically a zero-length "file" that resides only in memory and which to applications looks like any other file. Finally we will tell gnuais to read the audio from that same file/named pipe.
In short, the named pipe acts as a regular file... but it does not grow in size -- it only grows in memory -- and clog up your file system.
To create the named pipe, choose a path (location) in your system that works for you where you want to put the file. I chose /var/local/dev/aisdata, for example. Type:
The first command creates the named pipe. These second changes the permissions so that any user can write data to it. If you do not change the permissions, then only the superuser will be able to write to the file; in which case you will have to run the rtl_ais command as the superuser. This is strictly your choice.
6. Install and build GNU AIS Make sure you have the curl development libraries (raspberryPiOS: sudo apt-get install libcurl4-openssl-dev); it will build without them but they are needed for gnuais to send AIS position reports to APRS.fi. If you want to log data to a mySQL database, make sure you have the requisites.
7. Configure gnuais.conf
The example configuration file lives in /usr/share/doc/gnuais/examples/gnuais.conf-example. Copy this file to a place that makes sense, e.g. /etc or /usr/local/etc or your user home directory if you are a barbarian. Edit it it as follows:
8. Calibrate your SDR to find the PPM offset. This is a good idea. For the life of me I could not get kalibrate-rtl to compile. So I am just running on luck.
Command Line Operation
rtl_ais -> named pipe -> gnuais
The exciting and magic sauce here is the name pipe sitting between rtl_ais receiving from the SDR and gnuais first decoding the audio and subsequently sending it to aprs.fi. The named pipe looks zero-length but accumulates data in system memory from rtl_ais going in on one side and let's another application take data out of it
testing
In one terminal, run rtl_ais:
rtl_ais -l 161.975M -r 162.025M -A /path/to/filename_of_named_pipe
This command tells rtl_ais to listen to both "left" and "right" AIS frequencies (tho the official spec calls them A and B freqs). The -A turns off the decoding function, instead sending out the undecoded audio from the SDR.
If successful, you will see the output seem to hang forever. But it is actually sending the data to the named pipe instead of the screen. So as long as it is "hanging" it is working.
Here you will see decoded output, including AIVDM NMEA sentences, sailing by.
gnuais is also sending the data to aprs.fi
Pop up the map at https://aprs.fi and click on nearby boats to see if they are reporting via your iGate. Admitedly, the aprs.fi interface is not the best at showing that.
Outstanding Issues
Look into AIS Dispatcher for dispatching AIS data to multiple destinations.
Install GPSd both for ntp time syncing as well as geolocation.
VHF is really full of all sorts of crap, so using a tuned antenna and a band pass filter is really necessary if you want to receive anything else than FM radio on your rtl-sdr dongle. Remember that adding a cheap pre-amp will hardly solve any interference issues, since the pre-amp does not know the difference between what you want to receive and what you don’t. Fortunately, AIS signals are relatively strong and even under poor receiver conditions you should be able to pick up something if you are located near marine traffic. [source].