Catie fork
Dependencies: SDFileSystem X_NUCLEO_IKS01A1
Fork of ATT_AWS_IoT_demo by
main.cpp@16:02008a2a2569, 2016-12-02 (annotated)
- Committer:
- ampembeng
- Date:
- Fri Dec 02 22:39:56 2016 +0000
- Revision:
- 16:02008a2a2569
- Parent:
- 15:6f2798e45099
- Child:
- 18:6370da1de572
Cleaned up some project TODOs added a quick start guide to the README.md that's in the root folder.
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 | 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 | 16:02008a2a2569 | 259 | // Startup signal - blinks through RGBW 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 | 16:02008a2a2569 | 266 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 267 | wait(.5); |
ampembeng | 16:02008a2a2569 | 268 | SetLedColor(COLOR_OFF); |
ampembeng | 15:6f2798e45099 | 269 | |
ampembeng | 15:6f2798e45099 | 270 | // Setup SW3 button to falling edge interrupt |
ampembeng | 15:6f2798e45099 | 271 | Interrupt.fall(&sw3ButtonHandler); |
ampembeng | 15:6f2798e45099 | 272 | |
ampembeng | 15:6f2798e45099 | 273 | // Boot the Avnet Shield before any other operations |
ampembeng | 15:6f2798e45099 | 274 | net_modem_boot(); |
ampembeng | 15:6f2798e45099 | 275 | |
ampembeng | 15:6f2798e45099 | 276 | INFO("Initialize the MQTT client..."); |
ampembeng | 15:6f2798e45099 | 277 | MQTTClient_t mqttClient; |
ampembeng | 15:6f2798e45099 | 278 | aws_iot_mqtt_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 279 | |
ampembeng | 15:6f2798e45099 | 280 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 15:6f2798e45099 | 281 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 282 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 283 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 284 | sp.port = port; |
ampembeng | 15:6f2798e45099 | 285 | //sp.pClientCRT = clientCRT; |
ampembeng | 15:6f2798e45099 | 286 | //sp.pClientKey = clientKey; |
ampembeng | 15:6f2798e45099 | 287 | //sp.pRootCA = rootCA; |
ampembeng | 15:6f2798e45099 | 288 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 15:6f2798e45099 | 289 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 15:6f2798e45099 | 290 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 15:6f2798e45099 | 291 | |
ampembeng | 15:6f2798e45099 | 292 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 293 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 294 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 295 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 296 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 297 | } |
ampembeng | 15:6f2798e45099 | 298 | |
ampembeng | 15:6f2798e45099 | 299 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 300 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 301 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 302 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 303 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 304 | } |
Janos Follath |
0:fc70c47eecb4 | 305 | |
ampembeng | 15:6f2798e45099 | 306 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 307 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 308 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 309 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 310 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 311 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 15:6f2798e45099 | 312 | return rc; |
ampembeng | 15:6f2798e45099 | 313 | } |
ampembeng | 15:6f2798e45099 | 314 | |
ampembeng | 15:6f2798e45099 | 315 | // Example line of how to delete a shadow (not used in this demo) |
ampembeng | 15:6f2798e45099 | 316 | //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true); |
Janos Follath |
0:fc70c47eecb4 | 317 | |
ampembeng | 15:6f2798e45099 | 318 | INFO("Shadow Register Delta..."); |
ampembeng | 15:6f2798e45099 | 319 | rc = aws_iot_shadow_register_delta(&mqttClient, &ledController); |
ampembeng | 15:6f2798e45099 | 320 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 321 | ERROR("Shadow Register Delta Error"); |
ampembeng | 15:6f2798e45099 | 322 | return rc; |
ampembeng | 15:6f2798e45099 | 323 | } |
ampembeng | 15:6f2798e45099 | 324 | |
ampembeng | 15:6f2798e45099 | 325 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 326 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 327 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 328 | |
ampembeng | 15:6f2798e45099 | 329 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 330 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 331 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 332 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 333 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 334 | wait(1); |
ampembeng | 15:6f2798e45099 | 335 | continue; |
ampembeng | 15:6f2798e45099 | 336 | } |
ampembeng | 15:6f2798e45099 | 337 | |
ampembeng | 15:6f2798e45099 | 338 | INFO("\n=======================================================================================\n"); |
ampembeng | 15:6f2798e45099 | 339 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 340 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 341 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 342 | |
ampembeng | 15:6f2798e45099 | 343 | // If there has been a SW3 button press update the 'desired' color |
ampembeng | 15:6f2798e45099 | 344 | if (buttonOverride) { |
ampembeng | 15:6f2798e45099 | 345 | rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 346 | buttonOverride = false; |
Janos Follath |
0:fc70c47eecb4 | 347 | } |
ampembeng | 15:6f2798e45099 | 348 | |
ampembeng | 15:6f2798e45099 | 349 | // Updates the 'reported' color |
ampembeng | 15:6f2798e45099 | 350 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 351 | |
ampembeng | 15:6f2798e45099 | 352 | // TODO: format for adding temperature |
ampembeng | 15:6f2798e45099 | 353 | //rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &ledController, &temperatureHandler); |
ampembeng | 15:6f2798e45099 | 354 | |
ampembeng | 15:6f2798e45099 | 355 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 356 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 357 | |
ampembeng | 15:6f2798e45099 | 358 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 359 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 360 | rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, JsonDocumentBuffer, |
ampembeng | 15:6f2798e45099 | 361 | ShadowUpdateStatusCallback, NULL, 8, true); |
ampembeng | 15:6f2798e45099 | 362 | } |
ampembeng | 15:6f2798e45099 | 363 | } |
ampembeng | 15:6f2798e45099 | 364 | } |
ampembeng | 15:6f2798e45099 | 365 | INFO("*****************************************************************************************\n"); |
ampembeng | 15:6f2798e45099 | 366 | |
ampembeng | 15:6f2798e45099 | 367 | // Set LED color then wait an loop again |
ampembeng | 15:6f2798e45099 | 368 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 369 | wait(SHADOW_SYNC_INTERVAL); |
Janos Follath |
0:fc70c47eecb4 | 370 | } |
Janos Follath |
0:fc70c47eecb4 | 371 | |
ampembeng | 15:6f2798e45099 | 372 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 373 | ERROR("An error occurred in the loop %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 374 | } |
Janos Follath |
0:fc70c47eecb4 | 375 | |
ampembeng | 15:6f2798e45099 | 376 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 377 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 378 | |
ampembeng | 15:6f2798e45099 | 379 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 380 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 381 | } |
Janos Follath |
0:fc70c47eecb4 | 382 | |
ampembeng | 15:6f2798e45099 | 383 | return rc; |
ampembeng | 15:6f2798e45099 | 384 | } |