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