Contains example code to connect the mbed LPC1768 or FRDM-K64F devices to the IBM Internet of Things Cloud service via ethernet.

Dependencies:   C12832 MQTT LM75B MMA7660

Dependents:   MFT_IoT_demo_USB400 IBM_RFID

Committer:
jsutton
Date:
Wed Jul 22 13:51:22 2015 +0000
Revision:
13:85801e3b83d3
Parent:
12:8b480eb8a496
Child:
14:1f961d19f3cf
Added Debug information into menu.; Split menu out into a new thread to allow it to operate whilst connecting.

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