Servomotor-Button

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

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