Wednesday, November 28, 2012

GPS data from Wifly to Android phone (Part 2)

As a proof of concept, I have created a simple app that fetches the last UDP packet received, and displays its contents in a floating message box, each time the button is pressed.

Here is an excerpt of the app's java code that is used for that.

public class MainActivity extends Activity {
   public void sendMessage(View view) {
        new ServerAsyncTask(MainActivity.this).execute();
   public static class ServerAsyncTask
               extends AsyncTask<Void, String, Void> {
      private Activity act;
      public ServerAsyncTask(Activity _act) {
           this.act = _act;
      protected String doInBackground(Void... params) {
         byte[] receiveData = new byte[1024];
         DatagramSocket socket = null;
         try {
            socket = new DatagramSocket(50000);
            DatagramPacket packet =
               new DatagramPacket(receiveData, receiveData.length);
            String contents = new String(packet.getData());
         } catch (SocketException e) {
         } catch (SocketTimeoutException e) {
         } catch (IOException e) {

         return (Void)null;
      protected void onProgressUpdate(String... str) {
         Toast.makeText(act, str[0], Toast.LENGTH_SHORT).show();

Tuesday, November 20, 2012

GPS data from Wifly to Android phone (Part 1)

Once an Access Point has been created on the phone with the app described in the last post, we can use the app’s data to configure the RN-XV module. Let's keep the same example:

            - SSID :                     DIRECT-6U-Android_37e5
            - passphrase :           sK2QIENk           
            - phone’s IP :  

Here is the arrangement used to configure the RN-XV module:

  Only 4 pins will be used on the RN-XV module:
  • pin 1 (red) : VDD_3V3
  • pin2 (yellow) : UART_TX
  • pin3 (green) : UART_RX, with an external 10k pull-up to 3.3 V
  • pin 10 (black) : GND
The module is connected to a laptop running TeraTerm through a USB-to-Serial converter. Here, we use the Adafruit's USB to TTL Serial Cable. Note that the converter's red wire should NOT be used, as it is connected to the USB 5V power, which could damage or destroy the RN-XV. The TeraTerm monitor software is configured at 9600 baud, which is the default for the RN-XV UART.
Once connected, the following commands are sent to the RN-XV module:
factory R
set wlan ssid DIRECT-6U-Android_37e5
set wlan passphrase sK2QIEnk
set ip host
set ip remote 50000
set ip proto 1
set comm time 10000
set comm size 75

set comm match 13
With this configuration, the module will form and send a UDP packet from the already received data each time any one of the following conditions occurs:
  • 75 bytes of serial data has been received by the module through the UART_RX pin;
  • or the ASCII character 13 (carriage return) has just been received;
  • or 10 seconds have occured since the last packet has been sent.
In the following arrangement, a GPS NMEA output at 9600 baud is connected to the UART_RX pin, and the RN-XV module continuously forms and sends UDP packets with the GPS data. Each packet will contain a complete NMEA sentence, as these sentences all end with the carriage return byte, and are less than 75 bytes.
Our next step will be to create an Android app for the phone that will capture the packets, retrieve the GPS data and display the info on the screen.