Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
Revision 2:25ddff75a8c7, committed 2014-06-23
- Comitter:
- samdanbury
- Date:
- Mon Jun 23 20:12:18 2014 +0000
- Parent:
- 0:6276e9f72327
- Child:
- 3:ca5b84eb8f3b
- Commit message:
- reads in a config file as well as utilises the new topic space and clientId changes
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ConfigFile.lib Mon Jun 23 20:12:18 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/src/QuickstartClient.cpp Mon Jun 23 13:37:46 2014 +0000 +++ b/src/QuickstartClient.cpp Mon Jun 23 20:12:18 2014 +0000 @@ -13,18 +13,63 @@ #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 + string str = "iot-2/evt/status/fmt/json"; + strcpy(topic, str.c_str()); + + loadConfig(); tryMqttConnect(); } +void QuickstartClient::loadConfig() { + + ConfigFile cfg; + + char value[BUFSIZ]; + + if (cfg.read("/local/device.cfg")) { + quickstartMode = false; + + if (cfg.getValue("org", &value[0], sizeof(value))) { + stringstream ss(value); + ss >> org; + } else { + lcd.printf("No org defined in config\n"); + } + + if (cfg.getValue("type", &value[0], sizeof(value))) { + stringstream ss(value); + ss >> type; + } else { + lcd.printf("No type defined in config\n"); + } + + if (cfg.getValue("id", &value[0], sizeof(value))) { + stringstream ss(value); + ss >> id; + } else { + lcd.printf("No id defined in config\n"); + } + + if (cfg.getValue("token", &value[0], sizeof(value))) { + stringstream ss(value); + 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,16 +116,20 @@ //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[ strlen(str.c_str()) + 1]; + strcpy(clientId, str.c_str()); //Set MQTT connect options data.clientID.cstring = clientId; data.keepAliveInterval = 20; data.cleansession = 1; data.MQTTVersion = 3; + if (!quickstartMode) { + data.password.cstring = token.c_str(); + + } //Attempt MQTT connect len = MQTTSerialize_connect(buf, buflen, &data);
--- a/src/QuickstartClient.h Mon Jun 23 13:37:46 2014 +0000 +++ b/src/QuickstartClient.h Mon Jun 23 20:12:18 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[25]; + 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 Mon Jun 23 13:37:46 2014 +0000 +++ b/src/main.cpp Mon Jun 23 20:12:18 2014 +0000 @@ -52,6 +52,8 @@ float pot1; float pot2; +LocalFileSystem local("local"); + int main() { //Connect to the network