![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DIYmall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
Dependencies: Adafruit_GFX SDFileSystem
Fork of ATT_AWS_IoT_demo by
main.cpp@28:4650c541b029, 2018-10-09 (annotated)
- Committer:
- afmiee
- Date:
- Tue Oct 09 20:57:34 2018 +0000
- Revision:
- 28:4650c541b029
- Parent:
- 24:224c07ec3bd0
DIYmall 0.96" Inch I2c IIC Serial 128x64 Oled LCD LED White Display Module
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" |
afmiee | 28:4650c541b029 | 14 | //#include "./WNCInterface/WncControllerK64F/WncController/WncController.h" |
ampembeng | 15:6f2798e45099 | 15 | #include "network_interface.h" |
Janos Follath |
0:fc70c47eecb4 | 16 | |
ampembeng | 15:6f2798e45099 | 17 | // AWS includes |
ampembeng | 15:6f2798e45099 | 18 | #include "aws_iot_log.h" |
ampembeng | 15:6f2798e45099 | 19 | #include "aws_iot_version.h" |
ampembeng | 15:6f2798e45099 | 20 | #include "aws_iot_shadow_interface.h" |
ampembeng | 15:6f2798e45099 | 21 | #include "aws_iot_shadow_json_data.h" |
ampembeng | 15:6f2798e45099 | 22 | #include "aws_iot_config.h" |
ampembeng | 15:6f2798e45099 | 23 | #include "aws_iot_mqtt_interface.h" |
mbed_official | 12:1ae41c231014 | 24 | |
ampembeng | 20:ee34856ae510 | 25 | // Sensors |
ampembeng | 20:ee34856ae510 | 26 | #include "HTS221.h" |
ampembeng | 20:ee34856ae510 | 27 | |
Janos Follath |
0:fc70c47eecb4 | 28 | #if DEBUG_LEVEL > 0 |
Janos Follath |
0:fc70c47eecb4 | 29 | #include "mbedtls/debug.h" |
Janos Follath |
0:fc70c47eecb4 | 30 | #endif |
Janos Follath |
0:fc70c47eecb4 | 31 | |
afmiee | 28:4650c541b029 | 32 | #include "Adafruit_SSD1306.h" |
afmiee | 28:4650c541b029 | 33 | |
afmiee | 28:4650c541b029 | 34 | |
ampembeng | 15:6f2798e45099 | 35 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 36 | // |
ampembeng | 15:6f2798e45099 | 37 | // Defines |
ampembeng | 15:6f2798e45099 | 38 | // |
ampembeng | 15:6f2798e45099 | 39 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 40 | // LED Colors |
ampembeng | 15:6f2798e45099 | 41 | #define COLOR_OFF 0x00 |
ampembeng | 15:6f2798e45099 | 42 | #define COLOR_RED 0x01 |
ampembeng | 15:6f2798e45099 | 43 | #define COLOR_GREEN 0x02 |
ampembeng | 15:6f2798e45099 | 44 | #define COLOR_BLUE 0x04 |
ampembeng | 15:6f2798e45099 | 45 | #define COLOR_WHITE 0x07 |
ampembeng | 15:6f2798e45099 | 46 | #define NUM_COLORS 5 |
Janos Follath |
0:fc70c47eecb4 | 47 | |
ampembeng | 15:6f2798e45099 | 48 | // AWS defines |
ampembeng | 18:6370da1de572 | 49 | #define PATH_MAX 1024 |
ampembeng | 15:6f2798e45099 | 50 | #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // NOTE: Be wary of this if your JSON doc grows |
ampembeng | 15:6f2798e45099 | 51 | #define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds) |
Janos Follath |
0:fc70c47eecb4 | 52 | |
ampembeng | 15:6f2798e45099 | 53 | // Comment out the following line if color is not supported on the terminal |
ampembeng | 15:6f2798e45099 | 54 | //#define USE_COLOR |
ampembeng | 15:6f2798e45099 | 55 | #ifdef USE_COLOR |
ampembeng | 15:6f2798e45099 | 56 | #define BLK "\033[30m" |
ampembeng | 15:6f2798e45099 | 57 | #define RED "\033[31m" |
ampembeng | 15:6f2798e45099 | 58 | #define GRN "\033[32m" |
ampembeng | 15:6f2798e45099 | 59 | #define YEL "\033[33m" |
ampembeng | 15:6f2798e45099 | 60 | #define BLU "\033[34m" |
ampembeng | 15:6f2798e45099 | 61 | #define MAG "\033[35m" |
ampembeng | 15:6f2798e45099 | 62 | #define CYN "\033[36m" |
ampembeng | 15:6f2798e45099 | 63 | #define WHT "\033[37m" |
ampembeng | 15:6f2798e45099 | 64 | #define DEF "\033[39m" |
ampembeng | 15:6f2798e45099 | 65 | #else |
ampembeng | 15:6f2798e45099 | 66 | #define BLK |
ampembeng | 15:6f2798e45099 | 67 | #define RED |
ampembeng | 15:6f2798e45099 | 68 | #define GRN |
ampembeng | 15:6f2798e45099 | 69 | #define YEL |
ampembeng | 15:6f2798e45099 | 70 | #define BLU |
ampembeng | 15:6f2798e45099 | 71 | #define MAG |
ampembeng | 15:6f2798e45099 | 72 | #define CYN |
ampembeng | 15:6f2798e45099 | 73 | #define WHT |
ampembeng | 15:6f2798e45099 | 74 | #define DEF |
Janos Follath |
0:fc70c47eecb4 | 75 | #endif |
Janos Follath |
0:fc70c47eecb4 | 76 | |
ampembeng | 20:ee34856ae510 | 77 | // Sensor defines |
ampembeng | 20:ee34856ae510 | 78 | #define CTOF(x) ((x)*1.8+32) // Temperature |
ampembeng | 20:ee34856ae510 | 79 | |
ampembeng | 15:6f2798e45099 | 80 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 81 | // |
ampembeng | 15:6f2798e45099 | 82 | // Globals |
ampembeng | 15:6f2798e45099 | 83 | // |
ampembeng | 15:6f2798e45099 | 84 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 85 | // Controls LED color |
ampembeng | 15:6f2798e45099 | 86 | unsigned char ledColor = COLOR_OFF; |
ampembeng | 15:6f2798e45099 | 87 | |
ampembeng | 15:6f2798e45099 | 88 | // Color cycle array (used with SW3 button presses) |
ampembeng | 15:6f2798e45099 | 89 | unsigned char colorCycle[NUM_COLORS] = {COLOR_OFF, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE}; |
Janos Follath |
0:fc70c47eecb4 | 90 | |
ampembeng | 15:6f2798e45099 | 91 | // Button interrupts |
ampembeng | 15:6f2798e45099 | 92 | bool buttonOverride = false; |
ampembeng | 15:6f2798e45099 | 93 | InterruptIn Interrupt(SW3); |
ampembeng | 15:6f2798e45099 | 94 | |
ampembeng | 18:6370da1de572 | 95 | // These defines are pulled from aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 96 | char HostAddress[255] = AWS_IOT_MQTT_HOST; |
ampembeng | 18:6370da1de572 | 97 | char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 18:6370da1de572 | 98 | char ThingName[32] = AWS_IOT_MY_THING_NAME; |
ampembeng | 18:6370da1de572 | 99 | char PortString[5] = "8883"; |
ampembeng | 15:6f2798e45099 | 100 | uint32_t port = AWS_IOT_MQTT_PORT; |
ampembeng | 23:b9ff83dc965f | 101 | char iccidName[21] = "12345678901234567890"; |
Janos Follath |
0:fc70c47eecb4 | 102 | |
ampembeng | 20:ee34856ae510 | 103 | // Sensor data |
ampembeng | 20:ee34856ae510 | 104 | float temperature = 0.0; |
ampembeng | 20:ee34856ae510 | 105 | int humidity = 0; |
ampembeng | 20:ee34856ae510 | 106 | |
ampembeng | 23:b9ff83dc965f | 107 | // Temp/humidity object |
ampembeng | 23:b9ff83dc965f | 108 | HTS221 hts221; |
ampembeng | 23:b9ff83dc965f | 109 | |
afmiee | 28:4650c541b029 | 110 | |
afmiee | 28:4650c541b029 | 111 | unsigned int Counter = 0; // Packet counter |
afmiee | 28:4650c541b029 | 112 | int16_t RSSI = 0; |
ampembeng | 15:6f2798e45099 | 113 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 114 | // |
ampembeng | 15:6f2798e45099 | 115 | // Devices |
ampembeng | 15:6f2798e45099 | 116 | // |
ampembeng | 15:6f2798e45099 | 117 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 118 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 119 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 120 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 121 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 122 | |
ampembeng | 15:6f2798e45099 | 123 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 124 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 125 | |
ampembeng | 18:6370da1de572 | 126 | // SD card access (MOSI, MISO, SCK, CS) |
ampembeng | 18:6370da1de572 | 127 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
ampembeng | 18:6370da1de572 | 128 | |
ampembeng | 20:ee34856ae510 | 129 | // I2C bus (SDA, SCL) |
ampembeng | 20:ee34856ae510 | 130 | I2C i2c(PTC11, PTC10); |
ampembeng | 20:ee34856ae510 | 131 | |
afmiee | 28:4650c541b029 | 132 | class I2CPreInit : public I2C |
afmiee | 28:4650c541b029 | 133 | { |
afmiee | 28:4650c541b029 | 134 | public: |
afmiee | 28:4650c541b029 | 135 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
afmiee | 28:4650c541b029 | 136 | { |
afmiee | 28:4650c541b029 | 137 | // frequency(100000); |
afmiee | 28:4650c541b029 | 138 | // start(); |
afmiee | 28:4650c541b029 | 139 | }; |
afmiee | 28:4650c541b029 | 140 | }; |
afmiee | 28:4650c541b029 | 141 | |
afmiee | 28:4650c541b029 | 142 | |
afmiee | 28:4650c541b029 | 143 | // OLED |
afmiee | 28:4650c541b029 | 144 | I2CPreInit gI2C(D14,D15); |
afmiee | 28:4650c541b029 | 145 | Adafruit_SSD1306_I2c gOled2(gI2C,D4); |
afmiee | 28:4650c541b029 | 146 | |
ampembeng | 15:6f2798e45099 | 147 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 148 | // |
ampembeng | 15:6f2798e45099 | 149 | // Functions |
ampembeng | 15:6f2798e45099 | 150 | // |
ampembeng | 15:6f2798e45099 | 151 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 152 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 153 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 154 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 155 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 156 | { |
ampembeng | 15:6f2798e45099 | 157 | va_list vl; |
ampembeng | 15:6f2798e45099 | 158 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 159 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 160 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 161 | } |
Janos Follath |
0:fc70c47eecb4 | 162 | |
ampembeng | 15:6f2798e45099 | 163 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 164 | //* Set the RGB LED's Color |
ampembeng | 15:6f2798e45099 | 165 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 166 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 167 | void SetLedColor(unsigned char ucColor) |
ampembeng | 18:6370da1de572 | 168 | { |
ampembeng | 15:6f2798e45099 | 169 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 170 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 171 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 172 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 173 | } |
Janos Follath |
0:fc70c47eecb4 | 174 | |
ampembeng | 15:6f2798e45099 | 175 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 176 | //* SW3 Button handler. Finds the current LED color and sets the button to the next color in colorCycle[] |
ampembeng | 15:6f2798e45099 | 177 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 178 | void sw3ButtonHandler() |
ampembeng | 15:6f2798e45099 | 179 | { |
ampembeng | 15:6f2798e45099 | 180 | int i; |
ampembeng | 15:6f2798e45099 | 181 | for(i=0; i < NUM_COLORS; i++) { |
ampembeng | 15:6f2798e45099 | 182 | if (ledColor == colorCycle[i]) |
ampembeng | 15:6f2798e45099 | 183 | break; |
ampembeng | 15:6f2798e45099 | 184 | } |
ampembeng | 15:6f2798e45099 | 185 | |
ampembeng | 15:6f2798e45099 | 186 | // (circular-queue) |
ampembeng | 15:6f2798e45099 | 187 | if (++i == NUM_COLORS) |
ampembeng | 15:6f2798e45099 | 188 | i = 0; |
ampembeng | 15:6f2798e45099 | 189 | |
ampembeng | 15:6f2798e45099 | 190 | ledColor = colorCycle[i]; |
ampembeng | 15:6f2798e45099 | 191 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 192 | buttonOverride = true; |
ampembeng | 15:6f2798e45099 | 193 | } |
Janos Follath |
0:fc70c47eecb4 | 194 | |
ampembeng | 23:b9ff83dc965f | 195 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 196 | //* Print LED and sensor data |
ampembeng | 23:b9ff83dc965f | 197 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 198 | void printData() |
ampembeng | 23:b9ff83dc965f | 199 | { |
ampembeng | 23:b9ff83dc965f | 200 | INFO("Temperature is: %0.2f F", temperature); |
ampembeng | 23:b9ff83dc965f | 201 | INFO("Humidity is: %02d", humidity); |
ampembeng | 23:b9ff83dc965f | 202 | switch (ledColor) { |
ampembeng | 23:b9ff83dc965f | 203 | case COLOR_OFF: |
ampembeng | 23:b9ff83dc965f | 204 | INFO("LED: Off"); |
ampembeng | 23:b9ff83dc965f | 205 | break; |
ampembeng | 23:b9ff83dc965f | 206 | case COLOR_RED: |
ampembeng | 23:b9ff83dc965f | 207 | INFO("LED: Red"); |
ampembeng | 23:b9ff83dc965f | 208 | break; |
ampembeng | 23:b9ff83dc965f | 209 | case COLOR_GREEN: |
ampembeng | 23:b9ff83dc965f | 210 | INFO("LED: Green"); |
ampembeng | 23:b9ff83dc965f | 211 | break; |
ampembeng | 23:b9ff83dc965f | 212 | case COLOR_BLUE: |
ampembeng | 23:b9ff83dc965f | 213 | INFO("LED: Blue"); |
ampembeng | 23:b9ff83dc965f | 214 | break; |
ampembeng | 23:b9ff83dc965f | 215 | case COLOR_WHITE: |
ampembeng | 23:b9ff83dc965f | 216 | INFO("LED: White"); |
ampembeng | 23:b9ff83dc965f | 217 | break; |
ampembeng | 23:b9ff83dc965f | 218 | } |
afmiee | 28:4650c541b029 | 219 | INFO("RSSI = %d\n\r",RSSI); |
afmiee | 28:4650c541b029 | 220 | gOled2.setTextCursor(0,32); |
afmiee | 28:4650c541b029 | 221 | gOled2.printf("PKT#: %6d\n",Counter); |
afmiee | 28:4650c541b029 | 222 | gOled2.printf("RSSI: %6d dBms\n",RSSI); |
afmiee | 28:4650c541b029 | 223 | gOled2.display(); |
ampembeng | 23:b9ff83dc965f | 224 | } |
ampembeng | 23:b9ff83dc965f | 225 | |
ampembeng | 15:6f2798e45099 | 226 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 227 | // |
ampembeng | 15:6f2798e45099 | 228 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 229 | // |
ampembeng | 15:6f2798e45099 | 230 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 231 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 232 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 233 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 234 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, |
ampembeng | 15:6f2798e45099 | 235 | const char *pReceivedJsonDocument, void *pContextData) { |
Janos Follath |
0:fc70c47eecb4 | 236 | |
ampembeng | 15:6f2798e45099 | 237 | INFO("Shadow Update Status Callback"); |
ampembeng | 15:6f2798e45099 | 238 | |
ampembeng | 15:6f2798e45099 | 239 | if (status == SHADOW_ACK_TIMEOUT) { |
ampembeng | 15:6f2798e45099 | 240 | INFO("Update Timeout--"); |
ampembeng | 15:6f2798e45099 | 241 | } else if (status == SHADOW_ACK_REJECTED) { |
ampembeng | 15:6f2798e45099 | 242 | INFO("Update RejectedXX"); |
ampembeng | 15:6f2798e45099 | 243 | } else if (status == SHADOW_ACK_ACCEPTED) { |
ampembeng | 15:6f2798e45099 | 244 | INFO("Update Accepted!!"); // Good |
ampembeng | 15:6f2798e45099 | 245 | } |
ampembeng | 15:6f2798e45099 | 246 | } |
Janos Follath |
0:fc70c47eecb4 | 247 | |
ampembeng | 15:6f2798e45099 | 248 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 249 | //* This is the callback function that fires when AWS has sends out a shadow update. |
ampembeng | 15:6f2798e45099 | 250 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 251 | void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) { |
ampembeng | 15:6f2798e45099 | 252 | |
ampembeng | 15:6f2798e45099 | 253 | INFO("LED Callback Detected."); |
ampembeng | 15:6f2798e45099 | 254 | |
ampembeng | 15:6f2798e45099 | 255 | if (pContext != NULL) { |
ampembeng | 15:6f2798e45099 | 256 | switch (*(unsigned char *)(pContext->pData)){ |
ampembeng | 15:6f2798e45099 | 257 | case COLOR_OFF: |
ampembeng | 15:6f2798e45099 | 258 | INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 259 | break; |
ampembeng | 15:6f2798e45099 | 260 | case COLOR_RED: |
ampembeng | 15:6f2798e45099 | 261 | INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 262 | break; |
ampembeng | 15:6f2798e45099 | 263 | case COLOR_GREEN: |
ampembeng | 15:6f2798e45099 | 264 | INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 265 | break; |
ampembeng | 15:6f2798e45099 | 266 | case COLOR_BLUE: |
ampembeng | 15:6f2798e45099 | 267 | INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 268 | break; |
ampembeng | 15:6f2798e45099 | 269 | case COLOR_WHITE: |
ampembeng | 15:6f2798e45099 | 270 | INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 271 | break; |
Janos Follath |
0:fc70c47eecb4 | 272 | } |
ampembeng | 15:6f2798e45099 | 273 | } |
ampembeng | 15:6f2798e45099 | 274 | else { |
ampembeng | 15:6f2798e45099 | 275 | INFO("pContext was detected as NULL"); |
Janos Follath |
0:fc70c47eecb4 | 276 | } |
ampembeng | 15:6f2798e45099 | 277 | } |
ampembeng | 23:b9ff83dc965f | 278 | |
ampembeng | 23:b9ff83dc965f | 279 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 280 | //* Subscribe callback (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 281 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 282 | int MQTTcallbackHandler(MQTTCallbackParams params) { |
ampembeng | 23:b9ff83dc965f | 283 | |
ampembeng | 23:b9ff83dc965f | 284 | INFO("Subscribe callback"); |
ampembeng | 23:b9ff83dc965f | 285 | INFO("%.*s\t%.*s", |
ampembeng | 23:b9ff83dc965f | 286 | (int)params.TopicNameLen, params.pTopicName, |
ampembeng | 23:b9ff83dc965f | 287 | (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload); |
ampembeng | 23:b9ff83dc965f | 288 | |
ampembeng | 23:b9ff83dc965f | 289 | return 0; |
ampembeng | 23:b9ff83dc965f | 290 | } |
ampembeng | 23:b9ff83dc965f | 291 | |
ampembeng | 23:b9ff83dc965f | 292 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 293 | //* Disconnect handling (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 294 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 295 | void disconnectCallbackHandler(void) { |
ampembeng | 23:b9ff83dc965f | 296 | WARN("MQTT Disconnect"); |
ampembeng | 23:b9ff83dc965f | 297 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 298 | if(aws_iot_is_autoreconnect_enabled()){ |
ampembeng | 23:b9ff83dc965f | 299 | INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
ampembeng | 23:b9ff83dc965f | 300 | }else{ |
ampembeng | 23:b9ff83dc965f | 301 | WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
ampembeng | 23:b9ff83dc965f | 302 | rc = aws_iot_mqtt_attempt_reconnect(); |
ampembeng | 23:b9ff83dc965f | 303 | if(RECONNECT_SUCCESSFUL == rc){ |
ampembeng | 23:b9ff83dc965f | 304 | WARN("Manual Reconnect Successful"); |
ampembeng | 23:b9ff83dc965f | 305 | }else{ |
ampembeng | 23:b9ff83dc965f | 306 | WARN("Manual Reconnect Failed - %d", rc); |
ampembeng | 23:b9ff83dc965f | 307 | } |
ampembeng | 23:b9ff83dc965f | 308 | } |
ampembeng | 23:b9ff83dc965f | 309 | } |
ampembeng | 23:b9ff83dc965f | 310 | |
ampembeng | 23:b9ff83dc965f | 311 | //===================================================================================================================== |
ampembeng | 23:b9ff83dc965f | 312 | // |
ampembeng | 23:b9ff83dc965f | 313 | // 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 | 314 | // AWS device shadow, it publishes to an AWS Rule. The Rule is setup to store data to a DynamoDB, and |
ampembeng | 23:b9ff83dc965f | 315 | // the demo S3 website pulls that data from the DynamoDB and displays it. |
ampembeng | 23:b9ff83dc965f | 316 | // |
ampembeng | 23:b9ff83dc965f | 317 | //===================================================================================================================== |
afmiee | 28:4650c541b029 | 318 | const char PayLoad[] = "0123456789ABCDEF"; |
afmiee | 28:4650c541b029 | 319 | //char PayLoad1056[1057]; |
afmiee | 28:4650c541b029 | 320 | #define PACKETS16B 66 |
afmiee | 28:4650c541b029 | 321 | #define PACKET 16 |
afmiee | 28:4650c541b029 | 322 | char cPayload[PACKET*PACKETS16B+1]; |
afmiee | 28:4650c541b029 | 323 | |
afmiee | 28:4650c541b029 | 324 | |
afmiee | 28:4650c541b029 | 325 | |
afmiee | 28:4650c541b029 | 326 | int outOfBoxDemo(void) { |
ampembeng | 23:b9ff83dc965f | 327 | INFO("Running Out-of-Box Function (alternate demo)."); |
ampembeng | 23:b9ff83dc965f | 328 | |
ampembeng | 23:b9ff83dc965f | 329 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 330 | int32_t i = 0; |
ampembeng | 23:b9ff83dc965f | 331 | int publishCount = 0; |
ampembeng | 23:b9ff83dc965f | 332 | bool infinitePublishFlag = true; |
afmiee | 28:4650c541b029 | 333 | //char cPayload[PACKET*PACKETS16B+1]; |
ampembeng | 23:b9ff83dc965f | 334 | char cTopic[100]; |
ampembeng | 23:b9ff83dc965f | 335 | const string colorStrings[] = {"Off", "Red", "Green", "", "Blue", "", "", "White"}; |
ampembeng | 24:224c07ec3bd0 | 336 | float updateInterval = 1.0; // seconds |
afmiee | 28:4650c541b029 | 337 | int j,k; |
ampembeng | 23:b9ff83dc965f | 338 | |
afmiee | 28:4650c541b029 | 339 | char CntStrng[17]; |
afmiee | 28:4650c541b029 | 340 | |
afmiee | 28:4650c541b029 | 341 | |
afmiee | 28:4650c541b029 | 342 | for(j = 0; j < PACKETS16B; j++) |
afmiee | 28:4650c541b029 | 343 | for(k = 0; k < PACKET; k++) |
afmiee | 28:4650c541b029 | 344 | cPayload[PACKET*j + k] = PayLoad[k]; |
afmiee | 28:4650c541b029 | 345 | cPayload[16*PACKETS16B ] = 0x00; |
afmiee | 28:4650c541b029 | 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. |
afmiee | 28:4650c541b029 | 425 | buttonOverride = true; |
ampembeng | 23:b9ff83dc965f | 426 | if (buttonOverride) { |
ampembeng | 23:b9ff83dc965f | 427 | buttonOverride = false; |
ampembeng | 23:b9ff83dc965f | 428 | |
ampembeng | 23:b9ff83dc965f | 429 | // Get temp/humidity values |
ampembeng | 23:b9ff83dc965f | 430 | temperature = CTOF(hts221.readTemperature()); |
ampembeng | 23:b9ff83dc965f | 431 | humidity = hts221.readHumidity(); |
ampembeng | 23:b9ff83dc965f | 432 | |
ampembeng | 23:b9ff83dc965f | 433 | // Loading data into JSON format |
afmiee | 28:4650c541b029 | 434 | //sprintf(cPayload, "{\"color\":\"%s\",\"temperature\":%f,\"humidity\":%d}", colorStrings[ledColor], temperature, humidity); |
afmiee | 28:4650c541b029 | 435 | //sprintf(cPayload, PayLoad1056 ); |
afmiee | 28:4650c541b029 | 436 | sprintf(CntStrng, "%08d%08d", abs(RSSI), ++Counter ); |
afmiee | 28:4650c541b029 | 437 | printf("%s\n\r", CntStrng); |
afmiee | 28:4650c541b029 | 438 | for( k = 0; k < PACKET; k++ ) |
afmiee | 28:4650c541b029 | 439 | cPayload[k] = CntStrng[k]; |
ampembeng | 23:b9ff83dc965f | 440 | Msg.PayloadLen = strlen(cPayload) + 1; |
ampembeng | 23:b9ff83dc965f | 441 | Params.MessageParams = Msg; |
ampembeng | 23:b9ff83dc965f | 442 | |
ampembeng | 23:b9ff83dc965f | 443 | // Publish |
ampembeng | 23:b9ff83dc965f | 444 | rc = aws_iot_mqtt_publish(&Params); |
ampembeng | 23:b9ff83dc965f | 445 | if (publishCount > 0) { |
ampembeng | 23:b9ff83dc965f | 446 | publishCount--; |
ampembeng | 23:b9ff83dc965f | 447 | } |
ampembeng | 23:b9ff83dc965f | 448 | |
ampembeng | 23:b9ff83dc965f | 449 | printData(); |
ampembeng | 23:b9ff83dc965f | 450 | INFO("--> Update sent. Sleep for %f seconds", updateInterval); |
ampembeng | 23:b9ff83dc965f | 451 | wait(updateInterval-.02); |
ampembeng | 23:b9ff83dc965f | 452 | } |
ampembeng | 23:b9ff83dc965f | 453 | else { |
ampembeng | 23:b9ff83dc965f | 454 | wait(.3); // 300 ms |
ampembeng | 23:b9ff83dc965f | 455 | } |
ampembeng | 23:b9ff83dc965f | 456 | } |
ampembeng | 23:b9ff83dc965f | 457 | |
ampembeng | 23:b9ff83dc965f | 458 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 459 | ERROR("An error occurred in the loop.\n"); |
ampembeng | 23:b9ff83dc965f | 460 | } else { |
ampembeng | 23:b9ff83dc965f | 461 | INFO("Publish done\n"); |
ampembeng | 23:b9ff83dc965f | 462 | } |
ampembeng | 23:b9ff83dc965f | 463 | |
ampembeng | 23:b9ff83dc965f | 464 | return rc; |
ampembeng | 23:b9ff83dc965f | 465 | } |
ampembeng | 15:6f2798e45099 | 466 | |
afmiee | 28:4650c541b029 | 467 | |
ampembeng | 15:6f2798e45099 | 468 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 469 | // |
ampembeng | 15:6f2798e45099 | 470 | // Main |
ampembeng | 15:6f2798e45099 | 471 | // |
ampembeng | 15:6f2798e45099 | 472 | //===================================================================================================================== |
afmiee | 28:4650c541b029 | 473 | |
ampembeng | 15:6f2798e45099 | 474 | int main() { |
ampembeng | 15:6f2798e45099 | 475 | |
ampembeng | 15:6f2798e45099 | 476 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 477 | pc.baud(115200); |
ampembeng | 15:6f2798e45099 | 478 | INFO("Hello World from AT&T IoT Start Kit demo!"); |
afmiee | 28:4650c541b029 | 479 | gOled2.clearDisplay(); |
afmiee | 28:4650c541b029 | 480 | gOled2.display(); |
afmiee | 28:4650c541b029 | 481 | wait(0.5); |
afmiee | 28:4650c541b029 | 482 | gOled2.printf("Latch Cellular EVM\r\n"); |
afmiee | 28:4650c541b029 | 483 | gOled2.display(); |
afmiee | 28:4650c541b029 | 484 | |
ampembeng | 20:ee34856ae510 | 485 | int i; |
ampembeng | 23:b9ff83dc965f | 486 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 487 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 488 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 489 | |
ampembeng | 15:6f2798e45099 | 490 | // JSON struct for LED control |
ampembeng | 15:6f2798e45099 | 491 | jsonStruct_t ledController; |
ampembeng | 15:6f2798e45099 | 492 | ledController.cb = ledControl_Callback; |
ampembeng | 15:6f2798e45099 | 493 | ledController.pData = &ledColor; |
ampembeng | 15:6f2798e45099 | 494 | ledController.pKey = "ledColor"; |
ampembeng | 15:6f2798e45099 | 495 | ledController.type = SHADOW_JSON_UINT8; |
ampembeng | 15:6f2798e45099 | 496 | |
ampembeng | 20:ee34856ae510 | 497 | // JSON struct for temperature\humidity readings |
ampembeng | 15:6f2798e45099 | 498 | jsonStruct_t temperatureHandler; |
ampembeng | 15:6f2798e45099 | 499 | temperatureHandler.cb = NULL; |
ampembeng | 15:6f2798e45099 | 500 | temperatureHandler.pKey = "temperature"; |
ampembeng | 15:6f2798e45099 | 501 | temperatureHandler.pData = &temperature; |
ampembeng | 15:6f2798e45099 | 502 | temperatureHandler.type = SHADOW_JSON_FLOAT; |
ampembeng | 20:ee34856ae510 | 503 | |
ampembeng | 20:ee34856ae510 | 504 | jsonStruct_t humidityHandler; |
ampembeng | 20:ee34856ae510 | 505 | humidityHandler.cb = NULL; |
ampembeng | 20:ee34856ae510 | 506 | humidityHandler.pKey = "humidity"; |
ampembeng | 20:ee34856ae510 | 507 | humidityHandler.pData = &humidity; |
ampembeng | 20:ee34856ae510 | 508 | humidityHandler.type = SHADOW_JSON_INT16; |
ampembeng | 15:6f2798e45099 | 509 | |
ampembeng | 15:6f2798e45099 | 510 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
ampembeng | 15:6f2798e45099 | 511 | |
ampembeng | 18:6370da1de572 | 512 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 513 | // Paths for certs from SD card |
ampembeng | 18:6370da1de572 | 514 | INFO("Using SD card files for AWS config."); |
ampembeng | 18:6370da1de572 | 515 | DEBUG("- mqtt config path: %s", AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 516 | DEBUG("- rootCA path: %s", AWS_IOT_ROOT_CA_FILENAME); |
ampembeng | 18:6370da1de572 | 517 | DEBUG("- clientCRT path: %s", AWS_IOT_CERTIFICATE_FILENAME); |
ampembeng | 18:6370da1de572 | 518 | DEBUG("- clientKey path: %s", AWS_IOT_PRIVATE_KEY_FILENAME); |
ampembeng | 18:6370da1de572 | 519 | #else |
ampembeng | 18:6370da1de572 | 520 | INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
ampembeng | 18:6370da1de572 | 521 | #endif |
ampembeng | 15:6f2798e45099 | 522 | |
ampembeng | 16:02008a2a2569 | 523 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 524 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 525 | wait(.5); |
ampembeng | 15:6f2798e45099 | 526 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 527 | wait(.5); |
ampembeng | 15:6f2798e45099 | 528 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 529 | wait(.5); |
ampembeng | 16:02008a2a2569 | 530 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 531 | wait(.5); |
ampembeng | 16:02008a2a2569 | 532 | SetLedColor(COLOR_OFF); |
ampembeng | 20:ee34856ae510 | 533 | |
ampembeng | 20:ee34856ae510 | 534 | // Initialize sensors |
ampembeng | 23:b9ff83dc965f | 535 | INFO("Init sensors..."); |
ampembeng | 20:ee34856ae510 | 536 | void hts221_init(void); |
ampembeng | 20:ee34856ae510 | 537 | i = hts221.begin(); |
ampembeng | 20:ee34856ae510 | 538 | if(!i) { |
ampembeng | 20:ee34856ae510 | 539 | WARN(RED "HTS221 NOT DETECTED!!\n\r"); |
ampembeng | 20:ee34856ae510 | 540 | } |
ampembeng | 18:6370da1de572 | 541 | |
ampembeng | 15:6f2798e45099 | 542 | // Setup SW3 button to falling edge interrupt |
ampembeng | 23:b9ff83dc965f | 543 | INFO("Init interrupts..."); |
ampembeng | 15:6f2798e45099 | 544 | Interrupt.fall(&sw3ButtonHandler); |
ampembeng | 15:6f2798e45099 | 545 | |
ampembeng | 15:6f2798e45099 | 546 | // Boot the Avnet Shield before any other operations |
ampembeng | 23:b9ff83dc965f | 547 | INFO("Net Boot..."); |
ampembeng | 15:6f2798e45099 | 548 | net_modem_boot(); |
ampembeng | 23:b9ff83dc965f | 549 | |
ampembeng | 23:b9ff83dc965f | 550 | //========================================================================== |
ampembeng | 23:b9ff83dc965f | 551 | // NOTE: You can comment in the following line for an alternate demo that |
ampembeng | 23:b9ff83dc965f | 552 | // is used as the out-of-box demo binary that comes with the AT&T IoT |
ampembeng | 23:b9ff83dc965f | 553 | // Starter Kit. It loops instead of the rest of Main() |
afmiee | 28:4650c541b029 | 554 | // return outOfBoxDemo(); |
ampembeng | 23:b9ff83dc965f | 555 | //========================================================================== |
ampembeng | 23:b9ff83dc965f | 556 | |
ampembeng | 18:6370da1de572 | 557 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 558 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 559 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 560 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 561 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 562 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 563 | return rc; |
ampembeng | 18:6370da1de572 | 564 | } |
ampembeng | 18:6370da1de572 | 565 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 566 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 567 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 568 | #else |
ampembeng | 15:6f2798e45099 | 569 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 570 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 15:6f2798e45099 | 571 | sp.pHost = HostAddress; |
ampembeng | 15:6f2798e45099 | 572 | sp.port = port; |
ampembeng | 23:b9ff83dc965f | 573 | |
ampembeng | 23:b9ff83dc965f | 574 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 575 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 576 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 577 | #endif |
ampembeng | 20:ee34856ae510 | 578 | |
ampembeng | 18:6370da1de572 | 579 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 580 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 581 | aws_iot_mqtt_init(&mqttClient); |
afmiee | 28:4650c541b029 | 582 | return outOfBoxDemo(); |
ampembeng | 15:6f2798e45099 | 583 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 584 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 585 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 586 | ERROR("Shadow Init Error %d", rc); |
ampembeng | 15:6f2798e45099 | 587 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 588 | } |
ampembeng | 15:6f2798e45099 | 589 | |
ampembeng | 15:6f2798e45099 | 590 | INFO("Shadow Connect..."); |
ampembeng | 15:6f2798e45099 | 591 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 592 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 593 | ERROR("Shadow Connection Error %d", rc); |
ampembeng | 15:6f2798e45099 | 594 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 595 | } |
Janos Follath |
0:fc70c47eecb4 | 596 | |
ampembeng | 15:6f2798e45099 | 597 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 598 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 599 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 600 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 601 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 602 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 15:6f2798e45099 | 603 | return rc; |
ampembeng | 15:6f2798e45099 | 604 | } |
ampembeng | 15:6f2798e45099 | 605 | |
ampembeng | 15:6f2798e45099 | 606 | // Example line of how to delete a shadow (not used in this demo) |
ampembeng | 15:6f2798e45099 | 607 | //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true); |
Janos Follath |
0:fc70c47eecb4 | 608 | |
ampembeng | 15:6f2798e45099 | 609 | INFO("Shadow Register Delta..."); |
ampembeng | 15:6f2798e45099 | 610 | rc = aws_iot_shadow_register_delta(&mqttClient, &ledController); |
ampembeng | 15:6f2798e45099 | 611 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 612 | ERROR("Shadow Register Delta Error"); |
ampembeng | 15:6f2798e45099 | 613 | return rc; |
ampembeng | 15:6f2798e45099 | 614 | } |
ampembeng | 15:6f2798e45099 | 615 | |
ampembeng | 15:6f2798e45099 | 616 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 617 | // Loop and publish changes from the FRDM board |
ampembeng | 15:6f2798e45099 | 618 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { |
ampembeng | 15:6f2798e45099 | 619 | |
ampembeng | 15:6f2798e45099 | 620 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 621 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
ampembeng | 15:6f2798e45099 | 622 | if (NETWORK_ATTEMPTING_RECONNECT == rc) { |
ampembeng | 15:6f2798e45099 | 623 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 15:6f2798e45099 | 624 | INFO("Attempting to reconnect..."); |
ampembeng | 15:6f2798e45099 | 625 | wait(1); |
ampembeng | 15:6f2798e45099 | 626 | continue; |
ampembeng | 15:6f2798e45099 | 627 | } |
ampembeng | 20:ee34856ae510 | 628 | |
ampembeng | 20:ee34856ae510 | 629 | // Read sensor data |
ampembeng | 20:ee34856ae510 | 630 | temperature = CTOF(hts221.readTemperature()); |
ampembeng | 20:ee34856ae510 | 631 | humidity = hts221.readHumidity(); |
ampembeng | 20:ee34856ae510 | 632 | |
ampembeng | 15:6f2798e45099 | 633 | INFO("\n=======================================================================================\n"); |
ampembeng | 15:6f2798e45099 | 634 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 635 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 636 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 637 | |
ampembeng | 15:6f2798e45099 | 638 | // If there has been a SW3 button press update the 'desired' color |
ampembeng | 15:6f2798e45099 | 639 | if (buttonOverride) { |
ampembeng | 23:b9ff83dc965f | 640 | rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 641 | buttonOverride = false; |
Janos Follath |
0:fc70c47eecb4 | 642 | } |
ampembeng | 15:6f2798e45099 | 643 | |
ampembeng | 23:b9ff83dc965f | 644 | // Updates the 'reported' color/temp/humidity |
ampembeng | 20:ee34856ae510 | 645 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, |
ampembeng | 20:ee34856ae510 | 646 | &temperatureHandler, |
ampembeng | 20:ee34856ae510 | 647 | &humidityHandler); |
ampembeng | 15:6f2798e45099 | 648 | |
ampembeng | 15:6f2798e45099 | 649 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 650 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 651 | |
ampembeng | 15:6f2798e45099 | 652 | if (rc == NONE_ERROR) { |
ampembeng | 15:6f2798e45099 | 653 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 18:6370da1de572 | 654 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
ampembeng | 15:6f2798e45099 | 655 | ShadowUpdateStatusCallback, NULL, 8, true); |
ampembeng | 15:6f2798e45099 | 656 | } |
ampembeng | 15:6f2798e45099 | 657 | } |
ampembeng | 20:ee34856ae510 | 658 | } |
ampembeng | 20:ee34856ae510 | 659 | |
ampembeng | 20:ee34856ae510 | 660 | // Print data |
ampembeng | 23:b9ff83dc965f | 661 | printData(); |
ampembeng | 18:6370da1de572 | 662 | INFO("*****************************************************************************************"); |
ampembeng | 20:ee34856ae510 | 663 | |
ampembeng | 20:ee34856ae510 | 664 | // Set the LED color |
ampembeng | 15:6f2798e45099 | 665 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 666 | wait(SHADOW_SYNC_INTERVAL); |
Janos Follath |
0:fc70c47eecb4 | 667 | } |
Janos Follath |
0:fc70c47eecb4 | 668 | |
ampembeng | 15:6f2798e45099 | 669 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 670 | ERROR("An error occurred in the loop %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 671 | } |
Janos Follath |
0:fc70c47eecb4 | 672 | |
ampembeng | 15:6f2798e45099 | 673 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 674 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 675 | |
ampembeng | 15:6f2798e45099 | 676 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 677 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 678 | } |
Janos Follath |
0:fc70c47eecb4 | 679 | |
ampembeng | 15:6f2798e45099 | 680 | return rc; |
ampembeng | 15:6f2798e45099 | 681 | } |