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