Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSoftSerial SDFileSystem
Fork of ATT_AWS_IoT_demo by
main.cpp@33:48172a2972b6, 2017-08-25 (annotated)
- Committer:
- RaghuT
- Date:
- Fri Aug 25 20:38:12 2017 +0000
- Revision:
- 33:48172a2972b6
- Parent:
- 32:acf84c7d0075
- Child:
- 34:b153e7a4e2d5
Week 10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RaghuT | 28:d549de7c3aad | 1 | #include <cstdlib> |
ampembeng | 15:6f2798e45099 | 2 | #include "mbed.h" |
ampembeng | 15:6f2798e45099 | 3 | |
ampembeng | 15:6f2798e45099 | 4 | // Serial extension |
ampembeng | 15:6f2798e45099 | 5 | #include "MODSERIAL.h" |
RaghuT | 28:d549de7c3aad | 6 | #include "SoftSerial.h" |
RaghuT | 28:d549de7c3aad | 7 | #include "BufferedSoftSerial.h" |
Janos Follath |
0:fc70c47eecb4 | 8 | |
ampembeng | 15:6f2798e45099 | 9 | // Network includes |
ampembeng | 15:6f2798e45099 | 10 | #include "WNCInterface.h" |
ampembeng | 15:6f2798e45099 | 11 | #include "network_interface.h" |
RaghuT | 28:d549de7c3aad | 12 | #include "WncControllerK64F/WncController/WncController.h" |
Janos Follath |
0:fc70c47eecb4 | 13 | |
ampembeng | 15:6f2798e45099 | 14 | // AWS includes |
ampembeng | 15:6f2798e45099 | 15 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 16 | #include "aws_iot_version.h" |
ampembeng | 15:6f2798e45099 | 17 | #include "aws_iot_shadow_interface.h" |
ampembeng | 15:6f2798e45099 | 18 | #include "aws_iot_shadow_json_data.h" |
ampembeng | 15:6f2798e45099 | 19 | #include "aws_iot_config.h" |
ampembeng | 15:6f2798e45099 | 20 | #include "aws_iot_mqtt_interface.h" |
mbed_official | 12:1ae41c231014 | 21 | |
Janos Follath |
0:fc70c47eecb4 | 22 | #if DEBUG_LEVEL > 0 |
Janos Follath |
0:fc70c47eecb4 | 23 | #include "mbedtls/debug.h" |
Janos Follath |
0:fc70c47eecb4 | 24 | #endif |
Janos Follath |
0:fc70c47eecb4 | 25 | |
ampembeng | 15:6f2798e45099 | 26 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 27 | // |
ampembeng | 15:6f2798e45099 | 28 | // Defines |
ampembeng | 15:6f2798e45099 | 29 | // |
ampembeng | 15:6f2798e45099 | 30 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 31 | // LED Colors |
ampembeng | 15:6f2798e45099 | 32 | #define COLOR_OFF 0x00 |
ampembeng | 15:6f2798e45099 | 33 | #define COLOR_RED 0x01 |
ampembeng | 15:6f2798e45099 | 34 | #define COLOR_GREEN 0x02 |
ampembeng | 15:6f2798e45099 | 35 | #define COLOR_BLUE 0x04 |
ampembeng | 15:6f2798e45099 | 36 | #define COLOR_WHITE 0x07 |
ampembeng | 15:6f2798e45099 | 37 | #define NUM_COLORS 5 |
Janos Follath |
0:fc70c47eecb4 | 38 | |
ampembeng | 15:6f2798e45099 | 39 | // AWS defines |
ampembeng | 18:6370da1de572 | 40 | #define PATH_MAX 1024 |
RaghuT | 28:d549de7c3aad | 41 | #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 500 // NOTE: Be wary of this if your JSON doc grows |
RaghuT | 33:48172a2972b6 | 42 | #define SHADOW_SYNC_INTERVAL 5.0 // How often we sync with AWS Shadow (in seconds) |
Janos Follath |
0:fc70c47eecb4 | 43 | |
ampembeng | 15:6f2798e45099 | 44 | // Comment out the following line if color is not supported on the terminal |
ampembeng | 15:6f2798e45099 | 45 | //#define USE_COLOR |
ampembeng | 15:6f2798e45099 | 46 | #ifdef USE_COLOR |
RaghuT | 29:319446cd2880 | 47 | #define BLK "\033[30m" |
RaghuT | 29:319446cd2880 | 48 | #define RED "\033[31m" |
RaghuT | 29:319446cd2880 | 49 | #define GRN "\033[32m" |
RaghuT | 29:319446cd2880 | 50 | #define YEL "\033[33m" |
RaghuT | 29:319446cd2880 | 51 | #define BLU "\033[34m" |
RaghuT | 29:319446cd2880 | 52 | #define MAG "\033[35m" |
RaghuT | 29:319446cd2880 | 53 | #define CYN "\033[36m" |
RaghuT | 29:319446cd2880 | 54 | #define WHT "\033[37m" |
RaghuT | 29:319446cd2880 | 55 | #define DEF "\033[39m" |
ampembeng | 15:6f2798e45099 | 56 | #else |
RaghuT | 29:319446cd2880 | 57 | #define BLK |
RaghuT | 29:319446cd2880 | 58 | #define RED |
RaghuT | 29:319446cd2880 | 59 | #define GRN |
RaghuT | 29:319446cd2880 | 60 | #define YEL |
RaghuT | 29:319446cd2880 | 61 | #define BLU |
RaghuT | 29:319446cd2880 | 62 | #define MAG |
RaghuT | 29:319446cd2880 | 63 | #define CYN |
RaghuT | 29:319446cd2880 | 64 | #define WHT |
RaghuT | 29:319446cd2880 | 65 | #define DEF |
Janos Follath |
0:fc70c47eecb4 | 66 | #endif |
Janos Follath |
0:fc70c47eecb4 | 67 | |
ampembeng | 20:ee34856ae510 | 68 | // Sensor defines |
ampembeng | 20:ee34856ae510 | 69 | #define CTOF(x) ((x)*1.8+32) // Temperature |
ampembeng | 20:ee34856ae510 | 70 | |
ampembeng | 15:6f2798e45099 | 71 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 72 | // |
ampembeng | 15:6f2798e45099 | 73 | // Globals |
ampembeng | 15:6f2798e45099 | 74 | // |
ampembeng | 15:6f2798e45099 | 75 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 76 | // Controls LED color |
ampembeng | 15:6f2798e45099 | 77 | unsigned char ledColor = COLOR_OFF; |
ampembeng | 15:6f2798e45099 | 78 | |
ampembeng | 18:6370da1de572 | 79 | // These defines are pulled from aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 80 | char HostAddress[255] = AWS_IOT_MQTT_HOST; |
ampembeng | 18:6370da1de572 | 81 | char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 18:6370da1de572 | 82 | char ThingName[32] = AWS_IOT_MY_THING_NAME; |
ampembeng | 18:6370da1de572 | 83 | char PortString[5] = "8883"; |
ampembeng | 15:6f2798e45099 | 84 | uint32_t port = AWS_IOT_MQTT_PORT; |
ampembeng | 23:b9ff83dc965f | 85 | char iccidName[21] = "12345678901234567890"; |
Janos Follath |
0:fc70c47eecb4 | 86 | |
ampembeng | 20:ee34856ae510 | 87 | // Sensor data |
ampembeng | 20:ee34856ae510 | 88 | float temperature = 0.0; |
RaghuT | 28:d549de7c3aad | 89 | float humidity = 0.0; |
RaghuT | 32:acf84c7d0075 | 90 | char slaveData[500]; |
ampembeng | 20:ee34856ae510 | 91 | |
RaghuT | 28:d549de7c3aad | 92 | //Cell signal |
RaghuT | 28:d549de7c3aad | 93 | int signalQuality = 0; |
RaghuT | 28:d549de7c3aad | 94 | |
RaghuT | 28:d549de7c3aad | 95 | //Variable to store data usage |
RaghuT | 33:48172a2972b6 | 96 | unsigned int dataUsage = 0; |
RaghuT | 33:48172a2972b6 | 97 | |
RaghuT | 33:48172a2972b6 | 98 | //Variable to store latency |
RaghuT | 33:48172a2972b6 | 99 | unsigned int latency = 0; |
RaghuT | 33:48172a2972b6 | 100 | unsigned int latencyHistory[10]; |
RaghuT | 33:48172a2972b6 | 101 | int latencyHistoryIndex = 0; |
ampembeng | 23:b9ff83dc965f | 102 | |
RaghuT | 29:319446cd2880 | 103 | //slave JSON to publish |
RaghuT | 29:319446cd2880 | 104 | string slaveJSON = ""; |
RaghuT | 29:319446cd2880 | 105 | |
ampembeng | 15:6f2798e45099 | 106 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 107 | // |
ampembeng | 15:6f2798e45099 | 108 | // Devices |
ampembeng | 15:6f2798e45099 | 109 | // |
ampembeng | 15:6f2798e45099 | 110 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 111 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 112 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 113 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 114 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 115 | |
ampembeng | 15:6f2798e45099 | 116 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 117 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 118 | |
RaghuT | 30:d2a7e413f658 | 119 | // radio serial |
RaghuT | 30:d2a7e413f658 | 120 | BufferedSoftSerial radio(PTC4, PTA2); |
ampembeng | 18:6370da1de572 | 121 | |
ampembeng | 20:ee34856ae510 | 122 | // I2C bus (SDA, SCL) |
ampembeng | 20:ee34856ae510 | 123 | I2C i2c(PTC11, PTC10); |
ampembeng | 20:ee34856ae510 | 124 | |
RaghuT | 33:48172a2972b6 | 125 | //Timer |
RaghuT | 33:48172a2972b6 | 126 | Timer deviceTimer; |
RaghuT | 33:48172a2972b6 | 127 | Timer updateTimer; |
RaghuT | 33:48172a2972b6 | 128 | Timer disconnectTimer; |
RaghuT | 33:48172a2972b6 | 129 | |
ampembeng | 15:6f2798e45099 | 130 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 131 | // |
ampembeng | 15:6f2798e45099 | 132 | // Functions |
ampembeng | 15:6f2798e45099 | 133 | // |
ampembeng | 15:6f2798e45099 | 134 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 135 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 136 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 137 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 138 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 139 | { |
ampembeng | 15:6f2798e45099 | 140 | va_list vl; |
ampembeng | 15:6f2798e45099 | 141 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 142 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 143 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 144 | } |
Janos Follath |
0:fc70c47eecb4 | 145 | |
ampembeng | 15:6f2798e45099 | 146 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 147 | //* Set the RGB LED's Color |
RaghuT | 29:319446cd2880 | 148 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 149 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 150 | void SetLedColor(unsigned char ucColor) |
RaghuT | 29:319446cd2880 | 151 | { |
ampembeng | 15:6f2798e45099 | 152 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 153 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 154 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 155 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 156 | } |
Janos Follath |
0:fc70c47eecb4 | 157 | |
RaghuT | 33:48172a2972b6 | 158 | unsigned int getAverageLatencyHistory() |
RaghuT | 33:48172a2972b6 | 159 | { |
RaghuT | 33:48172a2972b6 | 160 | unsigned int total = 0; |
RaghuT | 33:48172a2972b6 | 161 | for(int i = 0; i < latencyHistoryIndex; i++) { |
RaghuT | 33:48172a2972b6 | 162 | total += latencyHistory[i]; |
RaghuT | 33:48172a2972b6 | 163 | } |
RaghuT | 33:48172a2972b6 | 164 | total = total / latencyHistoryIndex; |
RaghuT | 33:48172a2972b6 | 165 | return total; |
RaghuT | 33:48172a2972b6 | 166 | } |
RaghuT | 33:48172a2972b6 | 167 | |
ampembeng | 15:6f2798e45099 | 168 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 169 | // |
ampembeng | 15:6f2798e45099 | 170 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 171 | // |
ampembeng | 15:6f2798e45099 | 172 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 173 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 174 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 175 | //********************************************************************************************************************* |
RaghuT | 33:48172a2972b6 | 176 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, const char *pReceivedJsonDocument, void *pContextData) |
RaghuT | 29:319446cd2880 | 177 | { |
Janos Follath |
0:fc70c47eecb4 | 178 | |
ampembeng | 15:6f2798e45099 | 179 | INFO("Shadow Update Status Callback"); |
RaghuT | 29:319446cd2880 | 180 | |
ampembeng | 15:6f2798e45099 | 181 | if (status == SHADOW_ACK_TIMEOUT) { |
ampembeng | 15:6f2798e45099 | 182 | INFO("Update Timeout--"); |
ampembeng | 15:6f2798e45099 | 183 | } else if (status == SHADOW_ACK_REJECTED) { |
ampembeng | 15:6f2798e45099 | 184 | INFO("Update RejectedXX"); |
ampembeng | 15:6f2798e45099 | 185 | } else if (status == SHADOW_ACK_ACCEPTED) { |
ampembeng | 15:6f2798e45099 | 186 | INFO("Update Accepted!!"); // Good |
ampembeng | 15:6f2798e45099 | 187 | } |
RaghuT | 33:48172a2972b6 | 188 | deviceTimer.stop(); |
RaghuT | 33:48172a2972b6 | 189 | latencyHistory[latencyHistoryIndex] = latency; |
RaghuT | 33:48172a2972b6 | 190 | unsigned int average = getAverageLatencyHistory(); |
RaghuT | 33:48172a2972b6 | 191 | latency = deviceTimer.read_us() * 0.5 + average * 0.5; |
RaghuT | 33:48172a2972b6 | 192 | latencyHistoryIndex = (latencyHistoryIndex + 1) % 10; |
RaghuT | 33:48172a2972b6 | 193 | deviceTimer.reset(); |
ampembeng | 15:6f2798e45099 | 194 | } |
Janos Follath |
0:fc70c47eecb4 | 195 | |
RaghuT | 28:d549de7c3aad | 196 | //Callback when dataUsage stats are updated |
RaghuT | 29:319446cd2880 | 197 | void dataUsageCallback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) |
RaghuT | 29:319446cd2880 | 198 | { |
RaghuT | 29:319446cd2880 | 199 | INFO("Data usage callback detected"); |
RaghuT | 28:d549de7c3aad | 200 | } |
RaghuT | 32:acf84c7d0075 | 201 | |
ampembeng | 23:b9ff83dc965f | 202 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 203 | //* Disconnect handling (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 204 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 205 | void disconnectCallbackHandler(void) |
RaghuT | 29:319446cd2880 | 206 | { |
ampembeng | 23:b9ff83dc965f | 207 | WARN("MQTT Disconnect"); |
ampembeng | 23:b9ff83dc965f | 208 | IoT_Error_t rc = NONE_ERROR; |
RaghuT | 29:319446cd2880 | 209 | if(aws_iot_is_autoreconnect_enabled()) { |
ampembeng | 23:b9ff83dc965f | 210 | INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
RaghuT | 29:319446cd2880 | 211 | } else { |
ampembeng | 23:b9ff83dc965f | 212 | WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
ampembeng | 23:b9ff83dc965f | 213 | rc = aws_iot_mqtt_attempt_reconnect(); |
RaghuT | 29:319446cd2880 | 214 | if(RECONNECT_SUCCESSFUL == rc) { |
ampembeng | 23:b9ff83dc965f | 215 | WARN("Manual Reconnect Successful"); |
RaghuT | 29:319446cd2880 | 216 | } else { |
ampembeng | 23:b9ff83dc965f | 217 | WARN("Manual Reconnect Failed - %d", rc); |
ampembeng | 23:b9ff83dc965f | 218 | } |
ampembeng | 23:b9ff83dc965f | 219 | } |
ampembeng | 23:b9ff83dc965f | 220 | } |
ampembeng | 23:b9ff83dc965f | 221 | |
RaghuT | 28:d549de7c3aad | 222 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 223 | //* Get slave data over uart interface |
RaghuT | 28:d549de7c3aad | 224 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 225 | void getSlaveJSON() |
RaghuT | 29:319446cd2880 | 226 | { |
RaghuT | 30:d2a7e413f658 | 227 | slaveJSON.clear(); |
RaghuT | 30:d2a7e413f658 | 228 | if(radio.writeable()) { |
RaghuT | 30:d2a7e413f658 | 229 | radio.printf("d"); |
RaghuT | 30:d2a7e413f658 | 230 | while(radio.readable()) { |
RaghuT | 30:d2a7e413f658 | 231 | slaveJSON += radio.getc(); |
RaghuT | 30:d2a7e413f658 | 232 | } |
RaghuT | 29:319446cd2880 | 233 | } |
RaghuT | 32:acf84c7d0075 | 234 | strcpy(slaveData, slaveJSON.c_str()); |
ampembeng | 23:b9ff83dc965f | 235 | } |
ampembeng | 15:6f2798e45099 | 236 | |
RaghuT | 33:48172a2972b6 | 237 | //********************************************************************************************************************* |
RaghuT | 33:48172a2972b6 | 238 | //* Push master data over to radio |
RaghuT | 33:48172a2972b6 | 239 | //********************************************************************************************************************* |
RaghuT | 33:48172a2972b6 | 240 | void sendMasterData() |
RaghuT | 33:48172a2972b6 | 241 | { |
RaghuT | 33:48172a2972b6 | 242 | if(radio.writeable()) { |
RaghuT | 33:48172a2972b6 | 243 | radio.printf("*%d,%d,%d\n", dataUsage, latency, signalQuality); |
RaghuT | 33:48172a2972b6 | 244 | } |
RaghuT | 33:48172a2972b6 | 245 | } |
ampembeng | 15:6f2798e45099 | 246 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 247 | // |
ampembeng | 15:6f2798e45099 | 248 | // Main |
ampembeng | 15:6f2798e45099 | 249 | // |
ampembeng | 15:6f2798e45099 | 250 | //===================================================================================================================== |
RaghuT | 29:319446cd2880 | 251 | int main() |
RaghuT | 29:319446cd2880 | 252 | { |
ampembeng | 15:6f2798e45099 | 253 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 254 | pc.baud(115200); |
RaghuT | 30:d2a7e413f658 | 255 | radio.baud(9600); |
RaghuT | 28:d549de7c3aad | 256 | INFO("Program Start"); |
RaghuT | 29:319446cd2880 | 257 | |
RaghuT | 29:319446cd2880 | 258 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 259 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 260 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 261 | |
RaghuT | 28:d549de7c3aad | 262 | //JSON struct for signal strength readings |
RaghuT | 28:d549de7c3aad | 263 | jsonStruct_t signalStrengthHandler; |
RaghuT | 28:d549de7c3aad | 264 | signalStrengthHandler.cb = NULL; |
RaghuT | 33:48172a2972b6 | 265 | signalStrengthHandler.pKey = "signalStrength"; |
RaghuT | 28:d549de7c3aad | 266 | signalStrengthHandler.pData = &signalQuality; |
RaghuT | 28:d549de7c3aad | 267 | signalStrengthHandler.type = SHADOW_JSON_INT16; |
RaghuT | 29:319446cd2880 | 268 | |
RaghuT | 29:319446cd2880 | 269 | //JSON struct for data usage |
RaghuT | 28:d549de7c3aad | 270 | jsonStruct_t dataUsageHandler; |
RaghuT | 28:d549de7c3aad | 271 | dataUsageHandler.cb = dataUsageCallback; |
RaghuT | 33:48172a2972b6 | 272 | dataUsageHandler.pKey = "dataUsage"; |
RaghuT | 28:d549de7c3aad | 273 | dataUsageHandler.pData = &dataUsage; |
RaghuT | 28:d549de7c3aad | 274 | dataUsageHandler.type = SHADOW_JSON_UINT32; |
RaghuT | 33:48172a2972b6 | 275 | |
RaghuT | 30:d2a7e413f658 | 276 | //JSON struct for slave data |
RaghuT | 30:d2a7e413f658 | 277 | jsonStruct_t slaveDataHandler; |
RaghuT | 30:d2a7e413f658 | 278 | slaveDataHandler.cb = NULL; |
RaghuT | 30:d2a7e413f658 | 279 | slaveDataHandler.pKey = "slaves"; |
RaghuT | 30:d2a7e413f658 | 280 | slaveDataHandler.pData = slaveData; |
RaghuT | 30:d2a7e413f658 | 281 | slaveDataHandler.type = SHADOW_JSON_STRING; |
RaghuT | 33:48172a2972b6 | 282 | |
RaghuT | 33:48172a2972b6 | 283 | //JSON struct for latency |
RaghuT | 33:48172a2972b6 | 284 | jsonStruct_t latencyHandler; |
RaghuT | 33:48172a2972b6 | 285 | latencyHandler.cb = NULL; |
RaghuT | 33:48172a2972b6 | 286 | latencyHandler.pKey = "latency"; |
RaghuT | 33:48172a2972b6 | 287 | latencyHandler.pData = &latency; |
RaghuT | 33:48172a2972b6 | 288 | latencyHandler.type = SHADOW_JSON_UINT32; |
RaghuT | 33:48172a2972b6 | 289 | |
ampembeng | 15:6f2798e45099 | 290 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
ampembeng | 15:6f2798e45099 | 291 | |
RaghuT | 28:d549de7c3aad | 292 | |
RaghuT | 29:319446cd2880 | 293 | INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
RaghuT | 29:319446cd2880 | 294 | |
ampembeng | 16:02008a2a2569 | 295 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 296 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 297 | wait(.5); |
ampembeng | 15:6f2798e45099 | 298 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 299 | wait(.5); |
ampembeng | 15:6f2798e45099 | 300 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 301 | wait(.5); |
ampembeng | 16:02008a2a2569 | 302 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 303 | wait(.5); |
ampembeng | 16:02008a2a2569 | 304 | SetLedColor(COLOR_OFF); |
RaghuT | 29:319446cd2880 | 305 | |
ampembeng | 15:6f2798e45099 | 306 | // Boot the Avnet Shield before any other operations |
ampembeng | 23:b9ff83dc965f | 307 | INFO("Net Boot..."); |
ampembeng | 15:6f2798e45099 | 308 | net_modem_boot(); |
RaghuT | 33:48172a2972b6 | 309 | |
ampembeng | 18:6370da1de572 | 310 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 311 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 312 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 313 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 314 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 315 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 316 | return rc; |
RaghuT | 29:319446cd2880 | 317 | } |
ampembeng | 18:6370da1de572 | 318 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 319 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 320 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 321 | #else |
ampembeng | 15:6f2798e45099 | 322 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 323 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 324 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 325 | sp.port = port; |
RaghuT | 29:319446cd2880 | 326 | |
ampembeng | 23:b9ff83dc965f | 327 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 328 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 329 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 330 | #endif |
RaghuT | 29:319446cd2880 | 331 | |
ampembeng | 18:6370da1de572 | 332 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 333 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 334 | aws_iot_mqtt_init(&mqttClient); |
RaghuT | 29:319446cd2880 | 335 | |
ampembeng | 15:6f2798e45099 | 336 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 337 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 338 | if (NONE_ERROR != rc) { |
RaghuT | 33:48172a2972b6 | 339 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 340 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 341 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 342 | } |
RaghuT | 29:319446cd2880 | 343 | |
RaghuT | 29:319446cd2880 | 344 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 345 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 346 | if (NONE_ERROR != rc) { |
RaghuT | 33:48172a2972b6 | 347 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 348 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 349 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 350 | } |
Janos Follath |
0:fc70c47eecb4 | 351 | |
ampembeng | 15:6f2798e45099 | 352 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 353 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 354 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
RaghuT | 29:319446cd2880 | 355 | |
ampembeng | 15:6f2798e45099 | 356 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 357 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 358 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
RaghuT | 33:48172a2972b6 | 359 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 360 | return rc; |
ampembeng | 15:6f2798e45099 | 361 | } |
RaghuT | 29:319446cd2880 | 362 | |
ampembeng | 15:6f2798e45099 | 363 | INFO("Shadow Register Delta..."); |
RaghuT | 28:d549de7c3aad | 364 | rc = aws_iot_shadow_register_delta(&mqttClient, &dataUsageHandler); |
RaghuT | 28:d549de7c3aad | 365 | if (NONE_ERROR != rc) { |
RaghuT | 28:d549de7c3aad | 366 | ERROR("Shadow Register Delta dataUsage Error"); |
RaghuT | 33:48172a2972b6 | 367 | SetLedColor(COLOR_RED); |
RaghuT | 28:d549de7c3aad | 368 | return rc; |
RaghuT | 28:d549de7c3aad | 369 | } |
RaghuT | 33:48172a2972b6 | 370 | |
ampembeng | 15:6f2798e45099 | 371 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
RaghuT | 33:48172a2972b6 | 372 | updateTimer.start(); |
RaghuT | 33:48172a2972b6 | 373 | disconnectTimer.start(); |
RaghuT | 33:48172a2972b6 | 374 | |
ampembeng | 15:6f2798e45099 | 375 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 376 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
RaghuT | 33:48172a2972b6 | 377 | if(updateTimer.read() < SHADOW_SYNC_INTERVAL) { |
RaghuT | 33:48172a2972b6 | 378 | continue; |
RaghuT | 33:48172a2972b6 | 379 | } |
RaghuT | 33:48172a2972b6 | 380 | updateTimer.reset(); |
ampembeng | 15:6f2798e45099 | 381 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 382 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 383 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 384 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 385 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 386 | wait(1); |
ampembeng | 15:6f2798e45099 | 387 | continue; |
ampembeng | 15:6f2798e45099 | 388 | } |
RaghuT | 28:d549de7c3aad | 389 | //Read signal quality |
RaghuT | 28:d549de7c3aad | 390 | signalQuality = WNCInterface::_pwnc->getDbmRssi(); |
RaghuT | 30:d2a7e413f658 | 391 | //get slave data |
RaghuT | 30:d2a7e413f658 | 392 | getSlaveJSON(); |
RaghuT | 32:acf84c7d0075 | 393 | |
RaghuT | 29:319446cd2880 | 394 | INFO("\n=======================================================================================\n"); |
RaghuT | 29:319446cd2880 | 395 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 396 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 397 | if (rc == NONE_ERROR) { |
RaghuT | 29:319446cd2880 | 398 | |
ampembeng | 23:b9ff83dc965f | 399 | // Updates the 'reported' color/temp/humidity |
RaghuT | 33:48172a2972b6 | 400 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 4, &signalStrengthHandler, |
RaghuT | 33:48172a2972b6 | 401 | &dataUsageHandler, &slaveDataHandler, &latencyHandler); |
RaghuT | 29:319446cd2880 | 402 | |
RaghuT | 29:319446cd2880 | 403 | if (rc == NONE_ERROR) { |
RaghuT | 29:319446cd2880 | 404 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
RaghuT | 29:319446cd2880 | 405 | |
ampembeng | 15:6f2798e45099 | 406 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 407 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
RaghuT | 33:48172a2972b6 | 408 | deviceTimer.start(); |
ampembeng | 18:6370da1de572 | 409 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
RaghuT | 29:319446cd2880 | 410 | ShadowUpdateStatusCallback, NULL, 15, true); |
ampembeng | 15:6f2798e45099 | 411 | } |
ampembeng | 15:6f2798e45099 | 412 | } |
RaghuT | 28:d549de7c3aad | 413 | } |
RaghuT | 33:48172a2972b6 | 414 | sendMasterData(); |
RaghuT | 33:48172a2972b6 | 415 | if(disconnectTimer.read() >= 60) { |
RaghuT | 33:48172a2972b6 | 416 | //WNCInterface::_pwnc->at_reinitialize_mdm(); |
RaghuT | 33:48172a2972b6 | 417 | NVIC_SystemReset(); |
RaghuT | 33:48172a2972b6 | 418 | } |
Janos Follath |
0:fc70c47eecb4 | 419 | } |
Janos Follath |
0:fc70c47eecb4 | 420 | |
ampembeng | 15:6f2798e45099 | 421 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 422 | ERROR("An error occurred in the loop %d", rc); |
RaghuT | 33:48172a2972b6 | 423 | SetLedColor(COLOR_RED); |
Janos Follath |
0:fc70c47eecb4 | 424 | } |
Janos Follath |
0:fc70c47eecb4 | 425 | |
ampembeng | 15:6f2798e45099 | 426 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 427 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 428 | |
ampembeng | 15:6f2798e45099 | 429 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 430 | ERROR("Disconnect error %d", rc); |
RaghuT | 33:48172a2972b6 | 431 | SetLedColor(COLOR_RED); |
Janos Follath |
0:fc70c47eecb4 | 432 | } |
Janos Follath |
0:fc70c47eecb4 | 433 | |
RaghuT | 29:319446cd2880 | 434 | return rc; |
ampembeng | 15:6f2798e45099 | 435 | } |