a modified code based on the IoTClientEthernet code

Dependencies:   C12832 EthernetInterface LM75B MMA7660 MQTT mbed-rtos mbed

Fork of IoTClientEthernet by Zhengguo Sheng

Committer:
RAAA
Date:
Tue Jan 10 18:26:40 2017 +0000
Revision:
20:c90d68fbbb0e
Parent:
19:31b65908c2f3
Submitted version of RTES final project

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