IBM IoT example

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
BrentLei
Date:
Mon Sep 28 08:51:12 2015 +0000
Revision:
19:2ff7dd142f88
Parent:
18:94da9de96d54
Support Seeed Arch Max

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samdanbury 6:37b6d0d56190 1 /*******************************************************************************
icraggs 16:2420bfbf5f1c 2 * Copyright (c) 2014, 2015 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
icraggs 16:2420bfbf5f1c 18 * James Sutton - Mac fix and extra debug
icraggs 16:2420bfbf5f1c 19 * Ian Craggs - add not authorized messages
chris 10:0b5e0dfee08e 20 *
chris 10:0b5e0dfee08e 21 * To do :
chris 10:0b5e0dfee08e 22 * Add magnetometer sensor output to IoT data stream
chris 10:0b5e0dfee08e 23 *
samdanbury 6:37b6d0d56190 24 *******************************************************************************/
samdanbury 6:37b6d0d56190 25
samdanbury 6:37b6d0d56190 26 #include "LM75B.h"
samdanbury 6:37b6d0d56190 27 #include "MMA7660.h"
samdanbury 6:37b6d0d56190 28 #include "MQTTClient.h"
samdanbury 6:37b6d0d56190 29 #include "MQTTEthernet.h"
samdanbury 6:37b6d0d56190 30 #include "C12832.h"
samdanbury 6:37b6d0d56190 31 #include "Arial12x12.h"
samdanbury 6:37b6d0d56190 32 #include "rtos.h"
samdanbury 6:37b6d0d56190 33
chris 10:0b5e0dfee08e 34 // Update this to the next number *before* a commit
icraggs 18:94da9de96d54 35 #define __APP_SW_REVISION__ "18"
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"
BrentLei 19:2ff7dd142f88 58 #elif defined(TARGET_STM32F407VG)
BrentLei 19:2ff7dd142f88 59 #warning "Compiling for Seeed Arch Max"
BrentLei 19:2ff7dd142f88 60 #include "ArchMax.h"
samdanbury 6:37b6d0d56190 61 #endif
samdanbury 6:37b6d0d56190 62
jsutton 14:1f961d19f3cf 63
icraggs 8:80d49dd91542 64 bool quickstartMode = true;
samdanbury 6:37b6d0d56190 65 char org[11] = ORG;
samdanbury 6:37b6d0d56190 66 char type[30] = TYPE;
samdanbury 6:37b6d0d56190 67 char id[30] = ID; // mac without colons
samdanbury 6:37b6d0d56190 68 char auth_token[30] = AUTH_TOKEN; // Auth_token is only used in non-quickstart mode
samdanbury 6:37b6d0d56190 69
samdanbury 6:37b6d0d56190 70 bool connected = false;
jsutton 14:1f961d19f3cf 71 bool mqttConnecting = false;
jsutton 14:1f961d19f3cf 72 bool netConnected = false;
jsutton 14:1f961d19f3cf 73 bool netConnecting = false;
jsutton 14:1f961d19f3cf 74 bool ethernetInitialising = true;
icraggs 16:2420bfbf5f1c 75 int connack_rc = 0; // MQTT connack return code
jsutton 14:1f961d19f3cf 76 int retryAttempt = 0;
jsutton 14:1f961d19f3cf 77 int menuItem = 0;
jsutton 14:1f961d19f3cf 78
samdanbury 6:37b6d0d56190 79 char* joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 80 int blink_interval = 0;
samdanbury 6:37b6d0d56190 81
jsutton 13:85801e3b83d3 82 char* ip_addr = "";
jsutton 13:85801e3b83d3 83 char* gateway_addr = "";
jsutton 14:1f961d19f3cf 84 char* host_addr = "";
jsutton 14:1f961d19f3cf 85 int connectTimeout = 1000;
jsutton 14:1f961d19f3cf 86
jsutton 14:1f961d19f3cf 87 // If we wanted to manually set the MAC address,
jsutton 14:1f961d19f3cf 88 // this is how to do it. In this example, we take
jsutton 14:1f961d19f3cf 89 // the original Mbed Set MAC address and combine it
jsutton 14:1f961d19f3cf 90 // with a prefix of our choosing.
jsutton 14:1f961d19f3cf 91 /*
jsutton 14:1f961d19f3cf 92 extern "C" void $Super$$mbed_mac_address(char *s);
jsutton 14:1f961d19f3cf 93 extern "C" void $Sub$$mbed_mac_address(char *s)
jsutton 14:1f961d19f3cf 94 {
jsutton 15:09458079f4bb 95 char originalMAC[6] = "";
jsutton 15:09458079f4bb 96 $Super$$mbed_mac_address(originalMAC);
jsutton 14:1f961d19f3cf 97
jsutton 14:1f961d19f3cf 98 char mac[6];
jsutton 14:1f961d19f3cf 99 mac[0] = 0x00;
jsutton 14:1f961d19f3cf 100 mac[1] = 0x08;
jsutton 14:1f961d19f3cf 101 mac[2] = 0xdc;
jsutton 15:09458079f4bb 102 mac[3] = originalMAC[3];
jsutton 15:09458079f4bb 103 mac[4] = originalMAC[4];
jsutton 15:09458079f4bb 104 mac[5] = originalMAC[5];
jsutton 14:1f961d19f3cf 105 memcpy(s, mac, 6);
jsutton 14:1f961d19f3cf 106 }
jsutton 14:1f961d19f3cf 107 */
jsutton 13:85801e3b83d3 108
samdanbury 6:37b6d0d56190 109
samdanbury 6:37b6d0d56190 110 void off()
samdanbury 6:37b6d0d56190 111 {
samdanbury 6:37b6d0d56190 112 r = g = b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 113 }
samdanbury 6:37b6d0d56190 114
samdanbury 6:37b6d0d56190 115 void red()
samdanbury 6:37b6d0d56190 116 {
samdanbury 6:37b6d0d56190 117 r = 0.7; g = 1.0; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 118 }
samdanbury 6:37b6d0d56190 119
samdanbury 6:37b6d0d56190 120 void yellow()
samdanbury 6:37b6d0d56190 121 {
samdanbury 6:37b6d0d56190 122 r = 0.7; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 123 }
samdanbury 6:37b6d0d56190 124
samdanbury 6:37b6d0d56190 125 void green()
samdanbury 6:37b6d0d56190 126 {
samdanbury 6:37b6d0d56190 127 r = 1.0; g = 0.7; b = 1.0; // 1 is off, 0 is full brightness
samdanbury 6:37b6d0d56190 128 }
samdanbury 6:37b6d0d56190 129
samdanbury 6:37b6d0d56190 130
samdanbury 6:37b6d0d56190 131 void flashing_yellow(void const *args)
samdanbury 6:37b6d0d56190 132 {
samdanbury 6:37b6d0d56190 133 bool on = false;
icraggs 16:2420bfbf5f1c 134 while (!connected && connack_rc != MQTT_NOT_AUTHORIZED && connack_rc != MQTT_BAD_USERNAME_OR_PASSWORD) // flashing yellow only while connecting
samdanbury 6:37b6d0d56190 135 {
samdanbury 6:37b6d0d56190 136 on = !on;
samdanbury 6:37b6d0d56190 137 if (on)
samdanbury 6:37b6d0d56190 138 yellow();
samdanbury 6:37b6d0d56190 139 else
samdanbury 6:37b6d0d56190 140 off();
samdanbury 6:37b6d0d56190 141 wait(0.5);
samdanbury 6:37b6d0d56190 142 }
samdanbury 6:37b6d0d56190 143 }
samdanbury 6:37b6d0d56190 144
samdanbury 6:37b6d0d56190 145
samdanbury 6:37b6d0d56190 146 void flashing_red(void const *args) // to be used when the connection is lost
samdanbury 6:37b6d0d56190 147 {
samdanbury 6:37b6d0d56190 148 bool on = false;
samdanbury 6:37b6d0d56190 149 while (!connected)
samdanbury 6:37b6d0d56190 150 {
samdanbury 6:37b6d0d56190 151 on = !on;
samdanbury 6:37b6d0d56190 152 if (on)
samdanbury 6:37b6d0d56190 153 red();
samdanbury 6:37b6d0d56190 154 else
samdanbury 6:37b6d0d56190 155 off();
samdanbury 6:37b6d0d56190 156 wait(2.0);
samdanbury 6:37b6d0d56190 157 }
samdanbury 6:37b6d0d56190 158 }
samdanbury 6:37b6d0d56190 159
samdanbury 6:37b6d0d56190 160
samdanbury 6:37b6d0d56190 161 void printMenu(int menuItem)
samdanbury 6:37b6d0d56190 162 {
icraggs 18:94da9de96d54 163 static char last_line1[30] = "", last_line2[30] = "";
icraggs 18:94da9de96d54 164 char line1[30] = "", line2[30] = "";
icraggs 18:94da9de96d54 165
samdanbury 6:37b6d0d56190 166 switch (menuItem)
samdanbury 6:37b6d0d56190 167 {
samdanbury 6:37b6d0d56190 168 case 0:
icraggs 18:94da9de96d54 169 sprintf(line1, "IBM IoT Cloud");
icraggs 18:94da9de96d54 170 sprintf(line2, "Scroll with joystick");
samdanbury 6:37b6d0d56190 171 break;
samdanbury 6:37b6d0d56190 172 case 1:
icraggs 18:94da9de96d54 173 sprintf(line1, "Go to:");
icraggs 18:94da9de96d54 174 sprintf(line2, "http://ibm.biz/iotqstart");
samdanbury 6:37b6d0d56190 175 break;
samdanbury 6:37b6d0d56190 176 case 2:
icraggs 18:94da9de96d54 177 sprintf(line1, "Device Identity:");
icraggs 18:94da9de96d54 178 sprintf(line2, "%s", id);
samdanbury 6:37b6d0d56190 179 break;
samdanbury 6:37b6d0d56190 180 case 3:
icraggs 18:94da9de96d54 181 sprintf(line1, "MQTT Status:");
icraggs 16:2420bfbf5f1c 182 if (mqttConnecting)
icraggs 18:94da9de96d54 183 sprintf(line2, "Connecting... %d/5", retryAttempt);
icraggs 16:2420bfbf5f1c 184 else
icraggs 16:2420bfbf5f1c 185 {
icraggs 16:2420bfbf5f1c 186 if (connected)
icraggs 18:94da9de96d54 187 sprintf(line2, "Connected");
icraggs 16:2420bfbf5f1c 188 else
icraggs 16:2420bfbf5f1c 189 {
icraggs 16:2420bfbf5f1c 190 switch (connack_rc)
icraggs 16:2420bfbf5f1c 191 {
icraggs 16:2420bfbf5f1c 192 case MQTT_CLIENTID_REJECTED:
icraggs 18:94da9de96d54 193 sprintf(line2, "Clientid rejected");
icraggs 16:2420bfbf5f1c 194 break;
icraggs 16:2420bfbf5f1c 195 case MQTT_BAD_USERNAME_OR_PASSWORD:
icraggs 18:94da9de96d54 196 sprintf(line2, "Invalid username or password");
icraggs 16:2420bfbf5f1c 197 break;
icraggs 16:2420bfbf5f1c 198 case MQTT_NOT_AUTHORIZED:
icraggs 18:94da9de96d54 199 sprintf(line2, "Not authorized");
icraggs 16:2420bfbf5f1c 200 break;
icraggs 16:2420bfbf5f1c 201 default:
icraggs 18:94da9de96d54 202 sprintf(line2, "Disconnected");
icraggs 16:2420bfbf5f1c 203 }
icraggs 16:2420bfbf5f1c 204 }
jsutton 14:1f961d19f3cf 205 }
samdanbury 6:37b6d0d56190 206 break;
chris 10:0b5e0dfee08e 207 case 4:
icraggs 18:94da9de96d54 208 sprintf(line1, "Ethernet State:");
icraggs 18:94da9de96d54 209 sprintf(line2, ethernetInitialising ? "Initializing..." : "Initialized");
jsutton 14:1f961d19f3cf 210 break;
jsutton 14:1f961d19f3cf 211 case 5:
icraggs 18:94da9de96d54 212 sprintf(line1, "Socket State:");
icraggs 16:2420bfbf5f1c 213 if (netConnecting)
icraggs 18:94da9de96d54 214 sprintf(line2, "Connecting... %d/5", retryAttempt);
icraggs 16:2420bfbf5f1c 215 else
icraggs 18:94da9de96d54 216 sprintf(line2, netConnected ? "Connected" : "Disconnected");
jsutton 13:85801e3b83d3 217 break;
jsutton 14:1f961d19f3cf 218 case 6:
icraggs 18:94da9de96d54 219 sprintf(line1, "IP Address:");
icraggs 18:94da9de96d54 220 sprintf(line2, "%s", ip_addr);
jsutton 13:85801e3b83d3 221 break;
jsutton 14:1f961d19f3cf 222 case 7:
icraggs 18:94da9de96d54 223 sprintf(line1, "Gateway:");
icraggs 18:94da9de96d54 224 sprintf(line2, "%s", gateway_addr);
jsutton 13:85801e3b83d3 225 break;
jsutton 14:1f961d19f3cf 226 case 8:
icraggs 18:94da9de96d54 227 sprintf(line1, "App version:");
icraggs 18:94da9de96d54 228 sprintf(line2, "%s", __APP_SW_REVISION__);
chris 10:0b5e0dfee08e 229 break;
jsutton 14:1f961d19f3cf 230 case 9:
icraggs 18:94da9de96d54 231 sprintf(line1, "Current Timeout:");
icraggs 18:94da9de96d54 232 sprintf(line2, "%d ms", connectTimeout);
jsutton 14:1f961d19f3cf 233 break;
samdanbury 6:37b6d0d56190 234 }
icraggs 18:94da9de96d54 235
icraggs 18:94da9de96d54 236 if (strcmp(line1, last_line1) != 0 || strcmp(line2, last_line2) != 0)
icraggs 18:94da9de96d54 237 {
icraggs 18:94da9de96d54 238 lcd.cls();
icraggs 18:94da9de96d54 239 lcd.locate(0, 0);
icraggs 18:94da9de96d54 240 lcd.printf(line1);
icraggs 18:94da9de96d54 241 strncpy(last_line1, line1, sizeof(last_line1));
icraggs 18:94da9de96d54 242
icraggs 18:94da9de96d54 243 lcd.locate(0,16);
icraggs 18:94da9de96d54 244 lcd.printf(line2);
icraggs 18:94da9de96d54 245 strncpy(last_line2, line2, sizeof(last_line2));
icraggs 18:94da9de96d54 246 }
samdanbury 6:37b6d0d56190 247 }
samdanbury 6:37b6d0d56190 248
samdanbury 6:37b6d0d56190 249
samdanbury 6:37b6d0d56190 250 void setMenu()
samdanbury 6:37b6d0d56190 251 {
jsutton 14:1f961d19f3cf 252
samdanbury 6:37b6d0d56190 253 if (Down)
samdanbury 6:37b6d0d56190 254 {
samdanbury 6:37b6d0d56190 255 joystickPos = "DOWN";
jsutton 14:1f961d19f3cf 256 if (menuItem >= 0 && menuItem < 9)
samdanbury 6:37b6d0d56190 257 printMenu(++menuItem);
samdanbury 6:37b6d0d56190 258 }
samdanbury 6:37b6d0d56190 259 else if (Left)
samdanbury 6:37b6d0d56190 260 joystickPos = "LEFT";
samdanbury 6:37b6d0d56190 261 else if (Click)
samdanbury 6:37b6d0d56190 262 joystickPos = "CLICK";
samdanbury 6:37b6d0d56190 263 else if (Up)
samdanbury 6:37b6d0d56190 264 {
samdanbury 6:37b6d0d56190 265 joystickPos = "UP";
jsutton 14:1f961d19f3cf 266 if (menuItem <= 9 && menuItem > 0)
samdanbury 6:37b6d0d56190 267 printMenu(--menuItem);
samdanbury 6:37b6d0d56190 268 }
samdanbury 6:37b6d0d56190 269 else if (Right)
samdanbury 6:37b6d0d56190 270 joystickPos = "RIGHT";
samdanbury 6:37b6d0d56190 271 else
samdanbury 6:37b6d0d56190 272 joystickPos = "CENTRE";
samdanbury 6:37b6d0d56190 273 }
samdanbury 6:37b6d0d56190 274
jsutton 13:85801e3b83d3 275 void menu_loop(void const *args)
jsutton 13:85801e3b83d3 276 {
jsutton 14:1f961d19f3cf 277 int count = 0;
icraggs 16:2420bfbf5f1c 278 while(true)
icraggs 16:2420bfbf5f1c 279 {
jsutton 13:85801e3b83d3 280 setMenu();
icraggs 16:2420bfbf5f1c 281 if (++count % 10 == 0)
jsutton 14:1f961d19f3cf 282 printMenu(menuItem);
jsutton 14:1f961d19f3cf 283 Thread::wait(100);
jsutton 13:85801e3b83d3 284 }
jsutton 13:85801e3b83d3 285 }
jsutton 13:85801e3b83d3 286
samdanbury 6:37b6d0d56190 287
samdanbury 6:37b6d0d56190 288 /**
samdanbury 6:37b6d0d56190 289 * Display a message on the LCD screen prefixed with IBM IoT Cloud
samdanbury 6:37b6d0d56190 290 */
samdanbury 6:37b6d0d56190 291 void displayMessage(char* message)
samdanbury 6:37b6d0d56190 292 {
samdanbury 6:37b6d0d56190 293 lcd.cls();
samdanbury 6:37b6d0d56190 294 lcd.locate(0,0);
samdanbury 6:37b6d0d56190 295 lcd.printf("IBM IoT Cloud");
samdanbury 6:37b6d0d56190 296 lcd.locate(0,16);
samdanbury 6:37b6d0d56190 297 lcd.printf(message);
samdanbury 6:37b6d0d56190 298 }
samdanbury 6:37b6d0d56190 299
samdanbury 6:37b6d0d56190 300
samdanbury 6:37b6d0d56190 301 int connect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
samdanbury 6:37b6d0d56190 302 {
samdanbury 6:37b6d0d56190 303 const char* iot_ibm = ".messaging.internetofthings.ibmcloud.com";
samdanbury 6:37b6d0d56190 304
samdanbury 6:37b6d0d56190 305 char hostname[strlen(org) + strlen(iot_ibm) + 1];
samdanbury 6:37b6d0d56190 306 sprintf(hostname, "%s%s", org, iot_ibm);
jsutton 13:85801e3b83d3 307 EthernetInterface& eth = ipstack->getEth();
jsutton 13:85801e3b83d3 308 ip_addr = eth.getIPAddress();
jsutton 13:85801e3b83d3 309 gateway_addr = eth.getGateway();
jsutton 14:1f961d19f3cf 310
jsutton 14:1f961d19f3cf 311 // Construct clientId - d:org:type:id
jsutton 14:1f961d19f3cf 312 char clientId[strlen(org) + strlen(type) + strlen(id) + 5];
jsutton 14:1f961d19f3cf 313 sprintf(clientId, "d:%s:%s:%s", org, type, id);
jsutton 14:1f961d19f3cf 314
icraggs 16:2420bfbf5f1c 315 // Network debug statements
jsutton 14:1f961d19f3cf 316 LOG("=====================================\n");
jsutton 14:1f961d19f3cf 317 LOG("Connecting Ethernet.\n");
jsutton 14:1f961d19f3cf 318 LOG("IP ADDRESS: %s\n", eth.getIPAddress());
jsutton 14:1f961d19f3cf 319 LOG("MAC ADDRESS: %s\n", eth.getMACAddress());
jsutton 14:1f961d19f3cf 320 LOG("Gateway: %s\n", eth.getGateway());
jsutton 14:1f961d19f3cf 321 LOG("Network Mask: %s\n", eth.getNetworkMask());
jsutton 14:1f961d19f3cf 322 LOG("Server Hostname: %s\n", hostname);
jsutton 14:1f961d19f3cf 323 LOG("Client ID: %s\n", clientId);
jsutton 14:1f961d19f3cf 324 LOG("=====================================\n");
jsutton 14:1f961d19f3cf 325
jsutton 14:1f961d19f3cf 326 netConnecting = true;
jsutton 14:1f961d19f3cf 327 int rc = ipstack->connect(hostname, IBM_IOT_PORT, connectTimeout);
icraggs 16:2420bfbf5f1c 328 if (rc != 0)
icraggs 16:2420bfbf5f1c 329 {
icraggs 18:94da9de96d54 330 WARN("IP Stack connect returned: %d\n", rc);
samdanbury 6:37b6d0d56190 331 return rc;
jsutton 13:85801e3b83d3 332 }
jsutton 13:85801e3b83d3 333 netConnected = true;
jsutton 14:1f961d19f3cf 334 netConnecting = false;
jsutton 14:1f961d19f3cf 335
samdanbury 6:37b6d0d56190 336 // MQTT Connect
jsutton 14:1f961d19f3cf 337 mqttConnecting = true;
samdanbury 6:37b6d0d56190 338 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
samdanbury 6:37b6d0d56190 339 data.MQTTVersion = 3;
samdanbury 6:37b6d0d56190 340 data.clientID.cstring = clientId;
samdanbury 6:37b6d0d56190 341
samdanbury 6:37b6d0d56190 342 if (!quickstartMode)
samdanbury 6:37b6d0d56190 343 {
samdanbury 6:37b6d0d56190 344 data.username.cstring = "use-token-auth";
samdanbury 6:37b6d0d56190 345 data.password.cstring = auth_token;
samdanbury 6:37b6d0d56190 346 }
samdanbury 6:37b6d0d56190 347
icraggs 8:80d49dd91542 348 if ((rc = client->connect(data)) == 0)
samdanbury 6:37b6d0d56190 349 {
samdanbury 6:37b6d0d56190 350 connected = true;
samdanbury 6:37b6d0d56190 351 green();
samdanbury 6:37b6d0d56190 352 displayMessage("Connected");
jsutton 13:85801e3b83d3 353 wait(1);
samdanbury 6:37b6d0d56190 354 displayMessage("Scroll with joystick");
samdanbury 6:37b6d0d56190 355 }
icraggs 16:2420bfbf5f1c 356 else
icraggs 16:2420bfbf5f1c 357 WARN("MQTT connect returned %d\n", rc);
icraggs 16:2420bfbf5f1c 358 if (rc >= 0)
icraggs 16:2420bfbf5f1c 359 connack_rc = rc;
jsutton 14:1f961d19f3cf 360 mqttConnecting = false;
samdanbury 6:37b6d0d56190 361 return rc;
samdanbury 6:37b6d0d56190 362 }
samdanbury 6:37b6d0d56190 363
samdanbury 6:37b6d0d56190 364
samdanbury 6:37b6d0d56190 365 int getConnTimeout(int attemptNumber)
samdanbury 6:37b6d0d56190 366 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
samdanbury 6:37b6d0d56190 367 // after 20 attempts, retry every 10 minutes
samdanbury 6:37b6d0d56190 368 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
samdanbury 6:37b6d0d56190 369 }
samdanbury 6:37b6d0d56190 370
samdanbury 6:37b6d0d56190 371
samdanbury 6:37b6d0d56190 372 void attemptConnect(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
samdanbury 6:37b6d0d56190 373 {
samdanbury 6:37b6d0d56190 374 connected = false;
icraggs 16:2420bfbf5f1c 375
icraggs 8:80d49dd91542 376 // make sure a cable is connected before starting to connect
icraggs 16:2420bfbf5f1c 377 while (!linkStatus())
icraggs 16:2420bfbf5f1c 378 {
icraggs 8:80d49dd91542 379 wait(1.0f);
icraggs 8:80d49dd91542 380 WARN("Ethernet link not present. Check cable connection\n");
icraggs 8:80d49dd91542 381 }
samdanbury 6:37b6d0d56190 382
icraggs 16:2420bfbf5f1c 383 while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED)
samdanbury 6:37b6d0d56190 384 {
icraggs 16:2420bfbf5f1c 385 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
icraggs 16:2420bfbf5f1c 386 return; // don't reattempt to connect if credentials are wrong
icraggs 16:2420bfbf5f1c 387
samdanbury 6:37b6d0d56190 388 Thread red_thread(flashing_red);
chris 12:8b480eb8a496 389
samdanbury 6:37b6d0d56190 390 int timeout = getConnTimeout(++retryAttempt);
samdanbury 6:37b6d0d56190 391 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
icraggs 8:80d49dd91542 392
icraggs 8:80d49dd91542 393 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
icraggs 8:80d49dd91542 394 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
icraggs 8:80d49dd91542 395
icraggs 8:80d49dd91542 396 // this works - reset the system when the retry count gets to a threshold
icraggs 8:80d49dd91542 397 if (retryAttempt == 5)
icraggs 8:80d49dd91542 398 NVIC_SystemReset();
icraggs 8:80d49dd91542 399 else
icraggs 8:80d49dd91542 400 wait(timeout);
samdanbury 6:37b6d0d56190 401 }
samdanbury 6:37b6d0d56190 402 }
samdanbury 6:37b6d0d56190 403
samdanbury 6:37b6d0d56190 404
samdanbury 6:37b6d0d56190 405 int publish(MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTEthernet* ipstack)
samdanbury 6:37b6d0d56190 406 {
samdanbury 6:37b6d0d56190 407 MQTT::Message message;
samdanbury 6:37b6d0d56190 408 char* pubTopic = "iot-2/evt/status/fmt/json";
samdanbury 6:37b6d0d56190 409
samdanbury 6:37b6d0d56190 410 char buf[250];
samdanbury 6:37b6d0d56190 411 sprintf(buf,
samdanbury 6:37b6d0d56190 412 "{\"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 413 MMA.x(), MMA.y(), MMA.z(), sensor.temp(), joystickPos, ain1.read(), ain2.read());
samdanbury 6:37b6d0d56190 414 message.qos = MQTT::QOS0;
samdanbury 6:37b6d0d56190 415 message.retained = false;
samdanbury 6:37b6d0d56190 416 message.dup = false;
samdanbury 6:37b6d0d56190 417 message.payload = (void*)buf;
samdanbury 6:37b6d0d56190 418 message.payloadlen = strlen(buf);
samdanbury 6:37b6d0d56190 419
samdanbury 6:37b6d0d56190 420 LOG("Publishing %s\n", buf);
icraggs 8:80d49dd91542 421 return client->publish(pubTopic, message);
samdanbury 6:37b6d0d56190 422 }
samdanbury 6:37b6d0d56190 423
samdanbury 6:37b6d0d56190 424
samdanbury 6:37b6d0d56190 425 char* getMac(EthernetInterface& eth, char* buf, int buflen) // Obtain MAC address
samdanbury 6:37b6d0d56190 426 {
samdanbury 6:37b6d0d56190 427 strncpy(buf, eth.getMACAddress(), buflen);
samdanbury 6:37b6d0d56190 428
samdanbury 6:37b6d0d56190 429 char* pos; // Remove colons from mac address
samdanbury 6:37b6d0d56190 430 while ((pos = strchr(buf, ':')) != NULL)
samdanbury 6:37b6d0d56190 431 memmove(pos, pos + 1, strlen(pos) + 1);
samdanbury 6:37b6d0d56190 432 return buf;
samdanbury 6:37b6d0d56190 433 }
chris 12:8b480eb8a496 434
samdanbury 6:37b6d0d56190 435
samdanbury 6:37b6d0d56190 436 void messageArrived(MQTT::MessageData& md)
samdanbury 6:37b6d0d56190 437 {
samdanbury 6:37b6d0d56190 438 MQTT::Message &message = md.message;
samdanbury 6:37b6d0d56190 439 char topic[md.topicName.lenstring.len + 1];
samdanbury 6:37b6d0d56190 440
samdanbury 6:37b6d0d56190 441 sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data);
samdanbury 6:37b6d0d56190 442
samdanbury 6:37b6d0d56190 443 LOG("Message arrived on topic %s: %.*s\n", topic, message.payloadlen, message.payload);
samdanbury 6:37b6d0d56190 444
samdanbury 6:37b6d0d56190 445 // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/
samdanbury 6:37b6d0d56190 446 char* start = strstr(topic, "/cmd/") + 5;
samdanbury 6:37b6d0d56190 447 int len = strstr(topic, "/fmt/") - start;
samdanbury 6:37b6d0d56190 448
samdanbury 6:37b6d0d56190 449 if (memcmp(start, "blink", len) == 0)
samdanbury 6:37b6d0d56190 450 {
samdanbury 6:37b6d0d56190 451 char payload[message.payloadlen + 1];
samdanbury 6:37b6d0d56190 452 sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload);
samdanbury 6:37b6d0d56190 453
samdanbury 6:37b6d0d56190 454 char* pos = strchr(payload, '}');
samdanbury 6:37b6d0d56190 455 if (pos != NULL)
samdanbury 6:37b6d0d56190 456 {
samdanbury 6:37b6d0d56190 457 *pos = '\0';
samdanbury 6:37b6d0d56190 458 if ((pos = strchr(payload, ':')) != NULL)
samdanbury 6:37b6d0d56190 459 {
samdanbury 6:37b6d0d56190 460 int blink_rate = atoi(pos + 1);
samdanbury 6:37b6d0d56190 461 blink_interval = (blink_rate <= 0) ? 0 : (blink_rate > 50 ? 1 : 50/blink_rate);
samdanbury 6:37b6d0d56190 462 }
samdanbury 6:37b6d0d56190 463 }
samdanbury 6:37b6d0d56190 464 }
samdanbury 6:37b6d0d56190 465 else
samdanbury 6:37b6d0d56190 466 WARN("Unsupported command: %.*s\n", len, start);
samdanbury 6:37b6d0d56190 467 }
samdanbury 6:37b6d0d56190 468
samdanbury 6:37b6d0d56190 469
samdanbury 6:37b6d0d56190 470 int main()
samdanbury 6:37b6d0d56190 471 {
icraggs 8:80d49dd91542 472 quickstartMode = (strcmp(org, "quickstart") == 0);
icraggs 8:80d49dd91542 473
samdanbury 6:37b6d0d56190 474 lcd.set_font((unsigned char*) Arial12x12); // Set a nice font for the LCD screen
samdanbury 6:37b6d0d56190 475
samdanbury 6:37b6d0d56190 476 led2 = LED2_OFF; // K64F: turn off the main board LED
samdanbury 6:37b6d0d56190 477
samdanbury 6:37b6d0d56190 478 displayMessage("Connecting");
jsutton 13:85801e3b83d3 479 Thread yellow_thread(flashing_yellow);
jsutton 13:85801e3b83d3 480 Thread menu_thread(menu_loop);
jsutton 13:85801e3b83d3 481
jsutton 14:1f961d19f3cf 482 LOG("***** IBM IoT Client Ethernet Example *****\n");
samdanbury 6:37b6d0d56190 483 MQTTEthernet ipstack;
jsutton 14:1f961d19f3cf 484 ethernetInitialising = false;
samdanbury 6:37b6d0d56190 485 MQTT::Client<MQTTEthernet, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
icraggs 18:94da9de96d54 486 LOG("Ethernet Initialized\n");
jsutton 13:85801e3b83d3 487
samdanbury 6:37b6d0d56190 488 if (quickstartMode)
icraggs 16:2420bfbf5f1c 489 getMac(ipstack.getEth(), id, sizeof(id));
icraggs 16:2420bfbf5f1c 490
icraggs 16:2420bfbf5f1c 491 attemptConnect(&client, &ipstack);
icraggs 16:2420bfbf5f1c 492
icraggs 16:2420bfbf5f1c 493 if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD)
samdanbury 6:37b6d0d56190 494 {
icraggs 16:2420bfbf5f1c 495 red();
icraggs 16:2420bfbf5f1c 496 while (true)
icraggs 16:2420bfbf5f1c 497 wait(1.0); // Permanent failures - don't retry
samdanbury 6:37b6d0d56190 498 }
icraggs 16:2420bfbf5f1c 499
samdanbury 6:37b6d0d56190 500 if (!quickstartMode)
samdanbury 6:37b6d0d56190 501 {
samdanbury 6:37b6d0d56190 502 int rc = 0;
samdanbury 6:37b6d0d56190 503 if ((rc = client.subscribe("iot-2/cmd/+/fmt/json", MQTT::QOS1, messageArrived)) != 0)
samdanbury 6:37b6d0d56190 504 WARN("rc from MQTT subscribe is %d\n", rc);
samdanbury 6:37b6d0d56190 505 }
samdanbury 6:37b6d0d56190 506
samdanbury 6:37b6d0d56190 507 blink_interval = 0;
samdanbury 6:37b6d0d56190 508 int count = 0;
samdanbury 6:37b6d0d56190 509 while (true)
samdanbury 6:37b6d0d56190 510 {
samdanbury 6:37b6d0d56190 511 if (++count == 100)
samdanbury 6:37b6d0d56190 512 { // Publish a message every second
samdanbury 6:37b6d0d56190 513 if (publish(&client, &ipstack) != 0)
samdanbury 6:37b6d0d56190 514 attemptConnect(&client, &ipstack); // if we have lost the connection
samdanbury 6:37b6d0d56190 515 count = 0;
samdanbury 6:37b6d0d56190 516 }
samdanbury 6:37b6d0d56190 517
samdanbury 6:37b6d0d56190 518 if (blink_interval == 0)
samdanbury 6:37b6d0d56190 519 led2 = LED2_OFF;
samdanbury 6:37b6d0d56190 520 else if (count % blink_interval == 0)
samdanbury 6:37b6d0d56190 521 led2 = !led2;
samdanbury 6:37b6d0d56190 522 client.yield(10); // allow the MQTT client to receive messages
samdanbury 6:37b6d0d56190 523 }
samdanbury 6:37b6d0d56190 524 }