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:
Wed Jul 02 15:07:40 2014 +0000
Parent:
12:e9ff8869a99d
Commit message:
Rename QuickstartClient to ExampleClient

Changed in this revision

src/ExampleClient.cpp Show annotated file Show diff for this revision Revisions of this file
src/ExampleClient.h Show annotated file Show diff for this revision Revisions of this file
src/QuickstartClient.cpp Show diff for this revision Revisions of this file
src/QuickstartClient.h Show diff for this revision Revisions of this file
src/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ExampleClient.cpp	Wed Jul 02 15:07:40 2014 +0000
@@ -0,0 +1,237 @@
+/*******************************************************************************
+* Copyright (c) 2014 IBM Corporation and other Contributors.
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+*
+* Contributors: Sam Danbury
+* IBM - Initial Contribution
+*******************************************************************************/
+
+#include "ExampleClient.h"
+
+int i =0;
+ExampleClient::ExampleClient(string mac) {
+    quickstartMode = true;
+    connected = false;
+    macAddress = mac;
+    
+    //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();
+}
+
+void ExampleClient::loadConfig() {
+    
+    ConfigFile cfg;
+    
+    char value[30];
+    char value1[30];
+    char value2[30];
+    char value3[30];
+    char value4[30];
+    if (cfg.read("/local/device.cfg")) {
+        quickstartMode = false;
+        
+        if (cfg.getValue("org", value, sizeof(value))) {
+            stringstream ss(value);
+            ss >> org;
+        } else {
+            lcd.printf("No org defined in config\n");
+        }
+        
+        if (cfg.getValue("type", value1, sizeof(value1))) {
+            stringstream ss(value1);
+            ss >> type;
+        } else {
+            lcd.printf("No type defined in config\n");
+        }
+        
+        if (cfg.getValue("id", value2, sizeof(value2))) {
+            stringstream ss(value2);
+            ss >> id;
+        } else {
+            lcd.printf("No id defined in config\n");
+        }
+        
+        if (cfg.getValue("auth-method", value3, sizeof(value3))) {
+            stringstream ss(value3);
+            ss >> authMethod;
+        } else {
+            lcd.printf("No auth method defined in config\n");
+        }
+        
+        if (cfg.getValue("auth-token", value4, sizeof(value4))) {
+            stringstream ss(value4);
+            ss >> token;
+        } else {
+            lcd.printf("No token defined in config\n");
+        }
+        
+    } else {
+        org = "quickstart";
+        type = "iotsample-mbed-lpc1768";
+        id = macAddress;
+    }
+    wait(5.0);
+    
+}
+
+int ExampleClient::reconnectDelay(int i) {
+    if (i < 10) {
+        return 3; //First 10 attempts try within 3 seconds
+    } else if (i < 20) {
+        return 60; //Next 10 attempts retry after every 1 minute
+    } else {
+        return 600; //After 20 attempts, retry every 10 minutes
+    }
+}
+
+void ExampleClient::tryMqttConnect() {
+    int retryAttempt = 0;
+    
+    //Reinstantiate TCP socket connection object
+    mysock = TCPSocketConnection();
+    
+    while (connected == false) {
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.printf("Trying to connect...");
+        
+        //Based on number of connection attempts, determine timeout
+        int connDelayTimeout = reconnectDelay(++retryAttempt);
+        
+        //Attempt to reconnect
+        connect();
+        
+        //If connection was not established, continue retry
+        if (connected == false) {
+            wait(connDelayTimeout);
+        } else {
+            break;
+        }
+    }
+}
+
+void ExampleClient::connect() {
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    int  rc = 0;
+    int  len = 0;
+    char buf[200];
+    int  buflen = sizeof(buf);
+    
+    //Connect to TCP socket
+    mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
+    
+    //Construct client ID
+    string str = string("d:") + org + ":" + type + ":" + id;
+    char clientId[str.size()+1];
+    memcpy(clientId, str.c_str(), str.size() + 1);
+    
+    //Set MQTT connect options
+    data.clientID.cstring = clientId;
+    data.keepAliveInterval = 20;
+    data.cleansession = 1;
+    data.MQTTVersion = 3;
+    if (!quickstartMode) {
+        if ( ! authMethod.compare("token") ) {
+            data.username.cstring = clientId;
+            char* authToken= new char[token.size()];
+            memcpy(authToken, token.c_str(), token.size()+1);
+            data.password.cstring =  authToken;
+        }
+        else {
+            lcd.printf("Exiting\n");
+            exit(-1);
+        }
+    }
+    
+    //Attempt MQTT connect
+    len = MQTTSerialize_connect(buf, buflen, &data);
+    rc = 0;
+    while (rc < len) {
+        int rc1 = mysock.send(buf, len);
+        if (rc1 == -1) {
+            connected = false;
+            break;
+        } else {
+            rc += rc1;
+        }
+    }
+    if (rc == len) {
+        connected = true;
+    }
+    wait(0.2);
+}
+
+void ExampleClient::publish(string thePayload) {
+    int  rc = 0;
+    int  len = 0;
+    char buf[250];
+    int  buflen = sizeof(buf);
+
+    MQTTString topicString = MQTTString_initializer;
+    
+    topicString.cstring = topic;
+    
+    //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);
+        wait(3.0);
+        if (rc1 == -1) {
+            //If return code from MQTT publish is -1, attempt reconnect
+            connected = false;
+            tryMqttConnect();
+            break;
+        } else {
+            rc += rc1;
+        }
+    }
+    wait(0.2);
+    
+    if (payload) {
+        delete payload;
+    }
+}
+
+int ExampleClient::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, 1, 1, &topicString, 0);
+    rc = 0;
+    while (rc < len) {
+        int rc1 = mysock.send(buf, len);
+        if (rc1 == -1) {
+            break;
+        } 
+        else {
+            rc += rc1;
+        }
+    }
+    wait(0.2);
+    return rc;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ExampleClient.h	Wed Jul 02 15:07:40 2014 +0000
@@ -0,0 +1,56 @@
+/*******************************************************************************
+* Copyright (c) 2014 IBM Corporation and other Contributors.
+*
+* All rights reserved. This program and the accompanying materials
+* are made available under the terms of the Eclipse Public License v1.0
+* which accompanies this distribution, and is available at
+* http://www.eclipse.org/legal/epl-v10.html
+*
+* Contributors: Sam Danbury
+* IBM - Initial Contribution
+*******************************************************************************/
+
+#include "MQTTPacket.h"
+#include "rtos.h"
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "C12832_lcd.h"
+#include "ConfigFile.h"
+
+#include <string>
+#include <vector>
+#include <map>
+#include <sstream>
+#include <algorithm>
+
+#define IBM_IOT_BROKER "5.153.46.201"//"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;
+
+class ExampleClient {
+    public:
+        bool connected;
+        C12832_LCD lcd;
+        TCPSocketConnection mysock;
+        string macAddress;
+        char topic[30];
+        char subscribeTopic[30];
+        bool quickstartMode;
+        
+        string org;
+        string type;
+        string id;
+        string authMethod;
+        string token;
+                
+        ExampleClient(string mac);
+        void loadConfig();
+        void tryMqttConnect();
+        void connect();
+        void publish(string payload);
+        bool getConnection();
+        int reconnectDelay(int attempt);
+        int subscribe();
+        //int* processEvents(char* buf, size_t count);
+};
\ No newline at end of file
--- a/src/QuickstartClient.cpp	Wed Jul 02 12:02:47 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,237 +0,0 @@
-/*******************************************************************************
-* Copyright (c) 2014 IBM Corporation and other Contributors.
-*
-* All rights reserved. This program and the accompanying materials
-* are made available under the terms of the Eclipse Public License v1.0
-* which accompanies this distribution, and is available at
-* http://www.eclipse.org/legal/epl-v10.html
-*
-* Contributors: Sam Danbury
-* IBM - Initial Contribution
-*******************************************************************************/
-
-#include "QuickstartClient.h"
-
-int i =0;
-QuickstartClient::QuickstartClient(string mac) {
-    quickstartMode = true;
-    connected = false;
-    macAddress = mac;
-    
-    //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();
-}
-
-void QuickstartClient::loadConfig() {
-    
-    ConfigFile cfg;
-    
-    char value[30];
-    char value1[30];
-    char value2[30];
-    char value3[30];
-    char value4[30];
-    if (cfg.read("/local/device.cfg")) {
-        quickstartMode = false;
-        
-        if (cfg.getValue("org", value, sizeof(value))) {
-            stringstream ss(value);
-            ss >> org;
-        } else {
-            lcd.printf("No org defined in config\n");
-        }
-        
-        if (cfg.getValue("type", value1, sizeof(value1))) {
-            stringstream ss(value1);
-            ss >> type;
-        } else {
-            lcd.printf("No type defined in config\n");
-        }
-        
-        if (cfg.getValue("id", value2, sizeof(value2))) {
-            stringstream ss(value2);
-            ss >> id;
-        } else {
-            lcd.printf("No id defined in config\n");
-        }
-        
-        if (cfg.getValue("auth-method", value3, sizeof(value3))) {
-            stringstream ss(value3);
-            ss >> authMethod;
-        } else {
-            lcd.printf("No auth method defined in config\n");
-        }
-        
-        if (cfg.getValue("auth-token", value4, sizeof(value4))) {
-            stringstream ss(value4);
-            ss >> token;
-        } else {
-            lcd.printf("No token defined in config\n");
-        }
-        
-    } else {
-        org = "quickstart";
-        type = "iotsample-mbed-lpc1768";
-        id = macAddress;
-    }
-    wait(5.0);
-    
-}
-
-int QuickstartClient::reconnectDelay(int i) {
-    if (i < 10) {
-        return 3; //First 10 attempts try within 3 seconds
-    } else if (i < 20) {
-        return 60; //Next 10 attempts retry after every 1 minute
-    } else {
-        return 600; //After 20 attempts, retry every 10 minutes
-    }
-}
-
-void QuickstartClient::tryMqttConnect() {
-    int retryAttempt = 0;
-    
-    //Reinstantiate TCP socket connection object
-    mysock = TCPSocketConnection();
-    
-    while (connected == false) {
-        lcd.cls();
-        lcd.locate(0,0);
-        lcd.printf("Trying to connect...");
-        
-        //Based on number of connection attempts, determine timeout
-        int connDelayTimeout = reconnectDelay(++retryAttempt);
-        
-        //Attempt to reconnect
-        connect();
-        
-        //If connection was not established, continue retry
-        if (connected == false) {
-            wait(connDelayTimeout);
-        } else {
-            break;
-        }
-    }
-}
-
-void QuickstartClient::connect() {
-    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
-    int  rc = 0;
-    int  len = 0;
-    char buf[200];
-    int  buflen = sizeof(buf);
-    
-    //Connect to TCP socket
-    mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
-    
-    //Construct client ID
-    string str = string("d:") + org + ":" + type + ":" + id;
-    char clientId[str.size()+1];
-    memcpy(clientId, str.c_str(), str.size() + 1);
-    
-    //Set MQTT connect options
-    data.clientID.cstring = clientId;
-    data.keepAliveInterval = 20;
-    data.cleansession = 1;
-    data.MQTTVersion = 3;
-    if (!quickstartMode) {
-        if ( ! authMethod.compare("token") ) {
-            data.username.cstring = clientId;
-            char* authToken= new char[token.size()];
-            memcpy(authToken, token.c_str(), token.size()+1);
-            data.password.cstring =  authToken;
-        }
-        else {
-            lcd.printf("Exiting\n");
-            exit(-1);
-        }
-    }
-    
-    //Attempt MQTT connect
-    len = MQTTSerialize_connect(buf, buflen, &data);
-    rc = 0;
-    while (rc < len) {
-        int rc1 = mysock.send(buf, len);
-        if (rc1 == -1) {
-            connected = false;
-            break;
-        } else {
-            rc += rc1;
-        }
-    }
-    if (rc == len) {
-        connected = true;
-    }
-    wait(0.2);
-}
-
-void QuickstartClient::publish(string thePayload) {
-    int  rc = 0;
-    int  len = 0;
-    char buf[250];
-    int  buflen = sizeof(buf);
-
-    MQTTString topicString = MQTTString_initializer;
-    
-    topicString.cstring = topic;
-    
-    //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);
-        wait(3.0);
-        if (rc1 == -1) {
-            //If return code from MQTT publish is -1, attempt reconnect
-            connected = false;
-            tryMqttConnect();
-            break;
-        } else {
-            rc += rc1;
-        }
-    }
-    wait(0.2);
-    
-    if (payload) {
-        delete payload;
-    }
-}
-
-int 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, 1, 1, &topicString, 0);
-    rc = 0;
-    while (rc < len) {
-        int rc1 = mysock.send(buf, len);
-        if (rc1 == -1) {
-            break;
-        } 
-        else {
-            rc += rc1;
-        }
-    }
-    wait(0.2);
-    return rc;
-}
--- a/src/QuickstartClient.h	Wed Jul 02 12:02:47 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-/*******************************************************************************
-* Copyright (c) 2014 IBM Corporation and other Contributors.
-*
-* All rights reserved. This program and the accompanying materials
-* are made available under the terms of the Eclipse Public License v1.0
-* which accompanies this distribution, and is available at
-* http://www.eclipse.org/legal/epl-v10.html
-*
-* Contributors: Sam Danbury
-* IBM - Initial Contribution
-*******************************************************************************/
-
-#include "MQTTPacket.h"
-#include "rtos.h"
-#include "mbed.h"
-#include "EthernetInterface.h"
-#include "C12832_lcd.h"
-#include "ConfigFile.h"
-
-#include <string>
-#include <vector>
-#include <map>
-#include <sstream>
-#include <algorithm>
-
-#define IBM_IOT_BROKER "5.153.46.201"//"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;
-
-class QuickstartClient {
-    public:
-        bool connected;
-        C12832_LCD lcd;
-        TCPSocketConnection mysock;
-        string macAddress;
-        char topic[30];
-        char subscribeTopic[30];
-        bool quickstartMode;
-        
-        string org;
-        string type;
-        string id;
-        string authMethod;
-        string token;
-                
-        QuickstartClient(string mac);
-        void loadConfig();
-        void tryMqttConnect();
-        void connect();
-        void publish(string payload);
-        bool getConnection();
-        int reconnectDelay(int attempt);
-        int subscribe();
-        //int* processEvents(char* buf, size_t count);
-};
\ No newline at end of file
--- a/src/main.cpp	Wed Jul 02 12:02:47 2014 +0000
+++ b/src/main.cpp	Wed Jul 02 15:07:40 2014 +0000
@@ -18,7 +18,7 @@
 #include "LM75B.h"
 #include "MMA7660.h"
 #include "C12832_lcd.h"
