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

Files at this revision

API Documentation at this revision

Comitter:
skoda
Date:
Fri Nov 13 13:15:31 2015 +0000
Parent:
19:33804f085577
Child:
21:bb0d9f12da01
Commit message:
working version 1.92

Changed in this revision

K64F.h Show annotated file Show diff for this revision Revisions of this file
LPC1768.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/K64F.h	Mon Nov 09 13:33:45 2015 +0000
+++ b/K64F.h	Fri Nov 13 13:15:31 2015 +0000
@@ -23,11 +23,18 @@
 BusOut r (D5);
 BusOut g (D9);
 BusOut b (D8);
-MMA7660 MMA(PTE25, PTE24);
-LM75B sensor(PTE25, PTE24);
+//MMA7660 MMA(PTE25, PTE24);        //matjaz: ne uporablam !!!
+//LM75B sensor(PTE25, PTE24);
 DigitalIn Up(A2); DigitalIn Down(A3); DigitalIn Right(A4); DigitalIn Left(A5); DigitalIn Click(D4);
 AnalogIn ain1(A0); AnalogIn ain2(A1);
 
+//Matjaz
+//I2C masterI2C(PTE25,PTE24);        //I2C (PinName sda, PinName scl) 
+I2CSlave slave(PTE25, PTE24);
+
+
+
+
 #define LED2_OFF 1
 #define LED2_ON 0
 
--- a/LPC1768.h	Mon Nov 09 13:33:45 2015 +0000
+++ b/LPC1768.h	Fri Nov 13 13:15:31 2015 +0000
@@ -23,8 +23,8 @@
 PwmOut r(p23);
 PwmOut g(p24);
 PwmOut b(p25);
-MMA7660 MMA(p28, p27);
-LM75B sensor(p28, p27);
+//MMA7660 MMA(p28, p27);
+//LM75B sensor(p28, p27);
 DigitalIn Down(p12);
 DigitalIn Left(p13);
 DigitalIn Click(p14);
@@ -32,6 +32,9 @@
 DigitalIn Right(p16);
 AnalogIn ain1(p19);
 AnalogIn ain2(p20);
+//Matjaz
+//I2CSlave slave(p28, p27); //p9sda p10scl
+I2CSlave slave(p9, p10);
 
 #define LED2_OFF 0
 #define LED2_ON 1
--- a/main.cpp	Mon Nov 09 13:33:45 2015 +0000
+++ b/main.cpp	Fri Nov 13 13:15:31 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 "mbedFly"                        // 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
 
@@ -75,12 +75,27 @@
 
 char* joystickPos = "CENTRE";
 int blink_interval = 0;
+int respond = 0;
+char* response = "00000000000000000000"; 
 
 char* ip_addr = "";
 char* gateway_addr = "";
 char* host_addr = "";
 int connectTimeout = 1000;
-char* MSGpayload = "";
+
+uint16_t    lux = 0;
+uint16_t    rx = 0;
+uint16_t    t = 0;
+int16_t     ax = 0;
+int16_t     ay = 0;
+int16_t     az = 0;
+int16_t     deviceID = 0;
+
+
+Serial pc(USBTX, USBRX);
+
+
+
 
 // If we wanted to manually set the MAC address,
 // this is how to do it. In this example, we take
@@ -155,6 +170,88 @@
     }
 }
 
+//matjaz dodamo nit za komunikacijo preko i2c
+void firefly_com (void const *args)
+{
+
+    char buf[100];                     
+    int i = 0;
+
+
+    
+
+    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();
+         
+         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";                     
+                 break;
+                 
+             case I2CSlave::WriteGeneral:
+             
+                 slave.read(buf, 10);
+                 //LOG("Napacen case\r\n");
+                 break;
+                 
+             case I2CSlave::WriteAddressed:
+             
+                 slave.read(buf, 99);
+                 //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);
+                 
+                 deviceID   = ((uint16_t)buf[3] << 8) | buf[3];       //device ID from whom the message was received 
+                 deviceID = (deviceID >> 6);
+                 respond = 1;
+                 //rh   = ((uint16_t)buf[4] << 8) | buf[5];
+                 
+
+                 //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);
+                
+                 break;
+                 
+         } // switch .. 
+         //Thread::wait(1);   
+        
+    }
+
+
+}
+
 
 void printMenu(int menuItem) 
 {
@@ -229,10 +326,6 @@
             sprintf(line1, "Current Timeout:");
             sprintf(line2, "%d ms", connectTimeout);
             break;
-        case 10:
-            sprintf(line1, "Message payload:");
-            sprintf(line2, "%s", MSGpayload);
-            break;
     }
     
     if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0)
