Ultrasonic-Servo Combo

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

Files at this revision

API Documentation at this revision

Comitter:
stiotchallenge
Date:
Wed Jul 17 05:10:39 2019 +0000
Parent:
11:760e0ede36bb
Commit message:
Nucleo F411RE, SR04 and servomotor with Cayenne IoT version 2

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jul 16 21:54:30 2019 +0000
+++ b/main.cpp	Wed Jul 17 05:10:39 2019 +0000
@@ -27,13 +27,13 @@
 DigitalOut led1(LED1);
 DigitalOut ledGreen(D11);
 DigitalOut ledRed(D12);
-//HCSR04 sensor(D7, D6);
-//Servo myservo(D10);
+HCSR04 sensor(D7, D6);
+Servo myservo(D10);
 
 int iotvalue;
 
 //Function prototype new
-MQTTTimer publishData(MQTTTimer, int, int, int);
+MQTTTimer publishData(MQTTTimer, int, int);
 MQTTTimer myFunction(MQTTTimer);
 
 MQTTTimer myFunction (MQTTTimer timer){
@@ -45,47 +45,44 @@
     led1 = 0;
     wait(0.2);
 
-    ledGreen = iotvalue;
+//    ledGreen = iotvalue;
     
 //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;
-//    }
+    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);
+    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);
-//            }
+            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(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) {