IoT Starter Kit Code

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Committer:
icraggs
Date:
Tue Sep 30 15:57:57 2014 +0000
Revision:
1:8f47175f0f53
Parent:
0:0777b6b0c36f
Removed conditional compilation for IoT registration settings

Who changed what in which revision?

UserRevisionLine numberNew contents of line
icraggs 0:0777b6b0c36f 1 /*******************************************************************************
icraggs 0:0777b6b0c36f 2 * Copyright (c) 2014 IBM Corp.
icraggs 0:0777b6b0c36f 3 *
icraggs 0:0777b6b0c36f 4 * All rights reserved. This program and the accompanying materials
icraggs 0:0777b6b0c36f 5 * are made available under the terms of the Eclipse Public License v1.0
icraggs 0:0777b6b0c36f 6 * and Eclipse Distribution License v1.0 which accompany this distribution.
icraggs 0:0777b6b0c36f 7 *
icraggs 0:0777b6b0c36f 8 * The Eclipse Public License is available at
icraggs 0:0777b6b0c36f 9 * http://www.eclipse.org/legal/epl-v10.html
icraggs 0:0777b6b0c36f 10 * and the Eclipse Distribution License is available at
icraggs 0:0777b6b0c36f 11 * http://www.eclipse.org/org/documents/edl-v10.php.
icraggs 0:0777b6b0c36f 12 *
icraggs 0:0777b6b0c36f 13 * Contributors:
icraggs 0:0777b6b0c36f 14 * Sam Danbury - initial implementation
icraggs 0:0777b6b0c36f 15 * Ian Craggs - refactoring to remove STL and other changes
icraggs 0:0777b6b0c36f 16 * Sam Grove - added check for Ethernet cable.
icraggs 0:0777b6b0c36f 17 *******************************************************************************/
icraggs 0:0777b6b0c36f 18
icraggs 0:0777b6b0c36f 19 #include "LM75B.h"
icraggs 0:0777b6b0c36f 20 #include "MMA7660.h"
icraggs 0:0777b6b0c36f 21 #include "MQTTClient.h"
icraggs 0:0777b6b0c36f 22 #include "MQTTEthernet.h"
icraggs 0:0777b6b0c36f 23 #include "C12832.h"
icraggs 0:0777b6b0c36f 24 #include "Arial12x12.h"
icraggs 0:0777b6b0c36f 25 #include "rtos.h"
icraggs 0:0777b6b0c36f 26 #include "K64F.h"
icraggs 0:0777b6b0c36f 27
icraggs 0:0777b6b0c36f 28 // Configuration values needed to connect to IBM IoT Cloud
icraggs 1:8f47175f0f53 29 #define ORG "quickstart" // For a registered connection, replace with your org
icraggs 1:8f47175f0f53 30 #define ID "" // For a registered connection, replace with your id
icraggs 1:8f47175f0f53 31 #define AUTH_TOKEN "" // For a registered connection, replace with your auth-token
icraggs 1:8f47175f0f53 32 #define TYPE DEFAULT_TYPE_NAME // For a registered connection, replace with your type
icraggs 0:0777b6b0c36f 33
icraggs 0:0777b6b0c36f 34 #define MQTT_PORT 1883
icraggs 0:0777b6b0c36f 35 #define MQTT_TLS_PORT 8883
icraggs 0:0777b6b0c36f 36 #define IBM_IOT_PORT MQTT_PORT
icraggs 0:0777b6b0c36f 37
icraggs 0:0777b6b0c36f 38 #define MQTT_MAX_PACKET_SIZE 250
icraggs 0:0777b6b0c36f 39
icraggs 1:8f47175f0f53 40 bool quickstartMode = true;
icraggs 0:0777b6b0c36f 41 char org[11] = ORG;
icraggs 0:0777b6b0c36f 42 char type[30] = TYPE;
icraggs 0:0777b6b0c36f 43 char id[30] = ID; // mac without colons
icraggs 0:0777b6b0c36f 44 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
icraggs 0:0777b6b0c36f 45
icraggs 0:0777b6b0c36f 46 bool connected = false;
icraggs 0:0777b6b0c36f 47 char* joystickPos = "CENTRE";
icraggs 0:0777b6b0c36f 48 int blink_interval = 0;
icraggs 0:0777b6b0c36f 49
icraggs 0:0777b6b0c36f 50
icraggs 0:0777b6b0c36f 51 void off()
icraggs 0:0777b6b0c36f 52 {
icraggs 0:0777b6b0c36f 53 r = g = b = 1.0; // 1 is off, 0 is full brightness
icraggs 0:0777b6b0c36f 54 }
icraggs 0:0777b6b0c36f 55
icraggs 0:0777b6b0c36f 56 void red()
icraggs 0:0777b6b0c36f 57 {
icraggs 0:0777b6b0c36f 58 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness
icraggs 0:0777b6b0c36f 59 }
icraggs 0:0777b6b0c36f 60
icraggs 0:0777b6b0c36f 61 void yellow()
icraggs 0:0777b6b0c36f 62 {
icraggs 0:0777b6b0c36f 63 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
icraggs 0:0777b6b0c36f 64 }
icraggs 0:0777b6b0c36f 65
icraggs 0:0777b6b0c36f 66 void green()
icraggs 0:0777b6b0c36f 67 {
icraggs 0:0777b6b0c36f 68 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
icraggs 0:0777b6b0c36f 69 }
icraggs 0:0777b6b0c36f 70
icraggs 0:0777b6b0c36f 71
icraggs 0:0777b6b0c36f 72 void flashing_yellow(void const *args)
icraggs 0:0777b6b0c36f 73 {
icraggs 0:0777b6b0c36f 74 bool on = false;
icraggs 0:0777b6b0c36f 75 while (!connected) // flashing yellow only while connecting
icraggs 0:0777b6b0c36f 76 {
icraggs 0:0777b6b0c36f 77 on = !on;
icraggs 0:0777b6b0c36f 78 if (on)
icraggs 0:0777b6b0c36f 79 yellow();
icraggs 0:0777b6b0c36f 80 else
icraggs 0:0777b6b0c36f 81 off();
icraggs 0:0777b6b0c36f 82 wait(0.5);
icraggs 0:0777b6b0c36f 83 }
icraggs 0:0777b6b0c36f 84 }
icraggs 0:0777b6b0c36f 85
icraggs 0:0777b6b0c36f 86
icraggs 0:0777b6b0c36f 87 void flashing_red(void const *args) // to be used when the connection is lost
icraggs 0:0777b6b0c36f 88 {
icraggs 0:0777b6b0c36f 89 bool on = false;
icraggs 0:0777b6b0c36f 90 while (!connected)
icraggs 0:0777b6b0c36f 91 {
icraggs 0:0777b6b0c36f 92 on = !on;
icraggs 0:0777b6b0c36f 93 if (on)
icraggs 0:0777b6b0c36f 94 red();
icraggs 0:0777b6b0c36f 95 else
icraggs 0:0777b6b0c36f 96 off();
icraggs 0:0777b6b0c36f 97 wait(2.0);
icraggs 0:0777b6b0c36f 98 }
icraggs 0:0777b6b0c36f 99 }
icraggs 0:0777b6b0c36f 100
icraggs 0:0777b6b0c36f 101
icraggs 0:0777b6b0c36f 102 void printMenu(int menuItem)
icraggs 0:0777b6b0c36f 103 {
icraggs 0:0777b6b0c36f 104 lcd.cls();
icraggs 0:0777b6b0c36f 105 lcd.locate(0,0);
icraggs 0:0777b6b0c36f 106 switch (menuItem)
icraggs 0:0777b6b0c36f 107 {
icraggs 0:0777b6b0c36f 108 case 0:
icraggs 0:0777b6b0c36f 109 lcd.printf("IBM IoT Cloud");
icraggs 0:0777b6b0c36f 110 lcd.locate(0,16);
icraggs 0:0777b6b0c36f 111 lcd.printf("Scroll with joystick");
icraggs 0:0777b6b0c36f 112 break;
icraggs 0:0777b6b0c36f 113 case 1:
icraggs 0:0777b6b0c36f 114 lcd.printf("Go to:");
icraggs 0:0777b6b0c36f 115 lcd.locate(0,16);
icraggs 0:0777b6b0c36f 116 lcd.printf("http://ibm.biz/iotqstart");
icraggs 0:0777b6b0c36f 117 break;
icraggs 0:0777b6b0c36f 118 case 2:
icraggs 0:0777b6b0c36f 119 lcd.printf("Device Identity:");
icraggs 0:0777b6b0c36f 120 lcd.locate(0,16);
icraggs 0:0777b6b0c36f 121 lcd.printf("%s", id);
icraggs 0:0777b6b0c36f 122 break;
icraggs 0:0777b6b0c36f 123 case 3:
icraggs 0:0777b6b0c36f 124 lcd.printf("Status:");
icraggs 0:0777b6b0c36f 125 lcd.locate(0,16);
icraggs 0:0777b6b0c36f 126 lcd.printf(connected ? "Connected" : "Disconnected");
icraggs 0:0777b6b0c36f 127 break;
icraggs 0:0777b6b0c36f 128 }
icraggs 0:0777b6b0c36f 129 }
icraggs 0:0777b6b0c36f 130
icraggs 0:0777b6b0c36f 131
icraggs 0:0777b6b0c36f 132 void setMenu()
icraggs 0:0777b6b0c36f 133 {
icraggs 0:0777b6b0c36f 134 static int menuItem = 0;
icraggs 0:0777b6b0c36f 135 if (Down)
icraggs 0:0777b6b0c36f 136 {
icraggs 0:0777b6b0c36f 137 joystickPos = "DOWN";
icraggs 0:0777b6b0c36f 138 if (menuItem >= 0 && menuItem < 3)
icraggs 0:0777b6b0c36f 139 printMenu(++menuItem);
icraggs 0:0777b6b0c36f 140 }
icraggs 0:0777b6b0c36f 141 else if (Left)
icraggs 0:0777b6b0c36f 142 joystickPos = "LEFT";
icraggs 0:0777b6b0c36f 143 else if (Click)
icraggs 0:0777b6b0c36f 144 joystickPos = "CLICK";
icraggs 0:0777b6b0c36f 145 else if (Up)
icraggs 0:0777b6b0c36f 146 {
icraggs 0:0777b6b0c36f 147 joystickPos = "UP";
icraggs 0:0777b6b0c36f 148 if (menuItem <= 3 && menuItem > 0)
icraggs 0:0777b6b0c36f 149 printMenu(--menuItem);
icraggs 0:0777b6b0c36f 150 }
icraggs 0:0777b6b0c36f 151 else if (Right)
icraggs 0:0777b6b0c36f 152 joystickPos = "RIGHT";
icraggs 0:0777b6b0c36f 153 else
icraggs 0:0777b6b0c36f 154 joystickPos = "CENTRE";
icraggs 0:0777b6b0c36f 155 }
icraggs 0:0777b6b0c36f 156
icraggs 0:0777b6b0c36f 157
icraggs 0:0777b6b0c36f 158 /**
icraggs 0:0777b6b0c36f 159 * Display a message on the LCD screen prefixed with IBM IoT Cloud
icraggs 0:0777b6b0c36f 160 */
icraggs 0:0777b6b0c36f 161 void displayMessage(char* message)
icraggs 0:0777b6b0c36f 162 {
icraggs 0:0777b6b0c36f 163 lcd.cls();
icraggs 0:0777b6b0c36f 164 lcd.locate(0,0);
icraggs 0:0777b6b0c36f 165 lcd.printf("IBM IoT Cloud");
icraggs 0:0777b6b0c36f 166 lcd.locate(0,16);
icraggs 0:0777b6b0c36f 167 lcd.printf(message);
icraggs 0:0777b6b0c36f 168 }
icraggs 0:0777b6b0c36f 169
icraggs 0:0777b6b0c36f 170
icraggs 0:0777b6b0c36f 171 int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
icraggs 0:0777b6b0c36f 172 {
icraggs 0:0777b6b0c36f 173 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com";
icraggs 0:0777b6b0c36f 174
icraggs 0:0777b6b0c36f 175 char hostname[strlen(org) + strlen(iot_ibm) + 1];
icraggs 0:0777b6b0c36f 176 sprintf(hostname, "%s%s", org, iot_ibm);
icraggs 0:0777b6b0c36f 177 int rc = ipstack->connect(hostname, IBM_IOT_PORT);
icraggs 0:0777b6b0c36f 178 if (rc != 0)
icraggs 0:0777b6b0c36f 179 return rc;
icraggs 0:0777b6b0c36f 180
icraggs 0:0777b6b0c36f 181 // Construct clientId - d:org:type:id
icraggs 0:0777b6b0c36f 182 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
icraggs 0:0777b6b0c36f 183 sprintf(clientId, "d:%s:%s:%s", org, type, id);
icraggs 0:0777b6b0c36f 184 DEBUG("clientid is %s\n", clientId);
icraggs 0:0777b6b0c36f 185
icraggs 0:0777b6b0c36f 186 // MQTT Connect
icraggs 0:0777b6b0c36f 187 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
icraggs 0:0777b6b0c36f 188 data.MQTTVersion = 3;
icraggs 0:0777b6b0c36f 189 data.clientID.cstring = clientId;
icraggs 1:8f47175f0f53 190 data.keepAliveInterval = 5;
icraggs 0:0777b6b0c36f 191
icraggs 0:0777b6b0c36f 192 if (!quickstartMode)
icraggs 0:0777b6b0c36f 193 {
icraggs 0:0777b6b0c36f 194 data.username.cstring = "use-token-auth";
icraggs 0:0777b6b0c36f 195 data.password.cstring = auth_token;
icraggs 0:0777b6b0c36f 196 }
icraggs 0:0777b6b0c36f 197
icraggs 0:0777b6b0c36f 198 if ((rc = client->connect(data)) == 0)
icraggs 0:0777b6b0c36f 199 {
icraggs 0:0777b6b0c36f 200 connected = true;
icraggs 0:0777b6b0c36f 201 green();
icraggs 0:0777b6b0c36f 202 displayMessage("Connected");
icraggs 0:0777b6b0c36f 203 wait(2);
icraggs 0:0777b6b0c36f 204 displayMessage("Scroll with joystick");
icraggs 0:0777b6b0c36f 205 }
icraggs 0:0777b6b0c36f 206 return rc;
icraggs 0:0777b6b0c36f 207 }
icraggs 0:0777b6b0c36f 208
icraggs 0:0777b6b0c36f 209
icraggs 0:0777b6b0c36f 210 int getConnTimeout(int attemptNumber)
icraggs 0:0777b6b0c36f 211 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
icraggs 0:0777b6b0c36f 212 // after 20 attempts, retry every 10 minutes
icraggs 0:0777b6b0c36f 213 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
icraggs 0:0777b6b0c36f 214 }
icraggs 0:0777b6b0c36f 215
icraggs 0:0777b6b0c36f 216
icraggs 0:0777b6b0c36f 217 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
icraggs 0:0777b6b0c36f 218 {
icraggs 0:0777b6b0c36f 219 int retryAttempt = 0;
icraggs 0:0777b6b0c36f 220 connected = false;
icraggs 0:0777b6b0c36f 221
icraggs 0:0777b6b0c36f 222 // make sure a cable is connected before starting to connect
icraggs 0:0777b6b0c36f 223 while (!linkStatus()) {
icraggs 0:0777b6b0c36f 224 wait(1.0f);
icraggs 0:0777b6b0c36f 225 WARN("Ethernet link not present. Check cable connection\n");
icraggs 0:0777b6b0c36f 226 }
icraggs 0:0777b6b0c36f 227
icraggs 0:0777b6b0c36f 228 while (connect(client, ipstack) != 0)
icraggs 0:0777b6b0c36f 229 {
icraggs 0:0777b6b0c36f 230 red(); // Thread red_thread(flashing_red);
icraggs 0:0777b6b0c36f 231
icraggs 0:0777b6b0c36f 232 int timeout = getConnTimeout(++retryAttempt);
icraggs 0:0777b6b0c36f 233 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
icraggs 0:0777b6b0c36f 234 if (retryAttempt == 5)
icraggs 0:0777b6b0c36f 235 NVIC_SystemReset();
icraggs 0:0777b6b0c36f 236 else
icraggs 0:0777b6b0c36f 237 wait(timeout);
icraggs 0:0777b6b0c36f 238 }
icraggs 0:0777b6b0c36f 239 }
icraggs 0:0777b6b0c36f 240
icraggs 0:0777b6b0c36f 241
icraggs 0:0777b6b0c36f 242 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
icraggs 0:0777b6b0c36f 243 {
icraggs 0:0777b6b0c36f 244 MQTT::Message message;
icraggs 0:0777b6b0c36f 245 char* pubTopic = "iot-2/evt/status/fmt/json";
icraggs 0:0777b6b0c36f 246
icraggs 0:0777b6b0c36f 247 char buf[250];
icraggs 0:0777b6b0c36f 248 sprintf(buf,
icraggs 0:0777b6b0c36f 249 "{\"d\":{\"myName\":\"IoT mbed\",\"accelX\":%0.4f,\"accelY\":%0.4f,\"accelZ\":%0.4f,\"temp\":%0.4f,\"joystick\":\"%s\",\"potentiometer1\":%0.4f,\"potentiometer2\":%0.4f}}",
icraggs 0:0777b6b0c36f 250 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
icraggs 0:0777b6b0c36f 251 message.qos = MQTT::QOS0;
icraggs 0:0777b6b0c36f 252 message.retained = false;
icraggs 0:0777b6b0c36f 253 message.dup = false;
icraggs 0:0777b6b0c36f 254 message.payload = (void*)buf;
icraggs 0:0777b6b0c36f 255 message.payloadlen = strlen(buf);
icraggs 0:0777b6b0c36f 256
icraggs 0:0777b6b0c36f 257 LOG("Publishing %s\n", buf);
icraggs 0:0777b6b0c36f 258 return client->publish(pubTopic, message);
icraggs 0:0777b6b0c36f 259 }
icraggs 0:0777b6b0c36f 260
icraggs 0:0777b6b0c36f 261
icraggs 0:0777b6b0c36f 262 int getUUID48(char* buf, int buflen)
icraggs 0:0777b6b0c36f 263 {
icraggs 0:0777b6b0c36f 264 unsigned int UUID_LOC_WORD0 = 0x40048060;
icraggs 0:0777b6b0c36f 265 unsigned int UUID_LOC_WORD1 = 0x4004805C;
icraggs 0:0777b6b0c36f 266
icraggs 0:0777b6b0c36f 267 uint32_t word0 = *(uint32_t *)UUID_LOC_WORD0; // Fetch word 0
icraggs 0:0777b6b0c36f 268
icraggs 0:0777b6b0c36f 269 uint32_t word1 = *(uint32_t *)UUID_LOC_WORD1; // Fetch word 1
icraggs 0:0777b6b0c36f 270 // we only want bottom 16 bits of word1 (MAC bits 32-47)
icraggs 0:0777b6b0c36f 271 // and bit 9 forced to 1, bit 8 forced to 0
icraggs 0:0777b6b0c36f 272 // Locally administered MAC, reduced conflicts http://en.wikipedia.org/wiki/MAC_address
icraggs 0:0777b6b0c36f 273 word1 |= 0x00000200;
icraggs 0:0777b6b0c36f 274 word1 &= 0x0000FEFF;
icraggs 0:0777b6b0c36f 275
icraggs 0:0777b6b0c36f 276 int rc = snprintf(buf, buflen, "%4X%08X", word1, word0);
icraggs 0:0777b6b0c36f 277
icraggs 0:0777b6b0c36f 278 return rc;
icraggs 0:0777b6b0c36f 279 }
icraggs 0:0777b6b0c36f 280
icraggs 0:0777b6b0c36f 281
icraggs 0:0777b6b0c36f 282 void messageArrived(MQTT::MessageData& md)
icraggs 0:0777b6b0c36f 283 {
icraggs 0:0777b6b0c36f 284 MQTT::Message &message = md.message;
icraggs 0:0777b6b0c36f 285 char topic[md.topicName.lenstring.len + 1];
icraggs 0:0777b6b0c36f 286
icraggs 0:0777b6b0c36f 287 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
icraggs 0:0777b6b0c36f 288
icraggs 0:0777b6b0c36f 289 LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload);
icraggs 0:0777b6b0c36f 290
icraggs 0:0777b6b0c36f 291 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
icraggs 0:0777b6b0c36f 292 char* start = strstr(topic, "/cmd/") + 5;
icraggs 0:0777b6b0c36f 293 int len = strstr(topic, "/fmt/") - start;
icraggs 0:0777b6b0c36f 294
icraggs 0:0777b6b0c36f 295 if (memcmp(start, "blink", len) == 0)
icraggs 0:0777b6b0c36f 296 {
icraggs 0:0777b6b0c36f 297 char payload[message.payloadlen + 1];
icraggs 0:0777b6b0c36f 298 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
icraggs 0:0777b6b0c36f 299
icraggs 0:0777b6b0c36f 300 char* pos = strchr(payload, '}');
icraggs 0:0777b6b0c36f 301 if (pos != NULL)
icraggs 0:0777b6b0c36f 302 {
icraggs 0:0777b6b0c36f 303 *pos = '\0';
icraggs 0:0777b6b0c36f 304 if ((pos = strchr(payload, ':')) != NULL)
icraggs 0:0777b6b0c36f 305 {
icraggs 0:0777b6b0c36f 306 int blink_rate = atoi(pos + 1);
icraggs 0:0777b6b0c36f 307 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
icraggs 0:0777b6b0c36f 308 }
icraggs 0:0777b6b0c36f 309 }
icraggs 0:0777b6b0c36f 310 }
icraggs 0:0777b6b0c36f 311 else
icraggs 0:0777b6b0c36f 312 WARN("Unsupported command: %.*s\n", len, start);
icraggs 0:0777b6b0c36f 313 }
icraggs 0:0777b6b0c36f 314
icraggs 0:0777b6b0c36f 315
icraggs 0:0777b6b0c36f 316 int main()
icraggs 0:0777b6b0c36f 317 {
icraggs 1:8f47175f0f53 318 quickstartMode = (strcmp(org, "quickstart") == 0);
icraggs 1:8f47175f0f53 319
icraggs 0:0777b6b0c36f 320 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen
icraggs 0:0777b6b0c36f 321
icraggs 0:0777b6b0c36f 322 led2 = LED2_OFF; // turn off the main board LED
icraggs 0:0777b6b0c36f 323
icraggs 0:0777b6b0c36f 324 displayMessage("Connecting");
icraggs 0:0777b6b0c36f 325
icraggs 0:0777b6b0c36f 326 yellow(); //Thread yellow_thread(flashing_yellow); causes the EthernetInterface init call to hang
icraggs 0:0777b6b0c36f 327
icraggs 0:0777b6b0c36f 328 MQTTEthernet ipstack;
icraggs 0:0777b6b0c36f 329 MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
icraggs 0:0777b6b0c36f 330
icraggs 0:0777b6b0c36f 331 if (quickstartMode)
icraggs 0:0777b6b0c36f 332 getUUID48(id, sizeof(id));
icraggs 0:0777b6b0c36f 333
icraggs 0:0777b6b0c36f 334 attemptConnect(&client, &ipstack);
icraggs 0:0777b6b0c36f 335
icraggs 0:0777b6b0c36f 336 if (!quickstartMode)
icraggs 0:0777b6b0c36f 337 {
icraggs 0:0777b6b0c36f 338 int rc = 0;
icraggs 0:0777b6b0c36f 339 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0)
icraggs 0:0777b6b0c36f 340 WARN("rc from MQTT subscribe is %d\n", rc);
icraggs 0:0777b6b0c36f 341 }
icraggs 0:0777b6b0c36f 342
icraggs 0:0777b6b0c36f 343 blink_interval = 0;
icraggs 0:0777b6b0c36f 344 int count = 0;
icraggs 0:0777b6b0c36f 345 while (true)
icraggs 0:0777b6b0c36f 346 {
icraggs 0:0777b6b0c36f 347 if (++count == 100)
icraggs 0:0777b6b0c36f 348 { // Publish a message every second
icraggs 0:0777b6b0c36f 349 if (publish(&client, &ipstack) != 0)
icraggs 0:0777b6b0c36f 350 attemptConnect(&client, &ipstack); // if we have lost the connection
icraggs 0:0777b6b0c36f 351 count = 0;
icraggs 0:0777b6b0c36f 352 }
icraggs 0:0777b6b0c36f 353
icraggs 0:0777b6b0c36f 354 if (blink_interval == 0)
icraggs 0:0777b6b0c36f 355 led2 = LED2_OFF;
icraggs 0:0777b6b0c36f 356 else if (count % blink_interval == 0)
icraggs 0:0777b6b0c36f 357 led2 = !led2;
icraggs 0:0777b6b0c36f 358 if (count % 20 == 0)
icraggs 0:0777b6b0c36f 359 setMenu();
icraggs 0:0777b6b0c36f 360 client.yield(10); // allow the MQTT client to receive messages
icraggs 0:0777b6b0c36f 361 }
icraggs 0:0777b6b0c36f 362 }