Monday, November 4, 2013

From Scratch to NMEA Re-broadcasting on TCP, with a Raspberry PI

This post will demonstrate how to connect a Raspberry PI on some NMEA input - a GPS in this case - to re-broadcast all its data using other protocols, HTTP and TCP in this case.
The cool thing about this configuration is that:
  • As many devices as you want can access the NMEA data
  • The Raspberry PI only draws 700 mA
  • When they're done using rthe NMEA data, devices (laptops, tablets...) can shutdown, the NMEA data will still be read and available whenever a device connects on the Raspberry Pi's network
The NMEA data being read can also be logged on the Raspberry PI, if necessary.

We assume that you already have a Raspberry PI, along with its power supply (AC or DC, whatever).
We will show how to setup everything so the Raspberryt PI reads the NMEA data, and re-broadcasts them. As an example, we will show how to consume them from OpenCPN.
We will also need to edit several configuration files and other scripts. You can use any editor you wish, "nano" can do the job, I myself prefer "vi". At the end of the day, that's the same, only the result is important.

Requirements

First, flash the SD card, as explained here. I used the Raspbian image. Comes with Java.
Next, we will require several softwares to be installed on the Raspberry PI, some are necessary, others are convenient.

An FTP server

This one will be very convenient when you will need to transfer files on you Raspberry PI.
To install, type:

 Prompt> sudo apt-get install vsftpd
    
Then edit (sudo edit) /etc/vsftpd.conf:

 anonymouse_enable=NO
 local_enable=YES
 write_enable=YES
 local_umask=022
 chroot_local_user=YES
 user_sub_token=$USER
 local_root=/home/$USER/ftp
    
Start this ftp as a service on the PI:
  
  Prompt> sudo service vsftpd restart
    

Install RXTX Java libraries

Read this good article.
Just type:

 Prompt> sudo apt-get install librxtx-java
    
We will see in the next section what to do with this.

Fing

A very cool utility. Combination of Find and Ping.

 Prompt> wget http://www.overlooksoft.com/packages/download?plat=arm
 Prompt> sudo dpkg -i overlook-fing-2.3.deb
 Prompt> sudo apt-get install libpcap*
    
and finally:

 Prompt> sudo fing
    

OlivSoft

You have just installed the libraries you need to use the serial ports (Serial or USB), now we need to modify the launching script to take that in account.
Make sure you have the last version of the soft, up to date.
It is available on Google Code, in the download section.
The best way to do that is probably to install it on a box connected to the internet, and to let the software update itself.
Then you can transfer all the directory where it is installed (using FTP) on the Raspberry PI.
Once this is done, navigate to the all-scripts directory, and edit the script named olivsoft.
You can add a line in the menu:

  echo \| P for Weather Wizard, with http proxy 
  echo \| S for NMEA Sniffer
  echo \| H for Headless NMEA Console
  echo \| POL for Polar Tool
    
... and then the corresponding execution definition:
    ...
  elif [ $choice = "H" ] || [ $choice = "h" ] 
  then
    JAVA_OPTIONS="$JAVA_OPTIONS -Dverbose=false"
    JAVA_OPTIONS="$JAVA_OPTIONS -Dheadless=yes"
    JAVA_OPTIONS="$JAVA_OPTIONS -Dserial.port=/dev/ttyUSB0"
    JAVA_OPTIONS="$JAVA_OPTIONS -Djava.library.path=/usr/lib/jni"
    #
    HEADLESS_OPTIONS="-output=HTTP:9999"
    HEADLESS_OPTIONS="$HEADLESS_OPTIONS -output=TCP:7001"
    # HEADLESS_OPTIONS="$HEADLESS_OPTIONS -output=FILE:.\logged-data\headless.nmea
    java -client -Xms512m -Xmx1024m -classpath $CP $JAVA_OPTIONS olivsoftdesktop.OlivSoftDesktop $HEADLESS_OPTIONS
  elif ...
    
Notice that /dev/ttyUSB0 is the USB port the GPS in connected on. In this case, the data will be re-broadcasted on TCP, port 7001, as well as on HTTP port 9999.
We will see below how to start the program.

Create an ad-hoc network, from the Raspberry PI

Read this good article.
Make a backup:

 Prompt> sudo cp /etc/network/interfaces /etc/network/interfaces_backup
    
Then edit (sudo edit) /etc/network/interfaces:

 auto lo
 iface lo inet loopback
 iface eth0 inet dhcp
 
 auto wlan0
 iface wlan0 inet static
   address 192.168.1.1
   netmask 255.255.255.0
   wireless-channel 1
   wireless-essid RPiOnTheBoat
   wireless-mode ad-hoc
    
In the lines above, RPiOnTheBoat will be the name of your ad-hoc network.
Restart wlan0 (wlan0 is the name of the wireless network interface).

 Prompt> sudo ifdown wlan0
 Prompt> sudo ifup wlan0
    
(reboot in case you have an error message)

Then we might need a DHCP Server:

 Prompt> sudo apt-get update
 Prompt> sudo apt-get install dhcp3-server
    
Then edit (sudo edit) /etc/dhcp/dhcpd.conf

 ddns-update-style interim;
 default-lease-time 600;
 max-lease-time 7200; 
 authoritative;
 log-facility local7;
 subnet 192.168.1.0 netmask 255.255.255.0 {
   range 192.168.1.5 192.168.1.150;
 }
    
Reboot..., all should be all set!
Let's if that's true.

Run it

Connect the things first:

Only the Wireless dongle is in

Plug the SD Card in (contains the OS)

Plug in the GPS

Connect the power supply
Connecting the power supply will boot the device. Now, wait a couple of seconds for Linux to boot, and we are going to connect to the Raspberry PI from another machine, using ssh.
Notice in the steps above, that there is no mouse, no keyboard, and no screen connected on the Raspberry PI. We could have done so. But we didn't.
To demonstarte the full compatibility of this scenario, we will do the subsequent operations from a Windows box.

Connect to the Raspberry PI's ad-hoc network

The wireless networks are usually available in the Windows taskbar. If not, open your Network Control Panel, you'll find it there.
Notice the network's name (RPiOnTheBoat), this is the one you defined.
Then, we will use ssh (PuTTY) to start the re-broadcast:
Connect with your username (default: pi) and password (default: raspberry), and cd to olivsoft, and start the script olivsoft:

 Prompt> ./olivsoft
    
It's on its way! You can leave the session if you wish, the process will keep going.
To stop it:
Re-connect on the Raspberry PI with ssh
Find the process, by typing:

 Prompt> ps -ef | grep java    
    
Then send it a SIGTERM signal:

 Prompt> kill -SIGTERM 2493
    
Where 2493 is the process id. This way, the process will shut down nicely (by design, it understands that signal, which would be a Ctrl+C from the command line).

The process being up and running, define a network connection in OpenCPN:
Notice the Address, the one you have defined.
Click Apply...
See the top right, the GPS is connected. As you can see in the NMEA log.
You can connect like this with as many devices as you want.
Voilà!

Next, we will try with the full NMEA station, with Pi4j. Then the HTML5 console will also be available, from http://192.168.1.1:9999/html5/console.html.

No comments: