Local copy

Dependencies:   C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed

Fork of IBMIoTClientExampleForLPC1768 by Sam Danbury

Files at this revision

API Documentation at this revision

Comitter:
rajathishere
Date:
Mon Jun 30 21:10:25 2014 +0000
Parent:
5:1b54a0b7b39d
Child:
7:2c5d0dbd7985
Commit message:
hand over drop

Changed in this revision

MQTTPacket.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
src/QuickstartClient.cpp Show annotated file Show diff for this revision Revisions of this file
src/QuickstartClient.h Show annotated file Show diff for this revision Revisions of this file
src/QuickstartMessage.cpp Show annotated file Show diff for this revision Revisions of this file
src/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/MQTTPacket.lib	Tue Jun 24 10:47:12 2014 +0000
+++ b/MQTTPacket.lib	Mon Jun 30 21:10:25 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTTPacket/#68a06bea5429
+http://mbed.org/teams/mqtt/code/MQTTPacket/#3893bc7343f4
--- a/mbed.bld	Tue Jun 24 10:47:12 2014 +0000
+++ b/mbed.bld	Mon Jun 30 21:10:25 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/a9913a65894f
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/024bf7f99721
\ No newline at end of file
--- a/src/QuickstartClient.cpp	Tue Jun 24 10:47:12 2014 +0000
+++ b/src/QuickstartClient.cpp	Mon Jun 30 21:10:25 2014 +0000
@@ -12,15 +12,20 @@
 
 #include "QuickstartClient.h"
 
+int i =0;
 QuickstartClient::QuickstartClient(string mac) {
     quickstartMode = true;
     connected = false;
     macAddress = mac;
     
-    //Generate topic string
+    //Generate topic string for publish
     memcpy(topic, "iot-2/evt/status/fmt/json", 25);
     topic[25] = '\0';
     
+    //Generate topic string for subscribe
+    memcpy(subscribeTopic, "iot-2/cmd/blink/fmt/json",24);
+    subscribeTopic[24] = '\0';
+    
     loadConfig();
     
     tryMqttConnect();
@@ -34,7 +39,9 @@
     char value1[30];
     char value2[30];
     char value3[30];
-
+    lcd.cls();
+    lcd.locate(0,0);
+    
     if (cfg.read("/local/device.cfg")) {
         quickstartMode = false;
         
@@ -71,6 +78,7 @@
         type = "iotsample-mbed-lpc1768";
         id = macAddress;
     }
+    wait(5.0);
     
 }
 
@@ -124,6 +132,9 @@
     string str = string("d:") + org + ":" + type + ":" + id;
     char clientId[str.size()];
     memcpy(clientId, str.c_str(), str.size() + 1);
+    lcd.cls();
+    lcd.locate(0,0);
+    //lcd.printf("cleint id : %s",clientId);
     
     //Set MQTT connect options
     data.clientID.cstring = clientId;
@@ -131,8 +142,12 @@
     data.cleansession = 1;
     data.MQTTVersion = 3;
     if (!quickstartMode) {
+        data.username.cstring = clientId;
         //TODO: set mqtt password to the token in the config file it it is NOT in quickstart mode
-        //data.password.cstring = token;   
+        char authToken[token.size()+1];
+        memcpy(authToken, token.c_str(), token.size()+1);
+        //lcd.printf("password %s",authToken);
+        data.password.cstring = authToken;
     }
     
     //Attempt MQTT connect
