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:
22:e6b69714b59a
Parent:
21:bb0d9f12da01
Child:
23:03835082a0de
Child:
24:437c753be4fd
--- a/main.cpp	Thu Nov 19 12:50:08 2015 +0000
+++ b/main.cpp	Mon Nov 30 09:12:22 2015 +0000
@@ -36,7 +36,7 @@
 
 // Configuration values needed to connect to IBM IoT Cloud
 #define ORG "jfzeoz"             // For a registered connection, replace with your org
-#define ID "homeExample"                        // For a registered connection, replace with your id
+#define ID "mbedGateway"                        // 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
 
@@ -90,6 +90,13 @@
 int16_t     ay = 0;
 int16_t     az = 0;
 int16_t     deviceID = 0;
+const int BUFFER_SIZE = 30;
+const int DATA_LENGTH = 20;
+int indexBufWrite = 0;
+int indexBufRead = 0;
+char IoTbuffer[BUFFER_SIZE][DATA_LENGTH];
+
+int DELETE = 0;
 
 
 Serial pc(USBTX, USBRX);
@@ -197,28 +204,36 @@
 
 
          i = slave.receive();
-         
-         if(i>0)
-            //LOG("i: %d\r\n",i);
+         DELETE++;
+         /*if(i>0)
+            LOG("i: %d\r\n",i);*/
             
          switch (i) 
          {
              case I2CSlave::ReadAddressed:
-                                      
-                 slave.write(response,30);                 
-                 //LOG("Slave send mesage0x%x\r\n",buf[0]);
-                 response = "00000000000000000000";                     
+                if(indexBufRead != indexBufWrite){
+                    slave.write(IoTbuffer[indexBufRead],20);                 
+                    LOG("Slave sent mesage: %s\r\n",IoTbuffer[indexBufRead]);
+                    indexBufRead++;
+                    if(indexBufRead == BUFFER_SIZE){
+                        indexBufRead = 0;
+                    }
+                }else{
+                    response = "00000000000000000000"; 
+                    slave.write(response,20);                 
+                    //LOG("Slave sent mesage: %s\r\n",response);
+                }          
                  break;
                  
              case I2CSlave::WriteGeneral:
              
-                 slave.read(buf, 10);
+                 slave.read(buf, 20);
                  //LOG("Napacen case\r\n");
                  break;
                  
              case I2CSlave::WriteAddressed:
              
-                 slave.read(buf, 99);
+                 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
@@ -240,6 +255,9 @@
                  respond = 1;
                  //rh   = ((uint16_t)buf[4] << 8) | buf[5];
                  
+                 if(deviceID == 12){
+                    respond = 0;
+                }
 
                  //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]);
@@ -530,9 +548,13 @@
     faz = (float) az / 256.0f;
     faz = abs(faz);
           
+    /*sprintf(buf,
+     "{\"d\":{\"myName\":\"IoT mbed\",\"lux\":%d,\"accelX\":%0.3f,\"accelY\":%0.3f,\"accelZ\":%0.3f,\"temp\":%0.1f,\"deviceID\":%d}}",
+            lux, fax, fay, faz, tempC, deviceID);*/
+            
     sprintf(buf,
-     "{\"d\":{\"myName\":\"IoT mbed\",\"lux\":%d,\"accelX\":%0.3f,\"accelY\":%0.3f,\"accelZ\":%0.3f,\"temp\":%0.1f,\"deviceID%d\":%d}}",
-            lux, fax, fay, faz, tempC, deviceID, lux);
+     "{\"d\":{\"myName\":\"IoT mbed\",\"lux\":%d,\"temp\":%0.1f,\"deviceID\":%d}}",
+            lux,  tempC, deviceID);
     message.qos = MQTT::QOS0;
     message.retained = false;
     message.dup = false;
@@ -563,6 +585,7 @@
     sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
     
     LOG("Message arrived on topic %s: %.*s\n",  topic, message.payloadlen, message.payload);
+    LOG("Slave i2c working %d\n",  DELETE);
           
     // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
     char* start = strstr(topic, "/cmd/") + 5;
@@ -595,8 +618,15 @@
             if ((pos = strchr(payload, ':')) != NULL)
             {
                 response = (pos+1);
-                LOG("%s \n", response);   
-                //blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
+                
+                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);
             }
         }
     }else
@@ -605,7 +635,7 @@
 
 
 int main()
-{    
+{   
     pc.baud(115200);
     
     quickstartMode = (strcmp(org, "quickstart") == 0);