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