Ultrasonic-Servo Combo

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

Committer:
stiotchallenge
Date:
Tue Jul 16 21:15:50 2019 +0000
Revision:
10:2951beb4fca3
Parent:
9:e3a211745f71
Child:
11:760e0ede36bb
Nucleo F411RE, SR04 and servomotor with Cayenne IoT

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 9:e3a211745f71 30 //HCSR04 sensor(D7, D6);
stiotchallenge 9:e3a211745f71 31 //Servo myservo(D10);
jburhenn 1:a2d8aec2bb92 32
stiotchallenge 8:b19f86c36974 33 int iotvalue;
stiotchallenge 10:2951beb4fca3 34 int iotvalue2;
stiotchallenge 9:e3a211745f71 35
stiotchallenge 9:e3a211745f71 36 //Function prototype new
stiotchallenge 9:e3a211745f71 37 MQTTTimer publishData(MQTTTimer, int, int);
stiotchallenge 9:e3a211745f71 38 MQTTTimer myFunction(MQTTTimer);
stiotchallenge 9:e3a211745f71 39
stiotchallenge 9:e3a211745f71 40 MQTTTimer myFunction (MQTTTimer timer){
stiotchallenge 9:e3a211745f71 41 /**
stiotchallenge 9:e3a211745f71 42 * Write your codes here.
stiotchallenge 9:e3a211745f71 43 **/
stiotchallenge 9:e3a211745f71 44 led1 = 1;
stiotchallenge 9:e3a211745f71 45 wait(0.2);
stiotchallenge 9:e3a211745f71 46 led1 = 0;
stiotchallenge 9:e3a211745f71 47 wait(0.2);
stiotchallenge 9:e3a211745f71 48
stiotchallenge 9:e3a211745f71 49 ledGreen = iotvalue;
stiotchallenge 10:2951beb4fca3 50 ledRed = iotvalue2;
stiotchallenge 9:e3a211745f71 51
stiotchallenge 9:e3a211745f71 52 //ULTRASONIC SENSOR WITH LED/////////////////////
stiotchallenge 9:e3a211745f71 53 // long distance = sensor.distance();
stiotchallenge 9:e3a211745f71 54 // printf("distance %d \n",distance);
stiotchallenge 9:e3a211745f71 55 // wait(1.0); // 1 sec
stiotchallenge 9:e3a211745f71 56 // if (distance > 50) {
stiotchallenge 9:e3a211745f71 57 // ledGreen = 0;
stiotchallenge 9:e3a211745f71 58 // }
stiotchallenge 9:e3a211745f71 59 // if (distance <50) {
stiotchallenge 9:e3a211745f71 60 // ledGreen = 1;
stiotchallenge 9:e3a211745f71 61 // }
stiotchallenge 9:e3a211745f71 62 ////////////////////////////////////////////////
stiotchallenge 9:e3a211745f71 63
stiotchallenge 9:e3a211745f71 64 //SERVOMOTOR WITH BUTTON////////////////////////
stiotchallenge 9:e3a211745f71 65 // if (button1) {
stiotchallenge 9:e3a211745f71 66 // myservo = 1; //tutup
stiotchallenge 9:e3a211745f71 67 // wait(0.2);
stiotchallenge 9:e3a211745f71 68 // }
stiotchallenge 9:e3a211745f71 69 // if (~button1) {
stiotchallenge 9:e3a211745f71 70 // myservo = 0; //buka
stiotchallenge 9:e3a211745f71 71 // wait(0.2);
stiotchallenge 9:e3a211745f71 72 // }
stiotchallenge 9:e3a211745f71 73 /////////////////////////////////////////////////
stiotchallenge 9:e3a211745f71 74
stiotchallenge 9:e3a211745f71 75 //ULTRASONIC + SERVO/////////////////////////////
stiotchallenge 9:e3a211745f71 76 // int openclose;
stiotchallenge 9:e3a211745f71 77 // long distance = sensor.distance();
stiotchallenge 9:e3a211745f71 78 // printf("distance %d \n",distance);
stiotchallenge 9:e3a211745f71 79 // wait(1.0); // 1 sec
stiotchallenge 9:e3a211745f71 80 // if (distance > 50) {
stiotchallenge 9:e3a211745f71 81 // ledGreen = 0;
stiotchallenge 9:e3a211745f71 82 // myservo = 1; //tutup
stiotchallenge 9:e3a211745f71 83 // wait(0.2);
stiotchallenge 9:e3a211745f71 84 // openclose = 0;
stiotchallenge 9:e3a211745f71 85 // }
stiotchallenge 9:e3a211745f71 86 // if (distance <50) {
stiotchallenge 9:e3a211745f71 87 // ledGreen = 1;
stiotchallenge 9:e3a211745f71 88 // myservo = 0; //buka
stiotchallenge 9:e3a211745f71 89 // wait(0.2);
stiotchallenge 9:e3a211745f71 90 // openclose = 1;
stiotchallenge 9:e3a211745f71 91 // }
stiotchallenge 9:e3a211745f71 92
stiotchallenge 9:e3a211745f71 93 timer = publishData(timer, ledGreen, led1);
stiotchallenge 9:e3a211745f71 94 return timer;
stiotchallenge 9:e3a211745f71 95 }
stiotchallenge 9:e3a211745f71 96
stiotchallenge 9:e3a211745f71 97 MQTTTimer publishData(MQTTTimer timer, int data1, int data2){
stiotchallenge 9:e3a211745f71 98 // Publish some example data every few seconds. This should be changed to send your actual data to Cayenne.
stiotchallenge 9:e3a211745f71 99 if (timer.expired()) {
stiotchallenge 9:e3a211745f71 100 int error = 0;
stiotchallenge 9:e3a211745f71 101 if ((error = mqttClient.publishData(DATA_TOPIC, 1, TYPE_VOLTAGE, UNIT_DIGITAL, data1)) != CAYENNE_SUCCESS) {
stiotchallenge 9:e3a211745f71 102 printf("Publish temperature failed, error: %d\n", error);
stiotchallenge 9:e3a211745f71 103 }
stiotchallenge 9:e3a211745f71 104 // if ((error = mqttClient.publishData(DATA_TOPIC, 2, TYPE_VOLTAGE, UNIT_DIGITAL, data2)) != CAYENNE_SUCCESS) {
stiotchallenge 9:e3a211745f71 105 // printf("Publish data failed, error: %d\n", error);
stiotchallenge 9:e3a211745f71 106 // }
stiotchallenge 9:e3a211745f71 107 // Restart the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
stiotchallenge 10:2951beb4fca3 108 timer.countdown_ms(2500);
stiotchallenge 9:e3a211745f71 109 }
stiotchallenge 9:e3a211745f71 110 return timer;
stiotchallenge 9:e3a211745f71 111 }
stiotchallenge 9:e3a211745f71 112
jburhenn 0:d20655cd1f3c 113 /**
jburhenn 0:d20655cd1f3c 114 * Print the message info.
jburhenn 0:d20655cd1f3c 115 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 116 */
jburhenn 2:445f09de09bd 117 void outputMessage(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 118 {
jburhenn 0:d20655cd1f3c 119 switch (message.topic) {
jburhenn 0:d20655cd1f3c 120 case COMMAND_TOPIC:
jburhenn 0:d20655cd1f3c 121 printf("topic=Command");
jburhenn 0:d20655cd1f3c 122 break;
jburhenn 0:d20655cd1f3c 123 case CONFIG_TOPIC:
jburhenn 0:d20655cd1f3c 124 printf("topic=Config");
jburhenn 0:d20655cd1f3c 125 break;
jburhenn 0:d20655cd1f3c 126 default:
jburhenn 0:d20655cd1f3c 127 printf("topic=%d", message.topic);
jburhenn 0:d20655cd1f3c 128 break;
jburhenn 0:d20655cd1f3c 129 }
jburhenn 0:d20655cd1f3c 130 printf(" channel=%d", message.channel);
jburhenn 0:d20655cd1f3c 131 if (message.clientID) {
jburhenn 0:d20655cd1f3c 132 printf(" clientID=%s", message.clientID);
jburhenn 0:d20655cd1f3c 133 }
jburhenn 0:d20655cd1f3c 134 if (message.type) {
jburhenn 0:d20655cd1f3c 135 printf(" type=%s", message.type);
jburhenn 0:d20655cd1f3c 136 }
jburhenn 0:d20655cd1f3c 137 for (size_t i = 0; i < message.valueCount; ++i) {
jburhenn 2:445f09de09bd 138 if (message.getValue(i)) {
jburhenn 2:445f09de09bd 139 printf(" value=%s", message.getValue(i));
jburhenn 0:d20655cd1f3c 140 }
jburhenn 2:445f09de09bd 141 if (message.getUnit(i)) {
jburhenn 2:445f09de09bd 142 printf(" unit=%s", message.getUnit(i));
jburhenn 0:d20655cd1f3c 143 }
jburhenn 0:d20655cd1f3c 144 }
jburhenn 0:d20655cd1f3c 145 if (message.id) {
jburhenn 0:d20655cd1f3c 146 printf(" id=%s", message.id);
jburhenn 0:d20655cd1f3c 147 }
jburhenn 0:d20655cd1f3c 148 printf("\n");
jburhenn 0:d20655cd1f3c 149 }
jburhenn 0:d20655cd1f3c 150
jburhenn 0:d20655cd1f3c 151 /**
jburhenn 0:d20655cd1f3c 152 * Handle messages received from the Cayenne server.
jburhenn 0:d20655cd1f3c 153 * @param[in] message The message received from the Cayenne server.
jburhenn 0:d20655cd1f3c 154 */
jburhenn 2:445f09de09bd 155 void messageArrived(CayenneMQTT::MessageData& message)
jburhenn 0:d20655cd1f3c 156 {
jburhenn 0:d20655cd1f3c 157 int error = 0;
stiotchallenge 8:b19f86c36974 158
jburhenn 0:d20655cd1f3c 159 // Add code to process the message. Here we just ouput the message data.
jburhenn 0:d20655cd1f3c 160 outputMessage(message);
jburhenn 0:d20655cd1f3c 161
jburhenn 1:a2d8aec2bb92 162 if (message.topic == COMMAND_TOPIC) {
jburhenn 1:a2d8aec2bb92 163 switch(message.channel) {
jburhenn 1:a2d8aec2bb92 164 case 0:
jburhenn 1:a2d8aec2bb92 165 // Set the onboard LED state
stiotchallenge 8:b19f86c36974 166 iotvalue = atoi(message.getValue());
stiotchallenge 8:b19f86c36974 167 printf("From Cayenne = %d\n",iotvalue);
jburhenn 1:a2d8aec2bb92 168 // Publish the updated LED state
jburhenn 2:445f09de09bd 169 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 170 printf("Publish LED state failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 171 }
jburhenn 1:a2d8aec2bb92 172 break;
stiotchallenge 10:2951beb4fca3 173 case 5:
stiotchallenge 10:2951beb4fca3 174 // Set the onboard LED state
stiotchallenge 10:2951beb4fca3 175 iotvalue2 = atoi(message.getValue());
stiotchallenge 10:2951beb4fca3 176 printf("From Cayenne = %d\n",iotvalue);
stiotchallenge 10:2951beb4fca3 177 // Publish the updated LED state
stiotchallenge 10:2951beb4fca3 178 if ((error = mqttClient.publishData(DATA_TOPIC, message.channel, NULL, NULL, message.getValue())) != CAYENNE_SUCCESS) {
stiotchallenge 10:2951beb4fca3 179 printf("Publish LED state failure, error: %d\n", error);
stiotchallenge 10:2951beb4fca3 180 }
stiotchallenge 10:2951beb4fca3 181 break;
jburhenn 1:a2d8aec2bb92 182 }
jburhenn 1:a2d8aec2bb92 183
jburhenn 1:a2d8aec2bb92 184 // If this is a command message we publish a response. Here we are just sending a default 'OK' response.
jburhenn 1:a2d8aec2bb92 185 // An error response should be sent if there are issues processing the message.
jburhenn 4:939f308d004a 186 if ((error = mqttClient.publishResponse(message.id, NULL, message.clientID)) != CAYENNE_SUCCESS) {
jburhenn 1:a2d8aec2bb92 187 printf("Response failure, error: %d\n", error);
jburhenn 1:a2d8aec2bb92 188 }
jburhenn 0:d20655cd1f3c 189 }
jburhenn 0:d20655cd1f3c 190 }
jburhenn 0:d20655cd1f3c 191
jburhenn 0:d20655cd1f3c 192 /**
jburhenn 0:d20655cd1f3c 193 * Connect to the Cayenne server.
jburhenn 0:d20655cd1f3c 194 * @return Returns CAYENNE_SUCCESS if the connection succeeds, or an error code otherwise.
jburhenn 0:d20655cd1f3c 195 */
jburhenn 0:d20655cd1f3c 196 int connectClient(void)
jburhenn 0:d20655cd1f3c 197 {
jburhenn 0:d20655cd1f3c 198 int error = 0;
jburhenn 0:d20655cd1f3c 199 // Connect to the server.
jburhenn 0:d20655cd1f3c 200 printf("Connecting to %s:%d\n", CAYENNE_DOMAIN, CAYENNE_PORT);
jburhenn 0:d20655cd1f3c 201 while ((error = network.connect(CAYENNE_DOMAIN, CAYENNE_PORT)) != 0) {
jburhenn 0:d20655cd1f3c 202 printf("TCP connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 203 wait(2);
jburhenn 0:d20655cd1f3c 204 }
jburhenn 0:d20655cd1f3c 205
jburhenn 2:445f09de09bd 206 if ((error = mqttClient.connect()) != MQTT::SUCCESS) {
jburhenn 0:d20655cd1f3c 207 printf("MQTT connect failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 208 return error;
jburhenn 0:d20655cd1f3c 209 }
jburhenn 0:d20655cd1f3c 210 printf("Connected\n");
jburhenn 0:d20655cd1f3c 211
jburhenn 0:d20655cd1f3c 212 // Subscribe to required topics.
jburhenn 0:d20655cd1f3c 213 if ((error = mqttClient.subscribe(COMMAND_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 214 printf("Subscription to Command topic failed, error: %d\n", error);
jburhenn 0:d20655cd1f3c 215 }
jburhenn 0:d20655cd1f3c 216 if ((error = mqttClient.subscribe(CONFIG_TOPIC, CAYENNE_ALL_CHANNELS)) != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 217 printf("Subscription to Config topic failed, error:%d\n", error);
jburhenn 0:d20655cd1f3c 218 }
jburhenn 0:d20655cd1f3c 219
jburhenn 0:d20655cd1f3c 220 // 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 221 mqttClient.publishData(SYS_VERSION_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, CAYENNE_VERSION);
jburhenn 0:d20655cd1f3c 222 mqttClient.publishData(SYS_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "mbedDevice");
jburhenn 2:445f09de09bd 223 //mqttClient.publishData(SYS_CPU_MODEL_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "CPU Model");
jburhenn 2:445f09de09bd 224 //mqttClient.publishData(SYS_CPU_SPEED_TOPIC, CAYENNE_NO_CHANNEL, NULL, NULL, "1000000000");
jburhenn 0:d20655cd1f3c 225
jburhenn 0:d20655cd1f3c 226 return CAYENNE_SUCCESS;
jburhenn 0:d20655cd1f3c 227 }
jburhenn 0:d20655cd1f3c 228
jburhenn 0:d20655cd1f3c 229 /**
jburhenn 0:d20655cd1f3c 230 * Main loop where MQTT code is run.
jburhenn 0:d20655cd1f3c 231 */
jburhenn 0:d20655cd1f3c 232 void loop(void)
jburhenn 0:d20655cd1f3c 233 {
jburhenn 7:78cefe0937ab 234 // Start the countdown timer for publishing data every 5 seconds. Change the timeout parameter to publish at a different interval.
stiotchallenge 8:b19f86c36974 235 MQTTTimer timer(3000);
jburhenn 0:d20655cd1f3c 236
jburhenn 0:d20655cd1f3c 237 while (true) {
stiotchallenge 8:b19f86c36974 238
jburhenn 0:d20655cd1f3c 239 // Yield to allow MQTT message processing.
jburhenn 0:d20655cd1f3c 240 mqttClient.yield(1000);
jburhenn 0:d20655cd1f3c 241
jburhenn 0:d20655cd1f3c 242 // Check that we are still connected, if not, reconnect.
jburhenn 0:d20655cd1f3c 243 if (!network.connected() || !mqttClient.connected()) {
jburhenn 0:d20655cd1f3c 244 network.disconnect();
jburhenn 0:d20655cd1f3c 245 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 246 printf("Reconnecting\n");
jburhenn 0:d20655cd1f3c 247 while (connectClient() != CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 248 wait(2);
jburhenn 0:d20655cd1f3c 249 printf("Reconnect failed, retrying\n");
jburhenn 0:d20655cd1f3c 250 }
stiotchallenge 9:e3a211745f71 251 }
stiotchallenge 9:e3a211745f71 252 timer = myFunction (timer);
jburhenn 0:d20655cd1f3c 253 }
jburhenn 0:d20655cd1f3c 254 }
jburhenn 0:d20655cd1f3c 255
jburhenn 0:d20655cd1f3c 256 /**
jburhenn 0:d20655cd1f3c 257 * Main function.
jburhenn 0:d20655cd1f3c 258 */
jburhenn 0:d20655cd1f3c 259 int main()
jburhenn 0:d20655cd1f3c 260 {
jburhenn 0:d20655cd1f3c 261 // Initialize the network interface.
jburhenn 0:d20655cd1f3c 262 printf("Initializing interface\n");
jburhenn 0:d20655cd1f3c 263 interface.connect(ssid, wifiPassword, NSAPI_SECURITY_WPA2);
jburhenn 0:d20655cd1f3c 264
jburhenn 0:d20655cd1f3c 265 // Set the default function that receives Cayenne messages.
jburhenn 0:d20655cd1f3c 266 mqttClient.setDefaultMessageHandler(messageArrived);
jburhenn 0:d20655cd1f3c 267
jburhenn 0:d20655cd1f3c 268 // Connect to Cayenne.
jburhenn 0:d20655cd1f3c 269 if (connectClient() == CAYENNE_SUCCESS) {
jburhenn 0:d20655cd1f3c 270 // Run main loop.
jburhenn 0:d20655cd1f3c 271 loop();
jburhenn 0:d20655cd1f3c 272 }
jburhenn 0:d20655cd1f3c 273 else {
jburhenn 0:d20655cd1f3c 274 printf("Connection failed, exiting\n");
jburhenn 0:d20655cd1f3c 275 }
jburhenn 0:d20655cd1f3c 276
jburhenn 0:d20655cd1f3c 277 if (mqttClient.connected())
jburhenn 0:d20655cd1f3c 278 mqttClient.disconnect();
jburhenn 0:d20655cd1f3c 279 if (network.connected())
jburhenn 0:d20655cd1f3c 280 network.disconnect();
jburhenn 0:d20655cd1f3c 281
jburhenn 0:d20655cd1f3c 282 return 0;
stiotchallenge 9:e3a211745f71 283 }