Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
Diff: src/main.cpp
- Revision:
- 11:0fa63599f5a0
- Parent:
- 9:cbabd85f2d5d
- Child:
- 13:65e87bd958bd
diff -r 5555250b5ae5 -r 0fa63599f5a0 src/main.cpp --- a/src/main.cpp Tue Jul 01 19:14:45 2014 +0000 +++ b/src/main.cpp Wed Jul 02 10:37:56 2014 +0000 @@ -19,6 +19,7 @@ #include "MMA7660.h" #include "C12832_lcd.h" #include "QuickstartClient.h" +#include "cJSON.h" #include <string> @@ -45,6 +46,7 @@ BusIn Right(p16); string joystickPos; void joystickThread(void const *args); +int getCount(char* message); //Potentiometer AnalogIn ain1(p19); @@ -80,16 +82,29 @@ //QuickstartClient* c = new QuickstartClient(mac); c = new QuickstartClient(mac); - (c->mysock).set_blocking(true, 5000); + (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; + + if (MQTTDeserialize_connack(&connack_rc, buf, buflen) != 1 || connack_rc != 0) + { + lcd.printf("Unable to connect, return code %d\n", connack_rc); + goto exit; + } + } + else + { + lcd.printf("rc conn, %d\n", res); + goto exit; + } int rc = c->subscribe(); - while(res != 9) { res = MQTTPacket_read(buf, buflen, getdata); lcd.printf("res, %d\n", res); wait(2.0); - } if (res == SUBACK) /* wait for suback */ { int submsgid; @@ -165,9 +180,28 @@ rc = MQTTDeserialize_publish(&dup, &qos, &retained, &msgid, &receivedTopic, &payload_in, &payloadlen_in, buf, buflen); lcd.printf("message arrived %s\n", payload_in); - int count = 2; //default number of blinks - char num[2]; - //TODO parse request and get count + wait(4.0); + char message[50]; + int i =0, charNums = 0; + while(*payload_in && charNums <2) { + charNums = 0; + if(*payload_in == '\'') { + charNums++; + while(*payload_in != '\'') { + message[i] = *payload_in; + i++; payload_in++; + } + charNums++; + } + 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) { led4 = 1; wait(0.5); @@ -177,12 +211,26 @@ } } - wait(1.0); + wait(4.0); } exit: eth.disconnect(); } +int getCount(char* message) { + cJSON *json; + int count = 2; + json = cJSON_Parse(message); + if (!json) { + lcd.printf("Error before: [%s]\n", cJSON_GetErrorPtr()); + wait(2.0); + } else { + count = cJSON_GetObjectItem(json, "count")->valueint; + cJSON_Delete(json); + } + return count; +} + void joystickThread(void const *args) { while (1) { if (Down)