@@ -140,6 +155,8 @@
     rc = 0;
     while (rc < len) {
         int rc1 = mysock.send(buf, len);
+        lcd.printf(" connect rc %d\n",rc1);
+        wait(3.0);
         if (rc1 == -1) {
             connected = false;
             break;
@@ -150,6 +167,7 @@
     if (rc == len) {
         connected = true;
     }
+    wait(0.2);
 }
 
 void QuickstartClient::publish(string thePayload) {
@@ -157,21 +175,24 @@
     int  len = 0;
     char buf[250];
     int  buflen = sizeof(buf);
-    
+
     MQTTString topicString = MQTTString_initializer;
     
     topicString.cstring = topic;
-    
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("inside 1");
     //Convert payload from string to char array
     char* payload = new char [thePayload.length()+1];
     std::strcpy (payload, thePayload.c_str());
     int payloadlen = strlen(payload);
-    
     //Attempt MQTT publish
     len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen);
     rc = 0;
     while (rc < len) {
         int rc1 = mysock.send(buf, len);
+        lcd.printf(" publish rc1 %d\n",rc1);
+        wait(3.0);
         if (rc1 == -1) {
             //If return code from MQTT publish is -1, attempt reconnect
             connected = false;
@@ -186,4 +207,40 @@
     if (payload) {
         delete payload;
     }
-}
\ No newline at end of file
+}
+
+void QuickstartClient::subscribe() {
+    int  rc = 0;
+    int  len = 0;
+    char buf[250];
+    int  buflen = sizeof(buf);
+
+    MQTTString topicString = MQTTString_initializer;
+
+    topicString.cstring = subscribeTopic;
+
+    //Attempt MQTT subscribe
+    len = MQTTSerialize_subscribe(buf, buflen, 0, 11, 1, &topicString, 0);
+    rc = 0;
+    while (rc < len) {
+        int rc1 = mysock.send(buf, len);
+        if (rc1 == -1) {
+            break;
+        } 
+        else {
+            rc += rc1;
+        }
+    }
+    wait(0.2);
+    
+   /* if (MQTTPacket_read(buf, buflen, getdata) == SUBACK)
+    {
+        lcd.printf("SUBACK");
+    }*/
+}
+/*
+int getdata(char* buf, size_t count)
+{
+    return recv(mysock, buf, (size_t)count, 0);
+}*/
+    
--- a/src/QuickstartClient.h	Tue Jun 24 10:47:12 2014 +0000
+++ b/src/QuickstartClient.h	Mon Jun 30 21:10:25 2014 +0000
@@ -23,7 +23,7 @@
 #include <sstream>
 #include <algorithm>
 
-#define IBM_IOT_BROKER "37.58.109.238"
+#define IBM_IOT_BROKER "108.168.183.11"//"108.168.183.11" //-wdc01-2 msproxy //"208.43.110.186" //-wdc01-2 quickstart-msproxy  //"37.58.109.238"=old quickstart
 #define IBM_IOT_PORT 1883
 
 using namespace std;
@@ -35,6 +35,7 @@
         TCPSocketConnection mysock;
         string macAddress;
         char topic[30];
+        char subscribeTopic[30];
         bool quickstartMode;
         
         string org;
@@ -49,4 +50,6 @@
         void publish(string payload);
         bool getConnection();
         int reconnectDelay(int attempt);
+        void subscribe();
+        //int* processEvents(char* buf, size_t count);
 };
\ No newline at end of file
--- a/src/QuickstartMessage.cpp	Tue Jun 24 10:47:12 2014 +0000
+++ b/src/QuickstartMessage.cpp	Mon Jun 30 21:10:25 2014 +0000
@@ -23,10 +23,8 @@
 
 void QuickstartMessage::add(string key, float value) {
     char temp[7];
-    
     //Format value to an accuracy of four decimal places
     sprintf(temp, "%0.4f", value);
-    
     //Add datapoint to map of datapoints
     data[key] = temp;
 }
--- a/src/main.cpp	Tue Jun 24 10:47:12 2014 +0000
+++ b/src/main.cpp	Mon Jun 30 21:10:25 2014 +0000
@@ -73,37 +73,41 @@
     joystickPos = "CENTRE";
     
     QuickstartClient* c = new QuickstartClient(mac);
+    lcd.cls();
+    lcd.locate(0,0);
+        
+    lcd.printf("Mac address: %s\n", mac);
+    wait(3.0);
     
+    //subscribe(*c);
+    c->subscribe();
     while(1) 
     {
         //Initialize lcd
-        lcd.cls();
-        lcd.locate(0,0);
-        
-        lcd.printf("Mac address: %s\n", mac);
-        
         //Flash led to show message has been sent successfully
         led1 = 1;
-        
+
         //Construct quickstart message with desired datapoints
-        QuickstartMessage* m = new QuickstartMessage();
-        m->add("accelX", MMA.x());
-        m->add("accelY", MMA.y());
-        m->add("accelZ", MMA.z());
-        m->add("temp", tmp.read());
-        m->add("joystick", joystickPos);
+        //QuickstartMessage* m = new QuickstartMessage();
+        
+        //m->add("accelX", MMA.x());
         
-        pot1 = ain1;
-        pot2 = ain2;
-        m->add("potentiometer1", pot1);
-        m->add("potentiometer2", pot2);
+        //m->add("accelY", MMA.y());
+        //m->add("accelZ", MMA.z());
+        //m->add("temp", tmp.read());
+        //m->add("joystick", joystickPos);
+        //pot1 = ain1;
+        //pot2 = ain2;
+        //m->add("potentiometer1", pot1);
+        //m->add("potentiometer2", pot2);
         
         //Message is converted from datapoints into json format and then published
-        c->publish(m->convertToJson());
-        
-        if (m) {
-            delete m;
-        }
+        c->publish("{\"d\":{\"myName\":\"Mymbed\",\"temperature\":43,\"acelX\":0.3644,\"acelY\":0.9344,\"acelZ\":0.9334}}");//c->publish(m->convertToJson());
+        lcd.printf("After");
+        wait(3.0);
+        //if (m) {
+          //  delete m;
+        //}
         
         led1 = 0;
         
@@ -129,4 +133,4 @@
         else
             joystickPos = "CENTRE";
     }
-}
\ No newline at end of file
+}