-#include "QuickstartClient.h"
+#include "ExampleClient.h"
 #include "cJSON.h"
 
 #include <string>
@@ -53,7 +53,7 @@
 AnalogIn ain2(p20);
 float pot1;
 float pot2;
-QuickstartClient* c;
+ExampleClient* c;
 LocalFileSystem local("local");
 
 int getdata(char* buf, int count)
@@ -80,11 +80,10 @@
     Thread jThd(joystickThread);
     joystickPos = "CENTRE";
     
-    //QuickstartClient* c = new QuickstartClient(mac);
-    c = new QuickstartClient(mac);
+    //ExampleClient* c = new ExampleClient(mac);
+    c = new ExampleClient(mac);
     (c->mysock).set_blocking(true, 50000);
     int res = MQTTPacket_read(buf, buflen, getdata);
-    lcd.printf("res, %d\n", res);
     if ( res == CONNACK)
     {
         int connack_rc;
@@ -103,8 +102,6 @@
     
     int rc = c->subscribe();
     res = MQTTPacket_read(buf, buflen, getdata);
-    lcd.printf("res, %d\n", res);
-    wait(2.0);
     if (res == SUBACK)    /* wait for suback */
     {
         int submsgid;
@@ -165,7 +162,6 @@
         led1 = 0;
         //TODO get subscription working consistently
         res = MQTTPacket_read(buf, buflen, getdata);
-        lcd.printf("pub res : %d",res);
         if (  res == PUBLISH )
         {
             int dup;
@@ -179,8 +175,6 @@
 
             rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic,
                     &payload_in, &payloadlen_in, buf, buflen);
-            lcd.printf("message arrived %s\n", payload_in);
-             wait(4.0);
             char message[50];
             int i =0, charNums = 0;
             while(*payload_in && charNums <2) {
@@ -196,10 +190,6 @@
                 payload_in++;
             }
             message[i] = '\0';
-            lcd.cls();
-            lcd.locate(0,0);
-            lcd.printf(" Message %s", message);
-            wait(4.0);
             int count = getCount(payload_in);
             lcd.printf("Count %d\n", count);
             while(count) {
@@ -211,7 +201,7 @@
             }
             
         }
-        wait(4.0);
+        wait(1.0);
     }
 exit:
     eth.disconnect();