Raghu Tirumala / Mbed OS ATT_IoT_Project

Dependencies:   BufferedSoftSerial SDFileSystem

Fork of ATT_AWS_IoT_demo by AT&T IoT

Committer:
RaghuT
Date:
Mon Jul 31 22:45:26 2017 +0000
Revision:
29:319446cd2880
Parent:
28:d549de7c3aad
Child:
30:d2a7e413f658
Child:
31:dc2541e12a1a
Working copy week 6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RaghuT 28:d549de7c3aad 1 #include <cstdlib>
ampembeng 15:6f2798e45099 2 #include "mbed.h"
ampembeng 15:6f2798e45099 3
ampembeng 15:6f2798e45099 4 // Serial extension
ampembeng 15:6f2798e45099 5 #include "MODSERIAL.h"
RaghuT 28:d549de7c3aad 6 #include "SoftSerial.h"
RaghuT 28:d549de7c3aad 7 #include "BufferedSoftSerial.h"
Janos Follath 0:fc70c47eecb4 8
ampembeng 15:6f2798e45099 9 // Network includes
ampembeng 15:6f2798e45099 10 #include "WNCInterface.h"
ampembeng 15:6f2798e45099 11 #include "network_interface.h"
RaghuT 28:d549de7c3aad 12 #include "WncControllerK64F/WncController/WncController.h"
Janos Follath 0:fc70c47eecb4 13
ampembeng 15:6f2798e45099 14 // AWS includes
ampembeng 15:6f2798e45099 15 #include "aws_iot_log.h"
ampembeng 15:6f2798e45099 16 #include "aws_iot_version.h"
ampembeng 15:6f2798e45099 17 #include "aws_iot_shadow_interface.h"
ampembeng 15:6f2798e45099 18 #include "aws_iot_shadow_json_data.h"
ampembeng 15:6f2798e45099 19 #include "aws_iot_config.h"
ampembeng 15:6f2798e45099 20 #include "aws_iot_mqtt_interface.h"
mbed_official 12:1ae41c231014 21
Janos Follath 0:fc70c47eecb4 22 #if DEBUG_LEVEL > 0
Janos Follath 0:fc70c47eecb4 23 #include "mbedtls/debug.h"
Janos Follath 0:fc70c47eecb4 24 #endif
Janos Follath 0:fc70c47eecb4 25
ampembeng 15:6f2798e45099 26 //=====================================================================================================================
ampembeng 15:6f2798e45099 27 //
ampembeng 15:6f2798e45099 28 // Defines
ampembeng 15:6f2798e45099 29 //
ampembeng 15:6f2798e45099 30 //=====================================================================================================================
ampembeng 15:6f2798e45099 31 // LED Colors
ampembeng 15:6f2798e45099 32 #define COLOR_OFF 0x00
ampembeng 15:6f2798e45099 33 #define COLOR_RED 0x01
ampembeng 15:6f2798e45099 34 #define COLOR_GREEN 0x02
ampembeng 15:6f2798e45099 35 #define COLOR_BLUE 0x04
ampembeng 15:6f2798e45099 36 #define COLOR_WHITE 0x07
ampembeng 15:6f2798e45099 37 #define NUM_COLORS 5
Janos Follath 0:fc70c47eecb4 38
ampembeng 15:6f2798e45099 39 // AWS defines
ampembeng 18:6370da1de572 40 #define PATH_MAX 1024
RaghuT 28:d549de7c3aad 41 #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 500 // NOTE: Be wary of this if your JSON doc grows
ampembeng 15:6f2798e45099 42 #define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds)
Janos Follath 0:fc70c47eecb4 43
ampembeng 15:6f2798e45099 44 // Comment out the following line if color is not supported on the terminal
ampembeng 15:6f2798e45099 45 //#define USE_COLOR
ampembeng 15:6f2798e45099 46 #ifdef USE_COLOR
RaghuT 29:319446cd2880 47 #define BLK "\033[30m"
RaghuT 29:319446cd2880 48 #define RED "\033[31m"
RaghuT 29:319446cd2880 49 #define GRN "\033[32m"
RaghuT 29:319446cd2880 50 #define YEL "\033[33m"
RaghuT 29:319446cd2880 51 #define BLU "\033[34m"
RaghuT 29:319446cd2880 52 #define MAG "\033[35m"
RaghuT 29:319446cd2880 53 #define CYN "\033[36m"
RaghuT 29:319446cd2880 54 #define WHT "\033[37m"
RaghuT 29:319446cd2880 55 #define DEF "\033[39m"
ampembeng 15:6f2798e45099 56 #else
RaghuT 29:319446cd2880 57 #define BLK
RaghuT 29:319446cd2880 58 #define RED
RaghuT 29:319446cd2880 59 #define GRN
RaghuT 29:319446cd2880 60 #define YEL
RaghuT 29:319446cd2880 61 #define BLU
RaghuT 29:319446cd2880 62 #define MAG
RaghuT 29:319446cd2880 63 #define CYN
RaghuT 29:319446cd2880 64 #define WHT
RaghuT 29:319446cd2880 65 #define DEF
Janos Follath 0:fc70c47eecb4 66 #endif
Janos Follath 0:fc70c47eecb4 67
ampembeng 20:ee34856ae510 68 // Sensor defines
ampembeng 20:ee34856ae510 69 #define CTOF(x) ((x)*1.8+32) // Temperature
ampembeng 20:ee34856ae510 70
ampembeng 15:6f2798e45099 71 //=====================================================================================================================
ampembeng 15:6f2798e45099 72 //
ampembeng 15:6f2798e45099 73 // Globals
ampembeng 15:6f2798e45099 74 //
ampembeng 15:6f2798e45099 75 //=====================================================================================================================
ampembeng 15:6f2798e45099 76 // Controls LED color
ampembeng 15:6f2798e45099 77 unsigned char ledColor = COLOR_OFF;
ampembeng 15:6f2798e45099 78
ampembeng 18:6370da1de572 79 // These defines are pulled from aws_iot_config.h
ampembeng 15:6f2798e45099 80 char HostAddress[255] = AWS_IOT_MQTT_HOST;
ampembeng 18:6370da1de572 81 char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID;
ampembeng 18:6370da1de572 82 char ThingName[32] = AWS_IOT_MY_THING_NAME;
ampembeng 18:6370da1de572 83 char PortString[5] = "8883";
ampembeng 15:6f2798e45099 84 uint32_t port = AWS_IOT_MQTT_PORT;
ampembeng 23:b9ff83dc965f 85 char iccidName[21] = "12345678901234567890";
Janos Follath 0:fc70c47eecb4 86
ampembeng 20:ee34856ae510 87 // Sensor data
ampembeng 20:ee34856ae510 88 float temperature = 0.0;
RaghuT 28:d549de7c3aad 89 float humidity = 0.0;
ampembeng 20:ee34856ae510 90
RaghuT 28:d549de7c3aad 91 //Cell signal
RaghuT 28:d549de7c3aad 92 int signalQuality = 0;
RaghuT 28:d549de7c3aad 93
RaghuT 28:d549de7c3aad 94 //Variable to store data usage
RaghuT 28:d549de7c3aad 95 int dataUsage = 0;
ampembeng 23:b9ff83dc965f 96
RaghuT 29:319446cd2880 97 //slave JSON to publish
RaghuT 29:319446cd2880 98 string slaveJSON = "";
RaghuT 29:319446cd2880 99
ampembeng 15:6f2798e45099 100 //=====================================================================================================================
ampembeng 15:6f2798e45099 101 //
ampembeng 15:6f2798e45099 102 // Devices
ampembeng 15:6f2798e45099 103 //
ampembeng 15:6f2798e45099 104 //=====================================================================================================================
ampembeng 15:6f2798e45099 105 // GPIOs for RGB LED
ampembeng 15:6f2798e45099 106 DigitalOut led_green(LED_GREEN);
ampembeng 15:6f2798e45099 107 DigitalOut led_red(LED_RED);
ampembeng 15:6f2798e45099 108 DigitalOut led_blue(LED_BLUE);
Janos Follath 0:fc70c47eecb4 109
ampembeng 15:6f2798e45099 110 // USB Serial port (to PC)
ampembeng 15:6f2798e45099 111 MODSERIAL pc(USBTX,USBRX,256,256);
Janos Follath 0:fc70c47eecb4 112
RaghuT 28:d549de7c3aad 113 // Bluetooth serial
RaghuT 28:d549de7c3aad 114 BufferedSoftSerial bluetooth(PTC4, PTA2);
ampembeng 18:6370da1de572 115
ampembeng 20:ee34856ae510 116 // I2C bus (SDA, SCL)
ampembeng 20:ee34856ae510 117 I2C i2c(PTC11, PTC10);
ampembeng 20:ee34856ae510 118
ampembeng 15:6f2798e45099 119 //=====================================================================================================================
ampembeng 15:6f2798e45099 120 //
ampembeng 15:6f2798e45099 121 // Functions
ampembeng 15:6f2798e45099 122 //
ampembeng 15:6f2798e45099 123 //=====================================================================================================================
ampembeng 15:6f2798e45099 124 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 125 //* Prints the given format to the PC serial port. Exposed to all files via aws_iot_log.h
ampembeng 15:6f2798e45099 126 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 127 void pc_print(const char * format, ...)
ampembeng 15:6f2798e45099 128 {
ampembeng 15:6f2798e45099 129 va_list vl;
ampembeng 15:6f2798e45099 130 va_start(vl, format);
ampembeng 15:6f2798e45099 131 pc.vprintf(format, vl);
ampembeng 15:6f2798e45099 132 va_end(vl);
ampembeng 15:6f2798e45099 133 }
Janos Follath 0:fc70c47eecb4 134
ampembeng 15:6f2798e45099 135 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 136 //* Set the RGB LED's Color
RaghuT 29:319446cd2880 137 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
ampembeng 15:6f2798e45099 138 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 139 void SetLedColor(unsigned char ucColor)
RaghuT 29:319446cd2880 140 {
ampembeng 15:6f2798e45099 141 //Note that when an LED is on, you write a 0 to it:
ampembeng 15:6f2798e45099 142 led_red = !(ucColor & 0x1); //bit 0
ampembeng 15:6f2798e45099 143 led_green = !(ucColor & 0x2); //bit 1
ampembeng 15:6f2798e45099 144 led_blue = !(ucColor & 0x4); //bit 2
ampembeng 15:6f2798e45099 145 }
Janos Follath 0:fc70c47eecb4 146
ampembeng 15:6f2798e45099 147 //=====================================================================================================================
ampembeng 15:6f2798e45099 148 //
ampembeng 15:6f2798e45099 149 // AWS Shadow Callbacks
ampembeng 15:6f2798e45099 150 //
ampembeng 15:6f2798e45099 151 //=====================================================================================================================
ampembeng 15:6f2798e45099 152 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 153 //* This is the callback function that fires when an update is sent. It will print the update response status.
ampembeng 15:6f2798e45099 154 //*********************************************************************************************************************
ampembeng 15:6f2798e45099 155 void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status,
RaghuT 29:319446cd2880 156 const char *pReceivedJsonDocument, void *pContextData)
RaghuT 29:319446cd2880 157 {
Janos Follath 0:fc70c47eecb4 158
ampembeng 15:6f2798e45099 159 INFO("Shadow Update Status Callback");
RaghuT 29:319446cd2880 160
ampembeng 15:6f2798e45099 161 if (status == SHADOW_ACK_TIMEOUT) {
ampembeng 15:6f2798e45099 162 INFO("Update Timeout--");
ampembeng 15:6f2798e45099 163 } else if (status == SHADOW_ACK_REJECTED) {
ampembeng 15:6f2798e45099 164 INFO("Update RejectedXX");
ampembeng 15:6f2798e45099 165 } else if (status == SHADOW_ACK_ACCEPTED) {
ampembeng 15:6f2798e45099 166 INFO("Update Accepted!!"); // Good
ampembeng 15:6f2798e45099 167 }
ampembeng 15:6f2798e45099 168 }
Janos Follath 0:fc70c47eecb4 169
ampembeng 15:6f2798e45099 170 //*********************************************************************************************************************
RaghuT 29:319446cd2880 171 //* These are the callback functions that fire when AWS has sends out a shadow update.
ampembeng 15:6f2798e45099 172 //*********************************************************************************************************************
RaghuT 29:319446cd2880 173 void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext)
RaghuT 29:319446cd2880 174 {
RaghuT 29:319446cd2880 175
ampembeng 15:6f2798e45099 176 INFO("LED Callback Detected.");
RaghuT 29:319446cd2880 177
ampembeng 15:6f2798e45099 178 if (pContext != NULL) {
RaghuT 29:319446cd2880 179 switch (*(unsigned char *)(pContext->pData)) {
ampembeng 15:6f2798e45099 180 case COLOR_OFF:
ampembeng 15:6f2798e45099 181 INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 182 break;
ampembeng 15:6f2798e45099 183 case COLOR_RED:
ampembeng 15:6f2798e45099 184 INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 185 break;
ampembeng 15:6f2798e45099 186 case COLOR_GREEN:
ampembeng 15:6f2798e45099 187 INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 188 break;
ampembeng 15:6f2798e45099 189 case COLOR_BLUE:
ampembeng 15:6f2798e45099 190 INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData));
ampembeng 15:6f2798e45099 191 break;
ampembeng 15:6f2798e45099 192 case COLOR_WHITE:
ampembeng 15:6f2798e45099 193 INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData));
RaghuT 29:319446cd2880 194 break;
Janos Follath 0:fc70c47eecb4 195 }
RaghuT 29:319446cd2880 196 } else {
ampembeng 15:6f2798e45099 197 INFO("pContext was detected as NULL");
Janos Follath 0:fc70c47eecb4 198 }
ampembeng 15:6f2798e45099 199 }
RaghuT 28:d549de7c3aad 200 //Callback when dataUsage stats are updated
RaghuT 29:319446cd2880 201 void dataUsageCallback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext)
RaghuT 29:319446cd2880 202 {
RaghuT 29:319446cd2880 203 INFO("Data usage callback detected");
RaghuT 28:d549de7c3aad 204 }
ampembeng 23:b9ff83dc965f 205 //*********************************************************************************************************************
ampembeng 23:b9ff83dc965f 206 //* Subscribe callback (used with alternate demo)
ampembeng 23:b9ff83dc965f 207 //*********************************************************************************************************************
RaghuT 29:319446cd2880 208 int MQTTcallbackHandler(MQTTCallbackParams params)
RaghuT 29:319446cd2880 209 {
ampembeng 23:b9ff83dc965f 210
ampembeng 23:b9ff83dc965f 211 INFO("Subscribe callback");
ampembeng 23:b9ff83dc965f 212 INFO("%.*s\t%.*s",
RaghuT 29:319446cd2880 213 (int)params.TopicNameLen, params.pTopicName,
RaghuT 29:319446cd2880 214 (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload);
ampembeng 23:b9ff83dc965f 215
ampembeng 23:b9ff83dc965f 216 return 0;
ampembeng 23:b9ff83dc965f 217 }
ampembeng 23:b9ff83dc965f 218
ampembeng 23:b9ff83dc965f 219 //*********************************************************************************************************************
ampembeng 23:b9ff83dc965f 220 //* Disconnect handling (used with alternate demo)
ampembeng 23:b9ff83dc965f 221 //*********************************************************************************************************************
RaghuT 29:319446cd2880 222 void disconnectCallbackHandler(void)
RaghuT 29:319446cd2880 223 {
ampembeng 23:b9ff83dc965f 224 WARN("MQTT Disconnect");
ampembeng 23:b9ff83dc965f 225 IoT_Error_t rc = NONE_ERROR;
RaghuT 29:319446cd2880 226 if(aws_iot_is_autoreconnect_enabled()) {
ampembeng 23:b9ff83dc965f 227 INFO("Auto Reconnect is enabled, Reconnecting attempt will start now");
RaghuT 29:319446cd2880 228 } else {
ampembeng 23:b9ff83dc965f 229 WARN("Auto Reconnect not enabled. Starting manual reconnect...");
ampembeng 23:b9ff83dc965f 230 rc = aws_iot_mqtt_attempt_reconnect();
RaghuT 29:319446cd2880 231 if(RECONNECT_SUCCESSFUL == rc) {
ampembeng 23:b9ff83dc965f 232 WARN("Manual Reconnect Successful");
RaghuT 29:319446cd2880 233 } else {
ampembeng 23:b9ff83dc965f 234 WARN("Manual Reconnect Failed - %d", rc);
ampembeng 23:b9ff83dc965f 235 }
ampembeng 23:b9ff83dc965f 236 }
ampembeng 23:b9ff83dc965f 237 }
ampembeng 23:b9ff83dc965f 238
RaghuT 28:d549de7c3aad 239 //*********************************************************************************************************************
RaghuT 29:319446cd2880 240 //* Get slave data over uart interface
RaghuT 28:d549de7c3aad 241 //*********************************************************************************************************************
RaghuT 29:319446cd2880 242 void getSlaveJSON()
RaghuT 29:319446cd2880 243 {
RaghuT 29:319446cd2880 244 if(bluetooth.writeable()) {
RaghuT 29:319446cd2880 245 bluetooth.putc('d');
RaghuT 29:319446cd2880 246 }
RaghuT 29:319446cd2880 247 slaveJSON = "";
RaghuT 29:319446cd2880 248 while(bluetooth.readable()) {
RaghuT 29:319446cd2880 249 slaveJSON += bluetooth.getc();
RaghuT 29:319446cd2880 250 }
RaghuT 29:319446cd2880 251 }
RaghuT 29:319446cd2880 252
RaghuT 29:319446cd2880 253 //=====================================================================================================================
RaghuT 29:319446cd2880 254 //
RaghuT 29:319446cd2880 255 // Out-of-Box Demo: This function is used as part of the binary that comes with the Starter Kit. Instead of using an
RaghuT 29:319446cd2880 256 // AWS device shadow, it publishes to an AWS Rule. The Rule is setup to store data to a DynamoDB, and
RaghuT 29:319446cd2880 257 // the demo S3 website pulls that data from the DynamoDB and displays it.
RaghuT 29:319446cd2880 258 //
RaghuT 29:319446cd2880 259 //=====================================================================================================================
RaghuT 29:319446cd2880 260 int outOfBoxDemo()
RaghuT 29:319446cd2880 261 {
RaghuT 29:319446cd2880 262 INFO("Running Out-of-Box Function (alternate demo).");
RaghuT 29:319446cd2880 263
RaghuT 29:319446cd2880 264 IoT_Error_t rc = NONE_ERROR;
RaghuT 29:319446cd2880 265 int32_t i = 0;
RaghuT 29:319446cd2880 266 int publishCount = 0;
RaghuT 29:319446cd2880 267 bool infinitePublishFlag = true;
RaghuT 29:319446cd2880 268 char cPayload[100];
RaghuT 29:319446cd2880 269 char cTopic[100];
RaghuT 29:319446cd2880 270 float updateInterval = 3.0; // seconds
RaghuT 29:319446cd2880 271
RaghuT 29:319446cd2880 272 MQTTConnectParams connectParams = MQTTConnectParamsDefault;
RaghuT 29:319446cd2880 273 connectParams.KeepAliveInterval_sec = 10;
RaghuT 29:319446cd2880 274 connectParams.isCleansession = true;
RaghuT 29:319446cd2880 275 connectParams.MQTTVersion = MQTT_3_1_1;
RaghuT 29:319446cd2880 276 connectParams.pClientID = iccidName; // Using ICCID for unique Client ID
RaghuT 29:319446cd2880 277 connectParams.pHostURL = HostAddress;
RaghuT 29:319446cd2880 278 connectParams.port = port;
RaghuT 29:319446cd2880 279 connectParams.isWillMsgPresent = false;
RaghuT 29:319446cd2880 280 connectParams.pRootCALocation = AWS_IOT_ROOT_CA_FILENAME;
RaghuT 29:319446cd2880 281 connectParams.pDeviceCertLocation = AWS_IOT_CERTIFICATE_FILENAME;
RaghuT 29:319446cd2880 282 connectParams.pDevicePrivateKeyLocation = AWS_IOT_PRIVATE_KEY_FILENAME;
RaghuT 29:319446cd2880 283 connectParams.mqttCommandTimeout_ms = 10000;
RaghuT 29:319446cd2880 284 connectParams.tlsHandshakeTimeout_ms = 10000;
RaghuT 29:319446cd2880 285 connectParams.isSSLHostnameVerify = true; // ensure this is set to true for production
RaghuT 29:319446cd2880 286 connectParams.disconnectHandler = disconnectCallbackHandler;
RaghuT 29:319446cd2880 287
RaghuT 29:319446cd2880 288 INFO("Connecting...");
RaghuT 29:319446cd2880 289 rc = aws_iot_mqtt_connect(&connectParams);
RaghuT 29:319446cd2880 290 if (NONE_ERROR != rc) {
RaghuT 29:319446cd2880 291 ERROR("Error(%d) connecting to %s:%d", rc, connectParams.pHostURL, connectParams.port);
RaghuT 29:319446cd2880 292 }
RaghuT 29:319446cd2880 293
RaghuT 29:319446cd2880 294 /*
RaghuT 29:319446cd2880 295 * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h
RaghuT 29:319446cd2880 296 * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
RaghuT 29:319446cd2880 297 * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
RaghuT 29:319446cd2880 298 */
RaghuT 29:319446cd2880 299 INFO("Set Auto Reconnect...");
RaghuT 29:319446cd2880 300 rc = aws_iot_mqtt_autoreconnect_set_status(true);
RaghuT 29:319446cd2880 301 if (NONE_ERROR != rc) {
RaghuT 29:319446cd2880 302 ERROR("Unable to set Auto Reconnect to true - %d", rc);
RaghuT 29:319446cd2880 303 return rc;
RaghuT 29:319446cd2880 304 }
RaghuT 29:319446cd2880 305
RaghuT 29:319446cd2880 306 // Initialize the payload
RaghuT 29:319446cd2880 307 MQTTMessageParams Msg = MQTTMessageParamsDefault;
RaghuT 29:319446cd2880 308 Msg.qos = QOS_0;
RaghuT 29:319446cd2880 309 Msg.pPayload = (void *) cPayload;
RaghuT 29:319446cd2880 310
RaghuT 29:319446cd2880 311 MQTTPublishParams Params = MQTTPublishParamsDefault;
RaghuT 29:319446cd2880 312
RaghuT 29:319446cd2880 313 // Sets up the topic to publish to
RaghuT 29:319446cd2880 314 sprintf(cTopic, AWS_IOT_MY_TOPIC, iccidName);
RaghuT 29:319446cd2880 315 Params.pTopic = cTopic;
RaghuT 29:319446cd2880 316
RaghuT 29:319446cd2880 317 if (publishCount != 0) {
RaghuT 29:319446cd2880 318 infinitePublishFlag = false;
RaghuT 29:319446cd2880 319 }
RaghuT 29:319446cd2880 320
RaghuT 29:319446cd2880 321 while ((NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc)
RaghuT 29:319446cd2880 322 && (publishCount > 0 || infinitePublishFlag)) {
RaghuT 29:319446cd2880 323
RaghuT 29:319446cd2880 324 // Max time the yield function will wait for read messages
RaghuT 29:319446cd2880 325 rc = aws_iot_mqtt_yield(100);
RaghuT 29:319446cd2880 326 if(NETWORK_ATTEMPTING_RECONNECT == rc) {
RaghuT 29:319446cd2880 327 INFO("--> sleep (attempting to reconnect)");
RaghuT 29:319446cd2880 328 wait(1);
RaghuT 29:319446cd2880 329 // If the client is attempting to reconnect we will skip the rest of the loop.
RaghuT 29:319446cd2880 330 continue;
RaghuT 29:319446cd2880 331 }
RaghuT 29:319446cd2880 332
RaghuT 29:319446cd2880 333 getSlaveJSON();
RaghuT 29:319446cd2880 334 INFO("%s", slaveJSON);
RaghuT 29:319446cd2880 335 // Loading data into JSON format
RaghuT 29:319446cd2880 336 sprintf(cPayload, "Data");
RaghuT 29:319446cd2880 337 Msg.PayloadLen = strlen(cPayload) + 1;
RaghuT 29:319446cd2880 338 Params.MessageParams = Msg;
RaghuT 29:319446cd2880 339
RaghuT 29:319446cd2880 340 // Publish
RaghuT 29:319446cd2880 341 rc = aws_iot_mqtt_publish(&Params);
RaghuT 29:319446cd2880 342 if (publishCount > 0) {
RaghuT 29:319446cd2880 343 publishCount--;
RaghuT 29:319446cd2880 344 }
RaghuT 29:319446cd2880 345
RaghuT 29:319446cd2880 346 INFO("--> Update sent. Sleep for %f seconds", updateInterval);
RaghuT 29:319446cd2880 347 wait(updateInterval-.02);
RaghuT 29:319446cd2880 348 }
RaghuT 29:319446cd2880 349
RaghuT 29:319446cd2880 350
RaghuT 29:319446cd2880 351 if (NONE_ERROR != rc) {
RaghuT 29:319446cd2880 352 ERROR("An error occurred in the loop.\n");
RaghuT 29:319446cd2880 353 } else {
RaghuT 29:319446cd2880 354 INFO("Publish done\n");
RaghuT 29:319446cd2880 355 }
RaghuT 29:319446cd2880 356
RaghuT 29:319446cd2880 357 return rc;
ampembeng 23:b9ff83dc965f 358 }
ampembeng 15:6f2798e45099 359
ampembeng 15:6f2798e45099 360 //=====================================================================================================================
ampembeng 15:6f2798e45099 361 //
ampembeng 15:6f2798e45099 362 // Main
ampembeng 15:6f2798e45099 363 //
ampembeng 15:6f2798e45099 364 //=====================================================================================================================
RaghuT 29:319446cd2880 365 int main()
RaghuT 29:319446cd2880 366 {
ampembeng 15:6f2798e45099 367 // Set baud rate for PC Serial
ampembeng 15:6f2798e45099 368 pc.baud(115200);
RaghuT 28:d549de7c3aad 369 bluetooth.baud(9600);
RaghuT 28:d549de7c3aad 370 INFO("Program Start");
RaghuT 29:319446cd2880 371
RaghuT 29:319446cd2880 372 IoT_Error_t rc = NONE_ERROR;
ampembeng 15:6f2798e45099 373 char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
ampembeng 15:6f2798e45099 374 size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]);
ampembeng 15:6f2798e45099 375
RaghuT 28:d549de7c3aad 376 //JSON struct for signal strength readings
RaghuT 28:d549de7c3aad 377 jsonStruct_t signalStrengthHandler;
RaghuT 28:d549de7c3aad 378 signalStrengthHandler.cb = NULL;
RaghuT 28:d549de7c3aad 379 signalStrengthHandler.pKey = "Signal Strength";
RaghuT 28:d549de7c3aad 380 signalStrengthHandler.pData = &signalQuality;
RaghuT 28:d549de7c3aad 381 signalStrengthHandler.type = SHADOW_JSON_INT16;
RaghuT 29:319446cd2880 382
RaghuT 29:319446cd2880 383 //JSON struct for data usage
RaghuT 28:d549de7c3aad 384 jsonStruct_t dataUsageHandler;
RaghuT 28:d549de7c3aad 385 dataUsageHandler.cb = dataUsageCallback;
RaghuT 28:d549de7c3aad 386 dataUsageHandler.pKey = "Data Usage";
RaghuT 28:d549de7c3aad 387 dataUsageHandler.pData = &dataUsage;
RaghuT 28:d549de7c3aad 388 dataUsageHandler.type = SHADOW_JSON_UINT32;
RaghuT 29:319446cd2880 389
ampembeng 15:6f2798e45099 390 INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
ampembeng 15:6f2798e45099 391
RaghuT 28:d549de7c3aad 392
RaghuT 29:319446cd2880 393 INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config.");
RaghuT 29:319446cd2880 394
ampembeng 16:02008a2a2569 395 // Startup signal - blinks through RGBW then turns off
ampembeng 15:6f2798e45099 396 SetLedColor(COLOR_RED);
ampembeng 15:6f2798e45099 397 wait(.5);
ampembeng 15:6f2798e45099 398 SetLedColor(COLOR_GREEN);
ampembeng 15:6f2798e45099 399 wait(.5);
ampembeng 15:6f2798e45099 400 SetLedColor(COLOR_BLUE);
ampembeng 15:6f2798e45099 401 wait(.5);
ampembeng 16:02008a2a2569 402 SetLedColor(COLOR_WHITE);
ampembeng 16:02008a2a2569 403 wait(.5);
ampembeng 16:02008a2a2569 404 SetLedColor(COLOR_OFF);
RaghuT 29:319446cd2880 405
ampembeng 15:6f2798e45099 406 // Boot the Avnet Shield before any other operations
ampembeng 23:b9ff83dc965f 407 INFO("Net Boot...");
ampembeng 15:6f2798e45099 408 net_modem_boot();
RaghuT 29:319446cd2880 409
RaghuT 29:319446cd2880 410 outOfBoxDemo();
RaghuT 29:319446cd2880 411 return;
RaghuT 29:319446cd2880 412
ampembeng 18:6370da1de572 413 // Intialize MQTT/Cert parameters
ampembeng 15:6f2798e45099 414 ShadowParameters_t sp = ShadowParametersDefault;
ampembeng 18:6370da1de572 415 #ifdef USING_SD_CARD
ampembeng 18:6370da1de572 416 rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME);
ampembeng 18:6370da1de572 417 if (NONE_ERROR != rc) {
ampembeng 18:6370da1de572 418 ERROR("Failed to initialize mqtt parameters %d", rc);
ampembeng 18:6370da1de572 419 return rc;
RaghuT 29:319446cd2880 420 }
ampembeng 18:6370da1de572 421 sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME;
ampembeng 18:6370da1de572 422 sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME;
ampembeng 18:6370da1de572 423 sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME;
ampembeng 18:6370da1de572 424 #else
ampembeng 15:6f2798e45099 425 sp.pMyThingName = AWS_IOT_MY_THING_NAME;
ampembeng 15:6f2798e45099 426 sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID;
ampembeng 15:6f2798e45099 427 sp.pHost = HostAddress;
ampembeng 15:6f2798e45099 428 sp.port = port;
RaghuT 29:319446cd2880 429
ampembeng 23:b9ff83dc965f 430 sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME;
ampembeng 23:b9ff83dc965f 431 sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME;
ampembeng 23:b9ff83dc965f 432 sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME;
ampembeng 18:6370da1de572 433 #endif
RaghuT 29:319446cd2880 434
ampembeng 18:6370da1de572 435 INFO("Initialize the MQTT client...");
ampembeng 18:6370da1de572 436 MQTTClient_t mqttClient;
ampembeng 18:6370da1de572 437 aws_iot_mqtt_init(&mqttClient);
RaghuT 29:319446cd2880 438
ampembeng 15:6f2798e45099 439 INFO("Shadow Init...");
ampembeng 15:6f2798e45099 440 rc = aws_iot_shadow_init(&mqttClient);
ampembeng 15:6f2798e45099 441 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 442 ERROR("Shadow Init Error %d", rc);
ampembeng 15:6f2798e45099 443 return rc;
Janos Follath 0:fc70c47eecb4 444 }
RaghuT 29:319446cd2880 445
RaghuT 29:319446cd2880 446 INFO("Shadow Connect...");
ampembeng 15:6f2798e45099 447 rc = aws_iot_shadow_connect(&mqttClient, &sp);
ampembeng 15:6f2798e45099 448 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 449 ERROR("Shadow Connection Error %d", rc);
ampembeng 15:6f2798e45099 450 return rc;
Janos Follath 0:fc70c47eecb4 451 }
Janos Follath 0:fc70c47eecb4 452
ampembeng 15:6f2798e45099 453 // Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h
ampembeng 15:6f2798e45099 454 // #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL
ampembeng 15:6f2798e45099 455 // #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL
RaghuT 29:319446cd2880 456
ampembeng 15:6f2798e45099 457 rc = mqttClient.setAutoReconnectStatus(true);
ampembeng 15:6f2798e45099 458 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 459 ERROR("Unable to set Auto Reconnect to true - %d", rc);
ampembeng 15:6f2798e45099 460 return rc;
ampembeng 15:6f2798e45099 461 }
RaghuT 29:319446cd2880 462
ampembeng 15:6f2798e45099 463 INFO("Shadow Register Delta...");
RaghuT 28:d549de7c3aad 464 rc = aws_iot_shadow_register_delta(&mqttClient, &dataUsageHandler);
RaghuT 28:d549de7c3aad 465 if (NONE_ERROR != rc) {
RaghuT 28:d549de7c3aad 466 ERROR("Shadow Register Delta dataUsage Error");
RaghuT 28:d549de7c3aad 467 return rc;
RaghuT 28:d549de7c3aad 468 }
RaghuT 28:d549de7c3aad 469
ampembeng 15:6f2798e45099 470 INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL);
ampembeng 15:6f2798e45099 471 // Loop and publish changes from the FRDM board
ampembeng 15:6f2798e45099 472 while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) {
ampembeng 15:6f2798e45099 473 // Looks for incoming socket messages
ampembeng 15:6f2798e45099 474 rc = aws_iot_shadow_yield(&mqttClient, 200);
ampembeng 15:6f2798e45099 475 if (NETWORK_ATTEMPTING_RECONNECT == rc) {
ampembeng 15:6f2798e45099 476 // If the client is attempting to reconnect we will skip the rest of the loop.
ampembeng 15:6f2798e45099 477 INFO("Attempting to reconnect...");
ampembeng 15:6f2798e45099 478 wait(1);
ampembeng 15:6f2798e45099 479 continue;
ampembeng 15:6f2798e45099 480 }
RaghuT 28:d549de7c3aad 481 //Read signal quality
RaghuT 28:d549de7c3aad 482 signalQuality = WNCInterface::_pwnc->getDbmRssi();
RaghuT 29:319446cd2880 483
RaghuT 29:319446cd2880 484 INFO("\n=======================================================================================\n");
RaghuT 29:319446cd2880 485 // Initialize JSON shadow document
ampembeng 15:6f2798e45099 486 rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
ampembeng 15:6f2798e45099 487 if (rc == NONE_ERROR) {
RaghuT 29:319446cd2880 488
ampembeng 23:b9ff83dc965f 489 // Updates the 'reported' color/temp/humidity
RaghuT 29:319446cd2880 490 rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &signalStrengthHandler,
RaghuT 29:319446cd2880 491 &dataUsageHandler);
RaghuT 29:319446cd2880 492
RaghuT 29:319446cd2880 493 if (rc == NONE_ERROR) {
RaghuT 29:319446cd2880 494 rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
RaghuT 29:319446cd2880 495
ampembeng 15:6f2798e45099 496 if (rc == NONE_ERROR) {
ampembeng 15:6f2798e45099 497 INFO("Update Shadow: %s", JsonDocumentBuffer);
ampembeng 18:6370da1de572 498 rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer,
RaghuT 29:319446cd2880 499 ShadowUpdateStatusCallback, NULL, 15, true);
ampembeng 15:6f2798e45099 500 }
ampembeng 15:6f2798e45099 501 }
RaghuT 28:d549de7c3aad 502 }
RaghuT 29:319446cd2880 503 getSlaveJSON();
RaghuT 29:319446cd2880 504 INFO("%s" , slaveJSON);
ampembeng 15:6f2798e45099 505 wait(SHADOW_SYNC_INTERVAL);
Janos Follath 0:fc70c47eecb4 506 }
Janos Follath 0:fc70c47eecb4 507
ampembeng 15:6f2798e45099 508 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 509 ERROR("An error occurred in the loop %d", rc);
Janos Follath 0:fc70c47eecb4 510 }
Janos Follath 0:fc70c47eecb4 511
ampembeng 15:6f2798e45099 512 INFO("Disconnecting");
ampembeng 15:6f2798e45099 513 rc = aws_iot_shadow_disconnect(&mqttClient);
ampembeng 15:6f2798e45099 514
ampembeng 15:6f2798e45099 515 if (NONE_ERROR != rc) {
ampembeng 15:6f2798e45099 516 ERROR("Disconnect error %d", rc);
Janos Follath 0:fc70c47eecb4 517 }
Janos Follath 0:fc70c47eecb4 518
RaghuT 29:319446cd2880 519 return rc;
ampembeng 15:6f2798e45099 520 }