IoT Example

Dependencies:   C12832 LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
sam_grove
Date:
Sat Oct 18 07:45:52 2014 +0000
Revision:
14:5b7cf452e5c0
Parent:
11:9e3dfda31369
sync

Who changed what in which revision?

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