IBM IoT Client WiFi example using Murata Type-YD WiFi module

Dependencies:   C12832_fix LM75B MMA7660 MQTT PowerControl SNICInterface mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by Toyomasa Watarai

/media/uploads/MACRUM/ibm_iotkit.jpg

Committer:
Sam_Chu
Date:
Fri Jan 15 07:35:18 2016 +0000
Revision:
21:f243f013aeda
Parent:
18:ff8b191de621
added K64F plateform

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