AWS IoT demonstration using the Avnet Shield (AT&T LTE) and the FRDM-K64F target board.
Dependencies: K64F_FATFileSystem
Fork of mbed-os-example-tls-tls-client by
Diff: main.cpp
- Revision:
- 20:ee34856ae510
- Parent:
- 18:6370da1de572
- Child:
- 23:b9ff83dc965f
diff -r 488dad1e168e -r ee34856ae510 main.cpp --- a/main.cpp Tue Dec 06 22:45:00 2016 +0000 +++ b/main.cpp Wed Dec 07 20:37:20 2016 +0000 @@ -21,6 +21,9 @@ #include "aws_iot_config.h" #include "aws_iot_mqtt_interface.h" +// Sensors +#include "HTS221.h" + #if DEBUG_LEVEL > 0 #include "mbedtls/debug.h" #endif @@ -67,6 +70,9 @@ #define DEF #endif +// Sensor defines +#define CTOF(x) ((x)*1.8+32) // Temperature + //===================================================================================================================== // // Globals @@ -89,6 +95,10 @@ char PortString[5] = "8883"; uint32_t port = AWS_IOT_MQTT_PORT; +// Sensor data +float temperature = 0.0; +int humidity = 0; + //===================================================================================================================== // // Devices @@ -105,6 +115,9 @@ // SD card access (MOSI, MISO, SCK, CS) SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); +// I2C bus (SDA, SCL) +I2C i2c(PTC11, PTC10); + //===================================================================================================================== // // Functions @@ -216,8 +229,10 @@ // Set baud rate for PC Serial pc.baud(115200); INFO("Hello World from AT&T IoT Start Kit demo!"); - - IoT_Error_t rc = NONE_ERROR; + + int i; + IoT_Error_t rc = NONE_ERROR; + HTS221 hts221; // Temp/humidity char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER]; size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]); @@ -228,15 +243,18 @@ ledController.pKey = "ledColor"; ledController.type = SHADOW_JSON_UINT8; - // TODO Add FRDM temperature reading - /* - float temperature = 0.0; + // JSON struct for temperature\humidity readings jsonStruct_t temperatureHandler; temperatureHandler.cb = NULL; temperatureHandler.pKey = "temperature"; temperatureHandler.pData = &temperature; temperatureHandler.type = SHADOW_JSON_FLOAT; - */ + + jsonStruct_t humidityHandler; + humidityHandler.cb = NULL; + humidityHandler.pKey = "humidity"; + humidityHandler.pData = &humidity; + humidityHandler.type = SHADOW_JSON_INT16; INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); @@ -261,6 +279,13 @@ SetLedColor(COLOR_WHITE); wait(.5); SetLedColor(COLOR_OFF); + + // Initialize sensors + void hts221_init(void); + i = hts221.begin(); + if(!i) { + WARN(RED "HTS221 NOT DETECTED!!\n\r"); + } // Setup SW3 button to falling edge interrupt Interrupt.fall(&sw3ButtonHandler); @@ -285,7 +310,7 @@ sp.pHost = HostAddress; sp.port = port; #endif - + INFO("Initialize the MQTT client..."); MQTTClient_t mqttClient; aws_iot_mqtt_init(&mqttClient); @@ -335,7 +360,11 @@ wait(1); continue; } - + + // Read sensor data + temperature = CTOF(hts221.readTemperature()); + humidity = hts221.readHumidity(); + INFO("\n=======================================================================================\n"); // Initialize JSON shadow document rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); @@ -344,14 +373,19 @@ // If there has been a SW3 button press update the 'desired' color if (buttonOverride) { rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); + //rc = aws_iot_shadow_add_desired(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, + // &temperatureHandler, + // &humidityHandler); buttonOverride = false; } // Updates the 'reported' color - rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); + //rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 1, &ledController); // TODO: format for adding temperature - //rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 2, &ledController, &temperatureHandler); + rc = aws_iot_shadow_add_reported(JsonDocumentBuffer, sizeOfJsonDocumentBuffer, 3, &ledController, + &temperatureHandler, + &humidityHandler); if (rc == NONE_ERROR) { rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer); @@ -362,9 +396,11 @@ ShadowUpdateStatusCallback, NULL, 8, true); } } - } - - // Set LED color then wait an loop again + } + + // Print data + INFO("Temperature is: %0.2f F", temperature); + INFO("Humidity is: %02d", humidity); switch (ledColor) { case COLOR_OFF: INFO("LED: OFF"); @@ -383,7 +419,8 @@ break; } INFO("*****************************************************************************************"); - + + // Set the LED color SetLedColor(ledColor); wait(SHADOW_SYNC_INTERVAL); }