ULTRASONIC SERVO COMBO - ST IOT CHALLENGE 2020
Dependencies: Cayenne-MQTT-mbed mbed Servo X_NUCLEO_IDW01M1v2 NetworkSocketAPI HCSR04
Diff: main.cpp
- Revision:
- 11:760e0ede36bb
- Parent:
- 10:2951beb4fca3
- Child:
- 12:dd98953c0a6d
diff -r 2951beb4fca3 -r 760e0ede36bb main.cpp --- a/main.cpp Tue Jul 16 21:15:50 2019 +0000 +++ b/main.cpp Tue Jul 16 21:54:30 2019 +0000 @@ -31,10 +31,9 @@ //Servo myservo(D10); int iotvalue; -int iotvalue2; //Function prototype new -MQTTTimer publishData(MQTTTimer, int, int); +MQTTTimer publishData(MQTTTimer, int, int, int); MQTTTimer myFunction(MQTTTimer); MQTTTimer myFunction (MQTTTimer timer){ @@ -47,54 +46,32 @@ wait(0.2); ledGreen = iotvalue; - ledRed = iotvalue2; -//ULTRASONIC SENSOR WITH LED///////////////////// +//ULTRASONIC + SERVO///////////////////////////// + int openclose; // 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; +// 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, led1); + timer = publishData(timer, ledGreen, ledRed, openclose); return timer; } -MQTTTimer publishData(MQTTTimer timer, int data1, int data2){ +MQTTTimer publishData(MQTTTimer timer, int data1, int data2, int data3){ // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne. if (timer.expired()) { int error = 0; @@ -104,6 +81,9 @@ // 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); +// } // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval. timer.countdown_ms(2500); } @@ -170,15 +150,6 @@ printf("Publish LED state failure, error: %d\n", error); } break; - case 5: - // Set the onboard LED state - iotvalue2 = 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); - } - break; } // If this is a command message we publish a response. Here we are just sending a default 'OK' response.