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:
- 33:48172a2972b6
- Parent:
- 32:acf84c7d0075
- Child:
- 34:b153e7a4e2d5
diff -r acf84c7d0075 -r 48172a2972b6 main.cpp
--- a/main.cpp Wed Aug 02 08:04:25 2017 +0000
+++ b/main.cpp Fri Aug 25 20:38:12 2017 +0000
@@ -39,7 +39,7 @@
// AWS defines
#define PATH_MAX 1024
#define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 500 // NOTE: Be wary of this if your JSON doc grows
-#define SHADOW_SYNC_INTERVAL 3.0 // How often we sync with AWS Shadow (in seconds)
+#define SHADOW_SYNC_INTERVAL 5.0 // How often we sync with AWS Shadow (in seconds)
// Comment out the following line if color is not supported on the terminal
//#define USE_COLOR
@@ -93,7 +93,12 @@
int signalQuality = 0;
//Variable to store data usage
-int dataUsage = 0;
+unsigned int dataUsage = 0;
+
+//Variable to store latency
+unsigned int latency = 0;
+unsigned int latencyHistory[10];
+int latencyHistoryIndex = 0;
//slave JSON to publish
string slaveJSON = "";
@@ -117,6 +122,11 @@
// I2C bus (SDA, SCL)
I2C i2c(PTC11, PTC10);
+//Timer
+Timer deviceTimer;
+Timer updateTimer;
+Timer disconnectTimer;
+
//=====================================================================================================================
//
// Functions
@@ -145,6 +155,16 @@
led_blue = !(ucColor & 0x4); //bit 2
}
+unsigned int getAverageLatencyHistory()
+{
+ unsigned int total = 0;
+ for(int i = 0; i < latencyHistoryIndex; i++) {
+ total += latencyHistory[i];
+ }
+ total = total / latencyHistoryIndex;
+ return total;
+}
+
//=====================================================================================================================
//
// AWS Shadow Callbacks
@@ -153,8 +173,7 @@
//*********************************************************************************************************************
//* This is the callback function that fires when an update is sent. It will print the update response status.
//*********************************************************************************************************************
-void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status,
- const char *pReceivedJsonDocument, void *pContextData)
+void ShadowUpdateStatusCallback(const char *pThingName, ShadowActions_t action, Shadow_Ack_Status_t status, const char *pReceivedJsonDocument, void *pContextData)
{
INFO("Shadow Update Status Callback");
@@ -166,38 +185,14 @@
} else if (status == SHADOW_ACK_ACCEPTED) {
INFO("Update Accepted!!"); // Good
}
+ deviceTimer.stop();
+ latencyHistory[latencyHistoryIndex] = latency;
+ unsigned int average = getAverageLatencyHistory();
+ latency = deviceTimer.read_us() * 0.5 + average * 0.5;
+ latencyHistoryIndex = (latencyHistoryIndex + 1) % 10;
+ deviceTimer.reset();
}
-//*********************************************************************************************************************
-//* These are the callback functions that fire when AWS has sends out a shadow update.
-//*********************************************************************************************************************
-void ledControl_Callback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext)
-{
-
- INFO("LED Callback Detected.");
-
- if (pContext != NULL) {
- switch (*(unsigned char *)(pContext->pData)) {
- case COLOR_OFF:
- INFO("LED -> OFF (%d)", *(unsigned char *)(pContext->pData));
- break;
- case COLOR_RED:
- INFO("LED -> RED (%d)", *(unsigned char *)(pContext->pData));
- break;
- case COLOR_GREEN:
- INFO("LED -> GREEN (%d)", *(unsigned char *)(pContext->pData));
- break;
- case COLOR_BLUE:
- INFO("LED -> BLUE (%d)", *(unsigned char *)(pContext->pData));
- break;
- case COLOR_WHITE:
- INFO("LED -> WHITE (%d)", *(unsigned char *)(pContext->pData));
- break;
- }
- } else {
- INFO("pContext was detected as NULL");
- }
-}
//Callback when dataUsage stats are updated
void dataUsageCallback(const char *pJsonString, uint32_t JsonStringDataLen, jsonStruct_t *pContext)
{
@@ -205,20 +200,6 @@
}
//*********************************************************************************************************************
-//* Subscribe callback (used with alternate demo)
-//*********************************************************************************************************************
-int MQTTcallbackHandler(MQTTCallbackParams params)
-{
-
- INFO("Subscribe callback");
- INFO("%.*s\t%.*s",
- (int)params.TopicNameLen, params.pTopicName,
- (int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload);
-
- return 0;
-}
-
-//*********************************************************************************************************************
//* Disconnect handling (used with alternate demo)
//*********************************************************************************************************************
void disconnectCallbackHandler(void)
@@ -253,6 +234,15 @@
strcpy(slaveData, slaveJSON.c_str());
}
+//*********************************************************************************************************************
+//* Push master data over to radio
+//*********************************************************************************************************************
+void sendMasterData()
+{
+ if(radio.writeable()) {
+ radio.printf("*%d,%d,%d\n", dataUsage, latency, signalQuality);
+ }
+}
//=====================================================================================================================
//
// Main
@@ -272,24 +262,31 @@
//JSON struct for signal strength readings
jsonStruct_t signalStrengthHandler;
signalStrengthHandler.cb = NULL;
- signalStrengthHandler.pKey = "Signal Strength";
+ signalStrengthHandler.pKey = "signalStrength";
signalStrengthHandler.pData = &signalQuality;
signalStrengthHandler.type = SHADOW_JSON_INT16;
//JSON struct for data usage
jsonStruct_t dataUsageHandler;
dataUsageHandler.cb = dataUsageCallback;
- dataUsageHandler.pKey = "Data Usage";
+ dataUsageHandler.pKey = "dataUsage";
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;
-
+
+ //JSON struct for latency
+ jsonStruct_t latencyHandler;
+ latencyHandler.cb = NULL;
+ latencyHandler.pKey = "latency";
+ latencyHandler.pData = &latency;
+ latencyHandler.type = SHADOW_JSON_UINT32;
+
INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
@@ -309,7 +306,7 @@
// Boot the Avnet Shield before any other operations
INFO("Net Boot...");
net_modem_boot();
-
+
// Intialize MQTT/Cert parameters
ShadowParameters_t sp = ShadowParametersDefault;
#ifdef USING_SD_CARD
@@ -339,6 +336,7 @@
INFO("Shadow Init...");
rc = aws_iot_shadow_init(&mqttClient);
if (NONE_ERROR != rc) {
+ SetLedColor(COLOR_RED);
ERROR("Shadow Init Error %d", rc);
return rc;
}
@@ -346,6 +344,7 @@
INFO("Shadow Connect...");
rc = aws_iot_shadow_connect(&mqttClient, &sp);
if (NONE_ERROR != rc) {
+ SetLedColor(COLOR_RED);
ERROR("Shadow Connection Error %d", rc);
return rc;
}
@@ -357,6 +356,7 @@
rc = mqttClient.setAutoReconnectStatus(true);
if (NONE_ERROR != rc) {
ERROR("Unable to set Auto Reconnect to true - %d", rc);
+ SetLedColor(COLOR_RED);
return rc;
}
@@ -364,12 +364,20 @@
rc = aws_iot_shadow_register_delta(&mqttClient, &dataUsageHandler);
if (NONE_ERROR != rc) {
ERROR("Shadow Register Delta dataUsage Error");
+ SetLedColor(COLOR_RED);
return rc;
}
-
+
INFO("Will attempt to sync with device shadow every %f seconds.", SHADOW_SYNC_INTERVAL);
+ updateTimer.start();
+ disconnectTimer.start();
+
// Loop and publish changes from the FRDM board
while (NETWORK_ATTEMPTING_RECONNECT == rc || RECONNECT_SUCCESSFUL == rc || NONE_ERROR == rc) {
+ if(updateTimer.read() < SHADOW_SYNC_INTERVAL) {
+ continue;
+ }
+ updateTimer.reset();
// Looks for incoming socket messages
rc = aws_iot_shadow_yield(&mqttClient, 200);
if (NETWORK_ATTEMPTING_RECONNECT == rc) {
@@ -389,24 +397,30 @@
if (rc == NONE_ERROR) {
// Updates the 'reported' color/temp/humidity
- rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &signalStrengthHandler,
- &dataUsageHandler, &slaveDataHandler);
+ rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 4, &signalStrengthHandler,
+ &dataUsageHandler, &slaveDataHandler, &latencyHandler);
if (rc == NONE_ERROR) {
rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
if (rc == NONE_ERROR) {
INFO("Update Shadow: %s", JsonDocumentBuffer);
+ deviceTimer.start();
rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer,
ShadowUpdateStatusCallback, NULL, 15, true);
}
}
}
- wait(SHADOW_SYNC_INTERVAL);
+ sendMasterData();
+ if(disconnectTimer.read() >= 60) {
+ //WNCInterface::_pwnc->at_reinitialize_mdm();
+ NVIC_SystemReset();
+ }
}
if (NONE_ERROR != rc) {
ERROR("An error occurred in the loop %d", rc);
+ SetLedColor(COLOR_RED);
}
INFO("Disconnecting");
@@ -414,6 +428,7 @@
if (NONE_ERROR != rc) {
ERROR("Disconnect error %d", rc);
+ SetLedColor(COLOR_RED);
}
return rc;
