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

Dependencies:   C12832 EthernetInterface LM75B MFRC522 MMA7660 MQTT mbed-rtos mbed

Fork of IBMIoTClientEthernetExample by IBM Watson IoT

Committer:
haziq10
Date:
Tue Mar 01 08:05:27 2016 +0000
Revision:
19:6070ed3c41f5
Parent:
18:94da9de96d54
Salam aleykoum

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