Demo application for using the AT&T IoT Starter Kit Powered by AWS.

Dependencies:   SDFileSystem

Fork of ATT_AWS_IoT_demo by Anthony Phillips

IoT Starter Kit Powered by AWS Demo

This program demonstrates the AT&T IoT Starter Kit sending data directly into AWS IoT. It's explained and used in the Getting Started with the IoT Starter Kit Powered by AWS on starterkit.att.com.

What's required

  • AT&T IoT LTE Add-on (also known as the Cellular Shield)
  • NXP K64F - for programming
  • microSD card - used to store your AWS security credentials
  • AWS account
  • Python, locally installed

If you don't already have an IoT Starter Kit, you can purchase a kit here. The IoT Starter Kit Powered by AWS includes the LTE cellular shield, K64F, and a microSD card.

Committer:
ampembeng
Date:
Thu Dec 01 18:05:38 2016 +0000
Revision:
15:6f2798e45099
Parent:
12:1ae41c231014
Child:
16:02008a2a2569
Initial commit.  Demo works with both the FRDM wired Ethernet and the Avnet Shield wireless modem.

Who changed what in which revision?

UserRevisionLine numberNew 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 15:6f2798e45099 6 // Serial extension
ampembeng 15:6f2798e45099 7 #include "MODSERIAL.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"
Janos Follath 0:fc70c47eecb4 12
ampembeng 15:6f2798e45099 13 // AWS includes
ampembeng 15:6f2798e45099 14 #include "aws_iot_log.h"
ampembeng 15:6f2798e45099 15 #include "aws_iot_version.h"
ampembeng 15:6f2798e45099 16 #include "aws_iot_shadow_interface.h"
ampembeng 15:6f2798e45099 17 #include "aws_iot_shadow_json_data.h"
ampembeng 15:6f2798e45099 18 #include "aws_iot_config.h"
ampembeng 15:6f2798e45099 19 #include "aws_iot_mqtt_interface.h"
mbed_official 12:1ae41c231014 20
Janos Follath 0:fc70c47eecb4 21 #if DEBUG_LEVEL > 0
Janos Follath 0:fc70c47eecb4 22 #include "mbedtls/debug.h"
Janos Follath 0:fc70c47eecb4 23 #endif
Janos Follath 0:fc70c47eecb4 24
ampembeng 15:6f2798e45099 25 //=====================================================================================================================
ampembeng 15:6f2798e45099 26 //
ampembeng 15:6f2798e45099 27 // Defines
ampembeng 15:6f2798e45099 28 //
ampembeng 15:6f2798e45099 29 //=====================================================================================================================
ampembeng 15:6f2798e45099 30 // LED Colors
ampembeng 15:6f2798e45099 31 #define COLOR_OFF 0x00
ampembeng 15:6f2798e45099 32 #define COLOR_RED 0x01
ampembeng 15:6f2798e45099 33 #define COLOR_GREEN 0x02
ampembeng 15:6f2798e45099 34 #define COLOR_BLUE 0x04
ampembeng 15:6f2798e45099 35 #define COLOR_WHITE 0x07
ampembeng 15:6f2798e45099 36 #define NUM_COLORS 5
Janos Follath 0:fc70c47eecb4 37
ampembeng 15:6f2798e45099 38 // AWS defines
ampembeng 15:6f2798e45099 39 #define PATH_MAX 4096
ampembeng 15:6f2798e45099 40 #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // NOTE: Be wary of this if your JSON doc grows
ampembeng 15:6f2798e45099 41 #define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds)
Janos Follath 0:fc70c47eecb4 42
ampembeng 15:6f2798e45099 43 // Comment out the following line if color is not supported on the terminal
ampembeng 15:6f2798e45099 44 //#define USE_COLOR
ampembeng 15:6f2798e45099 45 #ifdef USE_COLOR
ampembeng 15:6f2798e45099 46 #define BLK "\033[30m"
ampembeng 15:6f2798e45099 47 #define RED "\033[31m"
ampembeng 15:6f2798e45099 48 #define GRN "\033[32m"
ampembeng 15:6f2798e45099 49 #define YEL "\033[33m"
ampembeng 15:6f2798e45099 50 #define BLU "\033[34m"
ampembeng 15:6f2798e45099 51 #define MAG "\033[35m"
ampembeng 15:6f2798e45099 52 #define CYN "\033[36m"
ampembeng 15:6f2798e45099 53 #define WHT "\033[37m"
ampembeng 15:6f2798e45099 54 #define DEF "\033[39m"
ampembeng 15:6f2798e45099 55 #else
ampembeng 15:6f2798e45099 56 #define BLK
ampembeng 15:6f2798e45099 57 #define RED
ampembeng 15:6f2798e45099 58 #define GRN
ampembeng 15:6f2798e45099 59 #define YEL
ampembeng 15:6f2798e45099 60 #define BLU
ampembeng 15:6f2798e45099 61 #define MAG
ampembeng 15:6f2798e45099 62 #define CYN
ampembeng 15:6f2798e45099 63 #define WHT
ampembeng 15:6f2798e45099 64 #define DEF
Janos Follath 0:fc70c47eecb4 65 #endif
Janos Follath 0:fc70c47eecb4 66
ampembeng 15:6f2798e45099 67 //=====================================================================================================================
ampembeng 15:6f2798e45099 68 //
ampembeng 15:6f2798e45099 69 // Globals
ampembeng 15:6f2798e45099 70 //
ampembeng 15:6f2798e45099 71 //=====================================================================================================================
ampembeng 15:6f2798e45099 72 // Controls LED color
ampembeng 15:6f2798e45099 73 unsigned char ledColor = COLOR_OFF;
ampembeng 15:6f2798e45099 74
ampembeng 15:6f2798e45099 75 // Color cycle array (used with SW3 button presses)
ampembeng 15:6f2798e45099 76 unsigned char colorCycle[NUM_COLORS] = {COLOR_OFF, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE};
Janos Follath 0:fc70c47eecb4 77
ampembeng 15:6f2798e45099 78 // Button interrupts
ampembeng 15:6f2798e45099 79 bool buttonOverride = false;
ampembeng 15:6f2798e45099 80 InterruptIn Interrupt(SW3);
ampembeng 15:6f2798e45099 81
ampembeng 15:6f2798e45099 82 // Default cert location
ampembeng 15:6f2798e45099 83 //char certDirectory[PATH_MAX + 1] = "../../../certs";
ampembeng 15:6f2798e45099 84
ampembeng 15:6f2798e45099 85 // Default MQTT HOST URL is pulled from the aws_iot_config.h
ampembeng 15:6f2798e45099 86 char HostAddress[255] = AWS_IOT_MQTT_HOST;
ampembeng 15:6f2798e45099 87
ampembeng 15:6f2798e45099 88 // Default MQTT port is pulled from the aws_iot_config.h
ampembeng 15:6f2798e45099 89 uint32_t port = AWS_IOT_MQTT_PORT;
Janos Follath 0:fc70c47eecb4 90
ampembeng 15:6f2798e45099 91 //=====================================================================================================================
ampembeng 15:6f2798e45099 92 //
ampembeng 15:6f2798e45099 93 // Devices
ampembeng 15:6f2798e45099 94 //
ampembeng 15:6f2798e45099 95 //=====================================================================================================================
ampembeng 15:6f2798e45099 96 // GPIOs for RGB LED
ampembeng 15:6f2798e45099 97 DigitalOut led_green(LED_GREEN);
ampembeng 15:6f2798e45099 98 DigitalOut led_red(LED_RED);
ampembeng 15:6f2798e45099 99 DigitalOut led_blue(LED_BLUE);
Janos Follath 0:fc70c47eecb4 100
ampembeng 15:6f2798e45099 101 // USB Serial port (to PC)
ampembeng 15:6f2798e45099 102 MODSERIAL pc(USBTX,USBRX,256,256);
Janos Follath 0:fc70c47eecb4 103
ampembeng 15:6f2798e45099 104 //=====================================================================================================================
ampembeng 15:6f2798e45099 105 //
ampembeng 15:6f2798e45099 106 // Functions
ampembeng 15:6f2798e45099 107 //
ampembeng 15:6f2798e45099 108 //=====================================================================================================================
ampembeng 15:6f2798e45099 109 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 110 //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h
ampembeng 15:6f2798e45099 111 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 112 void pc_print(const char * format, ...)
ampembeng 15:6f2798e45099 113 {
ampembeng 15:6f2798e45099 114 va_list vl;
ampembeng 15:6f2798e45099 115 va_start(vl, format);
ampembeng 15:6f2798e45099 116 pc.vprintf(format, vl);
ampembeng 15:6f2798e45099 117 va_end(vl);
ampembeng 15:6f2798e45099 118 }
Janos Follath 0:fc70c47eecb4 119
ampembeng 15:6f2798e45099 120 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 121 //* Set the RGB LED's Color
ampembeng 15:6f2798e45099 122 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
ampembeng 15:6f2798e45099 123 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 124 void SetLedColor(unsigned char ucColor)
ampembeng 15:6f2798e45099 125 {
ampembeng 15:6f2798e45099 126 //Note that when an LED is on, you write a 0 to it:
ampembeng 15:6f2798e45099 127 led_red = !(ucColor & 0x1); //bit 0
ampembeng 15:6f2798e45099 128 led_green = !(ucColor & 0x2); //bit 1
ampembeng 15:6f2798e45099 129 led_blue = !(ucColor & 0x4); //bit 2
ampembeng 15:6f2798e45099 130 }
Janos Follath 0:fc70c47eecb4 131
ampembeng 15:6f2798e45099 132 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 133 //* SW3 Button handler. Finds the current LED color and sets the button to the next color in colorCycle[]
ampembeng 15:6f2798e45099 134 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 135 void sw3ButtonHandler()
ampembeng 15:6f2798e45099 136 {
ampembeng 15:6f2798e45099 137 int i;
ampembeng 15:6f2798e45099 138 for(i=0; i < NUM_COLORS; i++) {
ampembeng 15:6f2798e45099 139 if (ledColor == colorCycle[i])
ampembeng 15:6f2798e45099 140 break;
ampembeng 15:6f2798e45099 141 }
ampembeng 15:6f2798e45099 142
ampembeng 15:6f2798e45099 143 // (circular-queue)
ampembeng 15:6f2798e45099 144 if (++i == NUM_COLORS)
ampembeng 15:6f2798e45099 145 i = 0;
ampembeng 15:6f2798e45099 146
ampembeng 15:6f2798e45099 147 ledColor = colorCycle[i];
ampembeng 15:6f2798e45099 148 SetLedColor(ledColor);
ampembeng 15:6f2798e45099 149 buttonOverride = true;
ampembeng 15:6f2798e45099 150 }
Janos Follath 0:fc70c47eecb4 151
ampembeng 15:6f2798e45099 152 //=====================================================================================================================
ampembeng 15:6f2798e45099 153 //
ampembeng 15:6f2798e45099 154 // AWS Shadow Callbacks
ampembeng 15:6f2798e45099 155 //
ampembeng 15:6f2798e45099 156 //=====================================================================================================================
ampembeng 15:6f2798e45099 157 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 158 //* This is the callback function that fires when an update is sent. It will print the update response status.
ampembeng 15:6f2798e45099 159 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 160 void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status,
ampembeng 15:6f2798e45099 161 const char *pReceivedJsonDocument, void *pContextData) {
Janos Follath 0:fc70c47eecb4 162
ampembeng 15:6f2798e45099 163 INFO("Shadow Update Status Callback");
ampembeng 15:6f2798e45099 164
ampembeng 15:6f2798e45099 165 if (status == SHADOW_ACK_TIMEOUT) {
ampembeng 15:6f2798e45099 166 INFO("Update Timeout--");
ampembeng 15:6f2798e45099 167 } else if (status == SHADOW_ACK_REJECTED) {
ampembeng 15:6f2798e45099 168 INFO("Update RejectedXX");
ampembeng 15:6f2798e45099 169 } else if (status == SHADOW_ACK_ACCEPTED) {
ampembeng 15:6f2798e45099 170 INFO("Update Accepted!!"); // Good
ampembeng 15:6f2798e45099 171 }
ampembeng 15:6f2798e45099 172 }
Janos Follath 0:fc70c47eecb4 173
ampembeng 15:6f2798e45099 174 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 175 //* This is the callback function that fires when AWS has sends out a shadow update.
ampembeng 15:6f2798e45099 176 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 177 void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) {
ampembeng 15:6f2798e45099 178
ampembeng 15:6f2798e45099 179 INFO("LED Callback Detected.");
ampembeng 15:6f2798e45099 180
ampembeng 15:6f2798e45099 181 if (pContext != NULL) {
ampembeng 15:6f2798e45099 182 switch (*(unsigned char *)(pContext->pData)){
ampembeng 15:6f2798e45099 183 case COLOR_OFF:
ampembeng 15:6f2798e45099 184 INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 185 break;
ampembeng 15:6f2798e45099 186 case COLOR_RED:
ampembeng 15:6f2798e45099 187 INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 188 break;
ampembeng 15:6f2798e45099 189 case COLOR_GREEN:
ampembeng 15:6f2798e45099 190 INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 191 break;
ampembeng 15:6f2798e45099 192 case COLOR_BLUE:
ampembeng 15:6f2798e45099 193 INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 194 break;
ampembeng 15:6f2798e45099 195 case COLOR_WHITE:
ampembeng 15:6f2798e45099 196 INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 197 break;
Janos Follath 0:fc70c47eecb4 198 }
ampembeng 15:6f2798e45099 199 }
ampembeng 15:6f2798e45099 200 else {
ampembeng 15:6f2798e45099 201 INFO("pContext was detected as NULL");
Janos Follath 0:fc70c47eecb4 202 }
ampembeng 15:6f2798e45099 203 }
ampembeng 15:6f2798e45099 204
ampembeng 15:6f2798e45099 205 //=====================================================================================================================
ampembeng 15:6f2798e45099 206 //
ampembeng 15:6f2798e45099 207 // Main
ampembeng 15:6f2798e45099 208 //
ampembeng 15:6f2798e45099 209 //=====================================================================================================================
ampembeng 15:6f2798e45099 210 int main() {
ampembeng 15:6f2798e45099 211
ampembeng 15:6f2798e45099 212 // Set baud rate for PC Serial
ampembeng 15:6f2798e45099 213 pc.baud(115200);
ampembeng 15:6f2798e45099 214 INFO("Hello World from AT&T IoT Start Kit demo!");
ampembeng 15:6f2798e45099 215
ampembeng 15:6f2798e45099 216 IoT_Error_t rc = NONE_ERROR;
ampembeng 15:6f2798e45099 217 char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
ampembeng 15:6f2798e45099 218 size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]);
ampembeng 15:6f2798e45099 219
ampembeng 15:6f2798e45099 220 // JSON struct for LED control
ampembeng 15:6f2798e45099 221 jsonStruct_t ledController;
ampembeng 15:6f2798e45099 222 ledController.cb = ledControl_Callback;
ampembeng 15:6f2798e45099 223 ledController.pData = &ledColor;
ampembeng 15:6f2798e45099 224 ledController.pKey = "ledColor";
ampembeng 15:6f2798e45099 225 ledController.type = SHADOW_JSON_UINT8;
ampembeng 15:6f2798e45099 226
ampembeng 15:6f2798e45099 227 // TODO Add FRDM temperature reading
ampembeng 15:6f2798e45099 228 /*
ampembeng 15:6f2798e45099 229 float temperature = 0.0;
ampembeng 15:6f2798e45099 230 jsonStruct_t temperatureHandler;
ampembeng 15:6f2798e45099 231 temperatureHandler.cb = NULL;
ampembeng 15:6f2798e45099 232 temperatureHandler.pKey = "temperature";
ampembeng 15:6f2798e45099 233 temperatureHandler.pData = &temperature;
ampembeng 15:6f2798e45099 234 temperatureHandler.type = SHADOW_JSON_FLOAT;
ampembeng 15:6f2798e45099 235 */
ampembeng 15:6f2798e45099 236
ampembeng 15:6f2798e45099 237 INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
ampembeng 15:6f2798e45099 238
ampembeng 15:6f2798e45099 239 // TODO: We could try to pull the certs from an SD card
ampembeng 15:6f2798e45099 240 /*
ampembeng 15:6f2798e45099 241 char rootCA[PATH_MAX + 1];
ampembeng 15:6f2798e45099 242 char clientCRT[PATH_MAX + 1];
ampembeng 15:6f2798e45099 243 char clientKey[PATH_MAX + 1];
ampembeng 15:6f2798e45099 244 char CurrentWD[PATH_MAX + 1];
ampembeng 15:6f2798e45099 245 char cafileName[] = AWS_IOT_ROOT_CA_FILENAME;
ampembeng 15:6f2798e45099 246 char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME;
ampembeng 15:6f2798e45099 247 char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME;
ampembeng 15:6f2798e45099 248
ampembeng 15:6f2798e45099 249 getcwd(CurrentWD, sizeof(CurrentWD));
ampembeng 15:6f2798e45099 250 sprintf(rootCA, "%s/%s/%s", CurrentWD, certDirectory, cafileName);
ampembeng 15:6f2798e45099 251 sprintf(clientCRT, "%s/%s/%s", CurrentWD, certDirectory, clientCRTName);
ampembeng 15:6f2798e45099 252 sprintf(clientKey, "%s/%s/%s", CurrentWD, certDirectory, clientKeyName);
ampembeng 15:6f2798e45099 253
ampembeng 15:6f2798e45099 254 DEBUG("Using rootCA %s", rootCA);
ampembeng 15:6f2798e45099 255 DEBUG("Using clientCRT %s", clientCRT);
ampembeng 15:6f2798e45099 256 DEBUG("Using clientKey %s", clientKey);
ampembeng 15:6f2798e45099 257 */
ampembeng 15:6f2798e45099 258
ampembeng 15:6f2798e45099 259 // Blinks through RGB then turns off
ampembeng 15:6f2798e45099 260 SetLedColor(COLOR_RED);
ampembeng 15:6f2798e45099 261 wait(.5);
ampembeng 15:6f2798e45099 262 SetLedColor(COLOR_GREEN);
ampembeng 15:6f2798e45099 263 wait(.5);
ampembeng 15:6f2798e45099 264 SetLedColor(COLOR_BLUE);
ampembeng 15:6f2798e45099 265 wait(.5);
ampembeng 15:6f2798e45099 266 SetLedColor(COLOR_OFF);
ampembeng 15:6f2798e45099 267
ampembeng 15:6f2798e45099 268 // Setup SW3 button to falling edge interrupt
ampembeng 15:6f2798e45099 269 Interrupt.fall(&sw3ButtonHandler);
ampembeng 15:6f2798e45099 270
ampembeng 15:6f2798e45099 271 // Boot the Avnet Shield before any other operations
ampembeng 15:6f2798e45099 272 net_modem_boot();
ampembeng 15:6f2798e45099 273
ampembeng 15:6f2798e45099 274 INFO("Initialize the MQTT client...");
ampembeng 15:6f2798e45099 275 MQTTClient_t mqttClient;
ampembeng 15:6f2798e45099 276 aws_iot_mqtt_init(&mqttClient);
ampembeng 15:6f2798e45099 277
ampembeng 15:6f2798e45099 278 ShadowParameters_t sp = ShadowParametersDefault;
ampembeng 15:6f2798e45099 279 sp.pMyThingName = AWS_IOT_MY_THING_NAME;
ampembeng 15:6f2798e45099 280 sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID;
ampembeng 15:6f2798e45099 281 sp.pHost = HostAddress;
ampembeng 15:6f2798e45099 282 sp.port = port;
ampembeng 15:6f2798e45099 283 //sp.pClientCRT = clientCRT;
ampembeng 15:6f2798e45099 284 //sp.pClientKey = clientKey;
ampembeng 15:6f2798e45099 285 //sp.pRootCA = rootCA;
ampembeng 15:6f2798e45099 286 sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME;
ampembeng 15:6f2798e45099 287 sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME;
ampembeng 15:6f2798e45099 288 sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME;
ampembeng 15:6f2798e45099 289
ampembeng 15:6f2798e45099 290 INFO("Shadow Init...");
ampembeng 15:6f2798e45099 291 rc = aws_iot_shadow_init(&mqttClient);
ampembeng 15:6f2798e45099 292 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 293 ERROR("Shadow Init Error %d", rc);
ampembeng 15:6f2798e45099 294 return rc;
Janos Follath 0:fc70c47eecb4 295 }
ampembeng 15:6f2798e45099 296
ampembeng 15:6f2798e45099 297 INFO("Shadow Connect...");
ampembeng 15:6f2798e45099 298 rc = aws_iot_shadow_connect(&mqttClient, &sp);
ampembeng 15:6f2798e45099 299 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 300 ERROR("Shadow Connection Error %d", rc);
ampembeng 15:6f2798e45099 301 return rc;
Janos Follath 0:fc70c47eecb4 302 }
Janos Follath 0:fc70c47eecb4 303
ampembeng 15:6f2798e45099 304 // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h
ampembeng 15:6f2798e45099 305 // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
ampembeng 15:6f2798e45099 306 // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
ampembeng 15:6f2798e45099 307 rc = mqttClient.setAutoReconnectStatus(true);
ampembeng 15:6f2798e45099 308 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 309 ERROR("Unable to set Auto Reconnect to true - %d", rc);
ampembeng 15:6f2798e45099 310 return rc;
ampembeng 15:6f2798e45099 311 }
ampembeng 15:6f2798e45099 312
ampembeng 15:6f2798e45099 313 // Example line of how to delete a shadow (not used in this demo)
ampembeng 15:6f2798e45099 314 //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true);
Janos Follath 0:fc70c47eecb4 315
ampembeng 15:6f2798e45099 316 INFO("Shadow Register Delta...");
ampembeng 15:6f2798e45099 317 rc = aws_iot_shadow_register_delta(&mqttClient, &ledController);
ampembeng 15:6f2798e45099 318 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 319 ERROR("Shadow Register Delta Error");
ampembeng 15:6f2798e45099 320 return rc;
ampembeng 15:6f2798e45099 321 }
ampembeng 15:6f2798e45099 322
ampembeng 15:6f2798e45099 323 INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL);
ampembeng 15:6f2798e45099 324 // Loop and publish changes from the FRDM board
ampembeng 15:6f2798e45099 325 while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) {
ampembeng 15:6f2798e45099 326
ampembeng 15:6f2798e45099 327 // Looks for incoming socket messages
ampembeng 15:6f2798e45099 328 rc = aws_iot_shadow_yield(&mqttClient, 200);
ampembeng 15:6f2798e45099 329 if (NETWORK_ATTEMPTING_RECONNECT == rc) {
ampembeng 15:6f2798e45099 330 // If the client is attempting to reconnect we will skip the rest of the loop.
ampembeng 15:6f2798e45099 331 INFO("Attempting to reconnect...");
ampembeng 15:6f2798e45099 332 wait(1);
ampembeng 15:6f2798e45099 333 continue;
ampembeng 15:6f2798e45099 334 }
ampembeng 15:6f2798e45099 335
ampembeng 15:6f2798e45099 336 INFO("\n=======================================================================================\n");
ampembeng 15:6f2798e45099 337 // Initialize JSON shadow document
ampembeng 15:6f2798e45099 338 rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
ampembeng 15:6f2798e45099 339 if (rc == NONE_ERROR) {
ampembeng 15:6f2798e45099 340
ampembeng 15:6f2798e45099 341 // If there has been a SW3 button press update the 'desired' color
ampembeng 15:6f2798e45099 342 if (buttonOverride) {
ampembeng 15:6f2798e45099 343 rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController);
ampembeng 15:6f2798e45099 344 buttonOverride = false;
Janos Follath 0:fc70c47eecb4 345 }
ampembeng 15:6f2798e45099 346
ampembeng 15:6f2798e45099 347 // Updates the 'reported' color
ampembeng 15:6f2798e45099 348 rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController);
ampembeng 15:6f2798e45099 349
ampembeng 15:6f2798e45099 350 // TODO: format for adding temperature
ampembeng 15:6f2798e45099 351 //rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &ledController, &temperatureHandler);
ampembeng 15:6f2798e45099 352
ampembeng 15:6f2798e45099 353 if (rc == NONE_ERROR) {
ampembeng 15:6f2798e45099 354 rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
ampembeng 15:6f2798e45099 355
ampembeng 15:6f2798e45099 356 if (rc == NONE_ERROR) {
ampembeng 15:6f2798e45099 357 INFO("Update Shadow: %s", JsonDocumentBuffer);
ampembeng 15:6f2798e45099 358 rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, JsonDocumentBuffer,
ampembeng 15:6f2798e45099 359 ShadowUpdateStatusCallback, NULL, 8, true);
ampembeng 15:6f2798e45099 360 }
ampembeng 15:6f2798e45099 361 }
ampembeng 15:6f2798e45099 362 }
ampembeng 15:6f2798e45099 363 INFO("*****************************************************************************************\n");
ampembeng 15:6f2798e45099 364
ampembeng 15:6f2798e45099 365 // Set LED color then wait an loop again
ampembeng 15:6f2798e45099 366 SetLedColor(ledColor);
ampembeng 15:6f2798e45099 367 wait(SHADOW_SYNC_INTERVAL);
Janos Follath 0:fc70c47eecb4 368 }
Janos Follath 0:fc70c47eecb4 369
ampembeng 15:6f2798e45099 370 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 371 ERROR("An error occurred in the loop %d", rc);
Janos Follath 0:fc70c47eecb4 372 }
Janos Follath 0:fc70c47eecb4 373
ampembeng 15:6f2798e45099 374 INFO("Disconnecting");
ampembeng 15:6f2798e45099 375 rc = aws_iot_shadow_disconnect(&mqttClient);
ampembeng 15:6f2798e45099 376
ampembeng 15:6f2798e45099 377 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 378 ERROR("Disconnect error %d", rc);
Janos Follath 0:fc70c47eecb4 379 }
Janos Follath 0:fc70c47eecb4 380
ampembeng 15:6f2798e45099 381 return rc;
ampembeng 15:6f2798e45099 382 }