Exercise 5

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
steveshun
Date:
Sun Dec 04 14:50:51 2016 +0000
Revision:
19:31b65908c2f3
Parent:
18:94da9de96d54
Child:
20:923a4ccddadd
Exercise 5-2

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