Servomotor-Button

Dependencies:   Cayenne-MQTT-mbed mbed Servo X_NUCLEO_IDW01M1v2 NetworkSocketAPI HCSR04

Revision:
1:a2d8aec2bb92
Parent:
0:d20655cd1f3c
Child:
2:445f09de09bd
--- a/main.cpp	Fri Oct 07 18:30:51 2016 +0000
+++ b/main.cpp	Thu Oct 20 22:57:56 2016 +0000
@@ -21,6 +21,8 @@
 MQTTNetwork<SpwfSAInterface> network(interface);
 Cayenne::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network);
 
+DigitalOut led1(LED1);
+
 /**
 * Print the message info.
 * @param[in] message The message received from the Cayenne server.
@@ -69,10 +71,23 @@
     // Add code to process the message. Here we just ouput the message data.
     outputMessage(message);
 
-    // 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 (message.topic == COMMAND_TOPIC && (error = mqttClient.publishResponse(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
-        printf("Response failure, error: %d\n", error);
+    if (message.topic == COMMAND_TOPIC) {
+        switch(message.channel) {
+        case 0:
+            // Set the onboard LED state
+            led1 = atoi(message.values[0].value);
+            // Publish the updated LED state
+            if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.values[0].value)) != CAYENNE_SUCCESS) {
+                printf("Publish LED state failure, error: %d\n", error);
+            }
+            break;
+        }
+        
+        // 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(message.channel, message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
+            printf("Response failure, error: %d\n", error);
+        }
     }
 }
 
@@ -138,13 +153,13 @@
         // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
         if (timer.expired()) {
             int error = 0;
-            if ((error = mqttClient.publishData(DATA_TOPIC, 0, TEMPERATURE, CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 1, TEMPERATURE, CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
                 printf("Publish temperature failed, error: %d\n", error);
             }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 1, LUMINOSITY, LUX, 1000)) != CAYENNE_SUCCESS) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, LUMINOSITY, LUX, 1000)) != CAYENNE_SUCCESS) {
                 printf("Publish luminosity failed, error: %d\n", error);
             }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 2, BAROMETRIC_PRESSURE, HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 3, BAROMETRIC_PRESSURE, HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
                 printf("Publish barometric pressure failed, error: %d\n", error);
             }
             timer.countdown_ms(5000);