IUT GEII SEM 4 ER

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

Fork of GEII4ERS4 by Philippe Bazot

Committer:
syasya
Date:
Thu Feb 22 18:47:12 2018 +0000
Revision:
21:f904564e26d2
Parent:
20:3045570c01ff
IUT GEII SEM4

Who changed what in which revision?

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