Tuesday, 27 December 2022

Sixty Dollar AIS Receiver

How I receive and decode AIS signals and display them in OpenCPN using a relatively cheap software defined radio. An RTL-SDR is a low-cost USB device that can be used as a computer-based radio for receiving live radio signals. Depending on the RTL-SDR it could receive frequencies from 500 kHz up to 1.75 GHz. Most of the software for the RTL-SDR is also community developed, open-source and most of the time free of charge. For this use the unit is made to collect and decode data from both AIS frequencies and forward the data to OpenCPN. The RTL-SDR with different programs can also be used for collecting weather information and faxes. 

The all up cost when I set it up (2021) was just shy of sixty AU dollars. The all up cost is still way below even the stand alone open-source units.  I have compared this set up along side a high end commercial unit and have been very happy with the performance. Yes the unit I am testing this setup against is thirty times more expensive, so I did expect it to be better. That said I was pleasantly surprised to find that the commercial unit only occasionally picked up some extra targets at the theoretical maximum range.  


AIS Display using RTL-SDR USB dongle plugged into a laptop computer running OpenCPN. The ship displayed on the far right is over 30 Nautical Miles from our vessel. 


To give a sense of scale, as a reference for the size of the dongle I have used a SD card carrier

** toward the end of the DIY I have included a couple of shots of the dongle connected to a Raspberry Pi

The other positive feature of using these units is that there isn't any need to wire in anything, no power supplies, fuses, switches, data wire plugs or converters to get it into the laptop or Raspberry Pi, and the additional power draw is minimal.  For a totally stand alone unit the only other thing to add is a GPS USB dongle.

AIS Target page showing part of the number of targets, range and distance. Due to location 132 targets being processed is about average for a mid week day and will increase on the week end.  The RTL-SDR will run and pick up the same number of targets as a commercial receiver. 

This tutorial will show you how to set up an AIS receiver using a RTL-SDR. Most parts of this tutorial is applicable to several brands of SDR Dongles, such as the RTL-SDR.COM V3. Keep in mind the best dongles to use are the newer class of dongles like the version 3’s in metal cases with very little oscillator offset. While this may be getting a bit technical the less the dongle oscillator is off frequency the better it will work to receive AIS signals. In the early dongles you had to have the SDR dongle turned on and warmed up for half an hour before it would be stable and not drift in and out of frequency. So keep in mind the more stable and closer to frequency the dongle oscillator is, the clearer signal it can receive. A unit advertised with a 0.5 or 1 PPM error by having a temperature compensated oscillator (TCXO) is the best sort of unit to purchase for this purpose. Watch out for cheap and not so cheap Chinese clones for sale on the net in plastic cases. 

While I have made note about units with large offset, that's not to say the units are not usable, they can be and as long as the offset is known and allowances are made for the offset in the decoding software they can be used successfully. When I first started using the RTL-SDR I used calibration software looking for the PPM offset number to enter. This however only caused a lot of confusion and for some reason came up with wildly inaccurate numbers. For what ever reason this wasn't an isolated incident, I found out others with the newer low offset oscillator RTL-SDR units were also seeing similar results when using the software to arrive at an offset solution. The work around was to enter 0 in the PPM offset and then later fine tuning the signal if necessary.

I use a nooelec Nano 3 RTL_SDR and I have received AIS targets up to 40 Nautical Miles away on my laptop when running SDRangel interfacing the signals to OpenCPN. The reason I like the Nano 3 is because its a high performance unit with 0.5PPM TCXO, and its compact enough to not congest the USB ports on my laptop or Raspberry Pi which allows room to plug in other dongles and cables. The size of the Nano 3 is tiny at 17mm x 8mm x 20mm & 15g, and I have seen photos of 4 of these units plugged into a raspberry pi usb ports. Being a RTL-SDR with very low offset (0.5PPM TCXO) I didn’t have to mess around working out frequency offset values.

The cost to get up and running was about AU$49 for the Nano 3 RTL-SDR and another AU$10 for some consumables to make an antenna and cable with connectors. While this isn't exactly a rock bottom cost, it is when compared with a commercial AIS receiver and or some open source units on the market.

Basic AIS knowledge.

