Servomotor-Button

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

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