MQTT cellular example
Dependencies: C027_Support C12832 MQTT mbed
Fork of Cellular_HelloMQTT by
Diff: main.cpp
- Revision:
- 14:d495a85ed55b
- Parent:
- 12:0ec1916059b5
- Child:
- 15:710f99b36ff2
--- a/main.cpp Tue May 20 14:33:32 2014 +0000 +++ b/main.cpp Tue May 27 09:23:07 2014 +0000 @@ -24,7 +24,6 @@ */ #include "mbed.h" -#include "MbedIPStack.h" //------------------------------------------------------------------------------------ // You need to configure these cellular modem / SIM parameters. @@ -44,38 +43,44 @@ //------------------------------------------------------------------------------------ #include "C12832.h" +#if defined(TARGET_LPC1768) && !defined(TARGET_UBLOX_C027) +C12832 lcd(p5, p7, p6, p8, p11); // The classic TARGET_MBED_LPC1768 +#else C12832 lcd(D11, D13, D12, D7, D10); +#endif -#include "FP.cpp" +#include "MQTTSocket.h" #include "MQTTClient.h" int arrivedcount = 0; - -void messageArrived(MQTT::Message* message) + +void messageArrived(MQTT::MessageData& md) { + MQTT::Message &message = md.message; lcd.cls(); lcd.locate(0,3); - printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id); - printf("Payload %.*s\n", message->payloadlen, (char*)message->payload); + printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message.qos, message.retained, message.dup, message.id); + printf("Payload %.*s\n", message.payloadlen, (char*)message.payload); ++arrivedcount; - lcd.puts((char*)message->payload); + lcd.puts((char*)message.payload); } - + + int main(int argc, char* argv[]) { MDMSerial mdm; //mdm.setDebug(4); // enable this for debugging issues if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) return -1; - - IPStack ipstack = IPStack(); - float version = 0.43; + + MQTTSocket ipstack = MQTTSocket(); + float version = 0.47; char* topic = "mbed-sample"; lcd.printf("Version is %f\n", version); printf("Version is %f\n", version); - MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack); + MQTT::Client<MQTTSocket, Countdown> client = MQTT::Client<MQTTSocket, Countdown>(ipstack); char* hostname = "m2m.eclipse.org"; int port = 1883; @@ -86,18 +91,15 @@ MQTTPacket_connectData data = MQTTPacket_connectData_initializer; data.MQTTVersion = 3; - data.clientID.cstring = "mbed-icraggs"; - rc = client.connect(&data); - if (rc != 0) + data.clientID.cstring = "mbed-sample"; + if ((rc = client.connect(&data)) != 0) lcd.printf("rc from MQTT connect is %d\n", rc); - rc = client.subscribe(topic, MQTT::QOS1, messageArrived); - if (rc != 0) { - printf("rc from MQTT subscribe is %d\n", rc); - } - + if ((rc = client.subscribe(topic, MQTT::QOS1, messageArrived)) != 0) + lcd.printf("rc from MQTT subscribe is %d\n", rc); + MQTT::Message message; - + // QoS 0 char buf[100]; sprintf(buf, "Hello World! QoS 0 message from app version %f\n", version); @@ -126,23 +128,20 @@ while (arrivedcount == 2) client.yield(100); - rc = client.unsubscribe(topic); - if (rc != 0) { + if ((rc = client.unsubscribe(topic)) != 0) printf("rc from unsubscribe was %d\n", rc); - } - rc = client.disconnect(); - if (rc != 0) { + if ((rc = client.disconnect()) != 0) printf("rc from disconnect was %d\n", rc); - } - + + ipstack.disconnect(); mdm.disconnect(); mdm.powerOff(); lcd.cls(); lcd.locate(0,3); - lcd.printf("Finish: %d msgs\n", arrivedcount); + lcd.printf("Version %.2f: finish %d msgs\n", version, arrivedcount); printf("Finishing with %d messages received\n", arrivedcount); - while(true); -} + return 0; +} \ No newline at end of file