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@32:acf84c7d0075, 2017-08-02 (annotated)
- Committer:
- RaghuT
- Date:
- Wed Aug 02 08:04:25 2017 +0000
- Revision:
- 32:acf84c7d0075
- Parent:
- 30:d2a7e413f658
- Child:
- 33:48172a2972b6
week 7
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 |
ampembeng | 15:6f2798e45099 | 42 | #define SHADOW_SYNC_INTERVAL 3.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 | 28:d549de7c3aad | 96 | int dataUsage = 0; |
ampembeng | 23:b9ff83dc965f | 97 | |
RaghuT | 29:319446cd2880 | 98 | //slave JSON to publish |
RaghuT | 29:319446cd2880 | 99 | string slaveJSON = ""; |
RaghuT | 29:319446cd2880 | 100 | |
ampembeng | 15:6f2798e45099 | 101 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 102 | // |
ampembeng | 15:6f2798e45099 | 103 | // Devices |
ampembeng | 15:6f2798e45099 | 104 | // |
ampembeng | 15:6f2798e45099 | 105 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 106 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 107 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 108 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 109 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 110 | |
ampembeng | 15:6f2798e45099 | 111 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 112 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 113 | |
RaghuT | 30:d2a7e413f658 | 114 | // radio serial |
RaghuT | 30:d2a7e413f658 | 115 | BufferedSoftSerial radio(PTC4, PTA2); |
ampembeng | 18:6370da1de572 | 116 | |
ampembeng | 20:ee34856ae510 | 117 | // I2C bus (SDA, SCL) |
ampembeng | 20:ee34856ae510 | 118 | I2C i2c(PTC11, PTC10); |
ampembeng | 20:ee34856ae510 | 119 | |
ampembeng | 15:6f2798e45099 | 120 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 121 | // |
ampembeng | 15:6f2798e45099 | 122 | // Functions |
ampembeng | 15:6f2798e45099 | 123 | // |
ampembeng | 15:6f2798e45099 | 124 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 125 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 126 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 127 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 128 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 129 | { |
ampembeng | 15:6f2798e45099 | 130 | va_list vl; |
ampembeng | 15:6f2798e45099 | 131 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 132 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 133 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 134 | } |
Janos Follath |
0:fc70c47eecb4 | 135 | |
ampembeng | 15:6f2798e45099 | 136 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 137 | //* Set the RGB LED's Color |
RaghuT | 29:319446cd2880 | 138 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 139 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 140 | void SetLedColor(unsigned char ucColor) |
RaghuT | 29:319446cd2880 | 141 | { |
ampembeng | 15:6f2798e45099 | 142 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 143 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 144 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 145 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 146 | } |
Janos Follath |
0:fc70c47eecb4 | 147 | |
ampembeng | 15:6f2798e45099 | 148 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 149 | // |
ampembeng | 15:6f2798e45099 | 150 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 151 | // |
ampembeng | 15:6f2798e45099 | 152 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 153 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 154 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 155 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 156 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, |
RaghuT | 29:319446cd2880 | 157 | const char *pReceivedJsonDocument, void *pContextData) |
RaghuT | 29:319446cd2880 | 158 | { |
Janos Follath |
0:fc70c47eecb4 | 159 | |
ampembeng | 15:6f2798e45099 | 160 | INFO("Shadow Update Status Callback"); |
RaghuT | 29:319446cd2880 | 161 | |
ampembeng | 15:6f2798e45099 | 162 | if (status == SHADOW_ACK_TIMEOUT) { |
ampembeng | 15:6f2798e45099 | 163 | INFO("Update Timeout--"); |
ampembeng | 15:6f2798e45099 | 164 | } else if (status == SHADOW_ACK_REJECTED) { |
ampembeng | 15:6f2798e45099 | 165 | INFO("Update RejectedXX"); |
ampembeng | 15:6f2798e45099 | 166 | } else if (status == SHADOW_ACK_ACCEPTED) { |
ampembeng | 15:6f2798e45099 | 167 | INFO("Update Accepted!!"); // Good |
ampembeng | 15:6f2798e45099 | 168 | } |
ampembeng | 15:6f2798e45099 | 169 | } |
Janos Follath |
0:fc70c47eecb4 | 170 | |
ampembeng | 15:6f2798e45099 | 171 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 172 | //* These are the callback functions that fire when AWS has sends out a shadow update. |
ampembeng | 15:6f2798e45099 | 173 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 174 | void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) |
RaghuT | 29:319446cd2880 | 175 | { |
RaghuT | 29:319446cd2880 | 176 | |
ampembeng | 15:6f2798e45099 | 177 | INFO("LED Callback Detected."); |
RaghuT | 29:319446cd2880 | 178 | |
ampembeng | 15:6f2798e45099 | 179 | if (pContext != NULL) { |
RaghuT | 29:319446cd2880 | 180 | switch (*(unsigned char *)(pContext->pData)) { |
ampembeng | 15:6f2798e45099 | 181 | case COLOR_OFF: |
ampembeng | 15:6f2798e45099 | 182 | INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 183 | break; |
ampembeng | 15:6f2798e45099 | 184 | case COLOR_RED: |
ampembeng | 15:6f2798e45099 | 185 | INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 186 | break; |
ampembeng | 15:6f2798e45099 | 187 | case COLOR_GREEN: |
ampembeng | 15:6f2798e45099 | 188 | INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 189 | break; |
ampembeng | 15:6f2798e45099 | 190 | case COLOR_BLUE: |
ampembeng | 15:6f2798e45099 | 191 | INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 192 | break; |
ampembeng | 15:6f2798e45099 | 193 | case COLOR_WHITE: |
ampembeng | 15:6f2798e45099 | 194 | INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData)); |
RaghuT | 29:319446cd2880 | 195 | break; |
Janos Follath |
0:fc70c47eecb4 | 196 | } |
RaghuT | 29:319446cd2880 | 197 | } else { |
ampembeng | 15:6f2798e45099 | 198 | INFO("pContext was detected as NULL"); |
Janos Follath |
0:fc70c47eecb4 | 199 | } |
ampembeng | 15:6f2798e45099 | 200 | } |
RaghuT | 28:d549de7c3aad | 201 | //Callback when dataUsage stats are updated |
RaghuT | 29:319446cd2880 | 202 | void dataUsageCallback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) |
RaghuT | 29:319446cd2880 | 203 | { |
RaghuT | 29:319446cd2880 | 204 | INFO("Data usage callback detected"); |
RaghuT | 28:d549de7c3aad | 205 | } |
RaghuT | 32:acf84c7d0075 | 206 | |
ampembeng | 23:b9ff83dc965f | 207 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 208 | //* Subscribe callback (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 209 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 210 | int MQTTcallbackHandler(MQTTCallbackParams params) |
RaghuT | 29:319446cd2880 | 211 | { |
ampembeng | 23:b9ff83dc965f | 212 | |
ampembeng | 23:b9ff83dc965f | 213 | INFO("Subscribe callback"); |
ampembeng | 23:b9ff83dc965f | 214 | INFO("%.*s\t%.*s", |
RaghuT | 29:319446cd2880 | 215 | (int)params.TopicNameLen, params.pTopicName, |
RaghuT | 29:319446cd2880 | 216 | (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload); |
ampembeng | 23:b9ff83dc965f | 217 | |
ampembeng | 23:b9ff83dc965f | 218 | return 0; |
ampembeng | 23:b9ff83dc965f | 219 | } |
ampembeng | 23:b9ff83dc965f | 220 | |
ampembeng | 23:b9ff83dc965f | 221 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 222 | //* Disconnect handling (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 223 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 224 | void disconnectCallbackHandler(void) |
RaghuT | 29:319446cd2880 | 225 | { |
ampembeng | 23:b9ff83dc965f | 226 | WARN("MQTT Disconnect"); |
ampembeng | 23:b9ff83dc965f | 227 | IoT_Error_t rc = NONE_ERROR; |
RaghuT | 29:319446cd2880 | 228 | if(aws_iot_is_autoreconnect_enabled()) { |
ampembeng | 23:b9ff83dc965f | 229 | INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
RaghuT | 29:319446cd2880 | 230 | } else { |
ampembeng | 23:b9ff83dc965f | 231 | WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
ampembeng | 23:b9ff83dc965f | 232 | rc = aws_iot_mqtt_attempt_reconnect(); |
RaghuT | 29:319446cd2880 | 233 | if(RECONNECT_SUCCESSFUL == rc) { |
ampembeng | 23:b9ff83dc965f | 234 | WARN("Manual Reconnect Successful"); |
RaghuT | 29:319446cd2880 | 235 | } else { |
ampembeng | 23:b9ff83dc965f | 236 | WARN("Manual Reconnect Failed - %d", rc); |
ampembeng | 23:b9ff83dc965f | 237 | } |
ampembeng | 23:b9ff83dc965f | 238 | } |
ampembeng | 23:b9ff83dc965f | 239 | } |
ampembeng | 23:b9ff83dc965f | 240 | |
RaghuT | 28:d549de7c3aad | 241 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 242 | //* Get slave data over uart interface |
RaghuT | 28:d549de7c3aad | 243 | //********************************************************************************************************************* |
RaghuT | 29:319446cd2880 | 244 | void getSlaveJSON() |
RaghuT | 29:319446cd2880 | 245 | { |
RaghuT | 30:d2a7e413f658 | 246 | slaveJSON.clear(); |
RaghuT | 30:d2a7e413f658 | 247 | if(radio.writeable()) { |
RaghuT | 30:d2a7e413f658 | 248 | radio.printf("d"); |
RaghuT | 30:d2a7e413f658 | 249 | while(radio.readable()) { |
RaghuT | 30:d2a7e413f658 | 250 | slaveJSON += radio.getc(); |
RaghuT | 30:d2a7e413f658 | 251 | } |
RaghuT | 29:319446cd2880 | 252 | } |
RaghuT | 32:acf84c7d0075 | 253 | strcpy(slaveData, slaveJSON.c_str()); |
ampembeng | 23:b9ff83dc965f | 254 | } |
ampembeng | 15:6f2798e45099 | 255 | |
ampembeng | 15:6f2798e45099 | 256 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 257 | // |
ampembeng | 15:6f2798e45099 | 258 | // Main |
ampembeng | 15:6f2798e45099 | 259 | // |
ampembeng | 15:6f2798e45099 | 260 | //===================================================================================================================== |
RaghuT | 29:319446cd2880 | 261 | int main() |
RaghuT | 29:319446cd2880 | 262 | { |
ampembeng | 15:6f2798e45099 | 263 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 264 | pc.baud(115200); |
RaghuT | 30:d2a7e413f658 | 265 | radio.baud(9600); |
RaghuT | 28:d549de7c3aad | 266 | INFO("Program Start"); |
RaghuT | 29:319446cd2880 | 267 | |
RaghuT | 29:319446cd2880 | 268 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 269 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 270 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 271 | |
RaghuT | 28:d549de7c3aad | 272 | //JSON struct for signal strength readings |
RaghuT | 28:d549de7c3aad | 273 | jsonStruct_t signalStrengthHandler; |
RaghuT | 28:d549de7c3aad | 274 | signalStrengthHandler.cb = NULL; |
RaghuT | 28:d549de7c3aad | 275 | signalStrengthHandler.pKey = "Signal Strength"; |
RaghuT | 28:d549de7c3aad | 276 | signalStrengthHandler.pData = &signalQuality; |
RaghuT | 28:d549de7c3aad | 277 | signalStrengthHandler.type = SHADOW_JSON_INT16; |
RaghuT | 29:319446cd2880 | 278 | |
RaghuT | 29:319446cd2880 | 279 | //JSON struct for data usage |
RaghuT | 28:d549de7c3aad | 280 | jsonStruct_t dataUsageHandler; |
RaghuT | 28:d549de7c3aad | 281 | dataUsageHandler.cb = dataUsageCallback; |
RaghuT | 28:d549de7c3aad | 282 | dataUsageHandler.pKey = "Data Usage"; |
RaghuT | 28:d549de7c3aad | 283 | dataUsageHandler.pData = &dataUsage; |
RaghuT | 28:d549de7c3aad | 284 | dataUsageHandler.type = SHADOW_JSON_UINT32; |
RaghuT | 30:d2a7e413f658 | 285 | |
RaghuT | 30:d2a7e413f658 | 286 | //JSON struct for slave data |
RaghuT | 30:d2a7e413f658 | 287 | jsonStruct_t slaveDataHandler; |
RaghuT | 30:d2a7e413f658 | 288 | slaveDataHandler.cb = NULL; |
RaghuT | 30:d2a7e413f658 | 289 | slaveDataHandler.pKey = "slaves"; |
RaghuT | 30:d2a7e413f658 | 290 | slaveDataHandler.pData = slaveData; |
RaghuT | 30:d2a7e413f658 | 291 | slaveDataHandler.type = SHADOW_JSON_STRING; |
RaghuT | 30:d2a7e413f658 | 292 | |
ampembeng | 15:6f2798e45099 | 293 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
ampembeng | 15:6f2798e45099 | 294 | |
RaghuT | 28:d549de7c3aad | 295 | |
RaghuT | 29:319446cd2880 | 296 | INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
RaghuT | 29:319446cd2880 | 297 | |
ampembeng | 16:02008a2a2569 | 298 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 299 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 300 | wait(.5); |
ampembeng | 15:6f2798e45099 | 301 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 302 | wait(.5); |
ampembeng | 15:6f2798e45099 | 303 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 304 | wait(.5); |
ampembeng | 16:02008a2a2569 | 305 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 306 | wait(.5); |
ampembeng | 16:02008a2a2569 | 307 | SetLedColor(COLOR_OFF); |
RaghuT | 29:319446cd2880 | 308 | |
ampembeng | 15:6f2798e45099 | 309 | // Boot the Avnet Shield before any other operations |
ampembeng | 23:b9ff83dc965f | 310 | INFO("Net Boot..."); |
ampembeng | 15:6f2798e45099 | 311 | net_modem_boot(); |
RaghuT | 29:319446cd2880 | 312 | |
ampembeng | 18:6370da1de572 | 313 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 314 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 315 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 316 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 317 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 318 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 319 | return rc; |
RaghuT | 29:319446cd2880 | 320 | } |
ampembeng | 18:6370da1de572 | 321 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 322 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 323 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 324 | #else |
ampembeng | 15:6f2798e45099 | 325 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 326 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 327 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 328 | sp.port = port; |
RaghuT | 29:319446cd2880 | 329 | |
ampembeng | 23:b9ff83dc965f | 330 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 331 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 332 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 333 | #endif |
RaghuT | 29:319446cd2880 | 334 | |
ampembeng | 18:6370da1de572 | 335 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 336 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 337 | aws_iot_mqtt_init(&mqttClient); |
RaghuT | 29:319446cd2880 | 338 | |
ampembeng | 15:6f2798e45099 | 339 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 340 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 341 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 342 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 343 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 344 | } |
RaghuT | 29:319446cd2880 | 345 | |
RaghuT | 29:319446cd2880 | 346 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 347 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 348 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 349 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 350 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 351 | } |
Janos Follath |
0:fc70c47eecb4 | 352 | |
ampembeng | 15:6f2798e45099 | 353 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 354 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 355 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
RaghuT | 29:319446cd2880 | 356 | |
ampembeng | 15:6f2798e45099 | 357 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 358 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 359 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
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 | 28:d549de7c3aad | 367 | return rc; |
RaghuT | 28:d549de7c3aad | 368 | } |
RaghuT | 30:d2a7e413f658 | 369 | |
ampembeng | 15:6f2798e45099 | 370 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 371 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 372 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 373 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 374 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 375 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 376 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 377 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 378 | wait(1); |
ampembeng | 15:6f2798e45099 | 379 | continue; |
ampembeng | 15:6f2798e45099 | 380 | } |
RaghuT | 28:d549de7c3aad | 381 | //Read signal quality |
RaghuT | 28:d549de7c3aad | 382 | signalQuality = WNCInterface::_pwnc->getDbmRssi(); |
RaghuT | 30:d2a7e413f658 | 383 | //get slave data |
RaghuT | 30:d2a7e413f658 | 384 | getSlaveJSON(); |
RaghuT | 32:acf84c7d0075 | 385 | |
RaghuT | 29:319446cd2880 | 386 | INFO("\n=======================================================================================\n"); |
RaghuT | 29:319446cd2880 | 387 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 388 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 389 | if (rc == NONE_ERROR) { |
RaghuT | 29:319446cd2880 | 390 | |
ampembeng | 23:b9ff83dc965f | 391 | // Updates the 'reported' color/temp/humidity |
RaghuT | 30:d2a7e413f658 | 392 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &signalStrengthHandler, |
RaghuT | 30:d2a7e413f658 | 393 | &dataUsageHandler, &slaveDataHandler); |
RaghuT | 29:319446cd2880 | 394 | |
RaghuT | 29:319446cd2880 | 395 | if (rc == NONE_ERROR) { |
RaghuT | 29:319446cd2880 | 396 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
RaghuT | 29:319446cd2880 | 397 | |
ampembeng | 15:6f2798e45099 | 398 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 399 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 18:6370da1de572 | 400 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
RaghuT | 29:319446cd2880 | 401 | ShadowUpdateStatusCallback, NULL, 15, true); |
ampembeng | 15:6f2798e45099 | 402 | } |
ampembeng | 15:6f2798e45099 | 403 | } |
RaghuT | 28:d549de7c3aad | 404 | } |
ampembeng | 15:6f2798e45099 | 405 | wait(SHADOW_SYNC_INTERVAL); |
Janos Follath |
0:fc70c47eecb4 | 406 | } |
Janos Follath |
0:fc70c47eecb4 | 407 | |
ampembeng | 15:6f2798e45099 | 408 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 409 | ERROR("An error occurred in the loop %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 410 | } |
Janos Follath |
0:fc70c47eecb4 | 411 | |
ampembeng | 15:6f2798e45099 | 412 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 413 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 414 | |
ampembeng | 15:6f2798e45099 | 415 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 416 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 417 | } |
Janos Follath |
0:fc70c47eecb4 | 418 | |
RaghuT | 29:319446cd2880 | 419 | return rc; |
ampembeng | 15:6f2798e45099 | 420 | } |