Catie fork
Dependencies: SDFileSystem X_NUCLEO_IKS01A1
Fork of ATT_AWS_IoT_demo by
main.cpp@29:537df716ba0b, 2017-04-05 (annotated)
- Committer:
- peyo
- Date:
- Wed Apr 05 16:05:41 2017 +0000
- Revision:
- 29:537df716ba0b
- Parent:
- 28:a31312c9756d
Use IKS01A1 sensors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Janos Follath |
0:fc70c47eecb4 | 1 | /* |
ampembeng | 15:6f2798e45099 | 2 | * AT&T IoT Starter Kit example using Amazon Web Service |
mbed_official | 12:1ae41c231014 | 3 | */ |
ampembeng | 15:6f2798e45099 | 4 | #include "mbed.h" |
ampembeng | 15:6f2798e45099 | 5 | |
ampembeng | 18:6370da1de572 | 6 | // SD File System |
ampembeng | 18:6370da1de572 | 7 | #include "SDFileSystem.h" |
ampembeng | 18:6370da1de572 | 8 | |
ampembeng | 15:6f2798e45099 | 9 | // Serial extension |
ampembeng | 15:6f2798e45099 | 10 | #include "MODSERIAL.h" |
Janos Follath |
0:fc70c47eecb4 | 11 | |
ampembeng | 15:6f2798e45099 | 12 | // Network includes |
ampembeng | 15:6f2798e45099 | 13 | #include "WNCInterface.h" |
ampembeng | 15:6f2798e45099 | 14 | #include "network_interface.h" |
Janos Follath |
0:fc70c47eecb4 | 15 | |
ampembeng | 15:6f2798e45099 | 16 | // AWS includes |
ampembeng | 15:6f2798e45099 | 17 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 18 | #include "aws_iot_version.h" |
ampembeng | 15:6f2798e45099 | 19 | #include "aws_iot_shadow_interface.h" |
ampembeng | 15:6f2798e45099 | 20 | #include "aws_iot_shadow_json_data.h" |
ampembeng | 15:6f2798e45099 | 21 | #include "aws_iot_config.h" |
ampembeng | 15:6f2798e45099 | 22 | #include "aws_iot_mqtt_interface.h" |
mbed_official | 12:1ae41c231014 | 23 | |
ampembeng | 20:ee34856ae510 | 24 | // Sensors |
peyo | 29:537df716ba0b | 25 | #include "x_nucleo_iks01a1.h" |
ampembeng | 20:ee34856ae510 | 26 | |
Janos Follath |
0:fc70c47eecb4 | 27 | #if DEBUG_LEVEL > 0 |
Janos Follath |
0:fc70c47eecb4 | 28 | #include "mbedtls/debug.h" |
Janos Follath |
0:fc70c47eecb4 | 29 | #endif |
Janos Follath |
0:fc70c47eecb4 | 30 | |
ampembeng | 15:6f2798e45099 | 31 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 32 | // |
ampembeng | 15:6f2798e45099 | 33 | // Defines |
ampembeng | 15:6f2798e45099 | 34 | // |
ampembeng | 15:6f2798e45099 | 35 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 36 | // LED Colors |
ampembeng | 15:6f2798e45099 | 37 | #define COLOR_OFF 0x00 |
ampembeng | 15:6f2798e45099 | 38 | #define COLOR_RED 0x01 |
ampembeng | 15:6f2798e45099 | 39 | #define COLOR_GREEN 0x02 |
ampembeng | 15:6f2798e45099 | 40 | #define COLOR_BLUE 0x04 |
ampembeng | 15:6f2798e45099 | 41 | #define COLOR_WHITE 0x07 |
ampembeng | 15:6f2798e45099 | 42 | #define NUM_COLORS 5 |
Janos Follath |
0:fc70c47eecb4 | 43 | |
ampembeng | 15:6f2798e45099 | 44 | // AWS defines |
ampembeng | 18:6370da1de572 | 45 | #define PATH_MAX 1024 |
peyo | 29:537df716ba0b | 46 | #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 300 // NOTE: Be wary of this if your JSON doc grows |
ampembeng | 15:6f2798e45099 | 47 | #define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds) |
Janos Follath |
0:fc70c47eecb4 | 48 | |
ampembeng | 15:6f2798e45099 | 49 | // Comment out the following line if color is not supported on the terminal |
ampembeng | 15:6f2798e45099 | 50 | //#define USE_COLOR |
ampembeng | 15:6f2798e45099 | 51 | #ifdef USE_COLOR |
ampembeng | 15:6f2798e45099 | 52 | #define BLK "\033[30m" |
ampembeng | 15:6f2798e45099 | 53 | #define RED "\033[31m" |
ampembeng | 15:6f2798e45099 | 54 | #define GRN "\033[32m" |
ampembeng | 15:6f2798e45099 | 55 | #define YEL "\033[33m" |
ampembeng | 15:6f2798e45099 | 56 | #define BLU "\033[34m" |
ampembeng | 15:6f2798e45099 | 57 | #define MAG "\033[35m" |
ampembeng | 15:6f2798e45099 | 58 | #define CYN "\033[36m" |
ampembeng | 15:6f2798e45099 | 59 | #define WHT "\033[37m" |
ampembeng | 15:6f2798e45099 | 60 | #define DEF "\033[39m" |
ampembeng | 15:6f2798e45099 | 61 | #else |
ampembeng | 15:6f2798e45099 | 62 | #define BLK |
ampembeng | 15:6f2798e45099 | 63 | #define RED |
ampembeng | 15:6f2798e45099 | 64 | #define GRN |
ampembeng | 15:6f2798e45099 | 65 | #define YEL |
ampembeng | 15:6f2798e45099 | 66 | #define BLU |
ampembeng | 15:6f2798e45099 | 67 | #define MAG |
ampembeng | 15:6f2798e45099 | 68 | #define CYN |
ampembeng | 15:6f2798e45099 | 69 | #define WHT |
ampembeng | 15:6f2798e45099 | 70 | #define DEF |
Janos Follath |
0:fc70c47eecb4 | 71 | #endif |
Janos Follath |
0:fc70c47eecb4 | 72 | |
ampembeng | 20:ee34856ae510 | 73 | // Sensor defines |
ampembeng | 20:ee34856ae510 | 74 | #define CTOF(x) ((x)*1.8+32) // Temperature |
ampembeng | 20:ee34856ae510 | 75 | |
ampembeng | 15:6f2798e45099 | 76 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 77 | // |
ampembeng | 15:6f2798e45099 | 78 | // Globals |
ampembeng | 15:6f2798e45099 | 79 | // |
ampembeng | 15:6f2798e45099 | 80 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 81 | // Controls LED color |
ampembeng | 15:6f2798e45099 | 82 | unsigned char ledColor = COLOR_OFF; |
ampembeng | 15:6f2798e45099 | 83 | |
ampembeng | 15:6f2798e45099 | 84 | // Color cycle array (used with SW3 button presses) |
ampembeng | 15:6f2798e45099 | 85 | unsigned char colorCycle[NUM_COLORS] = {COLOR_OFF, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE}; |
Janos Follath |
0:fc70c47eecb4 | 86 | |
ampembeng | 15:6f2798e45099 | 87 | // Button interrupts |
ampembeng | 15:6f2798e45099 | 88 | bool buttonOverride = false; |
ampembeng | 15:6f2798e45099 | 89 | InterruptIn Interrupt(SW3); |
ampembeng | 15:6f2798e45099 | 90 | |
ampembeng | 18:6370da1de572 | 91 | // These defines are pulled from aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 92 | char HostAddress[255] = AWS_IOT_MQTT_HOST; |
ampembeng | 18:6370da1de572 | 93 | char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 18:6370da1de572 | 94 | char ThingName[32] = AWS_IOT_MY_THING_NAME; |
ampembeng | 18:6370da1de572 | 95 | char PortString[5] = "8883"; |
ampembeng | 15:6f2798e45099 | 96 | uint32_t port = AWS_IOT_MQTT_PORT; |
ampembeng | 23:b9ff83dc965f | 97 | char iccidName[21] = "12345678901234567890"; |
Janos Follath |
0:fc70c47eecb4 | 98 | |
peyo | 29:537df716ba0b | 99 | /* Instantiate the expansion board */ |
peyo | 29:537df716ba0b | 100 | static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(I2C_SDA, I2C_SCL); |
peyo | 29:537df716ba0b | 101 | |
ampembeng | 20:ee34856ae510 | 102 | // Sensor data |
peyo | 29:537df716ba0b | 103 | static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor; |
peyo | 29:537df716ba0b | 104 | static PressureSensor *pressure_sensor = mems_expansion_board->pt_sensor; |
peyo | 29:537df716ba0b | 105 | static TempSensor *temp_sensor1 = mems_expansion_board->ht_sensor; |
peyo | 29:537df716ba0b | 106 | static TempSensor *temp_sensor2 = mems_expansion_board->pt_sensor; |
peyo | 29:537df716ba0b | 107 | float iks01a1_temperature = 0.0; |
peyo | 29:537df716ba0b | 108 | float iks01a1_pressure = 0.0; |
peyo | 29:537df716ba0b | 109 | float iks01a1_humidity = 0.0; |
peyo | 29:537df716ba0b | 110 | uint8_t id; |
ampembeng | 20:ee34856ae510 | 111 | |
ampembeng | 23:b9ff83dc965f | 112 | |
ampembeng | 15:6f2798e45099 | 113 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 114 | // |
ampembeng | 15:6f2798e45099 | 115 | // Devices |
ampembeng | 15:6f2798e45099 | 116 | // |
ampembeng | 15:6f2798e45099 | 117 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 118 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 119 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 120 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 121 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 122 | |
ampembeng | 15:6f2798e45099 | 123 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 124 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 125 | |
ampembeng | 18:6370da1de572 | 126 | // SD card access (MOSI, MISO, SCK, CS) |
ampembeng | 18:6370da1de572 | 127 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
ampembeng | 18:6370da1de572 | 128 | |
ampembeng | 15:6f2798e45099 | 129 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 130 | // |
ampembeng | 15:6f2798e45099 | 131 | // Functions |
ampembeng | 15:6f2798e45099 | 132 | // |
ampembeng | 15:6f2798e45099 | 133 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 134 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 135 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 136 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 137 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 138 | { |
ampembeng | 15:6f2798e45099 | 139 | va_list vl; |
ampembeng | 15:6f2798e45099 | 140 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 141 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 142 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 143 | } |
Janos Follath |
0:fc70c47eecb4 | 144 | |
ampembeng | 15:6f2798e45099 | 145 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 146 | //* Set the RGB LED's Color |
ampembeng | 15:6f2798e45099 | 147 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 148 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 149 | void SetLedColor(unsigned char ucColor) |
ampembeng | 18:6370da1de572 | 150 | { |
ampembeng | 15:6f2798e45099 | 151 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 152 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 153 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 154 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 155 | } |
Janos Follath |
0:fc70c47eecb4 | 156 | |
ampembeng | 15:6f2798e45099 | 157 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 158 | //* SW3 Button handler. Finds the current LED color and sets the button to the next color in colorCycle[] |
ampembeng | 15:6f2798e45099 | 159 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 160 | void sw3ButtonHandler() |
ampembeng | 15:6f2798e45099 | 161 | { |
ampembeng | 15:6f2798e45099 | 162 | int i; |
ampembeng | 15:6f2798e45099 | 163 | for(i=0; i < NUM_COLORS; i++) { |
ampembeng | 15:6f2798e45099 | 164 | if (ledColor == colorCycle[i]) |
ampembeng | 15:6f2798e45099 | 165 | break; |
ampembeng | 15:6f2798e45099 | 166 | } |
ampembeng | 15:6f2798e45099 | 167 | |
ampembeng | 15:6f2798e45099 | 168 | // (circular-queue) |
ampembeng | 15:6f2798e45099 | 169 | if (++i == NUM_COLORS) |
ampembeng | 15:6f2798e45099 | 170 | i = 0; |
ampembeng | 15:6f2798e45099 | 171 | |
ampembeng | 15:6f2798e45099 | 172 | ledColor = colorCycle[i]; |
ampembeng | 15:6f2798e45099 | 173 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 174 | buttonOverride = true; |
ampembeng | 15:6f2798e45099 | 175 | } |
Janos Follath |
0:fc70c47eecb4 | 176 | |
ampembeng | 23:b9ff83dc965f | 177 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 178 | //* Print LED and sensor data |
ampembeng | 23:b9ff83dc965f | 179 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 180 | void printData() |
ampembeng | 23:b9ff83dc965f | 181 | { |
peyo | 29:537df716ba0b | 182 | INFO("Temperature is: %0.2f C", iks01a1_temperature); |
peyo | 29:537df716ba0b | 183 | INFO("Humidity is: %0.2f", iks01a1_humidity); |
peyo | 29:537df716ba0b | 184 | INFO("Pressure is: %0.2f mbar", iks01a1_pressure); |
peyo | 29:537df716ba0b | 185 | |
ampembeng | 23:b9ff83dc965f | 186 | switch (ledColor) { |
ampembeng | 23:b9ff83dc965f | 187 | case COLOR_OFF: |
ampembeng | 23:b9ff83dc965f | 188 | INFO("LED: Off"); |
ampembeng | 23:b9ff83dc965f | 189 | break; |
ampembeng | 23:b9ff83dc965f | 190 | case COLOR_RED: |
ampembeng | 23:b9ff83dc965f | 191 | INFO("LED: Red"); |
ampembeng | 23:b9ff83dc965f | 192 | break; |
ampembeng | 23:b9ff83dc965f | 193 | case COLOR_GREEN: |
ampembeng | 23:b9ff83dc965f | 194 | INFO("LED: Green"); |
ampembeng | 23:b9ff83dc965f | 195 | break; |
ampembeng | 23:b9ff83dc965f | 196 | case COLOR_BLUE: |
ampembeng | 23:b9ff83dc965f | 197 | INFO("LED: Blue"); |
ampembeng | 23:b9ff83dc965f | 198 | break; |
ampembeng | 23:b9ff83dc965f | 199 | case COLOR_WHITE: |
ampembeng | 23:b9ff83dc965f | 200 | INFO("LED: White"); |
ampembeng | 23:b9ff83dc965f | 201 | break; |
ampembeng | 23:b9ff83dc965f | 202 | } |
ampembeng | 23:b9ff83dc965f | 203 | } |
ampembeng | 23:b9ff83dc965f | 204 | |
ampembeng | 15:6f2798e45099 | 205 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 206 | // |
ampembeng | 15:6f2798e45099 | 207 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 208 | // |
ampembeng | 15:6f2798e45099 | 209 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 210 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 211 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 212 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 213 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, |
ampembeng | 15:6f2798e45099 | 214 | const char *pReceivedJsonDocument, void *pContextData) { |
Janos Follath |
0:fc70c47eecb4 | 215 | |
ampembeng | 15:6f2798e45099 | 216 | INFO("Shadow Update Status Callback"); |
ampembeng | 15:6f2798e45099 | 217 | |
ampembeng | 15:6f2798e45099 | 218 | if (status == SHADOW_ACK_TIMEOUT) { |
ampembeng | 15:6f2798e45099 | 219 | INFO("Update Timeout--"); |
ampembeng | 15:6f2798e45099 | 220 | } else if (status == SHADOW_ACK_REJECTED) { |
ampembeng | 15:6f2798e45099 | 221 | INFO("Update RejectedXX"); |
ampembeng | 15:6f2798e45099 | 222 | } else if (status == SHADOW_ACK_ACCEPTED) { |
ampembeng | 15:6f2798e45099 | 223 | INFO("Update Accepted!!"); // Good |
ampembeng | 15:6f2798e45099 | 224 | } |
ampembeng | 15:6f2798e45099 | 225 | } |
Janos Follath |
0:fc70c47eecb4 | 226 | |
ampembeng | 15:6f2798e45099 | 227 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 228 | //* This is the callback function that fires when AWS has sends out a shadow update. |
ampembeng | 15:6f2798e45099 | 229 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 230 | void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) { |
ampembeng | 15:6f2798e45099 | 231 | |
ampembeng | 15:6f2798e45099 | 232 | INFO("LED Callback Detected."); |
ampembeng | 15:6f2798e45099 | 233 | |
ampembeng | 15:6f2798e45099 | 234 | if (pContext != NULL) { |
ampembeng | 15:6f2798e45099 | 235 | switch (*(unsigned char *)(pContext->pData)){ |
ampembeng | 15:6f2798e45099 | 236 | case COLOR_OFF: |
ampembeng | 15:6f2798e45099 | 237 | INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 238 | break; |
ampembeng | 15:6f2798e45099 | 239 | case COLOR_RED: |
ampembeng | 15:6f2798e45099 | 240 | INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 241 | break; |
ampembeng | 15:6f2798e45099 | 242 | case COLOR_GREEN: |
ampembeng | 15:6f2798e45099 | 243 | INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 244 | break; |
ampembeng | 15:6f2798e45099 | 245 | case COLOR_BLUE: |
ampembeng | 15:6f2798e45099 | 246 | INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 247 | break; |
ampembeng | 15:6f2798e45099 | 248 | case COLOR_WHITE: |
ampembeng | 15:6f2798e45099 | 249 | INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 250 | break; |
Janos Follath |
0:fc70c47eecb4 | 251 | } |
ampembeng | 15:6f2798e45099 | 252 | } |
ampembeng | 15:6f2798e45099 | 253 | else { |
ampembeng | 15:6f2798e45099 | 254 | INFO("pContext was detected as NULL"); |
Janos Follath |
0:fc70c47eecb4 | 255 | } |
ampembeng | 15:6f2798e45099 | 256 | } |
ampembeng | 23:b9ff83dc965f | 257 | |
ampembeng | 23:b9ff83dc965f | 258 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 259 | //* Subscribe callback (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 260 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 261 | int MQTTcallbackHandler(MQTTCallbackParams params) { |
ampembeng | 23:b9ff83dc965f | 262 | |
ampembeng | 23:b9ff83dc965f | 263 | INFO("Subscribe callback"); |
ampembeng | 23:b9ff83dc965f | 264 | INFO("%.*s\t%.*s", |
ampembeng | 23:b9ff83dc965f | 265 | (int)params.TopicNameLen, params.pTopicName, |
ampembeng | 23:b9ff83dc965f | 266 | (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload); |
ampembeng | 23:b9ff83dc965f | 267 | |
ampembeng | 23:b9ff83dc965f | 268 | return 0; |
ampembeng | 23:b9ff83dc965f | 269 | } |
ampembeng | 23:b9ff83dc965f | 270 | |
ampembeng | 23:b9ff83dc965f | 271 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 272 | //* Disconnect handling (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 273 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 274 | void disconnectCallbackHandler(void) { |
ampembeng | 23:b9ff83dc965f | 275 | WARN("MQTT Disconnect"); |
ampembeng | 23:b9ff83dc965f | 276 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 277 | if(aws_iot_is_autoreconnect_enabled()){ |
ampembeng | 23:b9ff83dc965f | 278 | INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
ampembeng | 23:b9ff83dc965f | 279 | }else{ |
ampembeng | 23:b9ff83dc965f | 280 | WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
ampembeng | 23:b9ff83dc965f | 281 | rc = aws_iot_mqtt_attempt_reconnect(); |
ampembeng | 23:b9ff83dc965f | 282 | if(RECONNECT_SUCCESSFUL == rc){ |
ampembeng | 23:b9ff83dc965f | 283 | WARN("Manual Reconnect Successful"); |
ampembeng | 23:b9ff83dc965f | 284 | }else{ |
ampembeng | 23:b9ff83dc965f | 285 | WARN("Manual Reconnect Failed - %d", rc); |
ampembeng | 23:b9ff83dc965f | 286 | } |
ampembeng | 23:b9ff83dc965f | 287 | } |
ampembeng | 23:b9ff83dc965f | 288 | } |
ampembeng | 23:b9ff83dc965f | 289 | |
ampembeng | 23:b9ff83dc965f | 290 | //===================================================================================================================== |
ampembeng | 23:b9ff83dc965f | 291 | // |
ampembeng | 23:b9ff83dc965f | 292 | // Out-of-Box Demo: This function is used as part of the binary that comes with the Starter Kit. Instead of using an |
ampembeng | 23:b9ff83dc965f | 293 | // AWS device shadow, it publishes to an AWS Rule. The Rule is setup to store data to a DynamoDB, and |
ampembeng | 23:b9ff83dc965f | 294 | // the demo S3 website pulls that data from the DynamoDB and displays it. |
ampembeng | 23:b9ff83dc965f | 295 | // |
ampembeng | 23:b9ff83dc965f | 296 | //===================================================================================================================== |
ampembeng | 23:b9ff83dc965f | 297 | int outOfBoxDemo() { |
ampembeng | 23:b9ff83dc965f | 298 | INFO("Running Out-of-Box Function (alternate demo)."); |
ampembeng | 23:b9ff83dc965f | 299 | |
ampembeng | 23:b9ff83dc965f | 300 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 301 | int32_t i = 0; |
ampembeng | 23:b9ff83dc965f | 302 | int publishCount = 0; |
ampembeng | 23:b9ff83dc965f | 303 | bool infinitePublishFlag = true; |
ampembeng | 23:b9ff83dc965f | 304 | char cPayload[100]; |
ampembeng | 23:b9ff83dc965f | 305 | char cTopic[100]; |
ampembeng | 23:b9ff83dc965f | 306 | const string colorStrings[] = {"Off", "Red", "Green", "", "Blue", "", "", "White"}; |
ampembeng | 24:224c07ec3bd0 | 307 | float updateInterval = 1.0; // seconds |
ampembeng | 23:b9ff83dc965f | 308 | |
ampembeng | 23:b9ff83dc965f | 309 | MQTTConnectParams connectParams = MQTTConnectParamsDefault; |
ampembeng | 23:b9ff83dc965f | 310 | connectParams.KeepAliveInterval_sec = 10; |
ampembeng | 23:b9ff83dc965f | 311 | connectParams.isCleansession = true; |
ampembeng | 23:b9ff83dc965f | 312 | connectParams.MQTTVersion = MQTT_3_1_1; |
ampembeng | 23:b9ff83dc965f | 313 | connectParams.pClientID = iccidName; // Using ICCID for unique Client ID |
ampembeng | 23:b9ff83dc965f | 314 | connectParams.pHostURL = HostAddress; |
ampembeng | 23:b9ff83dc965f | 315 | connectParams.port = port; |
ampembeng | 23:b9ff83dc965f | 316 | connectParams.isWillMsgPresent = false; |
ampembeng | 23:b9ff83dc965f | 317 | connectParams.pRootCALocation = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 23:b9ff83dc965f | 318 | connectParams.pDeviceCertLocation = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 319 | connectParams.pDevicePrivateKeyLocation = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 320 | connectParams.mqttCommandTimeout_ms = 10000; |
ampembeng | 23:b9ff83dc965f | 321 | connectParams.tlsHandshakeTimeout_ms = 10000; |
ampembeng | 23:b9ff83dc965f | 322 | connectParams.isSSLHostnameVerify = true; // ensure this is set to true for production |
ampembeng | 23:b9ff83dc965f | 323 | connectParams.disconnectHandler = disconnectCallbackHandler; |
ampembeng | 23:b9ff83dc965f | 324 | |
ampembeng | 23:b9ff83dc965f | 325 | INFO("Connecting..."); |
ampembeng | 23:b9ff83dc965f | 326 | rc = aws_iot_mqtt_connect(&connectParams); |
ampembeng | 23:b9ff83dc965f | 327 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 328 | ERROR("Error(%d) connecting to %s:%d", rc, connectParams.pHostURL, connectParams.port); |
ampembeng | 23:b9ff83dc965f | 329 | } |
ampembeng | 23:b9ff83dc965f | 330 | |
ampembeng | 23:b9ff83dc965f | 331 | /* |
ampembeng | 23:b9ff83dc965f | 332 | * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 23:b9ff83dc965f | 333 | * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 23:b9ff83dc965f | 334 | * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 23:b9ff83dc965f | 335 | */ |
ampembeng | 23:b9ff83dc965f | 336 | INFO("Set Auto Reconnect..."); |
ampembeng | 23:b9ff83dc965f | 337 | rc = aws_iot_mqtt_autoreconnect_set_status(true); |
ampembeng | 23:b9ff83dc965f | 338 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 339 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 23:b9ff83dc965f | 340 | return rc; |
ampembeng | 23:b9ff83dc965f | 341 | } |
ampembeng | 23:b9ff83dc965f | 342 | |
ampembeng | 23:b9ff83dc965f | 343 | // Comment this in if you want to subscribe |
ampembeng | 23:b9ff83dc965f | 344 | /*MQTTSubscribeParams subParams = MQTTSubscribeParamsDefault; |
ampembeng | 23:b9ff83dc965f | 345 | subParams.mHandler = MQTTcallbackHandler; |
ampembeng | 23:b9ff83dc965f | 346 | subParams.pTopic = "sdkTest/sub"; |
ampembeng | 23:b9ff83dc965f | 347 | subParams.qos = QOS_0; |
ampembeng | 23:b9ff83dc965f | 348 | |
ampembeng | 23:b9ff83dc965f | 349 | if (NONE_ERROR == rc) { |
ampembeng | 23:b9ff83dc965f | 350 | INFO("Subscribing..."); |
ampembeng | 23:b9ff83dc965f | 351 | rc = aws_iot_mqtt_subscribe(&subParams); |
ampembeng | 23:b9ff83dc965f | 352 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 353 | ERROR("Error subscribing"); |
ampembeng | 23:b9ff83dc965f | 354 | } |
ampembeng | 23:b9ff83dc965f | 355 | }*/ |
ampembeng | 23:b9ff83dc965f | 356 | |
ampembeng | 23:b9ff83dc965f | 357 | // Initializ the payload |
ampembeng | 23:b9ff83dc965f | 358 | MQTTMessageParams Msg = MQTTMessageParamsDefault; |
ampembeng | 23:b9ff83dc965f | 359 | Msg.qos = QOS_0; |
ampembeng | 23:b9ff83dc965f | 360 | Msg.pPayload = (void *) cPayload; |
ampembeng | 23:b9ff83dc965f | 361 | |
ampembeng | 23:b9ff83dc965f | 362 | MQTTPublishParams Params = MQTTPublishParamsDefault; |
ampembeng | 23:b9ff83dc965f | 363 | |
ampembeng | 23:b9ff83dc965f | 364 | // Sets up the topic to publish to |
ampembeng | 23:b9ff83dc965f | 365 | sprintf(cTopic, AWS_IOT_MY_TOPIC, iccidName); |
ampembeng | 23:b9ff83dc965f | 366 | Params.pTopic = cTopic; |
ampembeng | 23:b9ff83dc965f | 367 | |
ampembeng | 23:b9ff83dc965f | 368 | if (publishCount != 0) { |
ampembeng | 23:b9ff83dc965f | 369 | infinitePublishFlag = false; |
ampembeng | 23:b9ff83dc965f | 370 | } |
ampembeng | 23:b9ff83dc965f | 371 | |
ampembeng | 23:b9ff83dc965f | 372 | INFO("READY TO PUBLISH! Press SW3 button to publish current data."); |
ampembeng | 23:b9ff83dc965f | 373 | while ((NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) |
ampembeng | 23:b9ff83dc965f | 374 | && (publishCount > 0 || infinitePublishFlag)) { |
ampembeng | 23:b9ff83dc965f | 375 | |
ampembeng | 23:b9ff83dc965f | 376 | // Max time the yield function will wait for read messages |
ampembeng | 23:b9ff83dc965f | 377 | rc = aws_iot_mqtt_yield(100); |
ampembeng | 23:b9ff83dc965f | 378 | if(NETWORK_ATTEMPTING_RECONNECT == rc){ |
ampembeng | 23:b9ff83dc965f | 379 | INFO("--> sleep (attempting to reconnect)"); |
ampembeng | 23:b9ff83dc965f | 380 | wait(1); |
ampembeng | 23:b9ff83dc965f | 381 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 23:b9ff83dc965f | 382 | continue; |
ampembeng | 23:b9ff83dc965f | 383 | } |
ampembeng | 23:b9ff83dc965f | 384 | |
ampembeng | 23:b9ff83dc965f | 385 | // Whenever the software button (SW3) is pressed the LED will changes color and this will |
ampembeng | 23:b9ff83dc965f | 386 | // trigger a publish to the AWS topic specified. |
ampembeng | 23:b9ff83dc965f | 387 | if (buttonOverride) { |
ampembeng | 23:b9ff83dc965f | 388 | buttonOverride = false; |
ampembeng | 23:b9ff83dc965f | 389 | |
ampembeng | 23:b9ff83dc965f | 390 | // Get temp/humidity values |
peyo | 29:537df716ba0b | 391 | temp_sensor1->get_temperature(&iks01a1_temperature); |
peyo | 29:537df716ba0b | 392 | humidity_sensor->get_humidity(&iks01a1_humidity); |
peyo | 29:537df716ba0b | 393 | pressure_sensor->get_pressure(&iks01a1_pressure); |
ampembeng | 23:b9ff83dc965f | 394 | |
ampembeng | 23:b9ff83dc965f | 395 | // Loading data into JSON format |
peyo | 29:537df716ba0b | 396 | sprintf(cPayload, "{\"color\":\"%s\",\"temperature\":%f,\"humidity\":%f,\"pressure\":%f}", colorStrings[ledColor], iks01a1_temperature, iks01a1_humidity, iks01a1_pressure); |
ampembeng | 23:b9ff83dc965f | 397 | Msg.PayloadLen = strlen(cPayload) + 1; |
ampembeng | 23:b9ff83dc965f | 398 | Params.MessageParams = Msg; |
ampembeng | 23:b9ff83dc965f | 399 | |
ampembeng | 23:b9ff83dc965f | 400 | // Publish |
ampembeng | 23:b9ff83dc965f | 401 | rc = aws_iot_mqtt_publish(&Params); |
ampembeng | 23:b9ff83dc965f | 402 | if (publishCount > 0) { |
ampembeng | 23:b9ff83dc965f | 403 | publishCount--; |
ampembeng | 23:b9ff83dc965f | 404 | } |
ampembeng | 23:b9ff83dc965f | 405 | |
ampembeng | 23:b9ff83dc965f | 406 | printData(); |
ampembeng | 23:b9ff83dc965f | 407 | INFO("--> Update sent. Sleep for %f seconds", updateInterval); |
ampembeng | 23:b9ff83dc965f | 408 | wait(updateInterval-.02); |
ampembeng | 23:b9ff83dc965f | 409 | } |
ampembeng | 23:b9ff83dc965f | 410 | else { |
ampembeng | 23:b9ff83dc965f | 411 | wait(.3); // 300 ms |
ampembeng | 23:b9ff83dc965f | 412 | } |
ampembeng | 23:b9ff83dc965f | 413 | } |
ampembeng | 23:b9ff83dc965f | 414 | |
ampembeng | 23:b9ff83dc965f | 415 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 416 | ERROR("An error occurred in the loop.\n"); |
ampembeng | 23:b9ff83dc965f | 417 | } else { |
ampembeng | 23:b9ff83dc965f | 418 | INFO("Publish done\n"); |
ampembeng | 23:b9ff83dc965f | 419 | } |
ampembeng | 23:b9ff83dc965f | 420 | |
ampembeng | 23:b9ff83dc965f | 421 | return rc; |
ampembeng | 23:b9ff83dc965f | 422 | } |
ampembeng | 15:6f2798e45099 | 423 | |
ampembeng | 15:6f2798e45099 | 424 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 425 | // |
ampembeng | 15:6f2798e45099 | 426 | // Main |
ampembeng | 15:6f2798e45099 | 427 | // |
ampembeng | 15:6f2798e45099 | 428 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 429 | int main() { |
ampembeng | 15:6f2798e45099 | 430 | |
ampembeng | 15:6f2798e45099 | 431 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 432 | pc.baud(115200); |
ampembeng | 15:6f2798e45099 | 433 | INFO("Hello World from AT&T IoT Start Kit demo!"); |
ampembeng | 20:ee34856ae510 | 434 | |
ampembeng | 20:ee34856ae510 | 435 | int i; |
ampembeng | 23:b9ff83dc965f | 436 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 437 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 438 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 439 | |
ampembeng | 15:6f2798e45099 | 440 | // JSON struct for LED control |
ampembeng | 15:6f2798e45099 | 441 | jsonStruct_t ledController; |
ampembeng | 15:6f2798e45099 | 442 | ledController.cb = ledControl_Callback; |
ampembeng | 15:6f2798e45099 | 443 | ledController.pData = &ledColor; |
ampembeng | 15:6f2798e45099 | 444 | ledController.pKey = "ledColor"; |
ampembeng | 15:6f2798e45099 | 445 | ledController.type = SHADOW_JSON_UINT8; |
ampembeng | 15:6f2798e45099 | 446 | |
peyo | 29:537df716ba0b | 447 | // JSON struct for temperature\humidity\pressure readings |
ampembeng | 15:6f2798e45099 | 448 | jsonStruct_t temperatureHandler; |
ampembeng | 15:6f2798e45099 | 449 | temperatureHandler.cb = NULL; |
ampembeng | 15:6f2798e45099 | 450 | temperatureHandler.pKey = "temperature"; |
peyo | 29:537df716ba0b | 451 | temperatureHandler.pData = &iks01a1_temperature; |
ampembeng | 15:6f2798e45099 | 452 | temperatureHandler.type = SHADOW_JSON_FLOAT; |
ampembeng | 20:ee34856ae510 | 453 | |
ampembeng | 20:ee34856ae510 | 454 | jsonStruct_t humidityHandler; |
ampembeng | 20:ee34856ae510 | 455 | humidityHandler.cb = NULL; |
ampembeng | 20:ee34856ae510 | 456 | humidityHandler.pKey = "humidity"; |
peyo | 29:537df716ba0b | 457 | humidityHandler.pData = &iks01a1_humidity; |
peyo | 29:537df716ba0b | 458 | humidityHandler.type = SHADOW_JSON_FLOAT; |
peyo | 29:537df716ba0b | 459 | |
peyo | 29:537df716ba0b | 460 | jsonStruct_t pressureHandler; |
peyo | 29:537df716ba0b | 461 | pressureHandler.cb = NULL; |
peyo | 29:537df716ba0b | 462 | pressureHandler.pKey = "pressure"; |
peyo | 29:537df716ba0b | 463 | pressureHandler.pData = &iks01a1_pressure; |
peyo | 29:537df716ba0b | 464 | pressureHandler.type = SHADOW_JSON_FLOAT; |
ampembeng | 15:6f2798e45099 | 465 | |
ampembeng | 15:6f2798e45099 | 466 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
ampembeng | 15:6f2798e45099 | 467 | |
ampembeng | 18:6370da1de572 | 468 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 469 | // Paths for certs from SD card |
ampembeng | 18:6370da1de572 | 470 | INFO("Using SD card files for AWS config."); |
ampembeng | 18:6370da1de572 | 471 | DEBUG("- mqtt config path: %s", AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 472 | DEBUG("- rootCA path: %s", AWS_IOT_ROOT_CA_FILENAME); |
ampembeng | 18:6370da1de572 | 473 | DEBUG("- clientCRT path: %s", AWS_IOT_CERTIFICATE_FILENAME); |
ampembeng | 18:6370da1de572 | 474 | DEBUG("- clientKey path: %s", AWS_IOT_PRIVATE_KEY_FILENAME); |
ampembeng | 18:6370da1de572 | 475 | #else |
ampembeng | 18:6370da1de572 | 476 | INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
ampembeng | 18:6370da1de572 | 477 | #endif |
ampembeng | 15:6f2798e45099 | 478 | |
ampembeng | 16:02008a2a2569 | 479 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 480 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 481 | wait(.5); |
ampembeng | 15:6f2798e45099 | 482 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 483 | wait(.5); |
ampembeng | 15:6f2798e45099 | 484 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 485 | wait(.5); |
ampembeng | 16:02008a2a2569 | 486 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 487 | wait(.5); |
ampembeng | 16:02008a2a2569 | 488 | SetLedColor(COLOR_OFF); |
ampembeng | 20:ee34856ae510 | 489 | |
ampembeng | 20:ee34856ae510 | 490 | // Initialize sensors |
ampembeng | 23:b9ff83dc965f | 491 | INFO("Init sensors..."); |
peyo | 29:537df716ba0b | 492 | humidity_sensor->read_id(&id); |
peyo | 29:537df716ba0b | 493 | printf("HTS221 humidity & temperature = 0x%X\r\n", id); |
peyo | 29:537df716ba0b | 494 | pressure_sensor->read_id(&id); |
peyo | 29:537df716ba0b | 495 | printf("LPS25H pressure & temperature = 0x%X\r\n", id); |
ampembeng | 18:6370da1de572 | 496 | |
ampembeng | 15:6f2798e45099 | 497 | // Setup SW3 button to falling edge interrupt |
ampembeng | 23:b9ff83dc965f | 498 | INFO("Init interrupts..."); |
ampembeng | 15:6f2798e45099 | 499 | Interrupt.fall(&sw3ButtonHandler); |
ampembeng | 15:6f2798e45099 | 500 | |
ampembeng | 15:6f2798e45099 | 501 | // Boot the Avnet Shield before any other operations |
ampembeng | 23:b9ff83dc965f | 502 | INFO("Net Boot..."); |
ampembeng | 15:6f2798e45099 | 503 | net_modem_boot(); |
ampembeng | 23:b9ff83dc965f | 504 | |
ampembeng | 23:b9ff83dc965f | 505 | //========================================================================== |
ampembeng | 23:b9ff83dc965f | 506 | // NOTE: You can comment in the following line for an alternate demo that |
ampembeng | 23:b9ff83dc965f | 507 | // is used as the out-of-box demo binary that comes with the AT&T IoT |
ampembeng | 23:b9ff83dc965f | 508 | // Starter Kit. It loops instead of the rest of Main() |
ampembeng | 23:b9ff83dc965f | 509 | //return outOfBoxDemo(); |
ampembeng | 23:b9ff83dc965f | 510 | //========================================================================== |
ampembeng | 23:b9ff83dc965f | 511 | |
ampembeng | 18:6370da1de572 | 512 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 513 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 514 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 515 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 516 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 517 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 518 | return rc; |
ampembeng | 18:6370da1de572 | 519 | } |
ampembeng | 18:6370da1de572 | 520 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 521 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 522 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 523 | #else |
ampembeng | 15:6f2798e45099 | 524 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 525 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 526 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 527 | sp.port = port; |
ampembeng | 23:b9ff83dc965f | 528 | |
ampembeng | 23:b9ff83dc965f | 529 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 530 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 531 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 532 | #endif |
ampembeng | 20:ee34856ae510 | 533 | |
ampembeng | 18:6370da1de572 | 534 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 535 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 536 | aws_iot_mqtt_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 537 | |
ampembeng | 15:6f2798e45099 | 538 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 539 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 540 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 541 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 542 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 543 | } |
ampembeng | 15:6f2798e45099 | 544 | |
ampembeng | 15:6f2798e45099 | 545 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 546 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 547 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 548 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 549 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 550 | } |
Janos Follath |
0:fc70c47eecb4 | 551 | |
ampembeng | 15:6f2798e45099 | 552 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 553 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 554 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 555 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 556 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 557 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 15:6f2798e45099 | 558 | return rc; |
ampembeng | 15:6f2798e45099 | 559 | } |
ampembeng | 15:6f2798e45099 | 560 | |
ampembeng | 15:6f2798e45099 | 561 | // Example line of how to delete a shadow (not used in this demo) |
ampembeng | 15:6f2798e45099 | 562 | //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true); |
Janos Follath |
0:fc70c47eecb4 | 563 | |
ampembeng | 15:6f2798e45099 | 564 | INFO("Shadow Register Delta..."); |
ampembeng | 15:6f2798e45099 | 565 | rc = aws_iot_shadow_register_delta(&mqttClient, &ledController); |
ampembeng | 15:6f2798e45099 | 566 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 567 | ERROR("Shadow Register Delta Error"); |
ampembeng | 15:6f2798e45099 | 568 | return rc; |
ampembeng | 15:6f2798e45099 | 569 | } |
ampembeng | 15:6f2798e45099 | 570 | |
ampembeng | 15:6f2798e45099 | 571 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 572 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 573 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 574 | |
ampembeng | 15:6f2798e45099 | 575 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 576 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 577 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 578 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 579 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 580 | wait(1); |
ampembeng | 15:6f2798e45099 | 581 | continue; |
ampembeng | 15:6f2798e45099 | 582 | } |
ampembeng | 20:ee34856ae510 | 583 | |
ampembeng | 20:ee34856ae510 | 584 | // Read sensor data |
peyo | 29:537df716ba0b | 585 | temp_sensor1->get_temperature(&iks01a1_temperature); |
peyo | 29:537df716ba0b | 586 | humidity_sensor->get_humidity(&iks01a1_humidity); |
peyo | 29:537df716ba0b | 587 | pressure_sensor->get_pressure(&iks01a1_pressure); |
ampembeng | 20:ee34856ae510 | 588 | |
ampembeng | 15:6f2798e45099 | 589 | INFO("\n=======================================================================================\n"); |
ampembeng | 15:6f2798e45099 | 590 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 591 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 592 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 593 | |
ampembeng | 15:6f2798e45099 | 594 | // If there has been a SW3 button press update the 'desired' color |
ampembeng | 15:6f2798e45099 | 595 | if (buttonOverride) { |
ampembeng | 23:b9ff83dc965f | 596 | rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 597 | buttonOverride = false; |
Janos Follath |
0:fc70c47eecb4 | 598 | } |
ampembeng | 15:6f2798e45099 | 599 | |
peyo | 29:537df716ba0b | 600 | // Updates the 'reported' color/temp/humidity/pressure |
peyo | 29:537df716ba0b | 601 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 4, &ledController, |
ampembeng | 20:ee34856ae510 | 602 | &temperatureHandler, |
peyo | 29:537df716ba0b | 603 | &humidityHandler, |
peyo | 29:537df716ba0b | 604 | &pressureHandler); |
ampembeng | 15:6f2798e45099 | 605 | |
ampembeng | 15:6f2798e45099 | 606 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 607 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 608 | |
ampembeng | 15:6f2798e45099 | 609 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 610 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 18:6370da1de572 | 611 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
ampembeng | 15:6f2798e45099 | 612 | ShadowUpdateStatusCallback, NULL, 8, true); |
ampembeng | 15:6f2798e45099 | 613 | } |
ampembeng | 15:6f2798e45099 | 614 | } |
ampembeng | 20:ee34856ae510 | 615 | } |
ampembeng | 20:ee34856ae510 | 616 | |
ampembeng | 20:ee34856ae510 | 617 | // Print data |
ampembeng | 23:b9ff83dc965f | 618 | printData(); |
ampembeng | 18:6370da1de572 | 619 | INFO("*****************************************************************************************"); |
ampembeng | 20:ee34856ae510 | 620 | |
ampembeng | 20:ee34856ae510 | 621 | // Set the LED color |
ampembeng | 15:6f2798e45099 | 622 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 623 | wait(SHADOW_SYNC_INTERVAL); |
Janos Follath |
0:fc70c47eecb4 | 624 | } |
Janos Follath |
0:fc70c47eecb4 | 625 | |
ampembeng | 15:6f2798e45099 | 626 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 627 | ERROR("An error occurred in the loop %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 628 | } |
Janos Follath |
0:fc70c47eecb4 | 629 | |
ampembeng | 15:6f2798e45099 | 630 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 631 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 632 | |
ampembeng | 15:6f2798e45099 | 633 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 634 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 635 | } |
Janos Follath |
0:fc70c47eecb4 | 636 | |
ampembeng | 15:6f2798e45099 | 637 | return rc; |
ampembeng | 15:6f2798e45099 | 638 | } |