This is a program that turns your mbed device into a FireFly gateway, that publishes data from FireFly BLE modules to the IBM Watson IoT Platform.

Dependencies:   C12832 EthernetInterface MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Revision:
24:437c753be4fd
Parent:
22:e6b69714b59a
Child:
25:93368e752d2d
--- a/main.cpp	Mon Nov 30 09:12:22 2015 +0000
+++ b/main.cpp	Tue Mar 01 06:20:45 2016 +0000
@@ -22,7 +22,7 @@
  *    Add magnetometer sensor output to IoT data stream
  *
  *******************************************************************************/
-
+#include "mbed.h"
 #include "LM75B.h"
 #include "MMA7660.h"
 #include "MQTTClient.h"
@@ -35,8 +35,8 @@
 #define __APP_SW_REVISION__ "18"
 
 // Configuration values needed to connect to IBM IoT Cloud
-#define ORG "jfzeoz"             // For a registered connection, replace with your org
-#define ID "mbedGateway"                        // For a registered connection, replace with your id
+#define ORG "gpr4zb"             // For a registered connection, replace with your org
+#define ID "FFtesting"                        // For a registered connection, replace with your id
 #define AUTH_TOKEN "123456789"                // For a registered connection, replace with your auth-token
 #define TYPE "FireFly"       // For a registered connection, replace with your type
 
@@ -95,13 +95,12 @@
 int indexBufWrite = 0;
 int indexBufRead = 0;
 char IoTbuffer[BUFFER_SIZE][DATA_LENGTH];
+int SerialMessageLength = 142;
+char data[142];
 
 int DELETE = 0;
 
-
-Serial pc(USBTX, USBRX);
-
-
+Thread        *RX_THREAD_POINTER;
 
 
 // If we wanted to manually set the MAC address,
@@ -177,100 +176,43 @@
     }
 }
 
-//matjaz dodamo nit za komunikacijo preko i2c
-void firefly_com (void const *args)
-{
 
-    char buf[100];                     
-    int i = 0;
-
-
-    
+void rx_thread(void const *argument){
 
-    LOG("Slave here i am ready to punish me\r\n");
-    
-/*    
-    while(1){
-        
-        masterI2C.write(addr, send_test_byte, 1);    //write (int address, const char *data, int length, bool repeated=false)
-        Thread::wait(100);                          //da ima cas slave odgovorit
-        masterI2C.read(addr, recv_test_byte, 1); 
-        LOG("I2C send 0x56 slave reflect 0x%x:\n", recv_test_byte[0]);
-        Thread::wait(1000);
-    }
-*/
-
-    while(1){
-
-
-         i = slave.receive();
-         DELETE++;
-         /*if(i>0)
-            LOG("i: %d\r\n",i);*/
-            
-         switch (i) 
-         {
-             case I2CSlave::ReadAddressed:
-                if(indexBufRead != indexBufWrite){
-                    slave.write(IoTbuffer[indexBufRead],20);                 
-                    LOG("Slave sent mesage: %s\r\n",IoTbuffer[indexBufRead]);
-                    indexBufRead++;
-                    if(indexBufRead == BUFFER_SIZE){
-                        indexBufRead = 0;
-                    }
+    while(true){
+        int index = 0;
+        bool cloudSend = false;
+        while(xbee.readable()){
+            if(index < SerialMessageLength){
+                data[index] = xbee.getc();
+                pc.printf("%c",data[index]);
+                if(index == 0){
+                  if(data[index] == '!'){
+                    data[index] = ' ';
+                    index++;//somehow delete the \nfrom beginning and \r from the end
+                  }
                 }else{
-                    response = "00000000000000000000"; 
-                    slave.write(response,20);                 
-                    //LOG("Slave sent mesage: %s\r\n",response);
-                }          
-                 break;
-                 
-             case I2CSlave::WriteGeneral:
-             
-                 slave.read(buf, 20);
-                 //LOG("Napacen case\r\n");
-                 break;
-                 
-             case I2CSlave::WriteAddressed:
-             
-                 slave.read(buf, 20);
-                 //parsanje
-                 lux  = ((uint16_t)buf[11] << 8) | buf[12];       //že skalirano v luxe
-                 t    = ((uint16_t)buf[13] << 8) | buf[14];       //raw
-
-                //buf[4] = 0x55;
-                //buf[5] = 0xff;
-                 ax   = ((uint16_t)buf[15] << 8) | buf[16];       //pospeškometer ay                 ax   = (((buf[4]&0xFF) << 8) | (buf[5]&0xFF));       //pospeškometer ax
-                 ax = (ax >> 6);                
-                 ay   = ((uint16_t)buf[17] << 8) | buf[18];       //pospeškometer ay
-                 ay = (ay >> 6);
-                 az   = ((uint16_t)buf[19] << 8) | buf[20];       //pospeškometer az
-                 az = (az >> 6);
-                 
-                 int tmp = 48;
-                 deviceID = 0;
-                 //sprintf(tmp, "%c%c", buf[3],buf[4]);
-                 deviceID   =  (buf[3]-tmp)*10;      //device ID from whom the message was received 
-                 deviceID += (buf[4]-tmp);
-                 respond = 1;
-                 //rh   = ((uint16_t)buf[4] << 8) | buf[5];
-                 
-                 if(deviceID == 12){
-                    respond = 0;
+                  if(data[index] == '?'){
+                    data[index] = ' ';
+                    index = 0;
+                    cloudSend = true;
+                  }else{
+                    index++;
+                  }
                 }
-
-                 //LOG("Slave read 0x:%d\r\n", lux);
-                 //LOG("Slave read 0x:%x%x%x%x\r\n", buf[0],buf[1],buf[2],buf[3]);
-                 LOG("Slave Received %s \r\n", buf);
+              }else{
+                index = 0;
+              }
+              if(cloudSend == true){
+                cloudSend = false;
+                pc.printf(data);
+                pc.printf("\r");
                 
-                 break;
-                 
-         } // switch .. 
-         //Thread::wait(1);   
-        
+                //if (publish(&client, &ipstack) != 0) 
+                  //  attemptConnect(&client, &ipstack);   // if we have lost the connection
+            }
+        }
     }
-
-
 }
 
 
@@ -465,7 +407,6 @@
     {       
         connected = true;
         //green();    
-        //matjaz zelja je da je off
         off();   
         displayMessage("Connected");
         wait(1);
@@ -606,29 +547,38 @@
                 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
             }
         }
-    }else if (memcmp(start, "respond", len) == 0)
+    }else if (memcmp(start, "cmd1", len) == 0)
     {
         char payload[message.payloadlen + 1];
         sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
-    
-        char* pos = strchr(payload, '}');
+        
+          xbee.putc('\n');
+          pc.putc('\n');
+          
+          int msgLen = 0;
+          for (int i=8;i<message.payloadlen-2;i++) { //reading only the command from the payload
+            xbee.putc((char)payload[i]);
+            pc.putc((char)payload[i]);
+            msgLen++;
+          }
+          
+          while(msgLen < 7){
+            msgLen++;
+            xbee.putc('x');
+            pc.putc('x');
+          }
+          xbee.putc('\r');
+          pc.putc('\r');
+        /*char* pos = strchr(payload, '}');
         if (pos != NULL)
         {
             *pos = '\0';
             if ((pos = strchr(payload, ':')) != NULL)
             {
-                response = (pos+1);
-                
-                strcpy(IoTbuffer[indexBufWrite], response);
-                //LOG("%d sporocilo v bufferju: %s size of IoTbuffer: %d \r\n", indexBufWrite, IoTbuffer[indexBufWrite], response);
-                indexBufWrite++;
-                if(indexBufWrite == BUFFER_SIZE){
-                    indexBufWrite = 0;
-                }
-
-                //LOG("%s \n", response);
+                response = (pos+2);
+                xbee.puts(response);
             }
-        }
+        }*/
     }else
         WARN("Unsupported command: %.*s\n", len, start);
 }
