BUTTON LED COMBO - ST IOT CHALLENGE 2020

Dependencies:   Cayenne-MQTT-mbed mbed X_NUCLEO_IDW01M1v2 NetworkSocketAPI

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