![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Catie fork
Dependencies: SDFileSystem X_NUCLEO_IKS01A1
Fork of ATT_AWS_IoT_demo by
main.cpp@20:ee34856ae510, 2016-12-07 (annotated)
- Committer:
- ampembeng
- Date:
- Wed Dec 07 20:37:20 2016 +0000
- Revision:
- 20:ee34856ae510
- Parent:
- 18:6370da1de572
- Child:
- 23:b9ff83dc965f
The demo now reads the FRDM-K64F temperature and humidity sensors and adds that data to the reported AWS IoT device Shadow. Both the firmware and Python GUI are updated.
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 |
ampembeng | 20:ee34856ae510 | 25 | #include "HTS221.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 |
ampembeng | 15:6f2798e45099 | 46 | #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // 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; |
Janos Follath |
0:fc70c47eecb4 | 97 | |
ampembeng | 20:ee34856ae510 | 98 | // Sensor data |
ampembeng | 20:ee34856ae510 | 99 | float temperature = 0.0; |
ampembeng | 20:ee34856ae510 | 100 | int humidity = 0; |
ampembeng | 20:ee34856ae510 | 101 | |
ampembeng | 15:6f2798e45099 | 102 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 103 | // |
ampembeng | 15:6f2798e45099 | 104 | // Devices |
ampembeng | 15:6f2798e45099 | 105 | // |
ampembeng | 15:6f2798e45099 | 106 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 107 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 108 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 109 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 110 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 111 | |
ampembeng | 15:6f2798e45099 | 112 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 113 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 114 | |
ampembeng | 18:6370da1de572 | 115 | // SD card access (MOSI, MISO, SCK, CS) |
ampembeng | 18:6370da1de572 | 116 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
ampembeng | 18:6370da1de572 | 117 | |
ampembeng | 20:ee34856ae510 | 118 | // I2C bus (SDA, SCL) |
ampembeng | 20:ee34856ae510 | 119 | I2C i2c(PTC11, PTC10); |
ampembeng | 20:ee34856ae510 | 120 | |
ampembeng | 15:6f2798e45099 | 121 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 122 | // |
ampembeng | 15:6f2798e45099 | 123 | // Functions |
ampembeng | 15:6f2798e45099 | 124 | // |
ampembeng | 15:6f2798e45099 | 125 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 126 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 127 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 128 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 129 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 130 | { |
ampembeng | 15:6f2798e45099 | 131 | va_list vl; |
ampembeng | 15:6f2798e45099 | 132 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 133 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 134 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 135 | } |
Janos Follath |
0:fc70c47eecb4 | 136 | |
ampembeng | 15:6f2798e45099 | 137 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 138 | //* Set the RGB LED's Color |
ampembeng | 15:6f2798e45099 | 139 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 140 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 141 | void SetLedColor(unsigned char ucColor) |
ampembeng | 18:6370da1de572 | 142 | { |
ampembeng | 15:6f2798e45099 | 143 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 144 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 145 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 146 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 147 | } |
Janos Follath |
0:fc70c47eecb4 | 148 | |
ampembeng | 15:6f2798e45099 | 149 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 150 | //* SW3 Button handler. Finds the current LED color and sets the button to the next color in colorCycle[] |
ampembeng | 15:6f2798e45099 | 151 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 152 | void sw3ButtonHandler() |
ampembeng | 15:6f2798e45099 | 153 | { |
ampembeng | 15:6f2798e45099 | 154 | int i; |
ampembeng | 15:6f2798e45099 | 155 | for(i=0; i < NUM_COLORS; i++) { |
ampembeng | 15:6f2798e45099 | 156 | if (ledColor == colorCycle[i]) |
ampembeng | 15:6f2798e45099 | 157 | break; |
ampembeng | 15:6f2798e45099 | 158 | } |
ampembeng | 15:6f2798e45099 | 159 | |
ampembeng | 15:6f2798e45099 | 160 | // (circular-queue) |
ampembeng | 15:6f2798e45099 | 161 | if (++i == NUM_COLORS) |
ampembeng | 15:6f2798e45099 | 162 | i = 0; |
ampembeng | 15:6f2798e45099 | 163 | |
ampembeng | 15:6f2798e45099 | 164 | ledColor = colorCycle[i]; |
ampembeng | 15:6f2798e45099 | 165 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 166 | buttonOverride = true; |
ampembeng | 15:6f2798e45099 | 167 | } |
Janos Follath |
0:fc70c47eecb4 | 168 | |
ampembeng | 15:6f2798e45099 | 169 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 170 | // |
ampembeng | 15:6f2798e45099 | 171 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 172 | // |
ampembeng | 15:6f2798e45099 | 173 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 174 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 175 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 176 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 177 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, |
ampembeng | 15:6f2798e45099 | 178 | const char *pReceivedJsonDocument, void *pContextData) { |
Janos Follath |
0:fc70c47eecb4 | 179 | |
ampembeng | 15:6f2798e45099 | 180 | INFO("Shadow Update Status Callback"); |
ampembeng | 15:6f2798e45099 | 181 | |
ampembeng | 15:6f2798e45099 | 182 | if (status == SHADOW_ACK_TIMEOUT) { |
ampembeng | 15:6f2798e45099 | 183 | INFO("Update Timeout--"); |
ampembeng | 15:6f2798e45099 | 184 | } else if (status == SHADOW_ACK_REJECTED) { |
ampembeng | 15:6f2798e45099 | 185 | INFO("Update RejectedXX"); |
ampembeng | 15:6f2798e45099 | 186 | } else if (status == SHADOW_ACK_ACCEPTED) { |
ampembeng | 15:6f2798e45099 | 187 | INFO("Update Accepted!!"); // Good |
ampembeng | 15:6f2798e45099 | 188 | } |
ampembeng | 15:6f2798e45099 | 189 | } |
Janos Follath |
0:fc70c47eecb4 | 190 | |
ampembeng | 15:6f2798e45099 | 191 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 192 | //* This is the callback function that fires when AWS has sends out a shadow update. |
ampembeng | 15:6f2798e45099 | 193 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 194 | void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) { |
ampembeng | 15:6f2798e45099 | 195 | |
ampembeng | 15:6f2798e45099 | 196 | INFO("LED Callback Detected."); |
ampembeng | 15:6f2798e45099 | 197 | |
ampembeng | 15:6f2798e45099 | 198 | if (pContext != NULL) { |
ampembeng | 15:6f2798e45099 | 199 | switch (*(unsigned char *)(pContext->pData)){ |
ampembeng | 15:6f2798e45099 | 200 | case COLOR_OFF: |
ampembeng | 15:6f2798e45099 | 201 | INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 202 | break; |
ampembeng | 15:6f2798e45099 | 203 | case COLOR_RED: |
ampembeng | 15:6f2798e45099 | 204 | INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 205 | break; |
ampembeng | 15:6f2798e45099 | 206 | case COLOR_GREEN: |
ampembeng | 15:6f2798e45099 | 207 | INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 208 | break; |
ampembeng | 15:6f2798e45099 | 209 | case COLOR_BLUE: |
ampembeng | 15:6f2798e45099 | 210 | INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 211 | break; |
ampembeng | 15:6f2798e45099 | 212 | case COLOR_WHITE: |
ampembeng | 15:6f2798e45099 | 213 | INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 214 | break; |
Janos Follath |
0:fc70c47eecb4 | 215 | } |
ampembeng | 15:6f2798e45099 | 216 | } |
ampembeng | 15:6f2798e45099 | 217 | else { |
ampembeng | 15:6f2798e45099 | 218 | INFO("pContext was detected as NULL"); |
Janos Follath |
0:fc70c47eecb4 | 219 | } |
ampembeng | 15:6f2798e45099 | 220 | } |
ampembeng | 15:6f2798e45099 | 221 | |
ampembeng | 15:6f2798e45099 | 222 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 223 | // |
ampembeng | 15:6f2798e45099 | 224 | // Main |
ampembeng | 15:6f2798e45099 | 225 | // |
ampembeng | 15:6f2798e45099 | 226 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 227 | int main() { |
ampembeng | 15:6f2798e45099 | 228 | |
ampembeng | 15:6f2798e45099 | 229 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 230 | pc.baud(115200); |
ampembeng | 15:6f2798e45099 | 231 | INFO("Hello World from AT&T IoT Start Kit demo!"); |
ampembeng | 20:ee34856ae510 | 232 | |
ampembeng | 20:ee34856ae510 | 233 | int i; |
ampembeng | 20:ee34856ae510 | 234 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 20:ee34856ae510 | 235 | HTS221 hts221; // Temp/humidity |
ampembeng | 15:6f2798e45099 | 236 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 237 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 238 | |
ampembeng | 15:6f2798e45099 | 239 | // JSON struct for LED control |
ampembeng | 15:6f2798e45099 | 240 | jsonStruct_t ledController; |
ampembeng | 15:6f2798e45099 | 241 | ledController.cb = ledControl_Callback; |
ampembeng | 15:6f2798e45099 | 242 | ledController.pData = &ledColor; |
ampembeng | 15:6f2798e45099 | 243 | ledController.pKey = "ledColor"; |
ampembeng | 15:6f2798e45099 | 244 | ledController.type = SHADOW_JSON_UINT8; |
ampembeng | 15:6f2798e45099 | 245 | |
ampembeng | 20:ee34856ae510 | 246 | // JSON struct for temperature\humidity readings |
ampembeng | 15:6f2798e45099 | 247 | jsonStruct_t temperatureHandler; |
ampembeng | 15:6f2798e45099 | 248 | temperatureHandler.cb = NULL; |
ampembeng | 15:6f2798e45099 | 249 | temperatureHandler.pKey = "temperature"; |
ampembeng | 15:6f2798e45099 | 250 | temperatureHandler.pData = &temperature; |
ampembeng | 15:6f2798e45099 | 251 | temperatureHandler.type = SHADOW_JSON_FLOAT; |
ampembeng | 20:ee34856ae510 | 252 | |
ampembeng | 20:ee34856ae510 | 253 | jsonStruct_t humidityHandler; |
ampembeng | 20:ee34856ae510 | 254 | humidityHandler.cb = NULL; |
ampembeng | 20:ee34856ae510 | 255 | humidityHandler.pKey = "humidity"; |
ampembeng | 20:ee34856ae510 | 256 | humidityHandler.pData = &humidity; |
ampembeng | 20:ee34856ae510 | 257 | humidityHandler.type = SHADOW_JSON_INT16; |
ampembeng | 15:6f2798e45099 | 258 | |
ampembeng | 15:6f2798e45099 | 259 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
ampembeng | 15:6f2798e45099 | 260 | |
ampembeng | 18:6370da1de572 | 261 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 262 | // Paths for certs from SD card |
ampembeng | 18:6370da1de572 | 263 | INFO("Using SD card files for AWS config."); |
ampembeng | 18:6370da1de572 | 264 | DEBUG("- mqtt config path: %s", AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 265 | DEBUG("- rootCA path: %s", AWS_IOT_ROOT_CA_FILENAME); |
ampembeng | 18:6370da1de572 | 266 | DEBUG("- clientCRT path: %s", AWS_IOT_CERTIFICATE_FILENAME); |
ampembeng | 18:6370da1de572 | 267 | DEBUG("- clientKey path: %s", AWS_IOT_PRIVATE_KEY_FILENAME); |
ampembeng | 18:6370da1de572 | 268 | #else |
ampembeng | 18:6370da1de572 | 269 | INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
ampembeng | 18:6370da1de572 | 270 | #endif |
ampembeng | 15:6f2798e45099 | 271 | |
ampembeng | 16:02008a2a2569 | 272 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 273 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 274 | wait(.5); |
ampembeng | 15:6f2798e45099 | 275 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 276 | wait(.5); |
ampembeng | 15:6f2798e45099 | 277 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 278 | wait(.5); |
ampembeng | 16:02008a2a2569 | 279 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 280 | wait(.5); |
ampembeng | 16:02008a2a2569 | 281 | SetLedColor(COLOR_OFF); |
ampembeng | 20:ee34856ae510 | 282 | |
ampembeng | 20:ee34856ae510 | 283 | // Initialize sensors |
ampembeng | 20:ee34856ae510 | 284 | void hts221_init(void); |
ampembeng | 20:ee34856ae510 | 285 | i = hts221.begin(); |
ampembeng | 20:ee34856ae510 | 286 | if(!i) { |
ampembeng | 20:ee34856ae510 | 287 | WARN(RED "HTS221 NOT DETECTED!!\n\r"); |
ampembeng | 20:ee34856ae510 | 288 | } |
ampembeng | 18:6370da1de572 | 289 | |
ampembeng | 15:6f2798e45099 | 290 | // Setup SW3 button to falling edge interrupt |
ampembeng | 15:6f2798e45099 | 291 | Interrupt.fall(&sw3ButtonHandler); |
ampembeng | 15:6f2798e45099 | 292 | |
ampembeng | 15:6f2798e45099 | 293 | // Boot the Avnet Shield before any other operations |
ampembeng | 15:6f2798e45099 | 294 | net_modem_boot(); |
ampembeng | 15:6f2798e45099 | 295 | |
ampembeng | 18:6370da1de572 | 296 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 297 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 298 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 299 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 300 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 301 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 302 | return rc; |
ampembeng | 18:6370da1de572 | 303 | } |
ampembeng | 18:6370da1de572 | 304 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 305 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 306 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 307 | #else |
ampembeng | 15:6f2798e45099 | 308 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 309 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 310 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 311 | sp.port = port; |
ampembeng | 18:6370da1de572 | 312 | #endif |
ampembeng | 20:ee34856ae510 | 313 | |
ampembeng | 18:6370da1de572 | 314 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 315 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 316 | aws_iot_mqtt_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 317 | |
ampembeng | 15:6f2798e45099 | 318 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 319 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 320 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 321 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 322 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 323 | } |
ampembeng | 15:6f2798e45099 | 324 | |
ampembeng | 15:6f2798e45099 | 325 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 326 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 327 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 328 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 329 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 330 | } |
Janos Follath |
0:fc70c47eecb4 | 331 | |
ampembeng | 15:6f2798e45099 | 332 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 333 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 334 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 335 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 336 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 337 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 15:6f2798e45099 | 338 | return rc; |
ampembeng | 15:6f2798e45099 | 339 | } |
ampembeng | 15:6f2798e45099 | 340 | |
ampembeng | 15:6f2798e45099 | 341 | // Example line of how to delete a shadow (not used in this demo) |
ampembeng | 15:6f2798e45099 | 342 | //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true); |
Janos Follath |
0:fc70c47eecb4 | 343 | |
ampembeng | 15:6f2798e45099 | 344 | INFO("Shadow Register Delta..."); |
ampembeng | 15:6f2798e45099 | 345 | rc = aws_iot_shadow_register_delta(&mqttClient, &ledController); |
ampembeng | 15:6f2798e45099 | 346 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 347 | ERROR("Shadow Register Delta Error"); |
ampembeng | 15:6f2798e45099 | 348 | return rc; |
ampembeng | 15:6f2798e45099 | 349 | } |
ampembeng | 15:6f2798e45099 | 350 | |
ampembeng | 15:6f2798e45099 | 351 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 352 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 353 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 354 | |
ampembeng | 15:6f2798e45099 | 355 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 356 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 357 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 358 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 359 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 360 | wait(1); |
ampembeng | 15:6f2798e45099 | 361 | continue; |
ampembeng | 15:6f2798e45099 | 362 | } |
ampembeng | 20:ee34856ae510 | 363 | |
ampembeng | 20:ee34856ae510 | 364 | // Read sensor data |
ampembeng | 20:ee34856ae510 | 365 | temperature = CTOF(hts221.readTemperature()); |
ampembeng | 20:ee34856ae510 | 366 | humidity = hts221.readHumidity(); |
ampembeng | 20:ee34856ae510 | 367 | |
ampembeng | 15:6f2798e45099 | 368 | INFO("\n=======================================================================================\n"); |
ampembeng | 15:6f2798e45099 | 369 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 370 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 371 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 372 | |
ampembeng | 15:6f2798e45099 | 373 | // If there has been a SW3 button press update the 'desired' color |
ampembeng | 15:6f2798e45099 | 374 | if (buttonOverride) { |
ampembeng | 15:6f2798e45099 | 375 | rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 20:ee34856ae510 | 376 | //rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, |
ampembeng | 20:ee34856ae510 | 377 | // &temperatureHandler, |
ampembeng | 20:ee34856ae510 | 378 | // &humidityHandler); |
ampembeng | 15:6f2798e45099 | 379 | buttonOverride = false; |
Janos Follath |
0:fc70c47eecb4 | 380 | } |
ampembeng | 15:6f2798e45099 | 381 | |
ampembeng | 15:6f2798e45099 | 382 | // Updates the 'reported' color |
ampembeng | 20:ee34856ae510 | 383 | //rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 384 | |
ampembeng | 15:6f2798e45099 | 385 | // TODO: format for adding temperature |
ampembeng | 20:ee34856ae510 | 386 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, |
ampembeng | 20:ee34856ae510 | 387 | &temperatureHandler, |
ampembeng | 20:ee34856ae510 | 388 | &humidityHandler); |
ampembeng | 15:6f2798e45099 | 389 | |
ampembeng | 15:6f2798e45099 | 390 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 391 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 392 | |
ampembeng | 15:6f2798e45099 | 393 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 394 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 18:6370da1de572 | 395 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
ampembeng | 15:6f2798e45099 | 396 | ShadowUpdateStatusCallback, NULL, 8, true); |
ampembeng | 15:6f2798e45099 | 397 | } |
ampembeng | 15:6f2798e45099 | 398 | } |
ampembeng | 20:ee34856ae510 | 399 | } |
ampembeng | 20:ee34856ae510 | 400 | |
ampembeng | 20:ee34856ae510 | 401 | // Print data |
ampembeng | 20:ee34856ae510 | 402 | INFO("Temperature is: %0.2f F", temperature); |
ampembeng | 20:ee34856ae510 | 403 | INFO("Humidity is: %02d", humidity); |
ampembeng | 18:6370da1de572 | 404 | switch (ledColor) { |
ampembeng | 18:6370da1de572 | 405 | case COLOR_OFF: |
ampembeng | 18:6370da1de572 | 406 | INFO("LED: OFF"); |
ampembeng | 18:6370da1de572 | 407 | break; |
ampembeng | 18:6370da1de572 | 408 | case COLOR_RED: |
ampembeng | 18:6370da1de572 | 409 | INFO("LED: RED"); |
ampembeng | 18:6370da1de572 | 410 | break; |
ampembeng | 18:6370da1de572 | 411 | case COLOR_GREEN: |
ampembeng | 18:6370da1de572 | 412 | INFO("LED: GREEN"); |
ampembeng | 18:6370da1de572 | 413 | break; |
ampembeng | 18:6370da1de572 | 414 | case COLOR_BLUE: |
ampembeng | 18:6370da1de572 | 415 | INFO("LED: BLUE"); |
ampembeng | 18:6370da1de572 | 416 | break; |
ampembeng | 18:6370da1de572 | 417 | case COLOR_WHITE: |
ampembeng | 18:6370da1de572 | 418 | INFO("LED: WHITE"); |
ampembeng | 18:6370da1de572 | 419 | break; |
ampembeng | 18:6370da1de572 | 420 | } |
ampembeng | 18:6370da1de572 | 421 | INFO("*****************************************************************************************"); |
ampembeng | 20:ee34856ae510 | 422 | |
ampembeng | 20:ee34856ae510 | 423 | // Set the LED color |
ampembeng | 15:6f2798e45099 | 424 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 425 | wait(SHADOW_SYNC_INTERVAL); |
Janos Follath |
0:fc70c47eecb4 | 426 | } |
Janos Follath |
0:fc70c47eecb4 | 427 | |
ampembeng | 15:6f2798e45099 | 428 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 429 | ERROR("An error occurred in the loop %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 430 | } |
Janos Follath |
0:fc70c47eecb4 | 431 | |
ampembeng | 15:6f2798e45099 | 432 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 433 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 434 | |
ampembeng | 15:6f2798e45099 | 435 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 436 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 437 | } |
Janos Follath |
0:fc70c47eecb4 | 438 | |
ampembeng | 15:6f2798e45099 | 439 | return rc; |
ampembeng | 15:6f2798e45099 | 440 | } |