Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
Diff: src/QuickstartClient.cpp
- Revision:
- 6:a022f983f94b
- Parent:
- 5:1b54a0b7b39d
- Child:
- 7:2c5d0dbd7985
--- 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); +}*/ +