Demo application for using the AT&T IoT Starter Kit Powered by AWS.
Dependencies: SDFileSystem
Fork of ATT_AWS_IoT_demo by
IoT Starter Kit Powered by AWS Demo
This program demonstrates the AT&T IoT Starter Kit sending data directly into AWS IoT. It's explained and used in the Getting Started with the IoT Starter Kit Powered by AWS on starterkit.att.com.
What's required
- AT&T IoT LTE Add-on (also known as the Cellular Shield)
- NXP K64F - for programming
- microSD card - used to store your AWS security credentials
- AWS account
- Python, locally installed
If you don't already have an IoT Starter Kit, you can purchase a kit here. The IoT Starter Kit Powered by AWS includes the LTE cellular shield, K64F, and a microSD card.
Diff: main.cpp
- Revision:
- 18:6370da1de572
- Parent:
- 16:02008a2a2569
- Child:
- 20:ee34856ae510
diff -r 46780b2de3e4 -r 6370da1de572 main.cpp --- a/main.cpp Fri Dec 02 22:49:17 2016 +0000 +++ b/main.cpp Tue Dec 06 22:31:15 2016 +0000 @@ -3,6 +3,9 @@ */ #include "mbed.h" +// SD File System +#include "SDFileSystem.h" + // Serial extension #include "MODSERIAL.h" @@ -36,7 +39,7 @@ #define NUM_COLORS 5 // AWS defines -#define PATH_MAX 4096 +#define PATH_MAX 1024 #define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 200 // 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) @@ -79,13 +82,11 @@ bool buttonOverride = false; InterruptIn Interrupt(SW3); -// Default cert location -//char certDirectory[PATH_MAX + 1] = "../../../certs"; - -// Default MQTT HOST URL is pulled from the aws_iot_config.h +// These defines are pulled from aws_iot_config.h char HostAddress[255] = AWS_IOT_MQTT_HOST; - -// Default MQTT port is pulled from the aws_iot_config.h +char MqttClientID[32] = AWS_IOT_MQTT_CLIENT_ID; +char ThingName[32] = AWS_IOT_MY_THING_NAME; +char PortString[5] = "8883"; uint32_t port = AWS_IOT_MQTT_PORT; //===================================================================================================================== @@ -101,6 +102,9 @@ // USB Serial port (to PC) MODSERIAL pc(USBTX,USBRX,256,256); +// SD card access (MOSI, MISO, SCK, CS) +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); + //===================================================================================================================== // // Functions @@ -122,7 +126,7 @@ //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) //********************************************************************************************************************* void SetLedColor(unsigned char ucColor) -{ +{ //Note that when an LED is on, you write a 0 to it: led_red = !(ucColor & 0x1); //bit 0 led_green = !(ucColor & 0x2); //bit 1 @@ -236,25 +240,16 @@ INFO("AWS IoT SDK Version(dev) %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG); - // TODO: We could try to pull the certs from an SD card - /* - char rootCA[PATH_MAX + 1]; - char clientCRT[PATH_MAX + 1]; - char clientKey[PATH_MAX + 1]; - char CurrentWD[PATH_MAX + 1]; - char cafileName[] = AWS_IOT_ROOT_CA_FILENAME; - char clientCRTName[] = AWS_IOT_CERTIFICATE_FILENAME; - char clientKeyName[] = AWS_IOT_PRIVATE_KEY_FILENAME; - - getcwd(CurrentWD, sizeof(CurrentWD)); - sprintf(rootCA, "%s/%s/%s", CurrentWD, certDirectory, cafileName); - sprintf(clientCRT, "%s/%s/%s", CurrentWD, certDirectory, clientCRTName); - sprintf(clientKey, "%s/%s/%s", CurrentWD, certDirectory, clientKeyName); - - DEBUG("Using rootCA %s", rootCA); - DEBUG("Using clientCRT %s", clientCRT); - DEBUG("Using clientKey %s", clientKey); - */ +#ifdef USING_SD_CARD + // Paths for certs from SD card + INFO("Using SD card files for AWS config."); + DEBUG("- mqtt config path: %s", AWS_MQTT_CONFIG_FILENAME); + DEBUG("- rootCA path: %s", AWS_IOT_ROOT_CA_FILENAME); + DEBUG("- clientCRT path: %s", AWS_IOT_CERTIFICATE_FILENAME); + DEBUG("- clientKey path: %s", AWS_IOT_PRIVATE_KEY_FILENAME); +#else + INFO("Using #defines in aws_iot_config.h and certs from certs.cpp for AWS config."); +#endif // Startup signal - blinks through RGBW then turns off SetLedColor(COLOR_RED); @@ -266,28 +261,34 @@ SetLedColor(COLOR_WHITE); wait(.5); SetLedColor(COLOR_OFF); - + // Setup SW3 button to falling edge interrupt Interrupt.fall(&sw3ButtonHandler); // Boot the Avnet Shield before any other operations net_modem_boot(); - INFO("Initialize the MQTT client..."); - MQTTClient_t mqttClient; - aws_iot_mqtt_init(&mqttClient); - + // Intialize MQTT/Cert parameters ShadowParameters_t sp = ShadowParametersDefault; +#ifdef USING_SD_CARD + rc = (IoT_Error_t)mbedtls_mqtt_config_parse_file(&sp, AWS_MQTT_CONFIG_FILENAME); + if (NONE_ERROR != rc) { + ERROR("Failed to initialize mqtt parameters %d", rc); + return rc; + } + sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; + sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; + sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; +#else sp.pMyThingName = AWS_IOT_MY_THING_NAME; sp.pMqttClientId = AWS_IOT_MQTT_CLIENT_ID; sp.pHost = HostAddress; sp.port = port; - //sp.pClientCRT = clientCRT; - //sp.pClientKey = clientKey; - //sp.pRootCA = rootCA; - sp.pClientCRT = AWS_IOT_CERTIFICATE_FILENAME; - sp.pClientKey = AWS_IOT_PRIVATE_KEY_FILENAME; - sp.pRootCA = AWS_IOT_ROOT_CA_FILENAME; +#endif + + INFO("Initialize the MQTT client..."); + MQTTClient_t mqttClient; + aws_iot_mqtt_init(&mqttClient); INFO("Shadow Init..."); rc = aws_iot_shadow_init(&mqttClient); @@ -357,14 +358,32 @@ if (rc == NONE_ERROR) { INFO("Update Shadow: %s", JsonDocumentBuffer); - rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, JsonDocumentBuffer, + rc = aws_iot_shadow_update(&mqttClient, sp.pMyThingName, JsonDocumentBuffer, ShadowUpdateStatusCallback, NULL, 8, true); } } } - INFO("*****************************************************************************************\n"); - - // Set LED color then wait an loop again + + // Set LED color then wait an loop again + switch (ledColor) { + case COLOR_OFF: + INFO("LED: OFF"); + break; + case COLOR_RED: + INFO("LED: RED"); + break; + case COLOR_GREEN: + INFO("LED: GREEN"); + break; + case COLOR_BLUE: + INFO("LED: BLUE"); + break; + case COLOR_WHITE: + INFO("LED: WHITE"); + break; + } + INFO("*****************************************************************************************"); + SetLedColor(ledColor); wait(SHADOW_SYNC_INTERVAL); }