IoT Starter Kit Code

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Committer:
icraggs
Date:
Mon Sep 29 11:44:51 2014 +0000
Revision:
0:0777b6b0c36f
Child:
1:8f47175f0f53
Example for starter kit only

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