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:
samdanbury
Date:
Tue Jun 24 10:35:40 2014 +0000
Parent:
1:dd948ce80918
Parent:
3:ca5b84eb8f3b
Child:
5:1b54a0b7b39d
Commit message:
merge changes

Changed in this revision

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/ConfigFile.lib	Tue Jun 24 10:35:40 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/src/QuickstartClient.cpp	Tue Jun 24 10:24:33 2014 +0000
+++ b/src/QuickstartClient.cpp	Tue Jun 24 10:35:40 2014 +0000
@@ -13,18 +13,67 @@
 #include "QuickstartClient.h"
 
 QuickstartClient::QuickstartClient(string mac) {
+    quickstartMode = true;
     connected = false;
     macAddress = mac;
     
-    //Generate topic string from mac address
-    memcpy(topic, "iot-1/d/", 8);
-    memcpy(topic + 8, macAddress.c_str(), macAddress.size());
-    memcpy(topic + 8 + macAddress.size(), "/evt/mbed-quickstart/json", 25);
-    topic[8 + macAddress.size() + 25] = '\0';
+    //Generate topic string
+    memcpy(topic, "iot-2/evt/status/fmt/json", 25);
+    topic[25] = '\0';
+    
+    loadConfig();
     
     tryMqttConnect();
 }
 
+void QuickstartClient::loadConfig() {
+    
+    ConfigFile cfg;
+    
+    char value[30];
+    char value1[30];
+    char value2[30];
+    char value3[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("token", value3, sizeof(value3))) {
+            stringstream ss(value3);
+            ss >> token;
+        } else {
+            lcd.printf("No token defined in config\n");
+        }
+        
+    } else {
+        org = "quickstart";
+        type = "iotsample-mbed-lpc1768";
+        id = macAddress;
+    }
+    
+}
+
 int QuickstartClient::reconnectDelay(int i) {
     if (i < 10) {
         return 3; //First 10 attempts try within 3 seconds
@@ -71,10 +120,10 @@
     //Connect to TCP socket
     mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT);
     
-    //Construct clientId from mac address
-    char clientId[23];
-    memcpy(clientId, "quickstart:", 11);
-    memcpy(clientId + 11, macAddress.c_str(), macAddress.size() + 1);
+    //Construct client ID
+    string str = string("d:") + org + ":" + type + ":" + id;
+    char clientId[str.size()];
+    memcpy(clientId, str.c_str(), str.size() + 1);
     
     //Set MQTT connect options
     data.clientID.cstring = clientId;
--- a/src/QuickstartClient.h	Tue Jun 24 10:24:33 2014 +0000
+++ b/src/QuickstartClient.h	Tue Jun 24 10:35:40 2014 +0000
@@ -12,17 +12,18 @@
 
 #include "MQTTPacket.h"
 #include "rtos.h"
+#include "mbed.h"
 #include "EthernetInterface.h"
 #include "C12832_lcd.h"
+#include "ConfigFile.h"
 
-#include <iostream>
 #include <string>
 #include <vector>
 #include <map>
 #include <sstream>
 #include <algorithm>
 
-#define IBM_IOT_BROKER "m.qs.internetofthings.ibmcloud.com"
+#define IBM_IOT_BROKER "37.58.109.238"
 #define IBM_IOT_PORT 1883
 
 using namespace std;
@@ -33,12 +34,19 @@
         C12832_LCD lcd;
         TCPSocketConnection mysock;
         string macAddress;
-        char topic[55];
-        int reconnectDelay(int attempt);
+        char topic[30];
+        bool quickstartMode;
+        
+        string org;
+        string type;
+        string id;
+        string token;
                 
         QuickstartClient(string mac);
+        void loadConfig();
         void tryMqttConnect();
         void connect();
         void publish(string payload);
         bool getConnection();
+        int reconnectDelay(int attempt);
 };
\ No newline at end of file
--- a/src/main.cpp	Tue Jun 24 10:24:33 2014 +0000
+++ b/src/main.cpp	Tue Jun 24 10:35:40 2014 +0000
@@ -52,6 +52,8 @@
 float pot1;
 float pot2;
 
+LocalFileSystem local("local");
+
 int main()
 {
     //RAJATH's COMMIT