ULTRASONIC SERVO COMBO - ST IOT CHALLENGE 2020

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

Revision:
8:b19f86c36974
Parent:
7:78cefe0937ab
Child:
9:e3a211745f71
--- a/main.cpp	Fri Nov 11 18:29:29 2016 +0000
+++ b/main.cpp	Fri Apr 26 02:58:51 2019 +0000
@@ -7,22 +7,28 @@
 #include "CayenneMQTTClient.h"
 #include "MQTTNetworkIDW01M1.h"
 #include "SpwfInterface.h"
+#include "pwm_tone.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 led2(D12);
+DigitalOut led3(D11);
+DigitalIn irsensor(D13);
+//DigitalOut buzzer(D7);
 
+int iotvalue;
 /**
 * Print the message info.
 * @param[in] message The message received from the Cayenne server.
@@ -68,6 +74,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 +82,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 +142,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,6 +159,12 @@
                 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()) {
@@ -157,11 +172,8 @@
             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_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, 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);