ULTRASONIC SERVO COMBO - ST IOT CHALLENGE 2020

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

Committer:
stiotchallenge
Date:
Fri Feb 28 03:03:58 2020 +0000
Revision:
13:3b275daf1df3
Parent:
12:dd98953c0a6d
ULTRASONIC SERVO COMBO - ST IOT CHALLENGE 2020

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jburhenn 0:d20655cd1f3c 1 /**
jburhenn 0:d20655cd1f3c 2 * Example app for using the Cayenne MQTT C++ library to send and receive example data. This example uses
jburhenn 0:d20655cd1f3c 3 * the X-NUCLEO-IDW01M1 WiFi expansion board via the X_NUCLEO_IDW01M1v2 library.
jburhenn 0:d20655cd1f3c 4 */
jburhenn 0:d20655cd1f3c 5
jburhenn 0:d20655cd1f3c 6 #include "MQTTTimer.h"
jburhenn 0:d20655cd1f3c 7 #include "CayenneMQTTClient.h"
jburhenn 0:d20655cd1f3c 8 #include "MQTTNetworkIDW01M1.h"
jburhenn 0:d20655cd1f3c 9 #include "SpwfInterface.h"
stiotchallenge 9:e3a211745f71 10 #include "hcsr04.h"
stiotchallenge 9:e3a211745f71 11 #include "Servo.h"
jburhenn 0:d20655cd1f3c 12
jburhenn 0:d20655cd1f3c 13 // WiFi network info.
stiotchallenge 8:b19f86c36974 14 char* ssid = "iPhone";
stiotchallenge 8:b19f86c36974 15 char* wifiPassword = "abcd1234";
jburhenn 0:d20655cd1f3c 16
jburhenn 0:d20655cd1f3c 17 // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
stiotchallenge 8:b19f86c36974 18 char* username = "4f3fbcb0-3796-11e9-ad96-c15442ccb423";
stiotchallenge 8:b19f86c36974 19 char* password = "9e099f3d9aaedd7b76ca94044c6bb488c3999e3c";
stiotchallenge 9:e3a211745f71 20 char* clientID = "4288d2f0-a5a9-11e9-9636-f9904f7b864b";
jburhenn 0:d20655cd1f3c 21
jburhenn 0:d20655cd1f3c 22 SpwfSAInterface interface(D8, D2); // TX, RX
jburhenn 0:d20655cd1f3c 23 MQTTNetwork<SpwfSAInterface> network(interface);
jburhenn 2:445f09de09bd 24 CayenneMQTT::MQTTClient<MQTTNetwork<SpwfSAInterface>, MQTTTimer> mqttClient(network, username, password, clientID);
jburhenn 0:d20655cd1f3c 25
stiotchallenge 9:e3a211745f71 26 DigitalIn button1(USER_BUTTON);
jburhenn 1:a2d8aec2bb92 27 DigitalOut led1(LED1);
stiotchallenge 9:e3a211745f71 28 DigitalOut ledGreen(D11);
stiotchallenge 9:e3a211745f71 29 DigitalOut ledRed(D12);
stiotchallenge 12:dd98953c0a6d 30 HCSR04 sensor(D7, D6);
stiotchallenge 12:dd98953c0a6d 31 Servo myservo(D10);
jburhenn 1:a2d8aec2bb92 32
stiotchallenge 8:b19f86c36974 33 int iotvalue;
stiotchallenge 9:e3a211745f71 34
stiotchallenge 9:e3a211745f71 35 //Function prototype new
stiotchallenge 12:dd98953c0a6d 36 MQTTTimer publishData(MQTTTimer, int, int);
stiotchallenge 9:e3a211745f71 37 MQTTTimer myFunction(MQTTTimer);
stiotchallenge 9:e3a211745f71 38
stiotchallenge 9:e3a211745f71 39 MQTTTimer myFunction (MQTTTimer timer){
stiotchallenge 9:e3a211745f71 40 /**
stiotchallenge 9:e3a211745f71 41 * Write your codes here.
stiotchallenge 9:e3a211745f71 42 **/
stiotchallenge 9:e3a211745f71 43 led1 = 1;
stiotchallenge 9:e3a211745f71 44 wait(0.2);
stiotchallenge 9:e3a211745f71 45 led1 = 0;
stiotchallenge 9:e3a211745f71 46 wait(0.2);
stiotchallenge 9:e3a211745f71 47
stiotchallenge 12:dd98953c0a6d 48 // ledGreen = iotvalue;
stiotchallenge 9:e3a211745f71 49
stiotchallenge 11:760e0ede36bb 50 //ULTRASONIC + SERVO/////////////////////////////
stiotchallenge 11:760e0ede36bb 51 int openclose;
stiotchallenge 12:dd98953c0a6d 52 long distance = sensor.distance();
stiotchallenge 12:dd98953c0a6d 53 printf("distance %d \n",distance);
stiotchallenge 12:dd98953c0a6d 54 wait(1.0); // 1 sec
stiotchallenge 12:dd98953c0a6d 55 if (distance > 50) {
stiotchallenge 12:dd98953c0a6d 56 ledGreen = 0;
stiotchallenge 12:dd98953c0a6d 57 ledRed = 1;
stiotchallenge 12:dd98953c0a6d 58 myservo = 1; //tutup
stiotchallenge 12:dd98953c0a6d 59 wait(0.2);
stiotchallenge 12:dd98953c0a6d 60 openclose = 0;
stiotchallenge 12:dd98953c0a6d 61 }
stiotchallenge 12:dd98953c0a6d 62 if (distance <50) {
stiotchallenge 12:dd98953c0a6d 63 ledGreen = 1;
stiotchallenge 12:dd98953c0a6d 64 ledRed = 0;
stiotchallenge 12:dd98953c0a6d 65 myservo = 0; //buka
stiotchallenge 12:dd98953c0a6d 66 wait(0.2);
stiotchallenge 12:dd98953c0a6d 67 openclose = 1;
stiotchallenge 12:dd98953c0a6d 68 }
stiotchallenge 9:e3a211745f71 69
stiotchallenge 12:dd98953c0a6d 70 timer = publishData(timer, ledGreen, openclose);
stiotchallenge 9:e3a211745f71 71 return timer;
stiotchallenge 9:e3a211745f71 72 }
stiotchallenge 9:e3a211745f71 73
stiotchallenge 12:dd98953c0a6d 74 MQTTTimer publishData(MQTTTimer timer, int data1, int data2){
stiotchallenge 9:e3a211745f71 75 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
stiotchallenge 13:3b275daf1df3 76 // if (timer.expired()) {
stiotchallenge 9:e3a211745f71 77 int error = 0;
stiotchallenge 9:e3a211745f71 78 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_VOLTAGE, UNIT_DIGITAL, data1)) != CAYENNE_SUCCESS) {
stiotchallenge 12:dd98953c0a6d 79 printf("Publish data failed, error: %d\n", error);
stiotchallenge 9:e3a211745f71 80 }
stiotchallenge 12:dd98953c0a6d 81 if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_DIGITAL, data2)) != CAYENNE_SUCCESS) {
stiotchallenge 12:dd98953c0a6d 82 printf("Publish data failed, error: %d\n", error);
stiotchallenge 12:dd98953c0a6d 83 }
stiotchallenge 9:e3a211745f71 84 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
stiotchallenge 13:3b275daf1df3 85 // timer.countdown_ms(1500);
stiotchallenge 13:3b275daf1df3 86 // }
stiotchallenge 9:e3a211745f71 87 return timer;
stiotchallenge 9:e3a211745f71 88 }
stiotchallenge 9:e3a211745f71 89
jburhenn 0:d20655cd1f3c 90 /**
jburhenn 0:d20655cd1f3c 91 * Print the message info.
jburhenn 0:d20655cd1f3c 92 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 93 */
jburhenn 2:445f09de09bd 94 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 95 {
jburhenn 0:d20655cd1f3c 96 switch (message.topic) {
jburhenn 0:d20655cd1f3c 97 case COMMAND_TOPIC:
jburhenn 0:d20655cd1f3c 98 printf("topic=Command");
jburhenn 0:d20655cd1f3c 99 break;
jburhenn 0:d20655cd1f3c 100 case CONFIG_TOPIC:
jburhenn 0:d20655cd1f3c 101 printf("topic=Config");
jburhenn 0:d20655cd1f3c 102 break;
jburhenn 0:d20655cd1f3c 103 default:
jburhenn 0:d20655cd1f3c 104 printf("topic=%d", message.topic);
jburhenn 0:d20655cd1f3c 105 break;
jburhenn 0:d20655cd1f3c 106 }
jburhenn 0:d20655cd1f3c 107 printf(" channel=%d", message.channel);
jburhenn 0:d20655cd1f3c 108 if (message.clientID) {
jburhenn 0:d20655cd1f3c 109 printf(" clientID=%s", message.clientID);
jburhenn 0:d20655cd1f3c 110 }
jburhenn 0:d20655cd1f3c 111 if (message.type) {
jburhenn 0:d20655cd1f3c 112 printf(" type=%s", message.type);
jburhenn 0:d20655cd1f3c 113 }
jburhenn 0:d20655cd1f3c 114 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 2:445f09de09bd 115 if (message.getValue(i)) {
jburhenn 2:445f09de09bd 116 printf(" value=%s", message.getValue(i));
jburhenn 0:d20655cd1f3c 117 }
jburhenn 2:445f09de09bd 118 if (message.getUnit(i)) {
jburhenn 2:445f09de09bd 119 printf(" unit=%s", message.getUnit(i));
jburhenn 0:d20655cd1f3c 120 }
jburhenn 0:d20655cd1f3c 121 }
jburhenn 0:d20655cd1f3c 122 if (message.id) {
jburhenn 0:d20655cd1f3c 123 printf(" id=%s", message.id);
jburhenn 0:d20655cd1f3c 124 }
jburhenn 0:d20655cd1f3c 125 printf("\n");
jburhenn 0:d20655cd1f3c 126 }
jburhenn 0:d20655cd1f3c 127
jburhenn 0:d20655cd1f3c 128 /**
jburhenn 0:d20655cd1f3c 129 * Handle messages received from the Cayenne server.
jburhenn 0:d20655cd1f3c 130 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 131 */
jburhenn 2:445f09de09bd 132 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 133 {
jburhenn 0:d20655cd1f3c 134 int error = 0;
stiotchallenge 8:b19f86c36974 135
jburhenn 0:d20655cd1f3c 136 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:d20655cd1f3c 137 outputMessage(message);
jburhenn 0:d20655cd1f3c 138
jburhenn 1:a2d8aec2bb92 139 if (message.topic == COMMAND_TOPIC) {
jburhenn 1:a2d8aec2bb92 140 switch(message.channel) {
jburhenn 1:a2d8aec2bb92 141 case 0:
jburhenn 1:a2d8aec2bb92 142 // Set the onboard LED state
stiotchallenge 8:b19f86c36974 143 iotvalue = atoi(message.getValue());
stiotchallenge 8:b19f86c36974 144 printf("From Cayenne = %d\n",iotvalue);
jburhenn 1:a2d8aec2bb92 145 // Publish the updated LED state
jburhenn 2:445f09de09bd 146 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 147 printf("Publish LED state failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 148 }
jburhenn 1:a2d8aec2bb92 149 break;
jburhenn 1:a2d8aec2bb92 150 }
jburhenn 1:a2d8aec2bb92 151
jburhenn 1:a2d8aec2bb92 152 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 1:a2d8aec2bb92 153 // An error response should be sent if there are issues processing the message.
jburhenn 4:939f308d004a 154 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 155 printf("Response failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 156 }
jburhenn 0:d20655cd1f3c 157 }
jburhenn 0:d20655cd1f3c 158 }
jburhenn 0:d20655cd1f3c 159
jburhenn 0:d20655cd1f3c 160 /**
jburhenn 0:d20655cd1f3c 161 * Connect to the Cayenne server.
jburhenn 0:d20655cd1f3c 162 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:d20655cd1f3c 163 */
jburhenn 0:d20655cd1f3c 164 int connectClient(void)
jburhenn 0:d20655cd1f3c 165 {
jburhenn 0:d20655cd1f3c 166 int error = 0;
jburhenn 0:d20655cd1f3c 167 // Connect to the server.
jburhenn 0:d20655cd1f3c 168 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:d20655cd1f3c 169 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:d20655cd1f3c 170 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 171 wait(2);
jburhenn 0:d20655cd1f3c 172 }
jburhenn 0:d20655cd1f3c 173
jburhenn 2:445f09de09bd 174 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:d20655cd1f3c 175 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 176 return error;
jburhenn 0:d20655cd1f3c 177 }
jburhenn 0:d20655cd1f3c 178 printf("Connected\n");
jburhenn 0:d20655cd1f3c 179
jburhenn 0:d20655cd1f3c 180 // Subscribe to required topics.
jburhenn 0:d20655cd1f3c 181 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 182 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 183 }
jburhenn 0:d20655cd1f3c 184 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 185 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:d20655cd1f3c 186 }
jburhenn 0:d20655cd1f3c 187
jburhenn 0:d20655cd1f3c 188 // Send device info. Here we just send some example values for the system info. These should be changed to use actual system data, or removed if not needed.
jburhenn 0:d20655cd1f3c 189 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:d20655cd1f3c 190 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 2:445f09de09bd 191 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 2:445f09de09bd 192 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:d20655cd1f3c 193
jburhenn 0:d20655cd1f3c 194 return CAYENNE_SUCCESS;
jburhenn 0:d20655cd1f3c 195 }
jburhenn 0:d20655cd1f3c 196
jburhenn 0:d20655cd1f3c 197 /**
jburhenn 0:d20655cd1f3c 198 * Main loop where MQTT code is run.
jburhenn 0:d20655cd1f3c 199 */
jburhenn 0:d20655cd1f3c 200 void loop(void)
jburhenn 0:d20655cd1f3c 201 {
jburhenn 7:78cefe0937ab 202 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
stiotchallenge 12:dd98953c0a6d 203 MQTTTimer timer(1500);
jburhenn 0:d20655cd1f3c 204
jburhenn 0:d20655cd1f3c 205 while (true) {
stiotchallenge 8:b19f86c36974 206
jburhenn 0:d20655cd1f3c 207 // Yield to allow MQTT message processing.
jburhenn 0:d20655cd1f3c 208 mqttClient.yield(1000);
jburhenn 0:d20655cd1f3c 209
jburhenn 0:d20655cd1f3c 210 // Check that we are still connected, if not, reconnect.
jburhenn 0:d20655cd1f3c 211 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:d20655cd1f3c 212 network.disconnect();
jburhenn 0:d20655cd1f3c 213 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 214 printf("Reconnecting\n");
jburhenn 0:d20655cd1f3c 215 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 216 wait(2);
jburhenn 0:d20655cd1f3c 217 printf("Reconnect failed, retrying\n");
jburhenn 0:d20655cd1f3c 218 }
stiotchallenge 9:e3a211745f71 219 }
stiotchallenge 9:e3a211745f71 220 timer = myFunction (timer);
jburhenn 0:d20655cd1f3c 221 }
jburhenn 0:d20655cd1f3c 222 }
jburhenn 0:d20655cd1f3c 223
jburhenn 0:d20655cd1f3c 224 /**
jburhenn 0:d20655cd1f3c 225 * Main function.
jburhenn 0:d20655cd1f3c 226 */
jburhenn 0:d20655cd1f3c 227 int main()
jburhenn 0:d20655cd1f3c 228 {
jburhenn 0:d20655cd1f3c 229 // Initialize the network interface.
jburhenn 0:d20655cd1f3c 230 printf("Initializing interface\n");
jburhenn 0:d20655cd1f3c 231 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
jburhenn 0:d20655cd1f3c 232
jburhenn 0:d20655cd1f3c 233 // Set the default function that receives Cayenne messages.
jburhenn 0:d20655cd1f3c 234 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:d20655cd1f3c 235
jburhenn 0:d20655cd1f3c 236 // Connect to Cayenne.
jburhenn 0:d20655cd1f3c 237 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 238 // Run main loop.
jburhenn 0:d20655cd1f3c 239 loop();
jburhenn 0:d20655cd1f3c 240 }
jburhenn 0:d20655cd1f3c 241 else {
jburhenn 0:d20655cd1f3c 242 printf("Connection failed, exiting\n");
jburhenn 0:d20655cd1f3c 243 }
jburhenn 0:d20655cd1f3c 244
jburhenn 0:d20655cd1f3c 245 if (mqttClient.connected())
jburhenn 0:d20655cd1f3c 246 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 247 if (network.connected())
jburhenn 0:d20655cd1f3c 248 network.disconnect();
jburhenn 0:d20655cd1f3c 249
jburhenn 0:d20655cd1f3c 250 return 0;
stiotchallenge 9:e3a211745f71 251 }