![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
V.062 11/3
Dependencies: FT6206 SDFileSystem ILI9341_t3
Fork of ATT_AWS_IoT_demo_v06 by
main.cpp@30:8c979815f533, 2017-11-15 (annotated)
- Committer:
- eggsylah
- Date:
- Wed Nov 15 19:28:37 2017 +0000
- Revision:
- 30:8c979815f533
- Parent:
- 29:f71a0be59b99
- Child:
- 31:255a2c6f8f47
V08T
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 | |
ampembeng | 18:6370da1de572 | 7 | // SD File System |
ampembeng | 18:6370da1de572 | 8 | #include "SDFileSystem.h" |
ampembeng | 18:6370da1de572 | 9 | |
ampembeng | 15:6f2798e45099 | 10 | // Serial extension |
ampembeng | 15:6f2798e45099 | 11 | #include "MODSERIAL.h" |
Janos Follath |
0:fc70c47eecb4 | 12 | |
ampembeng | 15:6f2798e45099 | 13 | // Network includes |
ampembeng | 15:6f2798e45099 | 14 | #include "WNCInterface.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 | |
jilee | 29:f71a0be59b99 | 25 | #include "mbedtls/net.h" |
jilee | 29:f71a0be59b99 | 26 | #include "mbedtls/ssl.h" |
jilee | 29:f71a0be59b99 | 27 | #include "mbedtls/entropy.h" |
jilee | 29:f71a0be59b99 | 28 | #include "mbedtls/ctr_drbg.h" |
jilee | 29:f71a0be59b99 | 29 | #include "mbedtls/certs.h" |
jilee | 29:f71a0be59b99 | 30 | #include "mbedtls/x509.h" |
jilee | 29:f71a0be59b99 | 31 | #include "mbedtls/error.h" |
jilee | 29:f71a0be59b99 | 32 | #include "mbedtls/debug.h" |
jilee | 29:f71a0be59b99 | 33 | #include "mbedtls/timing.h" |
jilee | 29:f71a0be59b99 | 34 | #include "mbedtls/net_sockets.h" |
jilee | 29:f71a0be59b99 | 35 | #include "pem.h" |
jilee | 29:f71a0be59b99 | 36 | |
eggsylah | 30:8c979815f533 | 37 | #include "TextField.h" |
eggsylah | 30:8c979815f533 | 38 | |
eggsylah | 30:8c979815f533 | 39 | //TFT + Touch Panel |
eggsylah | 30:8c979815f533 | 40 | #include "ILI9341_t3.h" |
eggsylah | 30:8c979815f533 | 41 | #include "font_Arial.h" |
eggsylah | 30:8c979815f533 | 42 | #include "font_ArialBold.h" |
eggsylah | 30:8c979815f533 | 43 | #include "font_LiberationSans.h" |
eggsylah | 30:8c979815f533 | 44 | #include "font_LiberationSansBold.h" |
eggsylah | 30:8c979815f533 | 45 | #include "font_LiberationSansNarrow.h" |
eggsylah | 30:8c979815f533 | 46 | #include "font_LiberationSansNarrowBold.h" |
eggsylah | 30:8c979815f533 | 47 | |
eggsylah | 30:8c979815f533 | 48 | #define MAIN_FONT Arial_14 |
eggsylah | 30:8c979815f533 | 49 | //#define MAIN_FONT LiberationSansNarrow_14_Bold |
eggsylah | 30:8c979815f533 | 50 | //#define MAIN_FONT LiberationSans_14_Bold |
eggsylah | 30:8c979815f533 | 51 | #define BUTTON_FONT LiberationSansNarrow_24_Bold |
eggsylah | 30:8c979815f533 | 52 | #define DEFAULT_TEXT_COLOR ILI9341_CYAN |
eggsylah | 30:8c979815f533 | 53 | |
eggsylah | 30:8c979815f533 | 54 | #define ILI9341_ATT 0x04fb |
eggsylah | 30:8c979815f533 | 55 | |
eggsylah | 30:8c979815f533 | 56 | #include "logo.h" |
eggsylah | 30:8c979815f533 | 57 | #include "FT6206.h" |
jilee | 29:f71a0be59b99 | 58 | |
ampembeng | 20:ee34856ae510 | 59 | // Sensors |
ampembeng | 20:ee34856ae510 | 60 | #include "HTS221.h" |
ampembeng | 20:ee34856ae510 | 61 | |
Janos Follath |
0:fc70c47eecb4 | 62 | #if DEBUG_LEVEL > 0 |
Janos Follath |
0:fc70c47eecb4 | 63 | #include "mbedtls/debug.h" |
Janos Follath |
0:fc70c47eecb4 | 64 | #endif |
Janos Follath |
0:fc70c47eecb4 | 65 | |
jilee | 28:54d9a550adf1 | 66 | |
ampembeng | 15:6f2798e45099 | 67 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 68 | // |
ampembeng | 15:6f2798e45099 | 69 | // Defines |
ampembeng | 15:6f2798e45099 | 70 | // |
ampembeng | 15:6f2798e45099 | 71 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 72 | // LED Colors |
ampembeng | 15:6f2798e45099 | 73 | #define COLOR_OFF 0x00 |
ampembeng | 15:6f2798e45099 | 74 | #define COLOR_RED 0x01 |
ampembeng | 15:6f2798e45099 | 75 | #define COLOR_GREEN 0x02 |
ampembeng | 15:6f2798e45099 | 76 | #define COLOR_BLUE 0x04 |
ampembeng | 15:6f2798e45099 | 77 | #define COLOR_WHITE 0x07 |
ampembeng | 15:6f2798e45099 | 78 | #define NUM_COLORS 5 |
Janos Follath |
0:fc70c47eecb4 | 79 | |
ampembeng | 15:6f2798e45099 | 80 | // AWS defines |
ampembeng | 18:6370da1de572 | 81 | #define PATH_MAX 1024 |
ampembeng | 15:6f2798e45099 | 82 | #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // NOTE: Be wary of this if your JSON doc grows |
jilee | 29:f71a0be59b99 | 83 | #define SHADOW_SYNC_INTERVAL 6.0 // How often we sync with AWS Shadow (in seconds) |
Janos Follath |
0:fc70c47eecb4 | 84 | |
ampembeng | 15:6f2798e45099 | 85 | // Comment out the following line if color is not supported on the terminal |
ampembeng | 15:6f2798e45099 | 86 | //#define USE_COLOR |
ampembeng | 15:6f2798e45099 | 87 | #ifdef USE_COLOR |
ampembeng | 15:6f2798e45099 | 88 | #define BLK "\033[30m" |
ampembeng | 15:6f2798e45099 | 89 | #define RED "\033[31m" |
ampembeng | 15:6f2798e45099 | 90 | #define GRN "\033[32m" |
ampembeng | 15:6f2798e45099 | 91 | #define YEL "\033[33m" |
ampembeng | 15:6f2798e45099 | 92 | #define BLU "\033[34m" |
ampembeng | 15:6f2798e45099 | 93 | #define MAG "\033[35m" |
ampembeng | 15:6f2798e45099 | 94 | #define CYN "\033[36m" |
ampembeng | 15:6f2798e45099 | 95 | #define WHT "\033[37m" |
ampembeng | 15:6f2798e45099 | 96 | #define DEF "\033[39m" |
ampembeng | 15:6f2798e45099 | 97 | #else |
ampembeng | 15:6f2798e45099 | 98 | #define BLK |
ampembeng | 15:6f2798e45099 | 99 | #define RED |
ampembeng | 15:6f2798e45099 | 100 | #define GRN |
ampembeng | 15:6f2798e45099 | 101 | #define YEL |
ampembeng | 15:6f2798e45099 | 102 | #define BLU |
ampembeng | 15:6f2798e45099 | 103 | #define MAG |
ampembeng | 15:6f2798e45099 | 104 | #define CYN |
ampembeng | 15:6f2798e45099 | 105 | #define WHT |
ampembeng | 15:6f2798e45099 | 106 | #define DEF |
Janos Follath |
0:fc70c47eecb4 | 107 | #endif |
Janos Follath |
0:fc70c47eecb4 | 108 | |
ampembeng | 20:ee34856ae510 | 109 | // Sensor defines |
ampembeng | 20:ee34856ae510 | 110 | #define CTOF(x) ((x)*1.8+32) // Temperature |
ampembeng | 20:ee34856ae510 | 111 | |
eggsylah | 30:8c979815f533 | 112 | //TFT SPI |
jilee | 29:f71a0be59b99 | 113 | #define PIN_SCLK PTD5 |
jilee | 29:f71a0be59b99 | 114 | #define PIN_MISO PTD7 |
jilee | 29:f71a0be59b99 | 115 | #define PIN_MOSI PTD6 |
jilee | 29:f71a0be59b99 | 116 | #define PIN_CS_TFT PTD4 // chip select pin |
jilee | 29:f71a0be59b99 | 117 | #define PIN_DC_TFT PTB20 // data/command select pin. |
eggsylah | 30:8c979815f533 | 118 | #define PIN_RESET_TFT PTC18 //we don't need reset so just use DC instead. Could modify library |
jilee | 28:54d9a550adf1 | 119 | |
jilee | 28:54d9a550adf1 | 120 | #define PORTRAIT 0 |
jilee | 28:54d9a550adf1 | 121 | #define LANDSCAPE 1 |
jilee | 28:54d9a550adf1 | 122 | #define LANDSCAPE_R 3 |
jilee | 28:54d9a550adf1 | 123 | |
jilee | 28:54d9a550adf1 | 124 | //#if 0 |
jilee | 28:54d9a550adf1 | 125 | /* |
jilee | 28:54d9a550adf1 | 126 | #define PIN_SCL_FT6206 PTE24 |
jilee | 28:54d9a550adf1 | 127 | #define PIN_SDA_FT6206 PTE25 |
jilee | 29:f71a0be59b99 | 128 | //#define PIN_INT_FT6206 PTC6 |
jilee | 29:f71a0be59b99 | 129 | #define PIN_INT_FT6206 PTC3 |
jilee | 28:54d9a550adf1 | 130 | */ |
jilee | 28:54d9a550adf1 | 131 | //#else |
jilee | 29:f71a0be59b99 | 132 | |
jilee | 28:54d9a550adf1 | 133 | #define PIN_SCL_FT6206 D15 |
jilee | 28:54d9a550adf1 | 134 | #define PIN_SDA_FT6206 D14 |
jilee | 28:54d9a550adf1 | 135 | #define PIN_INT_FT6206 PTC3 |
jilee | 29:f71a0be59b99 | 136 | |
jilee | 28:54d9a550adf1 | 137 | //#endif |
jilee | 28:54d9a550adf1 | 138 | |
jilee | 29:f71a0be59b99 | 139 | |
eggsylah | 30:8c979815f533 | 140 | ILI9341_t3 tft(PIN_CS_TFT, PIN_DC_TFT, PIN_RESET_TFT, PIN_MOSI, PIN_SCLK, PIN_MISO); |
jilee | 29:f71a0be59b99 | 141 | FT6206 ft6206(PIN_SDA_FT6206, PIN_SCL_FT6206, PIN_INT_FT6206); // sda, scl, int |
jilee | 28:54d9a550adf1 | 142 | |
ampembeng | 15:6f2798e45099 | 143 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 144 | // |
ampembeng | 15:6f2798e45099 | 145 | // Globals |
ampembeng | 15:6f2798e45099 | 146 | // |
ampembeng | 15:6f2798e45099 | 147 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 148 | // Controls LED color |
ampembeng | 15:6f2798e45099 | 149 | unsigned char ledColor = COLOR_OFF; |
ampembeng | 15:6f2798e45099 | 150 | |
ampembeng | 15:6f2798e45099 | 151 | // Color cycle array (used with SW3 button presses) |
ampembeng | 15:6f2798e45099 | 152 | unsigned char colorCycle[NUM_COLORS] = {COLOR_OFF, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE}; |
Janos Follath |
0:fc70c47eecb4 | 153 | |
ampembeng | 15:6f2798e45099 | 154 | // Button interrupts |
ampembeng | 15:6f2798e45099 | 155 | bool buttonOverride = false; |
ampembeng | 15:6f2798e45099 | 156 | InterruptIn Interrupt(SW3); |
ampembeng | 15:6f2798e45099 | 157 | |
ampembeng | 18:6370da1de572 | 158 | // These defines are pulled from aws_iot_config.h |
jilee | 29:f71a0be59b99 | 159 | char HostAddress[255] = AWS_IOT_MQTT_HOST1; |
ampembeng | 18:6370da1de572 | 160 | char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID; |
ampembeng | 18:6370da1de572 | 161 | char ThingName[32] = AWS_IOT_MY_THING_NAME; |
ampembeng | 18:6370da1de572 | 162 | char PortString[5] = "8883"; |
ampembeng | 15:6f2798e45099 | 163 | uint32_t port = AWS_IOT_MQTT_PORT; |
ampembeng | 23:b9ff83dc965f | 164 | char iccidName[21] = "12345678901234567890"; |
Janos Follath |
0:fc70c47eecb4 | 165 | |
ampembeng | 20:ee34856ae510 | 166 | // Sensor data |
ampembeng | 20:ee34856ae510 | 167 | float temperature = 0.0; |
ampembeng | 20:ee34856ae510 | 168 | int humidity = 0; |
eggsylah | 30:8c979815f533 | 169 | unsigned int count = 0; |
jilee | 28:54d9a550adf1 | 170 | |
jilee | 29:f71a0be59b99 | 171 | //mqqt host name |
jilee | 29:f71a0be59b99 | 172 | char *aws_iot_mqtt_host; |
jilee | 29:f71a0be59b99 | 173 | |
jilee | 29:f71a0be59b99 | 174 | //certificate |
jilee | 29:f71a0be59b99 | 175 | static mbedtls_x509_crt clicert; |
jilee | 29:f71a0be59b99 | 176 | |
ampembeng | 23:b9ff83dc965f | 177 | // Temp/humidity object |
ampembeng | 23:b9ff83dc965f | 178 | HTS221 hts221; |
ampembeng | 23:b9ff83dc965f | 179 | |
jilee | 29:f71a0be59b99 | 180 | |
eggsylah | 30:8c979815f533 | 181 | /* Mapping between COLOR_XXX defnes and the color/text for display */ |
eggsylah | 30:8c979815f533 | 182 | int16_t colourMap[] = { ILI9341_BLACK, ILI9341_RED, ILI9341_GREEN, 0, ILI9341_BLUE, 0, 0, ILI9341_WHITE }; |
eggsylah | 30:8c979815f533 | 183 | char* colourNameMap[] = {"OFF", "RED", "GREEN", "", "BLUE", "INVALID", "INVALID", "WHITE" }; |
eggsylah | 30:8c979815f533 | 184 | |
jilee | 29:f71a0be59b99 | 185 | |
ampembeng | 15:6f2798e45099 | 186 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 187 | // |
ampembeng | 15:6f2798e45099 | 188 | // Devices |
ampembeng | 15:6f2798e45099 | 189 | // |
ampembeng | 15:6f2798e45099 | 190 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 191 | // GPIOs for RGB LED |
ampembeng | 15:6f2798e45099 | 192 | DigitalOut led_green(LED_GREEN); |
ampembeng | 15:6f2798e45099 | 193 | DigitalOut led_red(LED_RED); |
ampembeng | 15:6f2798e45099 | 194 | DigitalOut led_blue(LED_BLUE); |
Janos Follath |
0:fc70c47eecb4 | 195 | |
ampembeng | 15:6f2798e45099 | 196 | // USB Serial port (to PC) |
ampembeng | 15:6f2798e45099 | 197 | MODSERIAL pc(USBTX,USBRX,256,256); |
Janos Follath |
0:fc70c47eecb4 | 198 | |
ampembeng | 18:6370da1de572 | 199 | // SD card access (MOSI, MISO, SCK, CS) |
ampembeng | 18:6370da1de572 | 200 | SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); |
ampembeng | 18:6370da1de572 | 201 | |
ampembeng | 20:ee34856ae510 | 202 | // I2C bus (SDA, SCL) |
ampembeng | 20:ee34856ae510 | 203 | I2C i2c(PTC11, PTC10); |
ampembeng | 20:ee34856ae510 | 204 | |
jilee | 29:f71a0be59b99 | 205 | extern unsigned char cSubject[100]; |
jilee | 29:f71a0be59b99 | 206 | |
jilee | 29:f71a0be59b99 | 207 | int16_t dbm; |
jilee | 29:f71a0be59b99 | 208 | unsigned char cUpdateStatus = 0x00; |
jilee | 29:f71a0be59b99 | 209 | |
ampembeng | 15:6f2798e45099 | 210 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 211 | // |
ampembeng | 15:6f2798e45099 | 212 | // Functions |
ampembeng | 15:6f2798e45099 | 213 | // |
ampembeng | 15:6f2798e45099 | 214 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 215 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 216 | //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h |
ampembeng | 15:6f2798e45099 | 217 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 218 | void pc_print(const char * format, ...) |
ampembeng | 15:6f2798e45099 | 219 | { |
ampembeng | 15:6f2798e45099 | 220 | va_list vl; |
ampembeng | 15:6f2798e45099 | 221 | va_start(vl, format); |
ampembeng | 15:6f2798e45099 | 222 | pc.vprintf(format, vl); |
ampembeng | 15:6f2798e45099 | 223 | va_end(vl); |
ampembeng | 15:6f2798e45099 | 224 | } |
Janos Follath |
0:fc70c47eecb4 | 225 | |
ampembeng | 15:6f2798e45099 | 226 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 227 | //* Set the RGB LED's Color |
ampembeng | 15:6f2798e45099 | 228 | //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) |
ampembeng | 15:6f2798e45099 | 229 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 230 | void SetLedColor(unsigned char ucColor) |
ampembeng | 18:6370da1de572 | 231 | { |
ampembeng | 15:6f2798e45099 | 232 | //Note that when an LED is on, you write a 0 to it: |
ampembeng | 15:6f2798e45099 | 233 | led_red = !(ucColor & 0x1); //bit 0 |
ampembeng | 15:6f2798e45099 | 234 | led_green = !(ucColor & 0x2); //bit 1 |
ampembeng | 15:6f2798e45099 | 235 | led_blue = !(ucColor & 0x4); //bit 2 |
ampembeng | 15:6f2798e45099 | 236 | } |
Janos Follath |
0:fc70c47eecb4 | 237 | |
ampembeng | 15:6f2798e45099 | 238 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 239 | //* SW3 Button handler. Finds the current LED color and sets the button to the next color in colorCycle[] |
ampembeng | 15:6f2798e45099 | 240 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 241 | void sw3ButtonHandler() |
ampembeng | 15:6f2798e45099 | 242 | { |
ampembeng | 15:6f2798e45099 | 243 | int i; |
ampembeng | 15:6f2798e45099 | 244 | for(i=0; i < NUM_COLORS; i++) { |
ampembeng | 15:6f2798e45099 | 245 | if (ledColor == colorCycle[i]) |
ampembeng | 15:6f2798e45099 | 246 | break; |
ampembeng | 15:6f2798e45099 | 247 | } |
ampembeng | 15:6f2798e45099 | 248 | |
ampembeng | 15:6f2798e45099 | 249 | // (circular-queue) |
ampembeng | 15:6f2798e45099 | 250 | if (++i == NUM_COLORS) |
ampembeng | 15:6f2798e45099 | 251 | i = 0; |
ampembeng | 15:6f2798e45099 | 252 | |
ampembeng | 15:6f2798e45099 | 253 | ledColor = colorCycle[i]; |
ampembeng | 15:6f2798e45099 | 254 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 255 | buttonOverride = true; |
ampembeng | 15:6f2798e45099 | 256 | } |
Janos Follath |
0:fc70c47eecb4 | 257 | |
ampembeng | 23:b9ff83dc965f | 258 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 259 | //* Print LED and sensor data |
ampembeng | 23:b9ff83dc965f | 260 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 261 | void printData() |
ampembeng | 23:b9ff83dc965f | 262 | { |
ampembeng | 23:b9ff83dc965f | 263 | INFO("Temperature is: %0.2f F", temperature); |
eggsylah | 30:8c979815f533 | 264 | INFO("Humidity is: %02d", humidity); |
eggsylah | 30:8c979815f533 | 265 | INFO("LED: %s", colourNameMap[ledColor]); |
ampembeng | 23:b9ff83dc965f | 266 | } |
ampembeng | 23:b9ff83dc965f | 267 | |
eggsylah | 30:8c979815f533 | 268 | |
eggsylah | 30:8c979815f533 | 269 | #if 1 |
eggsylah | 30:8c979815f533 | 270 | void printDatatoTFT(bool bFirstTime) |
jilee | 29:f71a0be59b99 | 271 | { |
eggsylah | 30:8c979815f533 | 272 | static TextField texts[7]; |
jilee | 29:f71a0be59b99 | 273 | |
eggsylah | 30:8c979815f533 | 274 | if (bFirstTime) { |
eggsylah | 30:8c979815f533 | 275 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 276 | |
eggsylah | 30:8c979815f533 | 277 | tft.setCursor(0, 2); |
eggsylah | 30:8c979815f533 | 278 | tft.printf("ATT LTE: "); |
eggsylah | 30:8c979815f533 | 279 | texts[0].setFieldInt(&tft, "%d dbM", -99); |
eggsylah | 30:8c979815f533 | 280 | |
eggsylah | 30:8c979815f533 | 281 | tft.setCursor(200, 2); |
eggsylah | 30:8c979815f533 | 282 | tft.printf("AWS: "); |
eggsylah | 30:8c979815f533 | 283 | texts[1].setFieldInt(&tft, "%d", 99); |
eggsylah | 30:8c979815f533 | 284 | tft.printf("\n\n"); |
eggsylah | 30:8c979815f533 | 285 | |
eggsylah | 30:8c979815f533 | 286 | tft.setTextWrap(false); //no line wrap for CN or ICCID |
eggsylah | 30:8c979815f533 | 287 | |
eggsylah | 30:8c979815f533 | 288 | tft.printf("CN: "); |
eggsylah | 30:8c979815f533 | 289 | texts[2].setFieldLine(&tft, "%s"); |
eggsylah | 30:8c979815f533 | 290 | tft.printf("\n"); |
eggsylah | 30:8c979815f533 | 291 | |
eggsylah | 30:8c979815f533 | 292 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 293 | tft.printf("ICCID: "); |
eggsylah | 30:8c979815f533 | 294 | texts[3].setFieldLine(&tft, "%s"); |
eggsylah | 30:8c979815f533 | 295 | tft.printf("\n"); |
eggsylah | 30:8c979815f533 | 296 | |
eggsylah | 30:8c979815f533 | 297 | tft.setTextWrap(true); |
eggsylah | 30:8c979815f533 | 298 | |
eggsylah | 30:8c979815f533 | 299 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 300 | tft.printf("TEMPERATURE: "); |
eggsylah | 30:8c979815f533 | 301 | texts[4].setFieldFloat(&tft, "%0.1fF", 100.0f); |
eggsylah | 30:8c979815f533 | 302 | tft.printf("\n"); |
eggsylah | 30:8c979815f533 | 303 | |
eggsylah | 30:8c979815f533 | 304 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 305 | tft.printf("HUMIDITY: "); |
eggsylah | 30:8c979815f533 | 306 | texts[5].setFieldInt(&tft, "%02d%%", humidity); |
eggsylah | 30:8c979815f533 | 307 | tft.printf("\n"); |
eggsylah | 30:8c979815f533 | 308 | |
eggsylah | 30:8c979815f533 | 309 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 310 | tft.printf("LED: "); |
eggsylah | 30:8c979815f533 | 311 | texts[6].setFieldStr(&tft, "%s", "GREEN"); |
eggsylah | 30:8c979815f533 | 312 | tft.printf("\n"); |
jilee | 29:f71a0be59b99 | 313 | } |
eggsylah | 30:8c979815f533 | 314 | |
eggsylah | 30:8c979815f533 | 315 | /* Now update. We leave the more static ones (eg ICCID) to the end */ |
eggsylah | 30:8c979815f533 | 316 | texts[0].drawFieldInt(dbm); |
eggsylah | 30:8c979815f533 | 317 | texts[1].drawFieldInt(++count); |
eggsylah | 30:8c979815f533 | 318 | texts[4].drawFieldFloat(temperature); |
eggsylah | 30:8c979815f533 | 319 | texts[5].drawFieldInt(humidity); |
eggsylah | 30:8c979815f533 | 320 | |
eggsylah | 30:8c979815f533 | 321 | /* handle special case for "OFF" */ |
eggsylah | 30:8c979815f533 | 322 | int8_t textColor = (ledColor == COLOR_OFF) ? DEFAULT_TEXT_COLOR : colourMap[ledColor]; |
eggsylah | 30:8c979815f533 | 323 | |
eggsylah | 30:8c979815f533 | 324 | tft.setTextColor(textColor); |
eggsylah | 30:8c979815f533 | 325 | texts[6].drawFieldStr(colourNameMap[ledColor], true); |
eggsylah | 30:8c979815f533 | 326 | |
eggsylah | 30:8c979815f533 | 327 | tft.fillCircle (240,160, 27, ILI9341_DARKGREY); |
eggsylah | 30:8c979815f533 | 328 | tft.fillCircle (240,160, 25, colourMap[ledColor]); |
eggsylah | 30:8c979815f533 | 329 | tft.setTextColor(DEFAULT_TEXT_COLOR); |
eggsylah | 30:8c979815f533 | 330 | |
eggsylah | 30:8c979815f533 | 331 | tft.setTextWrap(false); |
eggsylah | 30:8c979815f533 | 332 | texts[2].drawFieldStr((const char*)cSubject, false); |
eggsylah | 30:8c979815f533 | 333 | texts[3].drawFieldStr(iccidName, false); |
eggsylah | 30:8c979815f533 | 334 | tft.setTextWrap(true); |
eggsylah | 30:8c979815f533 | 335 | |
jilee | 29:f71a0be59b99 | 336 | } |
jilee | 29:f71a0be59b99 | 337 | |
eggsylah | 30:8c979815f533 | 338 | #else |
eggsylah | 30:8c979815f533 | 339 | void printDatatoTFT(bool bFirstTime) |
jilee | 29:f71a0be59b99 | 340 | { |
eggsylah | 30:8c979815f533 | 341 | if (bFirstTime) { |
eggsylah | 30:8c979815f533 | 342 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 343 | tft.setCursor(0, 2); |
eggsylah | 30:8c979815f533 | 344 | tft.printf("ATT LTE: dBm"); |
eggsylah | 30:8c979815f533 | 345 | } |
eggsylah | 30:8c979815f533 | 346 | tft.fillRect(70, 2, 70+40, 2+14, ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 347 | tft.setCursor(70, 2); |
eggsylah | 30:8c979815f533 | 348 | tft.printf("%d", dbm); |
eggsylah | 30:8c979815f533 | 349 | |
eggsylah | 30:8c979815f533 | 350 | tft.setCursor(200, 2); |
eggsylah | 30:8c979815f533 | 351 | tft.printf("AWS: %d\n\n", ++count); |
eggsylah | 30:8c979815f533 | 352 | tft.printf("CN: %s\n", cSubject); |
eggsylah | 30:8c979815f533 | 353 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 354 | tft.printf("ICCID: %s\n", iccidName); |
eggsylah | 30:8c979815f533 | 355 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 356 | tft.printf("TEMPERATURE: %0.2f °F\n", temperature); |
eggsylah | 30:8c979815f533 | 357 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 358 | tft.printf("HUMIDITY: %02d %%\n", humidity); |
eggsylah | 30:8c979815f533 | 359 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 360 | tft.printf("LED: "); |
eggsylah | 30:8c979815f533 | 361 | |
eggsylah | 30:8c979815f533 | 362 | /* handle special case for OFF */ |
eggsylah | 30:8c979815f533 | 363 | int8_t textColor = (ledColor == COLOR_OFF) ? COLOR_WHITE : ledColor; |
jilee | 29:f71a0be59b99 | 364 | |
eggsylah | 30:8c979815f533 | 365 | tft.setTextColor(colourMap[textColor]); |
eggsylah | 30:8c979815f533 | 366 | tft.printf("%s \n", colourNameMap[ledColor]); |
eggsylah | 30:8c979815f533 | 367 | tft.fillCircle (240,160, 27, ILI9341_DARKGREY); |
eggsylah | 30:8c979815f533 | 368 | tft.fillCircle (240,160, 25, colourMap[ledColor]); |
eggsylah | 30:8c979815f533 | 369 | //tft.drawCircle (240,160, 25, colourMap[COLOR_WHITE]); |
eggsylah | 30:8c979815f533 | 370 | //tft.drawCircle (240,160, 26, colourMap[COLOR_WHITE]); |
eggsylah | 30:8c979815f533 | 371 | tft.setTextColor(DEFAULT_TEXT_COLOR); |
jilee | 29:f71a0be59b99 | 372 | } |
eggsylah | 30:8c979815f533 | 373 | #endif |
eggsylah | 30:8c979815f533 | 374 | |
jilee | 29:f71a0be59b99 | 375 | |
jilee | 29:f71a0be59b99 | 376 | void ShowINFO(const char *sInfo) |
jilee | 29:f71a0be59b99 | 377 | { |
jilee | 29:f71a0be59b99 | 378 | INFO(sInfo); |
eggsylah | 30:8c979815f533 | 379 | tft.printf(sInfo); |
eggsylah | 30:8c979815f533 | 380 | tft.printf("\n"); |
jilee | 29:f71a0be59b99 | 381 | } |
jilee | 29:f71a0be59b99 | 382 | |
jilee | 29:f71a0be59b99 | 383 | |
ampembeng | 15:6f2798e45099 | 384 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 385 | // |
ampembeng | 15:6f2798e45099 | 386 | // AWS Shadow Callbacks |
ampembeng | 15:6f2798e45099 | 387 | // |
ampembeng | 15:6f2798e45099 | 388 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 389 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 390 | //* This is the callback function that fires when an update is sent. It will print the update response status. |
ampembeng | 15:6f2798e45099 | 391 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 392 | void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, |
ampembeng | 15:6f2798e45099 | 393 | const char *pReceivedJsonDocument, void *pContextData) { |
Janos Follath |
0:fc70c47eecb4 | 394 | |
ampembeng | 15:6f2798e45099 | 395 | INFO("Shadow Update Status Callback"); |
eggsylah | 30:8c979815f533 | 396 | tft.printf("\n"); |
ampembeng | 15:6f2798e45099 | 397 | if (status == SHADOW_ACK_TIMEOUT) { |
jilee | 29:f71a0be59b99 | 398 | INFO("Update Timeout"); |
ampembeng | 15:6f2798e45099 | 399 | } else if (status == SHADOW_ACK_REJECTED) { |
jilee | 29:f71a0be59b99 | 400 | INFO("Update Rejected"); |
ampembeng | 15:6f2798e45099 | 401 | } else if (status == SHADOW_ACK_ACCEPTED) { |
jilee | 29:f71a0be59b99 | 402 | INFO("Update Accepted"); // Good |
ampembeng | 15:6f2798e45099 | 403 | } |
jilee | 29:f71a0be59b99 | 404 | |
ampembeng | 15:6f2798e45099 | 405 | } |
Janos Follath |
0:fc70c47eecb4 | 406 | |
ampembeng | 15:6f2798e45099 | 407 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 408 | //* This is the callback function that fires when AWS has sends out a shadow update. |
ampembeng | 15:6f2798e45099 | 409 | //********************************************************************************************************************* |
ampembeng | 15:6f2798e45099 | 410 | void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext) { |
ampembeng | 15:6f2798e45099 | 411 | |
ampembeng | 15:6f2798e45099 | 412 | INFO("LED Callback Detected."); |
ampembeng | 15:6f2798e45099 | 413 | |
ampembeng | 15:6f2798e45099 | 414 | if (pContext != NULL) { |
ampembeng | 15:6f2798e45099 | 415 | switch (*(unsigned char *)(pContext->pData)){ |
ampembeng | 15:6f2798e45099 | 416 | case COLOR_OFF: |
ampembeng | 15:6f2798e45099 | 417 | INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 418 | break; |
ampembeng | 15:6f2798e45099 | 419 | case COLOR_RED: |
ampembeng | 15:6f2798e45099 | 420 | INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 421 | break; |
ampembeng | 15:6f2798e45099 | 422 | case COLOR_GREEN: |
ampembeng | 15:6f2798e45099 | 423 | INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 424 | break; |
ampembeng | 15:6f2798e45099 | 425 | case COLOR_BLUE: |
ampembeng | 15:6f2798e45099 | 426 | INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 427 | break; |
ampembeng | 15:6f2798e45099 | 428 | case COLOR_WHITE: |
ampembeng | 15:6f2798e45099 | 429 | INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData)); |
ampembeng | 15:6f2798e45099 | 430 | break; |
Janos Follath |
0:fc70c47eecb4 | 431 | } |
ampembeng | 15:6f2798e45099 | 432 | } |
ampembeng | 15:6f2798e45099 | 433 | else { |
ampembeng | 15:6f2798e45099 | 434 | INFO("pContext was detected as NULL"); |
Janos Follath |
0:fc70c47eecb4 | 435 | } |
ampembeng | 15:6f2798e45099 | 436 | } |
ampembeng | 23:b9ff83dc965f | 437 | |
ampembeng | 23:b9ff83dc965f | 438 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 439 | //* Subscribe callback (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 440 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 441 | int MQTTcallbackHandler(MQTTCallbackParams params) { |
ampembeng | 23:b9ff83dc965f | 442 | |
ampembeng | 23:b9ff83dc965f | 443 | INFO("Subscribe callback"); |
ampembeng | 23:b9ff83dc965f | 444 | INFO("%.*s\t%.*s", |
ampembeng | 23:b9ff83dc965f | 445 | (int)params.TopicNameLen, params.pTopicName, |
ampembeng | 23:b9ff83dc965f | 446 | (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload); |
ampembeng | 23:b9ff83dc965f | 447 | |
ampembeng | 23:b9ff83dc965f | 448 | return 0; |
ampembeng | 23:b9ff83dc965f | 449 | } |
ampembeng | 23:b9ff83dc965f | 450 | |
ampembeng | 23:b9ff83dc965f | 451 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 452 | //* Disconnect handling (used with alternate demo) |
ampembeng | 23:b9ff83dc965f | 453 | //********************************************************************************************************************* |
ampembeng | 23:b9ff83dc965f | 454 | void disconnectCallbackHandler(void) { |
ampembeng | 23:b9ff83dc965f | 455 | WARN("MQTT Disconnect"); |
ampembeng | 23:b9ff83dc965f | 456 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 457 | if(aws_iot_is_autoreconnect_enabled()){ |
ampembeng | 23:b9ff83dc965f | 458 | INFO("Auto Reconnect is enabled, Reconnecting attempt will start now"); |
ampembeng | 23:b9ff83dc965f | 459 | }else{ |
ampembeng | 23:b9ff83dc965f | 460 | WARN("Auto Reconnect not enabled. Starting manual reconnect..."); |
ampembeng | 23:b9ff83dc965f | 461 | rc = aws_iot_mqtt_attempt_reconnect(); |
ampembeng | 23:b9ff83dc965f | 462 | if(RECONNECT_SUCCESSFUL == rc){ |
ampembeng | 23:b9ff83dc965f | 463 | WARN("Manual Reconnect Successful"); |
ampembeng | 23:b9ff83dc965f | 464 | }else{ |
ampembeng | 23:b9ff83dc965f | 465 | WARN("Manual Reconnect Failed - %d", rc); |
ampembeng | 23:b9ff83dc965f | 466 | } |
ampembeng | 23:b9ff83dc965f | 467 | } |
ampembeng | 23:b9ff83dc965f | 468 | } |
ampembeng | 23:b9ff83dc965f | 469 | |
ampembeng | 23:b9ff83dc965f | 470 | //===================================================================================================================== |
ampembeng | 23:b9ff83dc965f | 471 | // |
ampembeng | 23:b9ff83dc965f | 472 | // 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 | 473 | // AWS device shadow, it publishes to an AWS Rule. The Rule is setup to store data to a DynamoDB, and |
ampembeng | 23:b9ff83dc965f | 474 | // the demo S3 website pulls that data from the DynamoDB and displays it. |
ampembeng | 23:b9ff83dc965f | 475 | // |
ampembeng | 23:b9ff83dc965f | 476 | //===================================================================================================================== |
ampembeng | 23:b9ff83dc965f | 477 | int outOfBoxDemo() { |
ampembeng | 23:b9ff83dc965f | 478 | INFO("Running Out-of-Box Function (alternate demo)."); |
ampembeng | 23:b9ff83dc965f | 479 | |
ampembeng | 23:b9ff83dc965f | 480 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 23:b9ff83dc965f | 481 | int publishCount = 0; |
ampembeng | 23:b9ff83dc965f | 482 | bool infinitePublishFlag = true; |
ampembeng | 23:b9ff83dc965f | 483 | char cPayload[100]; |
ampembeng | 23:b9ff83dc965f | 484 | char cTopic[100]; |
ampembeng | 23:b9ff83dc965f | 485 | const string colorStrings[] = {"Off", "Red", "Green", "", "Blue", "", "", "White"}; |
ampembeng | 24:224c07ec3bd0 | 486 | float updateInterval = 1.0; // seconds |
ampembeng | 23:b9ff83dc965f | 487 | |
ampembeng | 23:b9ff83dc965f | 488 | MQTTConnectParams connectParams = MQTTConnectParamsDefault; |
ampembeng | 23:b9ff83dc965f | 489 | connectParams.KeepAliveInterval_sec = 10; |
ampembeng | 23:b9ff83dc965f | 490 | connectParams.isCleansession = true; |
ampembeng | 23:b9ff83dc965f | 491 | connectParams.MQTTVersion = MQTT_3_1_1; |
ampembeng | 23:b9ff83dc965f | 492 | connectParams.pClientID = iccidName; // Using ICCID for unique Client ID |
ampembeng | 23:b9ff83dc965f | 493 | connectParams.pHostURL = HostAddress; |
ampembeng | 23:b9ff83dc965f | 494 | connectParams.port = port; |
ampembeng | 23:b9ff83dc965f | 495 | connectParams.isWillMsgPresent = false; |
ampembeng | 23:b9ff83dc965f | 496 | connectParams.pRootCALocation = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 23:b9ff83dc965f | 497 | connectParams.pDeviceCertLocation = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 498 | connectParams.pDevicePrivateKeyLocation = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 499 | connectParams.mqttCommandTimeout_ms = 10000; |
ampembeng | 23:b9ff83dc965f | 500 | connectParams.tlsHandshakeTimeout_ms = 10000; |
ampembeng | 23:b9ff83dc965f | 501 | connectParams.isSSLHostnameVerify = true; // ensure this is set to true for production |
ampembeng | 23:b9ff83dc965f | 502 | connectParams.disconnectHandler = disconnectCallbackHandler; |
ampembeng | 23:b9ff83dc965f | 503 | |
ampembeng | 23:b9ff83dc965f | 504 | INFO("Connecting..."); |
ampembeng | 23:b9ff83dc965f | 505 | rc = aws_iot_mqtt_connect(&connectParams); |
ampembeng | 23:b9ff83dc965f | 506 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 507 | ERROR("Error(%d) connecting to %s:%d", rc, connectParams.pHostURL, connectParams.port); |
ampembeng | 23:b9ff83dc965f | 508 | } |
ampembeng | 23:b9ff83dc965f | 509 | |
ampembeng | 23:b9ff83dc965f | 510 | /* |
ampembeng | 23:b9ff83dc965f | 511 | * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 23:b9ff83dc965f | 512 | * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 23:b9ff83dc965f | 513 | * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 23:b9ff83dc965f | 514 | */ |
ampembeng | 23:b9ff83dc965f | 515 | INFO("Set Auto Reconnect..."); |
ampembeng | 23:b9ff83dc965f | 516 | rc = aws_iot_mqtt_autoreconnect_set_status(true); |
ampembeng | 23:b9ff83dc965f | 517 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 518 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
ampembeng | 23:b9ff83dc965f | 519 | return rc; |
ampembeng | 23:b9ff83dc965f | 520 | } |
ampembeng | 23:b9ff83dc965f | 521 | |
ampembeng | 23:b9ff83dc965f | 522 | // Comment this in if you want to subscribe |
ampembeng | 23:b9ff83dc965f | 523 | /*MQTTSubscribeParams subParams = MQTTSubscribeParamsDefault; |
ampembeng | 23:b9ff83dc965f | 524 | subParams.mHandler = MQTTcallbackHandler; |
ampembeng | 23:b9ff83dc965f | 525 | subParams.pTopic = "sdkTest/sub"; |
ampembeng | 23:b9ff83dc965f | 526 | subParams.qos = QOS_0; |
ampembeng | 23:b9ff83dc965f | 527 | |
ampembeng | 23:b9ff83dc965f | 528 | if (NONE_ERROR == rc) { |
ampembeng | 23:b9ff83dc965f | 529 | INFO("Subscribing..."); |
ampembeng | 23:b9ff83dc965f | 530 | rc = aws_iot_mqtt_subscribe(&subParams); |
ampembeng | 23:b9ff83dc965f | 531 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 532 | ERROR("Error subscribing"); |
ampembeng | 23:b9ff83dc965f | 533 | } |
ampembeng | 23:b9ff83dc965f | 534 | }*/ |
ampembeng | 23:b9ff83dc965f | 535 | |
ampembeng | 23:b9ff83dc965f | 536 | // Initializ the payload |
ampembeng | 23:b9ff83dc965f | 537 | MQTTMessageParams Msg = MQTTMessageParamsDefault; |
ampembeng | 23:b9ff83dc965f | 538 | Msg.qos = QOS_0; |
ampembeng | 23:b9ff83dc965f | 539 | Msg.pPayload = (void *) cPayload; |
ampembeng | 23:b9ff83dc965f | 540 | |
ampembeng | 23:b9ff83dc965f | 541 | MQTTPublishParams Params = MQTTPublishParamsDefault; |
ampembeng | 23:b9ff83dc965f | 542 | |
ampembeng | 23:b9ff83dc965f | 543 | // Sets up the topic to publish to |
ampembeng | 23:b9ff83dc965f | 544 | sprintf(cTopic, AWS_IOT_MY_TOPIC, iccidName); |
ampembeng | 23:b9ff83dc965f | 545 | Params.pTopic = cTopic; |
ampembeng | 23:b9ff83dc965f | 546 | |
ampembeng | 23:b9ff83dc965f | 547 | if (publishCount != 0) { |
ampembeng | 23:b9ff83dc965f | 548 | infinitePublishFlag = false; |
ampembeng | 23:b9ff83dc965f | 549 | } |
ampembeng | 23:b9ff83dc965f | 550 | |
ampembeng | 23:b9ff83dc965f | 551 | INFO("READY TO PUBLISH! Press SW3 button to publish current data."); |
ampembeng | 23:b9ff83dc965f | 552 | while ((NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) |
ampembeng | 23:b9ff83dc965f | 553 | && (publishCount > 0 || infinitePublishFlag)) { |
ampembeng | 23:b9ff83dc965f | 554 | |
ampembeng | 23:b9ff83dc965f | 555 | // Max time the yield function will wait for read messages |
ampembeng | 23:b9ff83dc965f | 556 | rc = aws_iot_mqtt_yield(100); |
ampembeng | 23:b9ff83dc965f | 557 | if(NETWORK_ATTEMPTING_RECONNECT == rc){ |
ampembeng | 23:b9ff83dc965f | 558 | INFO("--> sleep (attempting to reconnect)"); |
ampembeng | 23:b9ff83dc965f | 559 | wait(1); |
ampembeng | 23:b9ff83dc965f | 560 | // If the client is attempting to reconnect we will skip the rest of the loop. |
ampembeng | 23:b9ff83dc965f | 561 | continue; |
ampembeng | 23:b9ff83dc965f | 562 | } |
ampembeng | 23:b9ff83dc965f | 563 | |
ampembeng | 23:b9ff83dc965f | 564 | // Whenever the software button (SW3) is pressed the LED will changes color and this will |
ampembeng | 23:b9ff83dc965f | 565 | // trigger a publish to the AWS topic specified. |
ampembeng | 23:b9ff83dc965f | 566 | if (buttonOverride) { |
ampembeng | 23:b9ff83dc965f | 567 | buttonOverride = false; |
ampembeng | 23:b9ff83dc965f | 568 | |
ampembeng | 23:b9ff83dc965f | 569 | // Get temp/humidity values |
ampembeng | 23:b9ff83dc965f | 570 | temperature = CTOF(hts221.readTemperature()); |
ampembeng | 23:b9ff83dc965f | 571 | humidity = hts221.readHumidity(); |
ampembeng | 23:b9ff83dc965f | 572 | |
ampembeng | 23:b9ff83dc965f | 573 | // Loading data into JSON format |
ampembeng | 23:b9ff83dc965f | 574 | sprintf(cPayload, "{\"color\":\"%s\",\"temperature\":%f,\"humidity\":%d}", colorStrings[ledColor], temperature, humidity); |
ampembeng | 23:b9ff83dc965f | 575 | Msg.PayloadLen = strlen(cPayload) + 1; |
ampembeng | 23:b9ff83dc965f | 576 | Params.MessageParams = Msg; |
ampembeng | 23:b9ff83dc965f | 577 | |
ampembeng | 23:b9ff83dc965f | 578 | // Publish |
ampembeng | 23:b9ff83dc965f | 579 | rc = aws_iot_mqtt_publish(&Params); |
ampembeng | 23:b9ff83dc965f | 580 | if (publishCount > 0) { |
ampembeng | 23:b9ff83dc965f | 581 | publishCount--; |
ampembeng | 23:b9ff83dc965f | 582 | } |
ampembeng | 23:b9ff83dc965f | 583 | |
ampembeng | 23:b9ff83dc965f | 584 | printData(); |
ampembeng | 23:b9ff83dc965f | 585 | INFO("--> Update sent. Sleep for %f seconds", updateInterval); |
ampembeng | 23:b9ff83dc965f | 586 | wait(updateInterval-.02); |
ampembeng | 23:b9ff83dc965f | 587 | } |
ampembeng | 23:b9ff83dc965f | 588 | else { |
ampembeng | 23:b9ff83dc965f | 589 | wait(.3); // 300 ms |
ampembeng | 23:b9ff83dc965f | 590 | } |
ampembeng | 23:b9ff83dc965f | 591 | } |
ampembeng | 23:b9ff83dc965f | 592 | |
ampembeng | 23:b9ff83dc965f | 593 | if (NONE_ERROR != rc) { |
ampembeng | 23:b9ff83dc965f | 594 | ERROR("An error occurred in the loop.\n"); |
ampembeng | 23:b9ff83dc965f | 595 | } else { |
ampembeng | 23:b9ff83dc965f | 596 | INFO("Publish done\n"); |
ampembeng | 23:b9ff83dc965f | 597 | } |
ampembeng | 23:b9ff83dc965f | 598 | |
ampembeng | 23:b9ff83dc965f | 599 | return rc; |
ampembeng | 23:b9ff83dc965f | 600 | } |
ampembeng | 15:6f2798e45099 | 601 | |
jilee | 29:f71a0be59b99 | 602 | void InitTFT() |
jilee | 29:f71a0be59b99 | 603 | { |
jilee | 29:f71a0be59b99 | 604 | //Configure the display driver |
eggsylah | 30:8c979815f533 | 605 | tft.begin(); |
eggsylah | 30:8c979815f533 | 606 | tft.setTextColor(DEFAULT_TEXT_COLOR, ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 607 | tft.setRotation(3); //EBJ TODO name LANDSCAPE_R); |
eggsylah | 30:8c979815f533 | 608 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 609 | |
eggsylah | 30:8c979815f533 | 610 | tft.setFont(MAIN_FONT); |
eggsylah | 30:8c979815f533 | 611 | tft.setCursor(0, 1); |
jilee | 29:f71a0be59b99 | 612 | } |
jilee | 29:f71a0be59b99 | 613 | |
jilee | 29:f71a0be59b99 | 614 | |
jilee | 29:f71a0be59b99 | 615 | int DoAWSThingMenu() |
eggsylah | 30:8c979815f533 | 616 | { |
eggsylah | 30:8c979815f533 | 617 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 618 | INFO("AWS Host Selection\n"); |
eggsylah | 30:8c979815f533 | 619 | tft.setCursor(65, 1); |
eggsylah | 30:8c979815f533 | 620 | tft.printf("SELECT AWS HOST\n"); |
jilee | 29:f71a0be59b99 | 621 | |
eggsylah | 30:8c979815f533 | 622 | tft.setFont(BUTTON_FONT); |
eggsylah | 30:8c979815f533 | 623 | tft.fillCircle (160,75, 40, ILI9341_ATT); |
eggsylah | 30:8c979815f533 | 624 | tft.setCursor(132, 63); |
eggsylah | 30:8c979815f533 | 625 | tft.setTextColor(ILI9341_WHITE); |
eggsylah | 30:8c979815f533 | 626 | tft.printf("ATT"); |
eggsylah | 30:8c979815f533 | 627 | tft.fillCircle (160,160, 40, ILI9341_OLIVE); |
eggsylah | 30:8c979815f533 | 628 | tft.setCursor(140, 148); |
eggsylah | 30:8c979815f533 | 629 | tft.printf("GD"); |
eggsylah | 30:8c979815f533 | 630 | tft.setFont(MAIN_FONT); |
jilee | 29:f71a0be59b99 | 631 | |
eggsylah | 30:8c979815f533 | 632 | int X1, Y1; |
jilee | 29:f71a0be59b99 | 633 | TS_Point p; |
jilee | 29:f71a0be59b99 | 634 | |
jilee | 29:f71a0be59b99 | 635 | while(1) |
jilee | 29:f71a0be59b99 | 636 | { |
jilee | 29:f71a0be59b99 | 637 | if(ft6206.touched()) |
jilee | 29:f71a0be59b99 | 638 | { |
jilee | 29:f71a0be59b99 | 639 | p = ft6206.getPoint(); |
eggsylah | 30:8c979815f533 | 640 | X1 = tft.width()-p.x; |
eggsylah | 30:8c979815f533 | 641 | Y1 = tft.height()-p.y; |
jilee | 29:f71a0be59b99 | 642 | |
jilee | 29:f71a0be59b99 | 643 | //pc.printf("Touched at x=%3d y=%3d\n", p.x, p.y); |
jilee | 29:f71a0be59b99 | 644 | //pc.printf("Touched actual at x=%3d y=%3d\n", X1, Y1); |
jilee | 29:f71a0be59b99 | 645 | |
jilee | 29:f71a0be59b99 | 646 | if ((X1 > 120) && (X1 < 200) && (Y1 > 35) && (Y1 <115)) |
jilee | 29:f71a0be59b99 | 647 | { |
jilee | 29:f71a0be59b99 | 648 | INFO("ATT selected\r\n"); |
eggsylah | 30:8c979815f533 | 649 | return AWS_HOST1; |
jilee | 29:f71a0be59b99 | 650 | } |
jilee | 29:f71a0be59b99 | 651 | |
jilee | 29:f71a0be59b99 | 652 | if ((X1 > 120) && (X1 < 200) && (Y1 > 120) && (Y1 < 200)) |
jilee | 29:f71a0be59b99 | 653 | { |
jilee | 29:f71a0be59b99 | 654 | INFO ("GD selected\n"); |
eggsylah | 30:8c979815f533 | 655 | return AWS_HOST2; |
jilee | 29:f71a0be59b99 | 656 | } |
jilee | 29:f71a0be59b99 | 657 | |
jilee | 29:f71a0be59b99 | 658 | //ft6206.clearPoint(); |
jilee | 29:f71a0be59b99 | 659 | } |
jilee | 29:f71a0be59b99 | 660 | |
jilee | 29:f71a0be59b99 | 661 | } |
eggsylah | 30:8c979815f533 | 662 | |
eggsylah | 30:8c979815f533 | 663 | /* unreachable code */ |
jilee | 29:f71a0be59b99 | 664 | pc.printf ("leaving menu\n"); |
jilee | 29:f71a0be59b99 | 665 | wait (10.0); |
eggsylah | 30:8c979815f533 | 666 | tft.setTextColor(DEFAULT_TEXT_COLOR); |
jilee | 29:f71a0be59b99 | 667 | return 0; |
jilee | 29:f71a0be59b99 | 668 | } |
jilee | 28:54d9a550adf1 | 669 | |
ampembeng | 15:6f2798e45099 | 670 | //===================================================================================================================== |
ampembeng | 15:6f2798e45099 | 671 | // |
ampembeng | 15:6f2798e45099 | 672 | // Main |
ampembeng | 15:6f2798e45099 | 673 | // |
ampembeng | 15:6f2798e45099 | 674 | //===================================================================================================================== |
jilee | 29:f71a0be59b99 | 675 | int main() |
jilee | 29:f71a0be59b99 | 676 | { |
jilee | 29:f71a0be59b99 | 677 | bool bFirstTime = true; |
jilee | 29:f71a0be59b99 | 678 | |
jilee | 29:f71a0be59b99 | 679 | //Init Screen |
jilee | 29:f71a0be59b99 | 680 | InitTFT(); |
ampembeng | 15:6f2798e45099 | 681 | |
ampembeng | 15:6f2798e45099 | 682 | // Set baud rate for PC Serial |
ampembeng | 15:6f2798e45099 | 683 | pc.baud(115200); |
eggsylah | 30:8c979815f533 | 684 | ShowINFO("AT&T AWS IoT Demo V.08T"); //T for test AWS account config |
jilee | 29:f71a0be59b99 | 685 | |
eggsylah | 30:8c979815f533 | 686 | // Draw logos |
eggsylah | 30:8c979815f533 | 687 | tft.writeRect2BPP(38, 33, 234, 96, att, att_palette); |
eggsylah | 30:8c979815f533 | 688 | tft.writeRect2BPP(0, 165, 320, 56, gd, gd_palette); |
eggsylah | 30:8c979815f533 | 689 | |
eggsylah | 30:8c979815f533 | 690 | tft.setFont(MAIN_FONT); |
jilee | 29:f71a0be59b99 | 691 | |
ampembeng | 20:ee34856ae510 | 692 | int i; |
ampembeng | 23:b9ff83dc965f | 693 | IoT_Error_t rc = NONE_ERROR; |
ampembeng | 15:6f2798e45099 | 694 | char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; |
ampembeng | 15:6f2798e45099 | 695 | size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); |
ampembeng | 15:6f2798e45099 | 696 | |
ampembeng | 15:6f2798e45099 | 697 | // JSON struct for LED control |
ampembeng | 15:6f2798e45099 | 698 | jsonStruct_t ledController; |
ampembeng | 15:6f2798e45099 | 699 | ledController.cb = ledControl_Callback; |
ampembeng | 15:6f2798e45099 | 700 | ledController.pData = &ledColor; |
ampembeng | 15:6f2798e45099 | 701 | ledController.pKey = "ledColor"; |
ampembeng | 15:6f2798e45099 | 702 | ledController.type = SHADOW_JSON_UINT8; |
ampembeng | 15:6f2798e45099 | 703 | |
ampembeng | 20:ee34856ae510 | 704 | // JSON struct for temperature\humidity readings |
ampembeng | 15:6f2798e45099 | 705 | jsonStruct_t temperatureHandler; |
ampembeng | 15:6f2798e45099 | 706 | temperatureHandler.cb = NULL; |
ampembeng | 15:6f2798e45099 | 707 | temperatureHandler.pKey = "temperature"; |
ampembeng | 15:6f2798e45099 | 708 | temperatureHandler.pData = &temperature; |
ampembeng | 15:6f2798e45099 | 709 | temperatureHandler.type = SHADOW_JSON_FLOAT; |
ampembeng | 20:ee34856ae510 | 710 | |
ampembeng | 20:ee34856ae510 | 711 | jsonStruct_t humidityHandler; |
ampembeng | 20:ee34856ae510 | 712 | humidityHandler.cb = NULL; |
ampembeng | 20:ee34856ae510 | 713 | humidityHandler.pKey = "humidity"; |
ampembeng | 20:ee34856ae510 | 714 | humidityHandler.pData = &humidity; |
ampembeng | 20:ee34856ae510 | 715 | humidityHandler.type = SHADOW_JSON_INT16; |
ampembeng | 15:6f2798e45099 | 716 | |
jilee | 28:54d9a550adf1 | 717 | jsonStruct_t iccidHandler; |
jilee | 28:54d9a550adf1 | 718 | iccidHandler.cb = NULL; |
jilee | 28:54d9a550adf1 | 719 | iccidHandler.pKey = "iccid"; |
jilee | 28:54d9a550adf1 | 720 | iccidHandler.pData = iccidName; |
jilee | 28:54d9a550adf1 | 721 | iccidHandler.type = SHADOW_JSON_STRING; |
jilee | 28:54d9a550adf1 | 722 | |
ampembeng | 15:6f2798e45099 | 723 | INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); |
jilee | 29:f71a0be59b99 | 724 | |
ampembeng | 15:6f2798e45099 | 725 | |
ampembeng | 18:6370da1de572 | 726 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 727 | // Paths for certs from SD card |
ampembeng | 18:6370da1de572 | 728 | INFO("Using SD card files for AWS config."); |
ampembeng | 18:6370da1de572 | 729 | DEBUG("- mqtt config path: %s", AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 730 | DEBUG("- rootCA path: %s", AWS_IOT_ROOT_CA_FILENAME); |
ampembeng | 18:6370da1de572 | 731 | DEBUG("- clientCRT path: %s", AWS_IOT_CERTIFICATE_FILENAME); |
ampembeng | 18:6370da1de572 | 732 | DEBUG("- clientKey path: %s", AWS_IOT_PRIVATE_KEY_FILENAME); |
ampembeng | 18:6370da1de572 | 733 | #else |
jilee | 28:54d9a550adf1 | 734 | //INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); |
ampembeng | 18:6370da1de572 | 735 | #endif |
ampembeng | 15:6f2798e45099 | 736 | |
ampembeng | 16:02008a2a2569 | 737 | // Startup signal - blinks through RGBW then turns off |
ampembeng | 15:6f2798e45099 | 738 | SetLedColor(COLOR_RED); |
ampembeng | 15:6f2798e45099 | 739 | wait(.5); |
ampembeng | 15:6f2798e45099 | 740 | SetLedColor(COLOR_GREEN); |
ampembeng | 15:6f2798e45099 | 741 | wait(.5); |
ampembeng | 15:6f2798e45099 | 742 | SetLedColor(COLOR_BLUE); |
ampembeng | 15:6f2798e45099 | 743 | wait(.5); |
ampembeng | 16:02008a2a2569 | 744 | SetLedColor(COLOR_WHITE); |
ampembeng | 16:02008a2a2569 | 745 | wait(.5); |
ampembeng | 16:02008a2a2569 | 746 | SetLedColor(COLOR_OFF); |
ampembeng | 20:ee34856ae510 | 747 | |
eggsylah | 30:8c979815f533 | 748 | |
ampembeng | 20:ee34856ae510 | 749 | // Initialize sensors |
ampembeng | 23:b9ff83dc965f | 750 | INFO("Init sensors..."); |
ampembeng | 20:ee34856ae510 | 751 | void hts221_init(void); |
ampembeng | 20:ee34856ae510 | 752 | i = hts221.begin(); |
ampembeng | 20:ee34856ae510 | 753 | if(!i) { |
ampembeng | 20:ee34856ae510 | 754 | WARN(RED "HTS221 NOT DETECTED!!\n\r"); |
ampembeng | 20:ee34856ae510 | 755 | } |
jilee | 29:f71a0be59b99 | 756 | |
jilee | 29:f71a0be59b99 | 757 | wait (2.0); |
jilee | 29:f71a0be59b99 | 758 | string sMQTTHostName; |
eggsylah | 30:8c979815f533 | 759 | int iSelection = DoAWSThingMenu(); |
jilee | 29:f71a0be59b99 | 760 | |
eggsylah | 30:8c979815f533 | 761 | tft.setCursor(0, 1); |
eggsylah | 30:8c979815f533 | 762 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 763 | tft.setTextColor(DEFAULT_TEXT_COLOR, ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 764 | |
jilee | 29:f71a0be59b99 | 765 | switch (iSelection) |
jilee | 29:f71a0be59b99 | 766 | { |
eggsylah | 30:8c979815f533 | 767 | case AWS_HOST1: |
jilee | 29:f71a0be59b99 | 768 | aws_iot_mqtt_host = AWS_IOT_MQTT_HOST1; |
jilee | 29:f71a0be59b99 | 769 | sMQTTHostName = "ATT"; |
eggsylah | 30:8c979815f533 | 770 | tft.printf ("ATT AWS Host Selected\n"); |
jilee | 29:f71a0be59b99 | 771 | break; |
eggsylah | 30:8c979815f533 | 772 | |
eggsylah | 30:8c979815f533 | 773 | case AWS_HOST2: |
jilee | 29:f71a0be59b99 | 774 | aws_iot_mqtt_host = AWS_IOT_MQTT_HOST2; |
jilee | 29:f71a0be59b99 | 775 | sMQTTHostName = "GD"; |
eggsylah | 30:8c979815f533 | 776 | tft.printf ("GD AWS Host Selected\n"); |
jilee | 29:f71a0be59b99 | 777 | break; |
eggsylah | 30:8c979815f533 | 778 | |
jilee | 29:f71a0be59b99 | 779 | default: |
jilee | 29:f71a0be59b99 | 780 | ShowINFO ("Unknown MQTT HOST\n"); |
eggsylah | 30:8c979815f533 | 781 | break; |
jilee | 29:f71a0be59b99 | 782 | } |
ampembeng | 18:6370da1de572 | 783 | |
eggsylah | 30:8c979815f533 | 784 | // Setup SW3 button to falling edge interrupt |
ampembeng | 23:b9ff83dc965f | 785 | INFO("Init interrupts..."); |
ampembeng | 15:6f2798e45099 | 786 | Interrupt.fall(&sw3ButtonHandler); |
jilee | 29:f71a0be59b99 | 787 | |
ampembeng | 15:6f2798e45099 | 788 | // Boot the Avnet Shield before any other operations |
ampembeng | 23:b9ff83dc965f | 789 | INFO("Net Boot..."); |
eggsylah | 30:8c979815f533 | 790 | tft.printf("Connecting to ATT LTE Network....\n"); |
jilee | 29:f71a0be59b99 | 791 | if (net_modem_boot() != 0) |
jilee | 29:f71a0be59b99 | 792 | { |
eggsylah | 30:8c979815f533 | 793 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 794 | tft.printf("\nUnable to Connect to ATT Network.\n"); |
eggsylah | 30:8c979815f533 | 795 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 796 | tft.printf("Please try again by rebooting the\ndevice.\n"); |
jilee | 29:f71a0be59b99 | 797 | return 0; |
jilee | 29:f71a0be59b99 | 798 | } |
eggsylah | 30:8c979815f533 | 799 | tft.printf("Connected to ATT LTE Network.\n"); |
eggsylah | 30:8c979815f533 | 800 | wait(3.0); |
ampembeng | 23:b9ff83dc965f | 801 | |
jilee | 29:f71a0be59b99 | 802 | |
jilee | 29:f71a0be59b99 | 803 | if (GetSignalStrength(&dbm) != 0) |
jilee | 29:f71a0be59b99 | 804 | { |
eggsylah | 30:8c979815f533 | 805 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 806 | tft.printf("\nUnable to read Signal Strength.\n"); |
eggsylah | 30:8c979815f533 | 807 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 808 | tft.printf("Please try again by rebooting the\ndevice.\n"); |
jilee | 29:f71a0be59b99 | 809 | return 0; |
jilee | 29:f71a0be59b99 | 810 | } |
jilee | 29:f71a0be59b99 | 811 | |
jilee | 29:f71a0be59b99 | 812 | |
ampembeng | 23:b9ff83dc965f | 813 | //========================================================================== |
ampembeng | 23:b9ff83dc965f | 814 | // NOTE: You can comment in the following line for an alternate demo that |
ampembeng | 23:b9ff83dc965f | 815 | // is used as the out-of-box demo binary that comes with the AT&T IoT |
ampembeng | 23:b9ff83dc965f | 816 | // Starter Kit. It loops instead of the rest of Main() |
ampembeng | 23:b9ff83dc965f | 817 | //return outOfBoxDemo(); |
ampembeng | 23:b9ff83dc965f | 818 | //========================================================================== |
jilee | 29:f71a0be59b99 | 819 | |
jilee | 29:f71a0be59b99 | 820 | restart1: |
eggsylah | 30:8c979815f533 | 821 | count = 0; |
ampembeng | 23:b9ff83dc965f | 822 | |
ampembeng | 18:6370da1de572 | 823 | // Intialize MQTT/Cert parameters |
ampembeng | 15:6f2798e45099 | 824 | ShadowParameters_t sp = ShadowParametersDefault; |
ampembeng | 18:6370da1de572 | 825 | #ifdef USING_SD_CARD |
ampembeng | 18:6370da1de572 | 826 | rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); |
ampembeng | 18:6370da1de572 | 827 | if (NONE_ERROR != rc) { |
ampembeng | 18:6370da1de572 | 828 | ERROR("Failed to initialize mqtt parameters %d", rc); |
ampembeng | 18:6370da1de572 | 829 | return rc; |
ampembeng | 18:6370da1de572 | 830 | } |
ampembeng | 18:6370da1de572 | 831 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 18:6370da1de572 | 832 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 18:6370da1de572 | 833 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 834 | #else |
ampembeng | 15:6f2798e45099 | 835 | sp.pMyThingName = AWS_IOT_MY_THING_NAME; |
ampembeng | 15:6f2798e45099 | 836 | sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; |
jilee | 29:f71a0be59b99 | 837 | //sp.pHost = HostAddress; |
jilee | 29:f71a0be59b99 | 838 | INFO ("Host Name:"); |
jilee | 29:f71a0be59b99 | 839 | INFO (aws_iot_mqtt_host); |
jilee | 29:f71a0be59b99 | 840 | sp.pHost = aws_iot_mqtt_host; |
jilee | 29:f71a0be59b99 | 841 | INFO (sp.pHost); |
ampembeng | 15:6f2798e45099 | 842 | sp.port = port; |
ampembeng | 23:b9ff83dc965f | 843 | |
ampembeng | 23:b9ff83dc965f | 844 | sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; |
ampembeng | 23:b9ff83dc965f | 845 | sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; |
ampembeng | 23:b9ff83dc965f | 846 | sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; |
ampembeng | 18:6370da1de572 | 847 | #endif |
jilee | 29:f71a0be59b99 | 848 | /* |
jilee | 29:f71a0be59b99 | 849 | char cBlockOffset[7]; |
jilee | 29:f71a0be59b99 | 850 | string sObject; |
jilee | 29:f71a0be59b99 | 851 | pc.printf ("Length = %d\n", AWS_IOT_CERTIFICATE_LENGTH ); |
jilee | 29:f71a0be59b99 | 852 | for (int i = 0; i < AWS_IOT_CERTIFICATE_LENGTH; i++) |
jilee | 29:f71a0be59b99 | 853 | { |
jilee | 29:f71a0be59b99 | 854 | sprintf (cBlockOffset, "%02X", AWS_IOT_CERTIFICATE[i]); |
jilee | 29:f71a0be59b99 | 855 | sObject += string (cBlockOffset); |
jilee | 29:f71a0be59b99 | 856 | |
jilee | 29:f71a0be59b99 | 857 | } |
jilee | 29:f71a0be59b99 | 858 | pc.printf(sObject.c_str()); |
jilee | 29:f71a0be59b99 | 859 | pc.printf("\n"); |
jilee | 29:f71a0be59b99 | 860 | */ |
jilee | 29:f71a0be59b99 | 861 | int ret = 0; |
jilee | 29:f71a0be59b99 | 862 | mbedtls_x509_crt_init(&clicert); |
jilee | 29:f71a0be59b99 | 863 | ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *)AWS_IOT_CERTIFICATE, AWS_IOT_CERTIFICATE_LENGTH); |
jilee | 29:f71a0be59b99 | 864 | if (ret != 0) { |
jilee | 29:f71a0be59b99 | 865 | ERROR(" failed\n ! mbedtls_x509_crt_parse IOT returned 1 -0x%x, %d\n\n", -ret, AWS_IOT_CERTIFICATE_LENGTH); |
eggsylah | 30:8c979815f533 | 866 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 867 | tft.printf ("\nInvalid Certificate.\n"); |
eggsylah | 30:8c979815f533 | 868 | tft.moveCursor(0, 3); |
eggsylah | 30:8c979815f533 | 869 | tft.printf("Please check the certificate and\nreboot the device."); |
jilee | 29:f71a0be59b99 | 870 | return ret; |
jilee | 29:f71a0be59b99 | 871 | } |
jilee | 29:f71a0be59b99 | 872 | else |
jilee | 29:f71a0be59b99 | 873 | { |
eggsylah | 30:8c979815f533 | 874 | //EBJ TODO read from card -- or is it already ? |
jilee | 29:f71a0be59b99 | 875 | for (int i = 0; i < clicert.subject_raw.len; i++) |
jilee | 29:f71a0be59b99 | 876 | { |
jilee | 29:f71a0be59b99 | 877 | if (clicert.subject_raw.p[i] == 0x0C) |
jilee | 29:f71a0be59b99 | 878 | { |
jilee | 29:f71a0be59b99 | 879 | i++; |
jilee | 29:f71a0be59b99 | 880 | unsigned char cLength = clicert.subject_raw.p[i]; |
jilee | 29:f71a0be59b99 | 881 | DEBUG ("subject length = %d", cLength); |
jilee | 29:f71a0be59b99 | 882 | i++; |
jilee | 29:f71a0be59b99 | 883 | for (int j = 0; j < (int) cLength; j++) |
jilee | 29:f71a0be59b99 | 884 | { |
jilee | 29:f71a0be59b99 | 885 | cSubject[j] = clicert.subject_raw.p[i]; |
jilee | 29:f71a0be59b99 | 886 | i++; |
jilee | 29:f71a0be59b99 | 887 | } |
jilee | 29:f71a0be59b99 | 888 | cSubject[cLength] = 0x00; |
jilee | 29:f71a0be59b99 | 889 | break; |
jilee | 29:f71a0be59b99 | 890 | } |
jilee | 29:f71a0be59b99 | 891 | } |
jilee | 29:f71a0be59b99 | 892 | } |
jilee | 29:f71a0be59b99 | 893 | |
eggsylah | 30:8c979815f533 | 894 | tft.moveCursor(0, 9); |
eggsylah | 30:8c979815f533 | 895 | tft.printf("ICCID: %s\n", iccidName); |
eggsylah | 30:8c979815f533 | 896 | tft.printf("Logging into %s AWS\n", sMQTTHostName); |
eggsylah | 30:8c979815f533 | 897 | tft.printf("CN: %s\n", cSubject); |
ampembeng | 18:6370da1de572 | 898 | INFO("Initialize the MQTT client..."); |
ampembeng | 18:6370da1de572 | 899 | MQTTClient_t mqttClient; |
ampembeng | 18:6370da1de572 | 900 | aws_iot_mqtt_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 901 | |
jilee | 29:f71a0be59b99 | 902 | string sAWSError = "\nUnable to Log into AWS. Invalid certificate. Please make sure the certificates in the SIM card are valid and reboot the device.\n"; |
eggsylah | 30:8c979815f533 | 903 | tft.printf("."); |
ampembeng | 15:6f2798e45099 | 904 | INFO("Shadow Init..."); |
ampembeng | 15:6f2798e45099 | 905 | rc = aws_iot_shadow_init(&mqttClient); |
ampembeng | 15:6f2798e45099 | 906 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 907 | ERROR("Shadow Init Error %d", rc); |
eggsylah | 30:8c979815f533 | 908 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 909 | tft.printf(sAWSError.c_str()); |
ampembeng | 15:6f2798e45099 | 910 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 911 | } |
ampembeng | 15:6f2798e45099 | 912 | |
ampembeng | 15:6f2798e45099 | 913 | INFO("Shadow Connect..."); |
eggsylah | 30:8c979815f533 | 914 | tft.printf("."); |
ampembeng | 15:6f2798e45099 | 915 | rc = aws_iot_shadow_connect(&mqttClient, &sp); |
ampembeng | 15:6f2798e45099 | 916 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 917 | ERROR("Shadow Connection Error %d", rc); |
eggsylah | 30:8c979815f533 | 918 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 919 | tft.printf(sAWSError.c_str()); |
ampembeng | 15:6f2798e45099 | 920 | return rc; |
Janos Follath |
0:fc70c47eecb4 | 921 | } |
Janos Follath |
0:fc70c47eecb4 | 922 | |
ampembeng | 15:6f2798e45099 | 923 | // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h |
ampembeng | 15:6f2798e45099 | 924 | // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 925 | // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL |
ampembeng | 15:6f2798e45099 | 926 | rc = mqttClient.setAutoReconnectStatus(true); |
ampembeng | 15:6f2798e45099 | 927 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 928 | ERROR("Unable to set Auto Reconnect to true - %d", rc); |
eggsylah | 30:8c979815f533 | 929 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 930 | tft.printf(sAWSError.c_str()); |
ampembeng | 15:6f2798e45099 | 931 | return rc; |
ampembeng | 15:6f2798e45099 | 932 | } |
ampembeng | 15:6f2798e45099 | 933 | |
ampembeng | 15:6f2798e45099 | 934 | // Example line of how to delete a shadow (not used in this demo) |
ampembeng | 15:6f2798e45099 | 935 | //aws_iot_shadow_delete(&mqttClient, AWS_IOT_MY_THING_NAME, ShadowUpdateStatusCallback, NULL, 8, true); |
Janos Follath |
0:fc70c47eecb4 | 936 | |
ampembeng | 15:6f2798e45099 | 937 | INFO("Shadow Register Delta..."); |
eggsylah | 30:8c979815f533 | 938 | tft.printf("."); |
ampembeng | 15:6f2798e45099 | 939 | rc = aws_iot_shadow_register_delta(&mqttClient, &ledController); |
ampembeng | 15:6f2798e45099 | 940 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 941 | ERROR("Shadow Register Delta Error"); |
eggsylah | 30:8c979815f533 | 942 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 943 | tft.printf(sAWSError.c_str()); |
ampembeng | 15:6f2798e45099 | 944 | return rc; |
ampembeng | 15:6f2798e45099 | 945 | } |
ampembeng | 15:6f2798e45099 | 946 | |
ampembeng | 15:6f2798e45099 | 947 | INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); |
ampembeng | 15:6f2798e45099 | 948 | // Loop and publish changes from the FRDM board |
jilee | 29:f71a0be59b99 | 949 | |
jilee | 29:f71a0be59b99 | 950 | while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) |
jilee | 29:f71a0be59b99 | 951 | { |
ampembeng | 15:6f2798e45099 | 952 | |
ampembeng | 15:6f2798e45099 | 953 | // Looks for incoming socket messages |
ampembeng | 15:6f2798e45099 | 954 | rc = aws_iot_shadow_yield(&mqttClient, 200); |
jilee | 29:f71a0be59b99 | 955 | if (NETWORK_ATTEMPTING_RECONNECT == rc) |
jilee | 29:f71a0be59b99 | 956 | { |
ampembeng | 15:6f2798e45099 | 957 | // If the client is attempting to reconnect we will skip the rest of the loop. |
eggsylah | 30:8c979815f533 | 958 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 959 | tft.setCursor(0, 195); |
jilee | 29:f71a0be59b99 | 960 | ShowINFO("Attempting to reconnect..."); |
eggsylah | 30:8c979815f533 | 961 | tft.setTextColor(DEFAULT_TEXT_COLOR); |
ampembeng | 15:6f2798e45099 | 962 | wait(1); |
jilee | 29:f71a0be59b99 | 963 | bFirstTime = true; |
ampembeng | 15:6f2798e45099 | 964 | continue; |
ampembeng | 15:6f2798e45099 | 965 | } |
ampembeng | 20:ee34856ae510 | 966 | |
ampembeng | 20:ee34856ae510 | 967 | // Read sensor data |
ampembeng | 20:ee34856ae510 | 968 | temperature = CTOF(hts221.readTemperature()); |
ampembeng | 20:ee34856ae510 | 969 | humidity = hts221.readHumidity(); |
ampembeng | 20:ee34856ae510 | 970 | |
ampembeng | 15:6f2798e45099 | 971 | INFO("\n=======================================================================================\n"); |
ampembeng | 15:6f2798e45099 | 972 | // Initialize JSON shadow document |
ampembeng | 15:6f2798e45099 | 973 | rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
jilee | 29:f71a0be59b99 | 974 | if (rc == NONE_ERROR) |
jilee | 29:f71a0be59b99 | 975 | { |
ampembeng | 15:6f2798e45099 | 976 | |
ampembeng | 15:6f2798e45099 | 977 | // If there has been a SW3 button press update the 'desired' color |
jilee | 29:f71a0be59b99 | 978 | if (buttonOverride) |
jilee | 29:f71a0be59b99 | 979 | { |
ampembeng | 23:b9ff83dc965f | 980 | rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); |
ampembeng | 15:6f2798e45099 | 981 | buttonOverride = false; |
Janos Follath |
0:fc70c47eecb4 | 982 | } |
ampembeng | 15:6f2798e45099 | 983 | |
ampembeng | 23:b9ff83dc965f | 984 | // Updates the 'reported' color/temp/humidity |
jilee | 28:54d9a550adf1 | 985 | rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 4, &ledController, |
ampembeng | 20:ee34856ae510 | 986 | &temperatureHandler, |
jilee | 28:54d9a550adf1 | 987 | &humidityHandler, &iccidHandler); |
ampembeng | 15:6f2798e45099 | 988 | |
jilee | 29:f71a0be59b99 | 989 | if (rc == NONE_ERROR) |
jilee | 29:f71a0be59b99 | 990 | { |
ampembeng | 15:6f2798e45099 | 991 | rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); |
ampembeng | 15:6f2798e45099 | 992 | |
jilee | 29:f71a0be59b99 | 993 | if (rc == NONE_ERROR) |
jilee | 29:f71a0be59b99 | 994 | { |
ampembeng | 15:6f2798e45099 | 995 | INFO("Update Shadow: %s", JsonDocumentBuffer); |
ampembeng | 18:6370da1de572 | 996 | rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, |
ampembeng | 15:6f2798e45099 | 997 | ShadowUpdateStatusCallback, NULL, 8, true); |
ampembeng | 15:6f2798e45099 | 998 | } |
ampembeng | 15:6f2798e45099 | 999 | } |
ampembeng | 20:ee34856ae510 | 1000 | } |
ampembeng | 20:ee34856ae510 | 1001 | |
jilee | 29:f71a0be59b99 | 1002 | //get signal strength |
jilee | 29:f71a0be59b99 | 1003 | GetSignalStrength(&dbm); |
jilee | 29:f71a0be59b99 | 1004 | |
jilee | 29:f71a0be59b99 | 1005 | //check if certificate is updated |
jilee | 29:f71a0be59b99 | 1006 | |
jilee | 29:f71a0be59b99 | 1007 | if (GetUpdateStatus(&cUpdateStatus) != 0) |
jilee | 29:f71a0be59b99 | 1008 | { |
jilee | 29:f71a0be59b99 | 1009 | ERROR("Get Update Status Error"); |
jilee | 29:f71a0be59b99 | 1010 | } |
jilee | 29:f71a0be59b99 | 1011 | else |
jilee | 29:f71a0be59b99 | 1012 | { |
jilee | 29:f71a0be59b99 | 1013 | if (cUpdateStatus == 0xFF) |
jilee | 29:f71a0be59b99 | 1014 | { |
eggsylah | 30:8c979815f533 | 1015 | tft.fillScreen(ILI9341_BLACK); |
eggsylah | 30:8c979815f533 | 1016 | tft.setCursor(0, 10); |
eggsylah | 30:8c979815f533 | 1017 | ShowINFO ("Certificate Update Detected."); |
jilee | 29:f71a0be59b99 | 1018 | ShowINFO("Disconnecting AWS"); |
jilee | 29:f71a0be59b99 | 1019 | rc = aws_iot_shadow_disconnect(&mqttClient); |
jilee | 29:f71a0be59b99 | 1020 | if (NONE_ERROR != rc) |
jilee | 29:f71a0be59b99 | 1021 | { |
jilee | 29:f71a0be59b99 | 1022 | ERROR("Disconnect error %d. Please reboot the device", rc); |
jilee | 29:f71a0be59b99 | 1023 | return rc; |
jilee | 29:f71a0be59b99 | 1024 | } |
jilee | 29:f71a0be59b99 | 1025 | |
jilee | 29:f71a0be59b99 | 1026 | if (GetAllObjects() != 0) |
jilee | 29:f71a0be59b99 | 1027 | { |
eggsylah | 30:8c979815f533 | 1028 | ShowINFO ("Read Certificate Error. Check hardware and Reboot the device.."); |
jilee | 29:f71a0be59b99 | 1029 | return 0; |
jilee | 29:f71a0be59b99 | 1030 | } |
jilee | 29:f71a0be59b99 | 1031 | |
jilee | 29:f71a0be59b99 | 1032 | wait(3.0); |
jilee | 29:f71a0be59b99 | 1033 | bFirstTime = true; |
jilee | 29:f71a0be59b99 | 1034 | goto restart1; |
jilee | 29:f71a0be59b99 | 1035 | } |
jilee | 29:f71a0be59b99 | 1036 | } |
jilee | 29:f71a0be59b99 | 1037 | |
jilee | 29:f71a0be59b99 | 1038 | // Print data |
eggsylah | 30:8c979815f533 | 1039 | printDatatoTFT(bFirstTime); |
eggsylah | 30:8c979815f533 | 1040 | bFirstTime = false; |
eggsylah | 30:8c979815f533 | 1041 | |
ampembeng | 23:b9ff83dc965f | 1042 | printData(); |
ampembeng | 18:6370da1de572 | 1043 | INFO("*****************************************************************************************"); |
ampembeng | 20:ee34856ae510 | 1044 | |
ampembeng | 20:ee34856ae510 | 1045 | // Set the LED color |
ampembeng | 15:6f2798e45099 | 1046 | SetLedColor(ledColor); |
ampembeng | 15:6f2798e45099 | 1047 | wait(SHADOW_SYNC_INTERVAL); |
jilee | 29:f71a0be59b99 | 1048 | |
jilee | 29:f71a0be59b99 | 1049 | if (count > 50) |
jilee | 29:f71a0be59b99 | 1050 | { |
jilee | 29:f71a0be59b99 | 1051 | INFO ("Max Upload reached. Please reset the device to start again"); |
eggsylah | 30:8c979815f533 | 1052 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 1053 | tft.setCursor(0, 195); |
eggsylah | 30:8c979815f533 | 1054 | tft.printf("Max upload reached.\nPlease reset the device to continue."); |
jilee | 29:f71a0be59b99 | 1055 | return rc; |
jilee | 29:f71a0be59b99 | 1056 | } |
jilee | 29:f71a0be59b99 | 1057 | |
Janos Follath |
0:fc70c47eecb4 | 1058 | } |
Janos Follath |
0:fc70c47eecb4 | 1059 | |
jilee | 29:f71a0be59b99 | 1060 | if (NONE_ERROR != rc) |
jilee | 29:f71a0be59b99 | 1061 | { |
ampembeng | 15:6f2798e45099 | 1062 | ERROR("An error occurred in the loop %d", rc); |
eggsylah | 30:8c979815f533 | 1063 | tft.setTextColor(ILI9341_YELLOW); |
eggsylah | 30:8c979815f533 | 1064 | tft.setCursor(0, 195); |
eggsylah | 30:8c979815f533 | 1065 | tft.printf("Fatal Error. Please reboot the device.\n"); |
Janos Follath |
0:fc70c47eecb4 | 1066 | } |
Janos Follath |
0:fc70c47eecb4 | 1067 | |
ampembeng | 15:6f2798e45099 | 1068 | INFO("Disconnecting"); |
ampembeng | 15:6f2798e45099 | 1069 | rc = aws_iot_shadow_disconnect(&mqttClient); |
ampembeng | 15:6f2798e45099 | 1070 | |
ampembeng | 15:6f2798e45099 | 1071 | if (NONE_ERROR != rc) { |
ampembeng | 15:6f2798e45099 | 1072 | ERROR("Disconnect error %d", rc); |
Janos Follath |
0:fc70c47eecb4 | 1073 | } |
Janos Follath |
0:fc70c47eecb4 | 1074 | |
ampembeng | 15:6f2798e45099 | 1075 | return rc; |
ampembeng | 15:6f2798e45099 | 1076 | } |