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:
25:93368e752d2d
Parent:
24:437c753be4fd
Child:
26:65808ea318a3
--- a/main.cpp	Tue Mar 01 06:20:45 2016 +0000
+++ b/main.cpp	Fri Mar 04 07:22:09 2016 +0000
@@ -17,9 +17,7 @@
  *    Chris Styles - Added additional menu screen for software revision
  *    James Sutton - Mac fix and extra debug
  *    Ian Craggs - add not authorized messages
- *
- * To do :
- *    Add magnetometer sensor output to IoT data stream
+ *    Klemen Skoda - Changed the code for FireFly implementation
  *
  *******************************************************************************/
 #include "mbed.h"
@@ -75,7 +73,7 @@
 
 char* joystickPos = "CENTRE";
 int blink_interval = 0;
-int respond = 0;
+bool respond = false;
 char* response = "00000000000000000000"; 
 
 char* ip_addr = "";
@@ -95,12 +93,8 @@
 int indexBufWrite = 0;
 int indexBufRead = 0;
 char IoTbuffer[BUFFER_SIZE][DATA_LENGTH];
-int SerialMessageLength = 142;
-char data[142];
-
-int DELETE = 0;
-
-Thread        *RX_THREAD_POINTER;
+const int SerialMessageLength = 170;
+char data[SerialMessageLength];
 
 
 // If we wanted to manually set the MAC address,
@@ -176,46 +170,6 @@
     }
 }
 
-
-void rx_thread(void const *argument){
-
-    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{
-                  if(data[index] == '?'){
-                    data[index] = ' ';
-                    index = 0;
-                    cloudSend = true;
-                  }else{
-                    index++;
-                  }
-                }
-              }else{
-                index = 0;
-              }
-              if(cloudSend == true){
-                cloudSend = false;
-                pc.printf(data);
-                pc.printf("\r");
-                
-                //if (publish(&client, &ipstack) != 0) 
-                  //  attemptConnect(&client, &ipstack);   // if we have lost the connection
-            }
-        }
-    }
-}
-
-
 void printMenu(int menuItem) 
 {
     static char last_line1[30] = "", last_line2[30] = "";
@@ -463,40 +417,10 @@
 
 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
 {
-    //moje float spremenljivke
-    float tempC = 0.0f;
-    float fax   = 0.0f;
-    float fay   = 0.0f;
-    float faz   = 0.0f;
+    MQTT::Message message;
+    char* pubTopic = "iot-2/evt/status/fmt/json"; //publishing to topic status
     
-    MQTT::Message message;
-    char* pubTopic = "iot-2/evt/status/fmt/json";
-            
-    char buf[250];
-/*    
-    sprintf(buf,
-     "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
-            MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
-*/  
-    //moj float preracun
-    // Scaled by 100
-    tempC = (((float)175.72 * t) / 65536) - 46.85;
-    
-    fax = (float) ax / 256.0f;
-    fax = abs(fax);
-    fay = (float) ay / 256.0f;
-    fay = abs(fay);
-    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,\"temp\":%0.1f,\"deviceID\":%d}}",
-            lux,  tempC, deviceID);
-    message.qos = MQTT::QOS0;
+    message.qos = MQTT::QOS0; // set the quality of service you want for outgoing messages
     message.retained = false;
     message.dup = false;
     message.payload = (void*)buf;
@@ -526,7 +450,6 @@
     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;
@@ -569,20 +492,18 @@
           }
           xbee.putc('\r');
           pc.putc('\r');
-        /*char* pos = strchr(payload, '}');
-        if (pos != NULL)
-        {
-            *pos = '\0';
-            if ((pos = strchr(payload, ':')) != NULL)
-            {
-                response = (pos+2);
-                xbee.puts(response);
-            }
-        }*/
     }else
         WARN("Unsupported command: %.*s\n", len, start);
 }
 
+void callback() {
+    // Note: you need to actually read from the serial to clear the RX interrupt
+    char c = xbee.getc();
+    pc.putc(c);
+    if(c == '?')
+    pc.printf("\r");
+
+}
 
 int main()
 {   
@@ -603,9 +524,7 @@
     
     displayMessage("Connecting");
     Thread yellow_thread(flashing_yellow);
-    Thread menu_thread(menu_loop);  
-    Thread t_rx(rx_thread);
-    
+    Thread menu_thread(menu_loop);    
     
     LOG("***** IBM IoT Client Ethernet Example *****\n");
     MQTTEthernet ipstack;
@@ -628,23 +547,23 @@
     if (!quickstartMode) 
     {
         int rc = 0;
-        if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0)
+        if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0) //subscribe to all topics, to subscribe to one replace + with desired topic
             WARN("rc from MQTT subscribe is %d\n", rc); 
     }
     
+    xbee.attach(&callback);//xbee ISR callback
+    
     blink_interval = 0;
-    int count = 0;
     
     while (true)
     {
-        if (respond>0)
+        if (respond)
         {               // Publish a message every second
             if (publish(&client, &ipstack) != 0) 
                 attemptConnect(&client, &ipstack);   // if we have lost the connection
-            respond = 0;
+            respond = false;
         }
         
-        wait(3);
             
         client.yield(10);  // allow the MQTT client to receive messages
     }