MQTT cellular example

Dependencies:   C027_Support C12832 MQTT mbed

Fork of Cellular_HelloMQTT by Michael Ammann

Revision:
10:6f1c3b718b9c
Parent:
8:b32c94be6522
Child:
11:c074866fcad7
--- a/main.cpp	Mon May 12 10:19:47 2014 +0000
+++ b/main.cpp	Tue May 13 06:15:02 2014 +0000
@@ -71,102 +71,85 @@
 
 int main(int argc, char* argv[])
 {   
+    Serial pc(USBTX,USBRX);
+    pc.baud(115200);
+
     // turn on the supplies of the Modem and the GPS
     c027.mdmPower(true);
-    wait(2);
-    // Create the modem object
+    printf("Modem Initialize\r\n");
     MDMSerial mdm;
-    
-    // initialize the modem 
-    printf("Modem Initialize\r\n");
-    MDMParser::DevStatus devStatus;
-    bool mdmOk = mdm.init(SIMPIN, &devStatus);
-    if (mdmOk)
-    {
-        // wait until we are connected
-        printf("Network Check\r\n");
-        MDMParser::NetStatus netStatus;
-        while (!mdm.checkNetStatus(&netStatus))
-            wait_ms(1000);
-    
-        printf("Network Join\r\n");
-        // join the internet connection 
-        MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD);
-        if (ip != NOIP)
-        {
-            printf("  IP Address: " IPSTR "\r\n", IPNUM(ip));
-            
-            IPStack ipstack = IPStack();
-            float version = 0.43;
-            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);
-            
-            char* hostname = "m2m.eclipse.org";
-            int port = 1883;
-            lcd.printf("Connecting to %s:%d\n", hostname, port);
-            int rc = ipstack.connect(hostname, port);
-            if (rc != 0)
-                lcd.printf("rc from TCP connect is %d\n", rc);
-         
-            MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
-            data.MQTTVersion = 3;
-            data.clientID.cstring = "mbed-icraggs";
-            rc = client.connect(&data);
-            if (rc != 0)
-                lcd.printf("rc from MQTT connect is %d\n", rc);
+    if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true))
+        return -1;
             
-            rc = client.subscribe(topic, MQTT::QOS1, messageArrived);   
-            if (rc != 0) {
-                printf("rc from MQTT subscribe is %d\n", rc);
-            }
-        
-            MQTT::Message message;
+    IPStack ipstack = IPStack();
+    float version = 0.43;
+    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);
+    
+    char* hostname = "m2m.eclipse.org";
+    int port = 1883;
+    lcd.printf("Connecting to %s:%d\n", hostname, port);
+    int rc = ipstack.connect(hostname, port);
+    if (rc != 0)
+        lcd.printf("rc from TCP connect is %d\n", rc);
+ 
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;       
+    data.MQTTVersion = 3;
+    data.clientID.cstring = "mbed-icraggs";
+    rc = client.connect(&data);
+    if (rc != 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);
+    }
+
+    MQTT::Message message;
+
+    // QoS 0
+    char buf[100];
+    sprintf(buf, "Hello World!  QoS 0 message from app version %f\n", version);
+    message.qos = MQTT::QOS0;
+    message.retained = false;
+    message.dup = false;
+    message.payload = (void*)buf;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(topic, &message);
+    while (arrivedcount == 0)
+        client.yield(100);
         
-            // QoS 0
-            char buf[100];
-            sprintf(buf, "Hello World!  QoS 0 message from app version %f\n", version);
-            message.qos = MQTT::QOS0;
-            message.retained = false;
-            message.dup = false;
-            message.payload = (void*)buf;
-            message.payloadlen = strlen(buf)+1;
-            rc = client.publish(topic, &message);
-            while (arrivedcount == 0)
-                client.yield(100);
-                
-            // QoS 1
-            sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
-            message.qos = MQTT::QOS1;
-            message.payloadlen = strlen(buf)+1;
-            rc = client.publish(topic, &message);
-            while (arrivedcount == 1)
-                client.yield(100);
-                
-            // QoS 2
-            sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
-            message.qos = MQTT::QOS2;
-            message.payloadlen = strlen(buf)+1;
-            rc = client.publish(topic, &message);
-            while (arrivedcount == 2)
-                client.yield(100);
-            
-            rc = client.unsubscribe(topic);
-            if (rc != 0) {
-                printf("rc from unsubscribe was %d\n", rc);
-            }
-            
-            rc = client.disconnect();
-            if (rc != 0) {
-                printf("rc from disconnect was %d\n", rc);
-            }
+    // QoS 1
+    sprintf(buf, "Hello World!  QoS 1 message from app version %f\n", version);
+    message.qos = MQTT::QOS1;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(topic, &message);
+    while (arrivedcount == 1)
+        client.yield(100);
+        
+    // QoS 2
+    sprintf(buf, "Hello World!  QoS 2 message from app version %f\n", version);
+    message.qos = MQTT::QOS2;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(topic, &message);
+    while (arrivedcount == 2)
+        client.yield(100);
+    
+    rc = client.unsubscribe(topic);
+    if (rc != 0) {
+        printf("rc from unsubscribe was %d\n", rc);
+    }
+    
+    rc = client.disconnect();
+    if (rc != 0) {
+        printf("rc from disconnect was %d\n", rc);
+    }
 
-            mdm.disconnect();
-        }
-    }
+    mdm.disconnect();
     mdm.powerOff();
     c027.mdmPower(false);