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