Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
Diff: src/main.cpp
- Revision:
- 9:cbabd85f2d5d
- Parent:
- 8:e58e10ca4352
- Child:
- 11:0fa63599f5a0
--- a/src/main.cpp Tue Jul 01 10:08:39 2014 +0000 +++ b/src/main.cpp Tue Jul 01 19:10:47 2014 +0000 @@ -19,7 +19,6 @@ #include "MMA7660.h" #include "C12832_lcd.h" #include "QuickstartClient.h" -#include "QuickstartMessage.h" #include <string> @@ -30,6 +29,7 @@ //LED DigitalOut led1(LED1); +DigitalOut led4(LED4); //Accelerometer MMA7660 MMA(p28, p27); @@ -51,12 +51,18 @@ AnalogIn ain2(p20); float pot1; float pot2; +QuickstartClient* c; +LocalFileSystem local("local"); -LocalFileSystem local("local"); +int getdata(char* buf, int count) +{ + return (c->mysock).receive(buf, (size_t)count); +} int main() { - //RAJATH's COMMIT + char buf[250]; + int buflen = sizeof(buf); //Connect to the network EthernetInterface eth; eth.init(); @@ -72,9 +78,36 @@ Thread jThd(joystickThread); joystickPos = "CENTRE"; - QuickstartClient* c = new QuickstartClient(mac); + //QuickstartClient* c = new QuickstartClient(mac); + c = new QuickstartClient(mac); + (c->mysock).set_blocking(true, 5000); + int res = MQTTPacket_read(buf, buflen, getdata); + lcd.printf("res, %d\n", res); - //c->subscribe(); + 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; + int subcount; + int granted_qos; + + rc = MQTTDeserialize_suback(&submsgid, 1, &subcount, &granted_qos, buf, buflen); + if (granted_qos != 0) + { + lcd.printf("granted qos != 0, %d\n", granted_qos); + goto exit; + } + } + else { + lcd.printf("rc subs 2, %d\n", rc); + goto exit; + } + char value[10]; //string data_points = ""; while(1) @@ -86,52 +119,67 @@ //Flash led to show message has been sent successfully led1 = 1; string data_points = "\"myName\": \"IoT mbed\""; - //Construct quickstart message with desired datapoints - //QuickstartMessage* m = new QuickstartMessage(); - //m->add("accelX", MMA.x()); sprintf(value, "%0.4f", MMA.x()); data_points+=",\"accelX\":"; data_points+=value; - //m->add("accelY", MMA.y()); sprintf(value, "%0.4f", MMA.y()); data_points+=",\"accelY\":"; data_points+=value; - //m->add("accelZ", MMA.z()); sprintf(value, "%0.4f", MMA.z()); data_points+=",\"accelZ\":"; data_points+=value; - //m->add("temp", tmp.read()); sprintf(value, "%0.4f", tmp.read()); data_points+=",\"temp\":"; data_points+=value; - //m->add("joystick", joystickPos); data_points+=",\"joystick\":" + joystickPos; pot1 = ain1; pot2 = ain2; - //m->add("potentiometer1", pot1); sprintf(value, "%0.4f", pot1); data_points+=",\"potentiometer1\":"; data_points+=value; - //m->add("potentiometer2", pot2); sprintf(value, "%0.4f", pot1); data_points+=",\"potentiometer1\":"; data_points+=value; string message = "{\"d\":{" + data_points + "}}"; - //Message is converted from datapoints into json format and then published - c->publish(message);//c->publish(m->convertToJson()); - //if (m) { - // delete m; - //} - + //TODO Get both publish and subscribe working togather + //c->publish(message);//c->publish(m->convertToJson()); + led1 = 0; - - //Message published every second - wait(1); + //TODO get subscription working consistently + res = MQTTPacket_read(buf, buflen, getdata); + lcd.printf("pub res : %d",res); + if ( res == PUBLISH ) + { + int dup; + int qos; + int retained; + int msgid; + int payloadlen_in; + char* payload_in; + int rc; + MQTTString receivedTopic; + + 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 + while(count) { + led4 = 1; + wait(0.5); + led4 = 0; + wait(0.5); + count--; + } + + } + wait(1.0); } - +exit: eth.disconnect(); }