Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BufferedSoftSerial SDFileSystem
Fork of ATT_AWS_IoT_demo by
Diff: main.cpp
- Revision:
- 30:d2a7e413f658
- Parent:
- 29:319446cd2880
- Child:
- 32:acf84c7d0075
--- a/main.cpp Mon Jul 31 22:45:26 2017 +0000 +++ b/main.cpp Tue Aug 01 23:32:10 2017 +0000 @@ -87,6 +87,7 @@ // Sensor data float temperature = 0.0; float humidity = 0.0; +char *slaveData = ""; //Cell signal int signalQuality = 0; @@ -110,8 +111,8 @@ // USB Serial port (to PC) MODSERIAL pc(USBTX,USBRX,256,256); -// Bluetooth serial -BufferedSoftSerial bluetooth(PTC4, PTA2); +// radio serial +BufferedSoftSerial radio(PTC4, PTA2); // I2C bus (SDA, SCL) I2C i2c(PTC11, PTC10); @@ -241,120 +242,14 @@ //********************************************************************************************************************* void getSlaveJSON() { - if(bluetooth.writeable()) { - bluetooth.putc('d'); - } - slaveJSON = ""; - while(bluetooth.readable()) { - slaveJSON += bluetooth.getc(); - } -} - -//===================================================================================================================== -// -// Out-of-Box Demo: This function is used as part of the binary that comes with the Starter Kit. Instead of using an -// AWS device shadow, it publishes to an AWS Rule. The Rule is setup to store data to a DynamoDB, and -// the demo S3 website pulls that data from the DynamoDB and displays it. -// -//===================================================================================================================== -int outOfBoxDemo() -{ - INFO("Running Out-of-Box Function (alternate demo)."); - - IoT_Error_t rc = NONE_ERROR; - int32_t i = 0; - int publishCount = 0; - bool infinitePublishFlag = true; - char cPayload[100]; - char cTopic[100]; - float updateInterval = 3.0; // seconds - - MQTTConnectParams connectParams = MQTTConnectParamsDefault; - connectParams.KeepAliveInterval_sec = 10; - connectParams.isCleansession = true; - connectParams.MQTTVersion = MQTT_3_1_1; - connectParams.pClientID = iccidName; // Using ICCID for unique Client ID - connectParams.pHostURL = HostAddress; - connectParams.port = port; - connectParams.isWillMsgPresent = false; - connectParams.pRootCALocation = AWS_IOT_ROOT_CA_FILENAME; - connectParams.pDeviceCertLocation = AWS_IOT_CERTIFICATE_FILENAME; - connectParams.pDevicePrivateKeyLocation = AWS_IOT_PRIVATE_KEY_FILENAME; - connectParams.mqttCommandTimeout_ms = 10000; - connectParams.tlsHandshakeTimeout_ms = 10000; - connectParams.isSSLHostnameVerify = true; // ensure this is set to true for production - connectParams.disconnectHandler = disconnectCallbackHandler; - - INFO("Connecting..."); - rc = aws_iot_mqtt_connect(&connectParams); - if (NONE_ERROR != rc) { - ERROR("Error(%d) connecting to %s:%d", rc, connectParams.pHostURL, connectParams.port); + slaveJSON.clear(); + if(radio.writeable()) { + radio.printf("d"); + while(radio.readable()) { + slaveJSON += radio.getc(); + } } - - /* - * Enable Auto Reconnect functionality. Minimum and Maximum time of Exponential backoff are set in aws_iot_config.h - * #AWS_IOT_MQTT_MIN_RECONNECT_WAIT_INTERVAL - * #AWS_IOT_MQTT_MAX_RECONNECT_WAIT_INTERVAL - */ - INFO("Set Auto Reconnect..."); - rc = aws_iot_mqtt_autoreconnect_set_status(true); - if (NONE_ERROR != rc) { - ERROR("Unable to set Auto Reconnect to true - %d", rc); - return rc; - } - - // Initialize the payload - MQTTMessageParams Msg = MQTTMessageParamsDefault; - Msg.qos = QOS_0; - Msg.pPayload = (void *) cPayload; - - MQTTPublishParams Params = MQTTPublishParamsDefault; - - // Sets up the topic to publish to - sprintf(cTopic, AWS_IOT_MY_TOPIC, iccidName); - Params.pTopic = cTopic; - - if (publishCount != 0) { - infinitePublishFlag = false; - } - - while ((NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) - && (publishCount > 0 || infinitePublishFlag)) { - - // Max time the yield function will wait for read messages - rc = aws_iot_mqtt_yield(100); - if(NETWORK_ATTEMPTING_RECONNECT == rc) { - INFO("--> sleep (attempting to reconnect)"); - wait(1); - // If the client is attempting to reconnect we will skip the rest of the loop. - continue; - } - - getSlaveJSON(); - INFO("%s", slaveJSON); - // Loading data into JSON format - sprintf(cPayload, "Data"); - Msg.PayloadLen = strlen(cPayload) + 1; - Params.MessageParams = Msg; - - // Publish - rc = aws_iot_mqtt_publish(&Params); - if (publishCount > 0) { - publishCount--; - } - - INFO("--> Update sent. Sleep for %f seconds", updateInterval); - wait(updateInterval-.02); - } - - - if (NONE_ERROR != rc) { - ERROR("An error occurred in the loop.\n"); - } else { - INFO("Publish done\n"); - } - - return rc; + sprintf(slaveData, slaveJSON.c_str()); } //===================================================================================================================== @@ -366,7 +261,7 @@ { // Set baud rate for PC Serial pc.baud(115200); - bluetooth.baud(9600); + radio.baud(9600); INFO("Program Start"); IoT_Error_t rc = NONE_ERROR; @@ -386,7 +281,14 @@ dataUsageHandler.pKey = "Data Usage"; dataUsageHandler.pData = &dataUsage; dataUsageHandler.type = SHADOW_JSON_UINT32; - + + //JSON struct for slave data + jsonStruct_t slaveDataHandler; + slaveDataHandler.cb = NULL; + slaveDataHandler.pKey = "slaves"; + slaveDataHandler.pData = slaveData; + slaveDataHandler.type = SHADOW_JSON_STRING; + INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); @@ -407,9 +309,6 @@ INFO("Net Boot..."); net_modem_boot(); - outOfBoxDemo(); - return; - // Intialize MQTT/Cert parameters ShadowParameters_t sp = ShadowParametersDefault; #ifdef USING_SD_CARD @@ -466,7 +365,7 @@ ERROR("Shadow Register Delta dataUsage Error"); return rc; } - + INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL); // Loop and publish changes from the FRDM board while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) { @@ -480,15 +379,16 @@ } //Read signal quality signalQuality = WNCInterface::_pwnc->getDbmRssi(); - + //get slave data + getSlaveJSON(); INFO("\n=======================================================================================\n"); // Initialize JSON shadow document rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); if (rc == NONE_ERROR) { // Updates the 'reported' color/temp/humidity - rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &signalStrengthHandler, - &dataUsageHandler); + rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &signalStrengthHandler, + &dataUsageHandler, &slaveDataHandler); if (rc == NONE_ERROR) { rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); @@ -500,8 +400,6 @@ } } } - getSlaveJSON(); - INFO("%s" , slaveJSON); wait(SHADOW_SYNC_INTERVAL); }