smkhk_bin

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

Files at this revision

API Documentation at this revision

Comitter:
stiotchallenge
Date:
Fri Apr 26 03:27:06 2019 +0000
Parent:
7:78cefe0937ab
Commit message:
smkhk_bin

Changed in this revision

HCSR04.lib Show annotated file 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	Fri Apr 26 03:27:06 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/antoniolinux/code/HCSR04/#86b2086be101
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo.lib	Fri Apr 26 03:27:06 2019 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/Servo/#36b69a7ced07
--- a/main.cpp	Fri Nov 11 18:29:29 2016 +0000
+++ b/main.cpp	Fri Apr 26 03:27:06 2019 +0000
@@ -7,22 +7,29 @@
 #include "CayenneMQTTClient.h"
 #include "MQTTNetworkIDW01M1.h"
 #include "SpwfInterface.h"
+#include "hcsr04.h"
+#include "Servo.h"
 
 // WiFi network info.
-char* ssid = "ssid";
-char* wifiPassword = "wifiPassword";
+char* ssid = "iPhone";
+char* wifiPassword = "abcd1234";
 
 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
-char* username = "MQTT_USERNAME";
-char* password = "MQTT_PASSWORD";
-char* clientID = "CLIENT_ID";
+char* username = "4f3fbcb0-3796-11e9-ad96-c15442ccb423";
+char* password = "9e099f3d9aaedd7b76ca94044c6bb488c3999e3c";
+char* clientID = "49143ea0-60c8-11e9-888e-af3db3cb8c63";
 
 SpwfSAInterface interface(D8, D2); // TX, RX
 MQTTNetwork<SpwfSAInterface> network(interface);
 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
 
 DigitalOut led1(LED1);
+DigitalOut led(D11);
 
+HCSR04 sensor(D13, D12);
+Servo myservo(D10);
+
+int iotvalue;
 /**
 * Print the message info.
 * @param[in] message The message received from the Cayenne server.
@@ -68,6 +75,7 @@
 void messageArrived(CayenneMQTT::MessageData& message)
 {
     int error = 0;
+    
     // Add code to process the message. Here we just ouput the message data.
     outputMessage(message);
 
@@ -75,7 +83,8 @@
         switch(message.channel) {
         case 0:
             // Set the onboard LED state
-            led1 = atoi(message.getValue());
+            iotvalue = atoi(message.getValue());
+            printf("From Cayenne = %d\n",iotvalue);
             // Publish the updated LED state
             if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
                 printf("Publish LED state failure, error: %d\n", error);
@@ -134,9 +143,10 @@
 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(5000);
+    MQTTTimer timer(3000);
 
     while (true) {
+        
         // Yield to allow MQTT message processing.
         mqttClient.yield(1000);
 
@@ -150,18 +160,33 @@
                 printf("Reconnect failed, retrying\n");
             }
         }
-
+        
+        
+        int openclose;
+        long distance = sensor.distance();   
+        printf("distance  %d  \n",distance);
+        //wait(1.0); // 1 sec
+        if (distance > 50) {
+            led = 0; 
+            myservo = 1; //tutup
+            wait(0.2);
+            openclose = 0;
+        }
+        if (distance <50) {
+            led = 1;
+            myservo = 0; //buka
+            wait(0.2);
+            openclose = 1;
+        } 
+        
         // 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) {
+            if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_BATTERY, UNIT_DIGITAL, openclose)) != CAYENNE_SUCCESS) {
                 printf("Publish temperature failed, error: %d\n", error);
             }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_LUMINOSITY, UNIT_LUX, 1000)) != CAYENNE_SUCCESS) {
-                printf("Publish luminosity failed, error: %d\n", error);
-            }
-            if ((error = mqttClient.publishData(DATA_TOPIC, 3, TYPE_BAROMETRIC_PRESSURE, UNIT_HECTOPASCAL, 800)) != CAYENNE_SUCCESS) {
-                printf("Publish barometric pressure failed, error: %d\n", error);
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_VOLTS, distance)) != 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);