AIS RESEARCH USING A RASPBERRY PI

Gary C. Kessler
08 November 2019

The Raspberry Pi is a family of low-cost, single-board computers developed by the Raspberry Pi Foundation in the U.K. Since its introduction in 2012, it has emerged as a widely-used platform for embedded system research and project hobbyists. Most users use some form of the Linux operating system on the Raspberry Pi; equipped with several USB ports, quad-core processor, an IEEE 802.3/Ethernet port, IEEE 802.11 WiFi and Bluetooth capability, HDMI connector, audio output, and a general-purpose input/output (GPIO) bus, it is a very powerful tool for research, exploration, teaching, and learning.

There are many documents on the Internet that describe various aspects of using a Raspberry Pi to create a marine Automatic Identification System (AIS) display. While I don't want to repeat all of those sites — and, hopefully, I won't — I wanted to put everything into one place, particularly for other AIS researchers who are looking for a push up the learning curve. I hope that this page accomplishes that goal.

For purposes of this paper, I am assuming that readers understand the fundamentals of AIS. To get a lot more information, check out the USCG Navigation Center's Automatic Identification System Overview, IALA's An Overview of AIS (Edition 2), or the automatic identification system (AIS) article at Wikipedia.


Table of Contents



Hardware

Unless otherwise noted below, no endorsement of hardware or source is intended in the discussion below.


Installing Raspian

Before going any further, I would like you to be aware of an entirely different direction you can take. AISHub offers rPiAIS, an implementation of AIS Dispatcher for the Raspberry Pi. Depending upon what you're trying to do, rPiAIS might be worth looking into.

There are several lightweight Linux (and other) operating systems that can run on the Raspberry Pi. The native OS is Raspian, a variant of Debian. The best source for all of these is the Raspbery Pi Foundation's Downloads page. For starters, my recommendation is to download the New Out Of the Box Software (NOOBS) version.

  1. Download the NOOBS ZIP file, unZIP it, and extract the contents to a NOOBS directory your computer.
  2. Format the microSD card with the FAT32 file system. (NOTE: If you have a Mac and an SD card 64 GB or greater in size, use the Erase tool in Disk Utility to format the microSD card as MS-DOS (FAT).)
  3. To ensure that the SSH service can be used to access the Raspbery Pi (and I think that you should!), a small or empty text file named ssh has to be present in the boot partition. If such a file is not already present, create it and copy to the /boot subdirectory in the NOOBS directory on your computer.
  4. Copy the contents of your NOOBS directory onto the SD card.


  5. Insert the microSD card into the Raspberry Pi's SD card slot, attach your I/O devices (i.e., keyboard/mouse/monitor), and power it on.
  6. A boot screen will appear. Select the option to install full Raspian desktop and let it do its magic! During this period of time, you will be asked for a password and some other pretty standard configuration information, and then asked to reboot.

For additional information, see the Raspberry Pi Software Guide page.


Configuring the Raspberry Pi

This is where you make the Raspberry Pi your own! Enter the configuration menu by clicking on the Raspberry icon in the upper left of the screen, select Preferences, and then select Raspbery Pi Configuration:

You can perform the same functions from the command line by opening a terminal window and entering:

     sudo raspi-config

Additional information on using the command line configuration utility can be found at the Raspberry Pi Foundation's raspi-config page.

Once you've set a WiFi country, the Raspberry Pi should automatically find any available WiFi network that is broadcasting its SSID if a network cable is not physically attached. If you want to connect to any WiFi network that doesn't broadcast an SSID, you will need to edit the /etc/wpa_supplicant/wpa_supplicant.conf file in order to add the hidden network information.

As an example — suppose you have a non-broadcast SSID "Guest" network that requires no password and a non-broadcast SSID "Owner" network with the password "Secret" (using WPA). Open the configuration file by entering the following on the command line (you need to use sudo here because this is a system file):

     sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Now, add the following lines to the config file:

   network={
           scan_ssid=1
	   ssid="Guest"
	   key_mgmt=NONE}
   network={
           scan_ssid=1
           ssid="Owner"
           psk="Secret"
           key_mgmt=WPA-PSK}

After adding the lines, press control-O (^O) to write the file and ^X to exit. The Raspi will now see the hidden networks as well as any openly advertised networks.

NOTE: Raspian comes with several easy-to-use text editors. nano, referenced above, is a nice command line editor. A GUI text editor can be found by clicking the Raspberry icon, Accessories, and Text Editor.

