Code sample to be used with Bluemix for education purpose

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
samdanbury
Date:
Wed Aug 20 12:45:14 2014 +0000
Revision:
6:37b6d0d56190
Parent:
5:11fd21af0c0f
Child:
7:63a7aa4deaf8
Code completely changed to improve the structure, flow and memory usage of the application

Who changed what in which revision?

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