@@ -644,14 +594,18 @@
     
     led2 = LED2_OFF; // K64F: turn off the main board LED 
     
-    //matjaz set I2C parameters
-    slave.address(0xC2);
-    slave.frequency(400000);
+    xbee.baud(38400);
+    pc.baud(115200);
+    xbee.puts("\nRDYxxxx\r");
+    wait(5);
+    xbee.puts("\nRDYxxxx\r");
+    pc.printf("i sent RDY to FireFly\r\n");
     
     displayMessage("Connecting");
     Thread yellow_thread(flashing_yellow);
     Thread menu_thread(menu_loop);  
-    Thread firefly (firefly_com);
+    Thread t_rx(rx_thread);
+    
     
     LOG("***** IBM IoT Client Ethernet Example *****\n");
     MQTTEthernet ipstack;
@@ -680,8 +634,7 @@
     
     blink_interval = 0;
     int count = 0;
-
-    pc.baud(115200);
+    
     while (true)
     {
         if (respond>0)
@@ -691,51 +644,9 @@
             respond = 0;
         }
         
-        /*if (blink_interval == 0)
-            led2 = LED2_OFF;
-        else if (count % blink_interval == 0)
-            led2 = !led2;*/
+        wait(3);
             
         client.yield(10);  // allow the MQTT client to receive messages
     }
-
-
-/*
-    while(1){
-         static int i;
-         static char buf[20];
-         uint16_t lux;
-         
-         i = slave.receive();
-         
-         if(i>0)
-            LOG("i: %d\r\n",i);
-            
-         switch (i) 
-         {
-             case I2CSlave::ReadAddressed:
-                                      
-                 slave.write(buf,2);                 
-                 LOG("Slave send mesage0x%x\r\n",buf[0]);                              
-                 break;
-                 
-             case I2CSlave::WriteGeneral:
-             
-                 slave.read(buf, 10);
-                 LOG("Napacen case\r\n");
-                 break;
-                 
-             case I2CSlave::WriteAddressed:
-             
-                 slave.read(buf, 19);
-                 lux = ((uint16_t)buf[0] << 8) | buf[1];
-
-                 LOG("Slave read 0x:%d\r\n", lux);
-
-                 break;
-                 
-         } // switch ..    
-        
-    }
-*/    
+  
 }