MQTT cellular example

Dependencies:   C027_Support C12832 MQTT mbed

Fork of Cellular_HelloMQTT by Michael Ammann

Files at this revision

API Documentation at this revision

Comitter:
sam_grove
Date:
Fri May 09 23:06:43 2014 +0000
Parent:
4:c696f27c1850
Child:
7:3de634f2d40c
Commit message:
Updates to the example to use the virtual serial port and the LCD for feedback

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
C12832_lcd.lib Show diff for this revision Revisions of this file
MQTT.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Fri May 09 23:06:43 2014 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/components/code/C12832/#03069e3deaa4
--- a/C12832_lcd.lib	Fri May 09 21:33:48 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/users/dreschpe/code/C12832_lcd/#8f86576007d6
--- a/MQTT.lib	Fri May 09 21:33:48 2014 +0000
+++ b/MQTT.lib	Fri May 09 23:06:43 2014 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/teams/mqtt/code/MQTT/#c56a5c7d2a52
+http://mbed.org/teams/mqtt/code/MQTT/#d13a6c558164
--- a/main.cpp	Fri May 09 21:33:48 2014 +0000
+++ b/main.cpp	Fri May 09 23:06:43 2014 +0000
@@ -25,121 +25,45 @@
  */
 
 #include "mbed.h"
-#include "EthernetInterface.h"
+#include "EthernetInterfaceIPStack.h"
 
-#include "C12832_lcd.h"
-C12832_LCD lcd;
+#include "C12832.h"
+C12832 lcd(p5, p7, p6, p8, p11);
 
 #include "FP.cpp"
 #include "MQTTClient.h"
 
-
-
-class IPStack 
-{
-public:    
-    IPStack()
-    {
-        eth.init();                          // Use DHCP
-        eth.connect();
-        mysock.set_blocking(false, 1000);    // 1 second Timeout 
-    }
-    
-    int connect(char* hostname, int port)
-    {
-        return mysock.connect(hostname, port);
-    }
-
-    int read(char* buffer, int len, int timeout)
-    {
-        mysock.set_blocking(false, timeout);  
-        return mysock.receive(buffer, len);
-    }
-    
-    int write(char* buffer, int len, int timeout)
-    {
-        mysock.set_blocking(false, timeout);  
-        return mysock.send(buffer, len);
-    }
-    
-    int disconnect()
-    {
-        return mysock.close();
-    }
-    
-private:
-
-    EthernetInterface eth;
-    TCPSocketConnection mysock; 
-    
-};
-
-
-class Countdown
-{
-public:
-    Countdown()
-    {
-        t = Timer();   
-    }
-    
-    Countdown(int ms)
-    {
-        t = Timer();
-        countdown_ms(ms);   
-    }
-    
-    
-    bool expired()
-    {
-        return t.read_ms() >= interval_end_ms;
-    }
-    
-    void countdown_ms(int ms)  
-    {
-        t.stop();
-        interval_end_ms = ms;
-        t.reset();
-        t.start();
-    }
-    
-    void countdown(int seconds)
-    {
-        countdown_ms(seconds * 1000);
-    }
-    
-    int left_ms()
-    {
-        return interval_end_ms - t.read_ms();
-    }
-    
-private:
-    Timer t;
-    int interval_end_ms; 
-};
-
 int arrivedcount = 0;
 
 void messageArrived(MQTT::Message* message)
 {
-    lcd.printf("Message arrived: qos %d, retained %d, dup %d, packetid %d\n", message->qos, message->retained, message->dup, message->id);
-    lcd.printf("Payload %.*s\n", message->payloadlen, (char*)message->payload);
+    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);
     ++arrivedcount;
+    lcd.puts((char*)message->payload);
 }
 
 int connect(MQTT::Client<IPStack, Countdown>::connectionLostInfo* info)
 {
     char* hostname = "m2m.eclipse.org";
     int port = 1883;
-    lcd.printf("Connecting to %s:%d\n", hostname, port);
+    
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("%s:%d\n", hostname, port);
+    printf("Connecting to %s:%d\n", hostname, port);
     int rc = info->network->connect(hostname, port);
-    lcd.printf("rc from TCP connect is %d\n", rc);
+    lcd.printf("TCP connect = %d\n", rc);
+    printf("rc from TCP connect is %d\n", rc);
  
     MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
     data.MQTTVersion = 3;
     data.clientID.cstring = "mbed-icraggs";
     rc = info->client->connect(&data);
-    lcd.printf("rc from MQTT connect is %d\n", rc);
+    lcd.printf("MQTT connect = %d\n", rc);
+    printf("rc from MQTT connect is %d\n", rc);
     
     return rc;
 }
@@ -152,6 +76,7 @@
     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);
     
@@ -161,8 +86,9 @@
     int rc = connect(&info);
     
     rc = client.subscribe(topic, MQTT::QOS1, messageArrived);   
-    if (rc != 0)
-        lcd.printf("rc from MQTT subscribe is %d\n", rc);
+    if (rc != 0) {
+        printf("rc from MQTT subscribe is %d\n", rc);
+    }
 
     MQTT::Message message;
 
@@ -195,16 +121,21 @@
         client.yield(100);
     
     rc = client.unsubscribe(topic);
-    if (rc != 0)
-        lcd.printf("rc from unsubscribe was %d\n", rc);
+    if (rc != 0) {
+        printf("rc from unsubscribe was %d\n", rc);
+    }
     
     rc = client.disconnect();
-    if (rc != 0)
-        lcd.printf("rc from disconnect was %d\n", rc);
+    if (rc != 0) {
+        printf("rc from disconnect was %d\n", rc);
+    }
     
     ipstack.disconnect();
     
-    lcd.printf("Finishing with %d messages received\n", arrivedcount);
+    lcd.cls();
+    lcd.locate(0,3);
+    lcd.printf("Finish: %d msgs\n", arrivedcount);
+    printf("Finishing with %d messages received\n", arrivedcount);
     
     return 0;
 }