BUTTON LED COMBO - ST IOT CHALLENGE 2020

Dependencies:   Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI

Revision:
15:1d137afcf37c
Parent:
13:18b2819a1af8
Child:
16:80ddc2c6c23a
--- a/main.cpp	Fri Aug 02 05:02:14 2019 +0000
+++ b/main.cpp	Fri Feb 28 02:50:38 2020 +0000
@@ -24,13 +24,13 @@
 DigitalIn button1(USER_BUTTON);
 DigitalOut led1(LED1);
 DigitalOut ledGreen(D11);
-//DigitalOut ledRed(D12);
+DigitalOut ledRed(D12);
 
 int iotvalue;
-//int iotvalue2;
+int iotvalue2;
 
 //Function prototype new
-MQTTTimer publishData(MQTTTimer, int/*, int*/);
+MQTTTimer publishData(MQTTTimer, int, int);
 MQTTTimer myFunction(MQTTTimer);
 
 MQTTTimer myFunction (MQTTTimer timer){
@@ -43,24 +43,24 @@
     wait(0.2);
 
     ledGreen = iotvalue;
-      
-    timer = publishData(timer, ledGreen/*, ledRed*/);
+    ledRed = iotvalue2;  
+    timer = publishData(timer, ledGreen, ledRed);
     return timer;
 }
 
-MQTTTimer publishData(MQTTTimer timer, int data1/*, int data2*/){
+MQTTTimer publishData(MQTTTimer timer, int data1, int data2){
      // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
-        if (timer.expired()) {
+//        if (timer.expired()) {
             int error = 0;
             if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_VOLTAGE, UNIT_DIGITAL, data1)) != CAYENNE_SUCCESS) {
                 printf("Publish data failed, error: %d\n", error);
             }
-//            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_DIGITAL, data2)) != CAYENNE_SUCCESS) {
-//                printf("Publish data failed, error: %d\n", error);
-//            }
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_DIGITAL, data2)) != CAYENNE_SUCCESS) {
+                printf("Publish data failed, error: %d\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(1500);
-        }   
+//            timer.countdown_ms(1500);
+//        }   
         return timer;
 }
 
@@ -124,9 +124,9 @@
                 printf("Publish LED state failure, error: %d\n", error);
             }
             break;
-        }
+        
         
-/*        case 5:
+        case 5:
             // Set the onboard LED state
             iotvalue2 = atoi(message.getValue());
             printf("From Cayenne = %d\n",iotvalue2);
@@ -135,7 +135,7 @@
                 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.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
@@ -162,7 +162,7 @@
         printf("MQTT connect failed, error: %d\n", error);
         return error;
     }
-    printf("Connected\n");
+    printf("Cayenne Connected\n");
 
     // Subscribe to required topics.
     if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
@@ -173,8 +173,8 @@
     }
 
     // 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.
-    mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
-    mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
+//    mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
+//    mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
     //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
     //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
 
@@ -187,12 +187,12 @@
 void loop(void)
 {
     // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
-    MQTTTimer timer(1500);
+    MQTTTimer timer(200);
 
     while (true) {
         
         // Yield to allow MQTT message processing.
-        mqttClient.yield(1000);
+        mqttClient.yield(200);
 
         // Check that we are still connected, if not, reconnect.
         if (!network.connected() || !mqttClient.connected()) {
@@ -214,7 +214,7 @@
 int main()
 {   
     // Initialize the network interface.
-    printf("Initializing interface\n");
+    printf("Initializing connection\n");
     interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
 
     // Set the default function that receives Cayenne messages.
@@ -229,10 +229,13 @@
         printf("Connection failed, exiting\n");
     }
 
-    if (mqttClient.connected())
+    if (mqttClient.connected()){
         mqttClient.disconnect();
-    if (network.connected())
+        printf("mqtt disconnect\n");
+        }
+    if (network.connected()){
         network.disconnect();
-
+        printf("network disconnect\n");
+        }
     return 0;
 }
\ No newline at end of file