BUTTON LED COMBO - ST IOT CHALLENGE 2020

Dependencies:   Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI

Revision:
12:5c663a304fca
Parent:
11:760e0ede36bb
Child:
13:18b2819a1af8
--- a/main.cpp	Tue Jul 16 21:54:30 2019 +0000
+++ b/main.cpp	Tue Jul 23 01:51:31 2019 +0000
@@ -7,8 +7,8 @@
 #include "CayenneMQTTClient.h"
 #include "MQTTNetworkIDW01M1.h"
 #include "SpwfInterface.h"
-#include "hcsr04.h"
-#include "Servo.h"
+//#include "hcsr04.h"
+//#include "Servo.h"
 
 // WiFi network info.
 char* ssid = "iPhone";
@@ -33,7 +33,7 @@
 int iotvalue;
 
 //Function prototype new
-MQTTTimer publishData(MQTTTimer, int, int, int);
+MQTTTimer publishData(MQTTTimer, int/*, int*/);
 MQTTTimer myFunction(MQTTTimer);
 
 MQTTTimer myFunction (MQTTTimer timer){
@@ -47,45 +47,42 @@
 
     ledGreen = iotvalue;
     
-//ULTRASONIC + SERVO/////////////////////////////
+/*ULTRASONIC + SERVO/////////////////////////////
     int openclose;
-//    long distance = sensor.distance();   
-//    printf("distance  %d  \n",distance);
-//    wait(1.0); // 1 sec
-//    if (distance > 50) {
-//        ledGreen = 0;
-//        ledRed = 1;   
-//        myservo = 1; //tutup
-//        wait(0.2);
-//        openclose = 0;
-//    }
-//    if (distance <50) {
-//        ledGreen = 1;
-//        ledRed = 0;
-//        myservo = 0; //buka
-//        wait(0.2);
-//        openclose = 1;
-//    }
-    
-    timer = publishData(timer, ledGreen, ledRed, openclose);
+    long distance = sensor.distance();   
+    printf("distance  %d  \n",distance);
+    wait(1.0); // 1 sec
+    if (distance > 50) {
+        ledGreen = 0;
+        ledRed = 1;   
+        myservo = 1; //tutup
+        wait(0.2);
+        openclose = 0;
+    }
+    if (distance <50) {
+        ledGreen = 1;
+        ledRed = 0;
+        myservo = 0; //buka
+        wait(0.2);
+        openclose = 1;
+    }
+*/    
+    timer = publishData(timer, ledGreen/*, openclose*/);
     return timer;
 }
 
-MQTTTimer publishData(MQTTTimer timer, int data1, int data2, int data3){
+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()) {
             int error = 0;
             if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_VOLTAGE, UNIT_DIGITAL, data1)) != CAYENNE_SUCCESS) {
-                printf("Publish temperature failed, error: %d\n", error);
+                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, 3, TYPE_VOLTAGE, UNIT_DIGITAL, data3)) != 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(2500);
+            timer.countdown_ms(1500);
         }   
         return timer;
 }
@@ -203,7 +200,7 @@
 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(3000);
+    MQTTTimer timer(1500);
 
     while (true) {