2013-04-27

WiFly AdHoc UDP Server

The WiFly

After many tries, I finally got the WiFly module to work as a AdHoc server, and to serve NMEA data via Wi-Fi, using the UDP protocol.

I want to configure the WiFly module to act as a UDP server using a terminal connection. I use Putty, connect the WiFly using the USB connection break-out board, and fire up the terminal, then type $$$ to enter command mode.

First I reset the WiFly back to factory settings:


factory RESET
save
reboot

Then I configure the WiFly module using the following commands:


set wlan join 4
set wlan channel 1
set wlan ssid MyArduino
set wlan tx 6
set uart baudrate 38400
set ip address 192.168.1.1
set ip netmask 255.255.255.0
set ip dhcp 0
set ip protocol 1
set ip remote 50000
save
reboot

Please notice that I am using baudrate 38400, as I will be sending AIS data later via this channel. It means that if you re-enter command mode via a terminal emulator, you will need to connect on 38400 baud.
I am using port 50000 as my UDP port. I will be configuring the other Wi-Fi device using static IP, so I  disable DHCP.

The Arduino

I insert the WiFly module back into my Arduino setup.

I have already put together a sketch that multiplexes incoming NMEA data, and outputs this data using the UDP protocol, on port 50000. See my previous posting for source code on this sketch.

The iPhone

On my iPhone, I see and connect to the "MyArduino" WiFi network. I click on the right blue arrow, and specify "Static" IP Address. I then type in the following configuration:
  • IP Address:  192.168.1.1
  • Subnet Mask: 255.255.255.0
  • Router: 192.168.1.1
I leave the other fields blank.

I use a WiFi UDP Test Tool app downloaded from the App Store to monitor incoming UDP data. It shows that the NMEA data is flowing nicely via the UDP protocol all the way from my GPS, via my Arduino to my iPhone.


2013-04-02

Multiplexing serial port NMEA sentences, and streaming to WiFi

In order to connect the GPS together with the VHF/AIS radio, and bringing the data to my iPad, I want to multiplex the incoming NMEA sentences to one single UDP stream.

The code below listens for data on the Arduino Mega's Serial1 and Serial2 ports (can easily be expanded to Serial + Serial3 as well). Incoming data is forwarded to the WiFly module, which streams the data using UDP. See previous posting for how to configure the WiFly module.


#include <WiFlyHQ.h>
#include <SoftwareSerial.h>

String _sentenceGPS;
String _sentenceVHF;
boolean _completedGPS;
boolean _completedVHF;
SoftwareSerial _wiflySerial(10, 11);
WiFly _wifly;

void setup()
{
  // Serial1: Baudrate=4800, Receive=GPS, Send=n/a
  _sentenceGPS.reserve(128);
  _completedGPS = false;
  Serial1.begin(4800);

  // Serial2: Baudrate=38400, Receive=AIS+DSC, Send=GPS
  _sentenceVHF.reserve(128);
  _completedVHF = false;
  Serial2.begin(38400);
  
  // WiFly module
  _wiflySerial.begin(38400);
  _wifly.begin(&_wiflySerial);
  _wifly.setIpProtocol(WIFLY_PROTOCOL_UDP);
}

void loop()
{
  RunMultiplexer();
}

void RunMultiplexer()
{
  char s[128];
  if (_completedGPS)
  {
    memset(s, 0, 128);
    _sentenceGPS.toCharArray(s, 128);
    _wifly.sendto(s, "255.255.255.255", 50000);
    Serial2.print(_sentenceGPS);  // Write GPS sentence to VHF
    _sentenceGPS = "";
    _completedGPS = false;
  }
  
  if (_completedVHF)
  {
    memset(s, 0, 128);
    _sentenceGPS.toCharArray(s, 128);
    _wifly.sendto(s, "255.255.255.255", 50);
    _sentenceVHF = "";
    _completedVHF = false;
  }
}

void serialEvent1()
{
  if (Serial1.available())
  {
    char c = Serial1.read();
    _sentenceGPS += c;
    if (c == '\n')
    {
      _completedGPS = true;
    }
  }
}

void serialEvent2()
{
  if (Serial2.available())
  {
    char c = Serial2.read();
    _sentenceVHF += c;
    if (c == '\n')
    {
      _completedVHF = true;
    }
  }
}


Right now the incoming GPS data on Serial1 is forwarded to the outgoing Serial2 port. I will add parsers and more logic to this - stay tuned! :-)