smkhk_moist

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

Revision:
9:f8c559fc0246
Parent:
8:396e627a2ee6
--- a/main.cpp	Fri Apr 26 03:27:06 2019 +0000
+++ b/main.cpp	Fri Apr 26 03:52:42 2019 +0000
@@ -25,10 +25,13 @@
 
 DigitalOut led1(LED1);
 DigitalOut led(D11);
+AnalogIn moist(A0);
 
 HCSR04 sensor(D13, D12);
 Servo myservo(D10);
 
+float meas;
+
 int iotvalue;
 /**
 * Print the message info.
@@ -147,6 +150,7 @@
 
     while (true) {
         
+        
         // Yield to allow MQTT message processing.
         mqttClient.yield(1000);
 
@@ -162,30 +166,24 @@
         }
         
         
-        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;
+        meas = moist.read(); // Converts and read the analog input value (value from 0.0 to 1.0)
+        meas = meas * 3300; // Change the value to be in the 0 to 3300 range
+        printf("measure = %.0f mV\n", meas);
+        if (meas > 3000) { // If the value is greater than 2V then switch the LED on
+          led = 1;
         }
-        if (distance <50) {
-            led = 1;
-            myservo = 0; //buka
-            wait(0.2);
-            openclose = 1;
-        } 
+        else {
+          led = 0;
+        }
+        wait(0.2); // 200 ms 
         
         // 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_BATTERY, UNIT_DIGITAL, openclose)) != 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_VOLTAGE, UNIT_VOLTS, distance)) != CAYENNE_SUCCESS) {
+            }*/
+            if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_VOLTS, meas)) != 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.