Finally, a note about screen resolution. If you access the Raspberry Pi strictly via SSH or VNC, it is referred to as a headless connection. Regardless of whether you access remotely or output to an HDMI monitor, if you don't like the screen resolution, you can always change it by opening the /boot/config.txt file with your favorite text editor (be sure to use sudo). Scroll down to find these lines:

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

Uncomment the two framebuffer commands by removing the leading hash (#). Save the file, reboot, and your screen resolution will be different! You might need to experiment around a bit to find the right size for your monitor.


Installing dAISy Hat

Installing the dAISy Hat hardware is very straight-forward. You'll probably want to download the dAISy HAT AIS Receiver Manual for additional reference.

If you have played with computer hardware before, then installing the dAISy Hat daughterboard to the Raspberry Pi's GPIO pins won't be a problem. If you haven't done this before, all I can say is to work slowly, align the socket on the board onto the Raspberry Pi's pins carefully, and press the cards together carefully.

After installing the dAISy Hat, open up a terminal window and enter the following commands in order to 1) ensure that you are in your home directory, 2) download the uart_control program from GitHub into your home directory, 3) change permissions to make it easily executable, 4) install the gpio command, and 5) reboot the Raspberry Pi:

     cd ~
     wget https://github.com/itemir/rpi_boat_utils/raw/master/uart_control/uart_control
     chmod 755 ./uart_control
     sudo ./uart_control gpio
     sudo reboot now

Now that gpio is installed, you can find the status of the GPIO pins by using the command:

     gpio readall

To learn about the other GPIO command options, use:

     man gpio

Installing screen

screen is a terminal emulator with which you can communicate directly with the serial port. This provides a mechanism with which to control and manage the dAISy Hat hardware. screen is not native to Raspian, so you will need to install it by issuing the following command:

     sudo apt-get install screen

The dAISy Hat should be attached to Serial 0 port; under Raspian, note that /dev/serial0 is an alias for /dev/ttyS0. To connect to the hardware, type:

     screen /dev/serial0 38400

Press <ESC> and you should see a display such as the following:

Note that if you press T, you will enter a mode where you can send AIS test messages (as NMEA sentences):

If you press <ENTER>, the software will transmit the Type 4 message shown here every five seconds. Pressing <ESC> stops the transmission of test messages; pressing <ESC> again returns the device to receive mode.

You can exit screen by entering command mode (^A), then pressing K (kill process) followed by Y (yes). To obtain a complete list of screen commands and options, type:

     man screen

OpenCPN

OpenCPN is free, open source chartplotter and maritime GPS navigation software that can run on Android, Fedora, Linux, Mac OS X, Orange Pi, Raspberry Pi, Ubuntu, and Windows systems — it seems about every platform except iOS. While designed to be used while underway or for planning purposes, it also provides a useful tool for AIS projects.

To learn more about OpenCPN, visit OpenCPN.org, the source of all things OpenCPN! In particular, there are a wide variety of manuals to be found at that site, including:

The steps outlined below should work on Raspian version 9 (aka stretch). If you are not sure what version of Raspain or other Linux distro you are using, use the following command:

     cat /etc/os-release

Prepare for the OpenCPN installation by creating an opencpn directory under your home directory:

     mkdir ~/opencpn
     cd ~/opencpn

PGP Key Management

The first step is to manually create a Pretty Good Privacy (PGP) keyfile in order to download and install OpenCPN. This process will ensure that you have the latest, most current key.

  1. Point your browser to the OpenPGP Public Key Server.

  2. A new Web page, similar to the one below, will open. Click on the C865EB40 link provided in the pub section.

  3. This should take you to page similar to the one below that contains the key.

  4. Open a text file called opencpn_key (or, use another name if you want). Copy the Web page text starting from "-----BEGIN PGP PUBLIC KEY BLOCK-----" to the end of the page, and paste it into the opencpn_key file; save and close opencpn_key.

Download and Install OpenCPN

Start the OpenCPN installation by typing:

     sudo nano /etc/apt/sources.list

A text editor will open. Move the cursor to the end of the file and add this line (assuming that you are using Raspbian Stretch):

     deb http://ppa.launchpad.net/opencpn/opencpn/ubuntu/ xenial main

Save the updates and close the file.

Now, install the PGP key, get updated software sources, and install OpenCPN:

     sudo apt-key add opencpn_key
     sudo apt-get update
     sudo apt-get install opencpn

There may be some error messages; read them and document any additional commands that you need to execute.

Run and Configure OpenCPN

Run OpenCPN by typing the command:

     opencpn

or by clicking the Application (Raspberry) icon, Education, OpenCPN from the GUI.

