Example of sending MQTT data to MyDevices Cayenne using the MTSAS library

Dependencies:   Cayenne-MQTT-mbed-MTSAS X_NUCLEO_IKS01A1 mbed mtsas_lat3

Revision:
2:abc89d2aede3
Parent:
0:5107fce16490
Child:
3:a40321269c7f
diff -r 1c9e9c2e5bd1 -r abc89d2aede3 main.cpp
--- a/main.cpp	Tue Apr 25 15:34:36 2017 +0000
+++ b/main.cpp	Tue Apr 25 21:01:51 2017 +0000
@@ -15,9 +15,9 @@
 typedef CayenneMQTT::MQTTClient<MQTTNetwork<Cellular>, MQTTTimer> MQTTClient;
 
 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
-string username = "MQTT_USERNAME";
-string password = "MQTT_PASSWORD";
-string clientID = "CLIENT_ID";
+string username = "da497640-dcce-11e6-b089-9f6bfa78ab33";
+string password = "68e890972b6cc0fc47fcd152554db7e78ec9b29f";
+string clientID = "4dad1980-2382-11e7-82dd-51ccf9b8e46b";
 
 DigitalOut Led1Out(LED1);
 
@@ -106,7 +106,7 @@
     if (message.id) {
         printf(" id=%s", message.id);
     }
-    printf("\n");
+    printf("\r\n");
 }
 
 /**
@@ -130,24 +130,24 @@
 {
     int error = 0;
     // Connect to the server.
-    printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
+    printf("Connecting to %s:%d\r\n", CAYENNE_DOMAIN, CAYENNE_PORT);
     while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
-        printf("TCP connect failed, error: %d\n", error);
+        printf("TCP connect failed, error: %d\r\n", error);
         wait(2);
     }
 
     if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
-        printf("MQTT connect failed, error: %d\n", error);
+        printf("MQTT connect failed, error: %d\r\n", error);
         return error;
     }
-    printf("Connected\n");
+    printf("Connected\r\n");
 
     // Subscribe to required topics.
     if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
-        printf("Subscription to Command topic failed, error: %d\n", error);
+        printf("Subscription to Command topic failed, error: %d\r\n", error);
     }
     if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
-        printf("Subscription to Config topic failed, error:%d\n", error);
+        printf("Subscription to Config topic failed, error:%d\r\n", error);
     }
 
     // Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
@@ -165,11 +165,11 @@
 void loop(MQTTClient &mqttClient, MQTTNetwork<Cellular> &network)
 {
     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
-    MQTTTimer timer(5000);
-    printf("Starting loop.\n");
+    MQTTTimer timer(1000);
+    printf("Starting loop.\r\n");
     while (true) {
         // Yield to allow MQTT message processing.
-        mqttClient.yield(1000);
+        mqttClient.yield(10);
         if(messageReady){
             int error = 0;
             messageReady = false;
@@ -183,7 +183,7 @@
                     Led1Out = atoi(lastMessage.getValue());
                     // Publish the updated LED state
                     if ((error = mqttClient.publishData(DATA_TOPIC, lastMessage.channel, NULL, NULL, lastMessage.getValue())) != CAYENNE_SUCCESS) {
-                        printf("Publish LED state failure, error: %d\n", error);
+                        printf("Publish LED state failure, error: %d\r\n", error);
                     }
                     break;
                 }
@@ -191,7 +191,7 @@
                 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
                 // An error response should be sent if there are issues processing the message.
                 if ((error = mqttClient.publishResponse(lastMessage.id, NULL, lastMessage.clientID)) != CAYENNE_SUCCESS) {
-                    printf("Response failure, error: %d\n", error);
+                    printf("Response failure, error: %d\r\n", error);
                 }
             }
         }
@@ -200,10 +200,10 @@
         if (!network.connected() || !mqttClient.connected()) {
             network.disconnect();
             mqttClient.disconnect();
-            printf("Reconnecting\n");
+            printf("Reconnecting\r\n");
             while (connectClient(mqttClient, network) != CAYENNE_SUCCESS) {
                 wait(2);
-                printf("Reconnect failed, retrying\n");
+                printf("Reconnect failed, retrying\r\n");
             }
         }
 
@@ -213,25 +213,29 @@
             int error = 0;
             float temp_data;
             temp_sensor1->get_temperature(&temp_data);
-            printf("Temperature was: %f \n", temp_data);
+            printf("Temperature was: %f \r\n", temp_data);
             if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_TEMPERATURE, UNIT_CELSIUS, temp_data)) != CAYENNE_SUCCESS) {
-                printf("Publish temperature failed, error: %d\n", error);
+                printf("Publish temperature failed, error: %d\r\n", error);
             }
             humidity_sensor->get_humidity(&temp_data);
-            printf("Humidity was: %f \n", temp_data);
+            printf("Humidity was: %f \r\n", temp_data);
             if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_RELATIVE_HUMIDITY, UNIT_PERCENT, temp_data)) != CAYENNE_SUCCESS) {
-                printf("Publish luminosity failed, error: %d\n", error);
+                printf("Publish luminosity failed, error: %d\r\n", error);
             }
             pressure_sensor->get_pressure(&temp_data);
-            printf("Pressure was: %f \n", temp_data);
+            printf("Pressure was: %f \r\n", temp_data);
             if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, temp_data)) != CAYENNE_SUCCESS) {
-                printf("Publish barometric pressure failed, error: %d\n", error);
+                printf("Publish barometric pressure failed, error: %d\r\n", error);
             }
-            Led1Out = 0;
+            printf("Led is: %s\r\n", Led1Out.read() ? "on" : "off");
+             if ((error = mqttClient.publishData(DATA_TOPIC, 4, "led", UNIT_DIGITAL, Led1Out.read())) != CAYENNE_SUCCESS) {
+                printf("Publish LED status failed, error: %d\r\n", error);
+            }
             // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
             timer.countdown_ms(5000);
         } else {
-            printf("Timer: %d", timer.left_ms());
+           // debug
+           // printf("Timer: %d", timer.left_ms());
         }
     }
 }
@@ -250,9 +254,9 @@
     }
     // Test with a ping
     if(radio->ping("www.google.com")){
-        printf("Ping test succeeded!\n");
+        printf("Ping test succeeded!\r\n");
     } else {
-        printf("Failed ping test!\n");
+        printf("Failed ping test!\r\n");
     }
     MQTTNetwork<Cellular> network(*radio);
     messageReady = false;
@@ -267,7 +271,7 @@
         loop(mqttClient, network);
     }
     else {
-        printf("Connection failed, exiting\n");
+        printf("Connection failed, exiting\r\n");
     }
 
     if (mqttClient.connected())