Servomotor-Button

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

Files at this revision

API Documentation at this revision

Comitter:
stiotchallenge
Date:
Tue Jul 16 03:54:11 2019 +0000
Parent:
8:b19f86c36974
Child:
10:2951beb4fca3
Commit message:
Nucleo F411RE with SR04, servomotor and Cayenne IoT

Changed in this revision

HCSR04.lib Show annotated file Show diff for this revision Revisions of this file
PWM_Tone_Library.lib Show diff for this revision Revisions of this file
Servo.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HCSR04.lib	Tue Jul 16 03:54:11 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/antoniolinux/code/HCSR04/#86b2086be101
--- a/PWM_Tone_Library.lib	Fri Apr 26 02:58:51 2019 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/users/justinkim/code/PWM_Tone_Library/#d06af372d384
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Tue Jul 16 03:54:11 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/main.cpp	Fri Apr 26 02:58:51 2019 +0000
+++ b/main.cpp	Tue Jul 16 03:54:11 2019 +0000
@@ -7,7 +7,8 @@
 #include "CayenneMQTTClient.h"
 #include "MQTTNetworkIDW01M1.h"
 #include "SpwfInterface.h"
-#include "pwm_tone.h"
+#include "hcsr04.h"
+#include "Servo.h"
 
 // WiFi network info.
 char* ssid = "iPhone";
@@ -16,19 +17,97 @@
 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
 char* username = "4f3fbcb0-3796-11e9-ad96-c15442ccb423";
 char* password = "9e099f3d9aaedd7b76ca94044c6bb488c3999e3c";
-char* clientID = "49143ea0-60c8-11e9-888e-af3db3cb8c63";
+char* clientID = "4288d2f0-a5a9-11e9-9636-f9904f7b864b";
 
 SpwfSAInterface interface(D8, D2); // TX, RX
 MQTTNetwork<SpwfSAInterface> network(interface);
 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
 
+DigitalIn button1(USER_BUTTON);
 DigitalOut led1(LED1);
-DigitalOut led2(D12);
-DigitalOut led3(D11);
-DigitalIn irsensor(D13);
-//DigitalOut buzzer(D7);
+DigitalOut ledGreen(D11);
+DigitalOut ledRed(D12);
+//HCSR04 sensor(D7, D6);
+//Servo myservo(D10);
 
 int iotvalue;
+
+//Function prototype new
+MQTTTimer publishData(MQTTTimer, int, int);
+MQTTTimer myFunction(MQTTTimer);
+
+MQTTTimer myFunction (MQTTTimer timer){
+    /**
+    * Write your codes here.
+    **/
+    led1 = 1;
+    wait(0.2);
+    led1 = 0;
+    wait(0.2);
+
+    ledGreen = iotvalue;
+    
+//ULTRASONIC SENSOR WITH LED/////////////////////
+//    long distance = sensor.distance();   
+//    printf("distance  %d  \n",distance);
+//    wait(1.0); // 1 sec
+//    if (distance > 50) {
+//        ledGreen = 0; 
+//      }
+//    if (distance <50) {
+//        ledGreen = 1;
+//      }  
+////////////////////////////////////////////////    
+
+//SERVOMOTOR WITH BUTTON////////////////////////
+//      if (button1) {
+//        myservo = 1; //tutup
+//        wait(0.2);
+//        }
+//      if (~button1) {
+//        myservo = 0; //buka
+//        wait(0.2);
+//        }
+/////////////////////////////////////////////////
+
+//ULTRASONIC + SERVO/////////////////////////////
+//    int openclose;
+//    long distance = sensor.distance();   
+//    printf("distance  %d  \n",distance);
+//    wait(1.0); // 1 sec
+//    if (distance > 50) {
+//        ledGreen = 0; 
+//        myservo = 1; //tutup
+//        wait(0.2);
+//        openclose = 0;
+//    }
+//    if (distance <50) {
+//        ledGreen = 1;
+//        myservo = 0; //buka
+//        wait(0.2);
+//        openclose = 1;
+//    }
+    
+    timer = publishData(timer, ledGreen, led1);
+    return timer;
+}
+
+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);
+            }
+//            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(5000);
+        }   
+        return timer;
+}
+
 /**
 * Print the message info.
 * @param[in] message The message received from the Cayenne server.
@@ -158,26 +237,8 @@
                 wait(2);
                 printf("Reconnect failed, retrying\n");
             }
-        }
-        
-        int ir;
-
-        ir = !irsensor;
-        if (ir == 1) led2 = 1; else led2 = 0; 
-        if (iotvalue == 1) led3 = 1; else led3 = 0;
-
-        // 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_TEMPERATURE, UNIT_CELSIUS, 30.5)) != CAYENNE_SUCCESS) {
-                printf("Publish temperature failed, error: %d\n", error);
-            }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_VOLTS, ir)) != CAYENNE_SUCCESS) {
-                printf("Publish sensor 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(5000);
-        }
+        }        
+        timer = myFunction (timer);
     }
 }
 
@@ -208,5 +269,4 @@
         network.disconnect();
 
     return 0;
-}
-
+}
\ No newline at end of file