Once into OpenCPN, configure your data source:

  1. Click on Options (the crescent wrench icon)
  2. Click on Connections
  3. Scroll down and click the Add Connection button
  4. Under Properties:




Under Connections, you can check the Show NMEA Debug Window box to see the actual NMEA sentences (screen shots below for the serial and network connections, respectively):


Note that when using OpenCPN with a network data source, the raw data feed can be captured directly using the netcat (nc) or telnet command, in the form:

     nc SOURCE_IP_ADDRESS PORT
     telnet SOURCE_IP_ADDRESS PORT


OpenCPN can also be used as a data server and create a data stream from its input. This is particularly useful if you are reading from a serial interface; OpenCPN can read from serial0, for example, and produce a TCP data stream. Setting this up is very straight-forward:


At this point, you have two open connections, and OpenCPN is accepting input from whatever source you had been using and sending output on your desired TCP port.




Read your raw data stream, as above, from a terminal or any other application using netcat or telnet.


Finally... one of the problems that is immediately apparent from the raw output stream is that AIS lacks timestamp information. While OpenCPN displays a timestamp in the Debug Window, that is not part of the AIS message and, therefore, not a part of the raw data stream that can be captured. To remedy this, I wrote a small Perl program called timestamp_data that can connect to a TCP or UDP socket on a local or remote host, and read a data stream for a specified amount of time. Output is directed to a "|"-delimited file along with a timestamp of when the data was received. Users can, optionally, also see the data in real-time. The program can be found on my software page.




SIDENOTE: Sending data to OpenCPN

If you have a file filled with AIS messages, it is straight-forward to send that data to OpenCPN for display. First, you need to set up OpenCPN to listen on a TCP port rather than send. Just create a new OpenCPN connection on the port of your choice to address 0.0.0.0, as shown above, but check the box to "Receive input on this port."

There are a variety of methods with which to set the data to OpenCPN at this point. You could just transmit the entire file via netcat:

     cat FILENAME | nc localhost PORT

Using the method above will work but all it does is dump the entire AIS message stream to OpenCPN at one time so you just see the last sentence transmitted by each vessel. If you want to slow things down a little, add a delay between the display of each line in the data file using some shell scripting:

     cat FILENAME | { while read line; do sleep 2; echo "$line" | nc localhost PORT ; done; }
     cat FILENAME | { while read line; do sleep 1; echo "$line" > /dev/tcp/localhost/PORT ; done; }

Again, the method above works but it puts in a fixed delay. If you used a tool that puts a timestamp on your data, or you have otherwise created data so that you have a timestamp on each message (e.g., you got the data using the timestamp_data tool above), you can replay the data on OpenCPN is relative real-time using another one of my tools, play_ais. This program only needs a character-delimited file where each record has a timestamp (in seconds) and valid AIS sentence; a short video about using this approach can be found on my Web page (NOTE: The program was called replay at the time that I made the video).

Another tool that is quite useful for AIS research is the NMEASimulator, an easy-to-use NMEA sentence generator for messages related to position, speed, and heading, among other items. This software is an app that runs only on the Google Chrome browser.

To send information to OpenCPN, you need the IP address and listening port. Enter this information into the NMEA Simulator by clicking on Options, Settings and selecting the Server tab. Be sure to click the Save button.

Start up OpenCPN and then click Start in NMEA Simulator; simulated AIS data should now be displayed on OpenCPN.



Interpreting the Data

There are a variety of sources that will help you interpret the data that you are seeing; i.e., the raw NMEA sentences. The "AIVDM" sentences that AIS devices typically see are the NMEA 0183 AIS incoming transmissions (as opposed to AIVDO outgoing transmissions).

Possibly the best online, open-source reference as to the details of the NMEA sentence content is Eric Raymond's AIVDM/AIVDO protocol decoding page. (As it happens, Raymond's page has a few relatively minor typo or formatting errors; I have an updated version of his page available at my update page.)

There are also a number of AIS parsers online, where you can submit an NMEA sentence for interpretation, as shown below (from the Maritec Solutions online parser):

There are other AIS online parsers with a variety of capabilities, including:

In addition, I have a set of AIS tools (written in Perl) available on my software page. Among other things is an AIS parser:

If you are looking for real AIS data for research, testing, or curiosity, check out AISHub, an AIS data sharing and vessel tracking site.

And, finally... if you are working with NMEA 2000 binary data, take a look at the CANboat project.



COPYRIGHT NOTICE

All information on this page © 2019, Gary C. Kessler. Permission to use the material here is extended to any of this page's visitors, as long as appropriate attribution is provided and the information is not altered in any way without express written permission of the author.