The AIS signals have a horizontal range of about 40 nautical miles (74 km), depends on antenna height on transmitter and receiver. AIS traffic information is only available in coastal areas or in ship to ship zones. AIS communication takes place using two VHF frequencies, 161.975 MHz and 162.025 MHz, using a bandwidth of 25 kHz.

The centre Frequency is 162.000 MHz.

Channel A 161.975 MHz (87B)

Channel B 162.025 MHz (88B)

The main difference between Class A and Class B units is the power output, Class B transmits at 2W, giving a range of between 8-10 miles, whilst Class A transmits at 12.5W giving the much larger range.

For a shipboard fitting, there are three types of AIS equipment, Class A and Class B both are capable to transmit/receive data and then there are the receive only units. Class A is intended for vessels where the fit is mandatory, (usually most but not all commercial vessels). Class A transmits more information, more frequently and at a higher power than Class B.

Requirements and Setup

To set up an AIS ship radar on a windows system you will need these things.

  1. An RTL-SDR Dongle

  2. Antenna tuned to 162MHz (Marine VHF antenna)

  3. Installed  the drivers for RTL-SDR Dongle

  4. SDRangel or AISdeco2, software that configures the RTL-SDR, listens on both AIS channels then transfers “NMEA” information on to the internal laptop network.

  5. OpenCPN, receives the “NMEA” data from the internal laptop network for display on the chart.

      6. Trouble shooting software, SDRangel, (SDR#  aka SDRsharp ) are visual tools for checking the received signals. Using SDRangel removes the need to install the other programs.

     7. To get the full use from the AIS signals get a GPS USB Dongle as well, this is needed to calculate the distance from the vessel and place you easily on the chart. 

RTL-SDR Dongle

The Nano 3 RTL-SDR I use is listed on Amazon shop: NESDR Nano 3 - Premium Tiny RTL-SDR w/Aluminum Enclosure, 0.5PPM TCXO, SMA & MCX Input & Custom Heatsink. RTL2832U & R820T2-Based Software Defined Radio. The link if using the AU site is https://www.amazon.com.au/NooElec-NESDR-Nano-Enclosure-R820T2-Based/dp/B073JZ8CC2 or direct from nooelec https://www.nooelec.com/store/sdr.html It appears Nooelec do have Amazon shops in a lot of countries and makes delivery cheaper and faster.

Another unit I would consider using is RTL-SDR Blog V3 RTL2832U 1PPM TCXO HF BiasT SMA Software Defined Radio and is from an ebay shop https://www.ebay.com.au/itm/272411458376 or from their web site https://www.rtl-sdr.com/.

AIS Antennas

AIS signals are broadcast on both 161.975 MHz and 162.025 MHz and have a maximum range of approximately 40 nautical miles (75 kilometres). If your radio is set up is more than 45 Nm away from any boats, there will be little chance of receiving AIS signals. AIS/VHF is considered a line of sight signal, meaning that if there are large trees, buildings, hills or mountains or curve of the earth in the way between your antenna and the boats the AIS signals could be blocked. By putting your antenna as high up as possible you can in most cases increase the received range. As an example, in my case an antenna on the back rail has a range of 16 nautical miles in good weather, the same antenna at the mast head receives signals up 35 nautical miles in most weather and up to 40 nautical miles on clear days. I am still not sure if the 40 Nm signal is a true direct signal or whether it was repeated or has been repeated from another station.

Making your own antenna:

https://www.sailworldcruising.com/news/227075/Build-your-own-inexpensive-VHF-AIS-antenna

https://www.66pacific.com/calculators/quarter-wave-vertical-antenna-calculator.aspx

Also a variety of antenna types to make here

https://www.rtl-sdr.com/rtl-sdr-tutorial-cheap-ais-ship-tracking/

Drivers for RTL-SDR Dongle

The Zadig driver installer is available here https://zadig.akeo.ie/ Runs on Windows 7 or later. Usage: Download the executable and run it — no installation is necessary

SDRangel

SDRAngel set up and running, use this pic when setting up. 

Since writing the original “how to” the site supporting AISDeco2 (AIS decoding program) has closed, the software is available from the web archive, however I couldn’t say for how long.

I have experimented with the software SDRangel and have found it to be slightly easier to use, and I no longer need SDR# to view the signal spectrum to see what is going on. SDRangel handles the spectrum visual and also the decoding of the radio signals and converts them into a NMEA data stream output to OpenCPN or other program.

SDRangel prerequisite for Windows distribution

If you do not have the Visual C++ runtime already installed you will have to install the Visual C++ runtime environment vc_redist.x64.exe from Microsoft.

Download Microsoft Visual C++ 2017 Redistributable from here: https://www.visualstudio.com/downloads/ - Microsoft Visual C++ Redistributable for Visual Studio 2017

You will need to install file of 32 bit version VC_redist.x86.exe regardless of how many bits (EG 32 or 64) your operating system is! The microsoft site is a little complicated as it talks of Visual studio 2022 but press the Windows and C++ buttons then go to down to the Other tools, Frameworks, and Redistributables tab and select the x86 button and then download.

Cheat sheet for setting up SDRangel. Look at the screen shot of what I am using to help identify the windows to configure. In order to receive data packets on SDRangel,

In my configuration the Sampling device = RTL-SDR[0]AIS01 (yours name may vary and be missing the AIS01)

= 0,162,000 kHz

Spectrum Display

Channels=AIS Demod + 25kHz

Channels=AIS Demod - 25kHz

Features=AIS

Presets=Save Current Config

The “Sampling Device” is set to RTL-SDR[0] and tuned at 162MHz half way between the two marine CH87B and CH88B.

Two “Channels” units are added: the first AIS Demodulator is set to Δf + 0,025,000 Hz and the second AIS Demodulator to Δf - 0,025,000 Hz.

The RTL-SDR “Sampling Device” is set to 2.4Msps and the decimation to 16 to show the pulses on the two channels clearly.

It is important to set the ppm accurately, in my case ppm = -1.

Decoded AIS messages show up in each channel in the Received Messages window. In order to display on OpenCPN, tick the UDP box and setup identical Port parameters on both AIS demodulator windows and in OpenCPN connections: UDP 127.0.0.1 4159 NMEA.

AISDeco2

If your not going to use AISdeco2 you can skip this section and move on the configuring OpenCPN

The software I first used was AISdeco2. It can be downloaded from https://web.archive.org/web/20210816124943/http://xdeco.org/?page_id=30

AISdeco2 directly connects to the RTL-SDR (requires no audio piping) and can listen to both AIS channels simultaneously. To simply use AISdeco2 on Windows follow these instructions.

1.Download AISdeco2 for windows and extract the zip file into a folder on your PC. I use the C:\ root so if I need to use a terminal window it reduces the typing to change directory into the target. So my example is C:\AISDECO2 Don’t forget to read the file aisdeco2.readme in the extracted file. It will either make sense or make your eyes glaze over, if the later, continue on to the next step.

2.Make a short cut to the aisdeco2.exe file, then move the shortcut file out to the desktop and edit by it using the right mouse click, select properties.

I then replace the line in the Target with this line as written below, cut and paste will make it easy.

C:\AISDECO2\aisdeco2.exe --gain 49.6 --freq-correction 01 --udp 127.0.0.1:4159

Shortcut properties check the Target Line contains: 
C:\AISDECO2\aisdeco2.exe --gain 49.6 --freq-correction 01 --udp 127.0.0.1:4159

What this does is, when you double click the short cut it will start the program in a terminal window, the SDR will be set with a high gain of 49.6 a frequency correction of 01, and the data from the SDR is sent out on the internal network of the laptop. --udp 127.0.0.1:4159 enables a UDP client for transmitting AIS messages to remote UDP server with IP 127.0.0.1 and UDP port 4159. Simply put this sends the NMEA data out for collection by OpenCPN on the internal computer network

3.Now double click the aisdeco2 shortcut file starting the command line software. Decoding will begin automatically, when AIS targets are received.

AISdeco2 program running, the decoded AIS signals are displayed. The available gain setting are displayed on start up. To change the gain, simply replace the gain value in the target line in the Shortcut properties. 

If the program window doesn’t come up its time to check the target line in the shortcut is formatted correctly and is as listed.

If you run aisdeco2 and get an error that msvcp141.dll and msvcr141.dll are missing in your OS. Please, download the Microsoft Visual C++ 2017 Redistributable from the Microsoft site. This downloaded package installs run-time components of Visual C++ libraries and can be used to run such applications on a computer even if it does not have Visual Studio 2017 installed.

Download Microsoft Visual C++ 2017 Redistributable from here: https://www.visualstudio.com/downloads/ - Microsoft Visual C++ Redistributable for Visual Studio 2017

You will need to install file of 32 bit version VC_redist.x86.exe regardless of how many bits (EG 32 or 64) your operating system is! The microsoft site is a little complicated as it talks of Visual studio 2022 but press the Windows and C++ buttons then go to down to the Other tools, Frameworks, and Redistributables tab and select the x86 button and then download.

Tips: Adjust the gain for better reception, there will be a lot of times when it is not necessary to run the gain at the max. 

OpenCPN

The procedure is the same for SDRangel or AISDECO2.

  1. Start OpenCPN

  2. Open the Options window (cog on the tool bar) then select Connections

  3. Create a connection, Type: Network; Direction: Input; Protocol: UDP; Network Address: 127.0.0.1; Network Port: 4159; Priority: 1. The comment can be AIS RTL_SDR. Then enable the connection once the port is created.

Apply and OK to close the connection and Options window.

OpenCPN, Options: Connection Page

For a quick check data is being received into OpenCPN open an NMEA debug window in the connections tab, this should show data input and the vessel AIS positions should begin to be displayed on the OpenCPN chart. The first part of the input line is the network connection address, if your not getting information check the port number is correct. 


(SDR SHARP)SDR# Pre-requisites

If you do not intend to install SDR# your done. 

The installation of SDR Sharp is really unnecessary if you choose to use SDRangel to decode your signals, and even if using AISdeco2 the signal can also be checked in SDRangel. The reason most people installed SRD Sharp previously was to check the received signals and if the PPM number entered to adjust the offset was correct. 

In order to run SDR# software, you will need the .NET 5 Desktop x86 Runtime. This download is only required once, then Windows Update will maintain it automatically.

----------------------------------------

    Other tips

    First ensure that you have accurately determined your dongle’s PPM offset using SDRangel or SDR# or another program (unless you are using a TCXO dongle). Also use SDRangel to determine the optimum gain setting for maximum SNR. Once the PPM offset and optimum gain is known the two programs (aisdeco2 and SDRangel) can be adjusted to reflect the settings..

    I found from my experimenting around I got more targets using SDRangel. Its a hard call as I had to switch between programs and it really depends on how many vessels are in range and what they are doing at the time, anchored or moored vessels can be very random with transmissions and may have thrown out the count however I am confident that SDRangel can decode more targets.


    OpenPlotter set up on Raspberry Pi the AIS receiver is a NANO3 RTL-SDR  




    RTL-SDR Nano3 dongle connected to a Raspberry Pi 3  USB port 

    A compact Chartplotter setup using a Raspberry Pi 3B+ running OpenPlotter image. Lots of features including the RTL-SDR AIS receiver, temp, barometer, humidity, single wire external temp sensing, voltage monitor, 9 Dorf Gyro/accelerometer/compass, GPS, ship wide Wi-Fi access point and connected to the internet, and to simplify turning the unit off an automated shutdown.   





Sunday, 29 November 2020

Command Line Mail setup OpenPlotter

Command Line Mail setup for OpenPlotter2. We use this to send an email triggered by the OpenCPN plugin, Watchdog.  

OpenCPN Watchdog plugin (command function) can execute a command to send out an email, we use this for remote monitoring by Gmail. 

Using SSMTP on openplotter2 to send email, well SSMTP does not seem to work any longer, try as I did, it didnt connect with gmail. 

Eventually I found msmtp as an alternative to SSMPT and I was able to get it up an running in no time and had OpenCPN Watchdog emailing me with alarms.  

First thing I did was open a Gmail account, I use a Gmail account specially for the boat because I needed to change one of the the security settings to be less secure during login. DO NOT use existing accounts or usual passwords because they can be exposed.

Open a new Gmail account and enable:

https://www.google.com/settings/security/lesssecureapps

1. Enabling less secure apps to access Gmail

2. Open your Google Admin console (admin.google.com).

3. Click Security > Basic settings .

4. Under Less secure apps, select Go to settings for less secure apps .

5. In the subwindow, select the Enforce access to less secure apps for all users radio button. 

6. Click the Save button.



You can install the msmtp package with:

$sudo apt-get install msmtp msmtp-mta

The configuration file is located here : /etc/msmtprc

Strangely enough, it is empty with no clues, which is different from the old ssmtp.conf file.

However, there is a description with details if you use the man $man msmtp, or just copy and paste the contents I have supplied changing the necessary acount details.

For gmail accounts, these are the specific elements you should put into /etc/msmtprc:  

$sudo nano /etc/msmtprc


# Generics, important tls needs to be on when using port 587
defaults
auth           on
tls            on
# following is different from ssmtp:
tls_trust_file /etc/ssl/certs/ca-certificates.crt
# user specific log location, otherwise use /var/log/msmtp.log, however, 
# this will create an access violation if you are user pi, and have not changes the access rights
logfile        ~/.msmtp.log
# Gmail specifics, you shouldnt need to change these
account        gmail
host           smtp.gmail.com
port           587
# You need to change these
from          your-vessel-gmail-account_name@gmail.com
user          your-vessel-gmail-account_name@gmail.com
password      your-vessel-gmail-account-password
# Default
account default : gmail


Save the file close nano and exit.


You can test if this now works with:

$echo 'your test message' | msmtp destination-email-address@xxxxxxx.com

I have not yet found a way to set the subject, there is no -s flag as with ssmpt.

However, if you also install mailutils:

$sudo apt-get install mailutils

Then these should work:

$echo 'message' | mail -s "Hello" destination-email-address@xxxxxxx.com

$echo 'message' | sendmail destination-email-address@xxxxxx.com

You can use both mailutils and mpack with msmtp just as you did with ssmtp.

So any scripts that you have used before with ssmtp, mailutils or mpack should work just the same using msmtp.

Setting up the command script file to use in OpenCPN plugin, Watchdog.

 You can have Watchdog execute a computer command. We wanted Watchdog to send us an email if our boat moves or the NMEA data stops. We stored the command to email a boat moving alarm in a shell file called: AnchorAlarmEmail.sh and for the loss of NMEA data: NMEA_Data_Missing.sh

First up create a new directory called bin in /home/pi  

$mkdir bin  

Reboot the computer, the bin file will be seen on reboot.

After creating the files with nano $sudo nano AnchorAlarmEmail.sh and $sudo nano NMEA_Data_Missing.sh  

Save them to the created /home/pi/bin directory I changed the permissions of the file to make them executable. 

$sudo chmod 755 AnchorAlarmEmail.sh 

$sudo chmod 755 NMEA_Data_Missing.sh


As an example of the file, here is the format of the NMEA data loss script file NMEA_Data_Missing.sh: 

#!/bin/bash
echo "GPS NMEA Data Loss" | mail -s "Danger Will Robertson" destination-email-address@xxxxxxx.com 

Or for the Anchor watch script file AnchorAlarmEmail.sh

#!/bin/bash
echo "Boat appears to be on the move. Best regards, the Watchdog " | mail -s "ON THE MOVE" destination-email-address@xxxxxxx.com

You may want to send the email to your most used account that will alarm on your phone when an email is received, we don't send mail to the same account as we receive it from, but this is a personal choice.

Before running the test to check if the shell script works, shutdown and reboot OpenPlotter

 Then test the shell file is going to work. After reboot open a terminal window and type in AnchorAlarmEmail.sh or NMEA_Data_Missing.sh 

If these files are working, you receive an email or two, then you can go ahead and configure WatchDog to select these files as a command to execute.

Tuesday, 31 December 2019

December Newsletter

News from the Crew of Matilda
December  2019
This was a special moment, our sailing friends Helen & Lester on their beautiful Joule came out to welcome us home........and they had a wonderful surprise install for us.
Ships log 

  • December 1: 58.9nm's to Mourilyan Harbour. Anchored at 17 36.35S, 146 07.47E in 4.8mtrs.
  • December 2: 65.8nm's to Orpheous Island. Anchored at 18 35.732S, 146 29.221E in 13.3mtrs.
  • December 3 & 4: 158nm's to Grassy Island, The Whitsundays. Anchored at 20 09.305S, 148 36.447E in 7.1mtrs.
  • December 5: 10.4nm's to Muddy Bay, Airlie. Anchored at 20 15.58S, 148 43.26E in 5.1mtrs.
  • December 5: 32.5nm's to Shaw Island, Southern anchorage. Anchored at 20 31.27S, 149 03.43E in 9.8mtrs.
  • December 7 & 8: 199nm's to Great Keppel Island. Anchored at 23 11.523S, 150 56.052E in 5.5mtrs.
  • December 11: 54.69nm's to Gladstone, Facing Island. Anchored at 23 51.707S, 151 21.420E in 5mtrs.
  • December 12 & 14: 314nm's to Moreton Bay Trailer Boat Club (MBTBC) in a pen - HOME.

Crew log:
December saw us complete 3 1/2 years from Moreton Bay Trailer Boat Club (MBTBC) to SE Asia and back. On our journey we traveled a total of 19,338nm's. Not bad for a couple who mainly only sailed in Moreton Bay. We went to Indonesia, Malaysia, Thailand and Borneo-Kalimantan (Indonesia end) and had a ball. We landed almost in the same pen as we left from on December 14 2016.


Sandy has been a FB friend from WWSA for many years. It is very rare these days to come across someone who is so incredibly generous with their time and affection. Sandy ran us around in her car to gather loads of supplies, then had us back to Matilda before the northerly blow returned so we could move her to a safer anchorage - being Shaw Island.

On our way to Shaw Island we sailed (yes sailed, not motored) past Solway Lass under full sail - isnt she magnificent.


GKI is still suffering from the effects of erosion after enduring two cyclone's: Marcia in Feb. 2015 and  Debbie in April 2017. These are the largest sand bags we have ever seen.


GKI has to be the prettiest island on Australia's east coast we reckon.

Deb at the big sharks mouth on GKI and happy to be here.


There are some very good walks on GKI. Here Bruce is trying to work out which is the best way to go.

Captain B, taking in the view of Long Beach - its really beautiful.

I love this shot looking towards the main land.

GKI over looking the anchorage off the resort.

Not sure if these are a good thing or a bad thing but because most of the resort is now defunct the goats are taking over.

On another walk we spotted Matilda at anchor through the trees.


Heading into Gladstone under Facing Island, we had to stop and let this big boy pass.

Arriving on Australia's east coast meant as we traveled south we saw the effects of the devastating fires. This is smoke haze,,,,it looked eerie.

Anchored at Facing Island the sunset was showing us the effects of the fires.

After leaving Gladstone, Facing Island we did two nights out to reach Brisbane as there were many storms around. These two Booby birds joined us on our first night out and by morning we actually had 5 on the pulpit. The poo was dreadful but they were incredibly comforting at the same time. 



After all this time the main finally gave in as we sailed past Noosa. There were some cracking storms over us, and a gust of wind changing direction did her in. The sound of the dacron ripping was heart breaking.

Not long after the rip occurred the wind really cranked up and just as Bruce was about to drop the damaged main sail it really gave way.


After two nights out and feeling dog tired our friends appeared from the haze over Brisbane to welcome us home.

Lester was yelling out that they had something to show us and for some funny reason i thought they were going to bring out a puppy. But to our amazement Lisa popped out of the companion way - OMG it brought tears to our eyes, everyone was so excited.

And we have her on board after we saddled up close to Joule, just brilliant. Joule easing back nicely with Lester clicking his camera like mad. 

Lisa and Bruce - happy days.

Having a bit of fun, I couldn't wipe the smile off my face.

And we are done for now. Matilda back in a pen for a well earn't rest and we get to enjoy Christmas with family and friends.
Publications
What a way to finish off the year, we have Matilda on the front cover of Cruising Helmsman. Jeremy took this shot as we sailed with our big blue spinnaker up past Lombok, Indonesia. Then in January 2020 Bruce had his article on washing machines on-board published - congratulations Captain!!