@@ -255,7 +348,7 @@
     if (Down)
     {
         joystickPos = "DOWN";
-        if (menuItem >= 0 && menuItem < 10)
+        if (menuItem >= 0 && menuItem < 9)
             printMenu(++menuItem);
     } 
     else if (Left)
@@ -265,7 +358,7 @@
     else if (Up)
     {
         joystickPos = "UP";
-        if (menuItem <= 10 && menuItem > 0)
+        if (menuItem <= 9 && menuItem > 0)
             printMenu(--menuItem);
     }
     else if (Right)
@@ -350,7 +443,9 @@
     if ((rc = client->connect(data)) == 0) 
     {       
         connected = true;
-        green();    
+        //green();    
+        //matjaz zelja je da je off
+        off();   
         displayMessage("Connected");
         wait(1);
         displayMessage("Scroll with joystick");
@@ -406,13 +501,35 @@
 
 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";
             
     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);
     message.qos = MQTT::QOS0;
     message.retained = false;
     message.dup = false;
@@ -441,12 +558,9 @@
     char topic[md.topicName.lenstring.len + 1];
     
     sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
-    if(topic != NULL){
-        MSGpayload = topic;
-    }
     
     LOG("Message arrived on topic %s: %.*s\n",  topic, message.payloadlen, message.payload);
-    
+          
     // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
     char* start = strstr(topic, "/cmd/") + 5;
     int len = strstr(topic, "/fmt/") - start;
@@ -455,7 +569,7 @@
     {
         char payload[message.payloadlen + 1];
         sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
-        
+    
         char* pos = strchr(payload, '}');
         if (pos != NULL)
         {
@@ -466,23 +580,45 @@
                 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
             }
         }
-    }
-    else
+    }else if (memcmp(start, "respond", len) == 0)
+    {
+        char payload[message.payloadlen + 1];
+        sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
+    
+        char* pos = strchr(payload, '}');
+        if (pos != NULL)
+        {
+            *pos = '\0';
+            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);
+            }
+        }
+    }else
         WARN("Unsupported command: %.*s\n", len, start);
 }
 
 
 int main()
 {    
+    pc.baud(115200);
+    
     quickstartMode = (strcmp(org, "quickstart") == 0);
 
     lcd.set_font((unsigned char*) Arial12x12);  // Set a nice font for the LCD screen
     
     led2 = LED2_OFF; // K64F: turn off the main board LED 
     
+    //matjaz set I2C parameters
+    slave.address(0xC2);
+    slave.frequency(400000);
+    
     displayMessage("Connecting");
     Thread yellow_thread(flashing_yellow);
     Thread menu_thread(menu_loop);  
+    Thread firefly (firefly_com);
     
     LOG("***** IBM IoT Client Ethernet Example *****\n");
     MQTTEthernet ipstack;
@@ -511,19 +647,62 @@
     
     blink_interval = 0;
     int count = 0;
+
+    pc.baud(115200);
     while (true)
     {
-        if (++count == 100)
+        if (respond>0)
         {               // Publish a message every second
             if (publish(&client, &ipstack) != 0) 
                 attemptConnect(&client, &ipstack);   // if we have lost the connection
-            count = 0;
+            respond = 0;
         }
         
-        if (blink_interval == 0)
+        /*if (blink_interval == 0)
             led2 = LED2_OFF;
         else if (count % blink_interval == 0)
-            led2 = !led2;
+            led2 = !led2;*/
+            
         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 ..    
+        
+    }
+*/    
 }