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