Sample code for AT&T IoT Services DevLab with IoT StarterKit.
Dependencies: FXOS8700CQ M2XStreamClient-JMF WNCInterface jsonlite mbed-rtos mbed
Fork of WNCInterface_M2Xdemo by
Diff: main.cpp
- Revision:
- 7:721eb6bb68d3
- Parent:
- 6:731f412e6571
- Child:
- 8:b82d330e10e9
--- a/main.cpp Wed Apr 05 17:46:42 2017 +0000 +++ b/main.cpp Thu Apr 06 03:39:06 2017 +0000 @@ -5,6 +5,7 @@ #include "mbed.h" #include "WNCInterface.h" +#include "IOTSMS.h" #define MBED_PLATFORM #define M2X_ENABLE_READER @@ -13,18 +14,15 @@ #include "M2XStreamClient.h" #include "sensors.h" +#include "config_me.h" #define CRLF "\n\r" -char deviceId[] = "9fc504277536c2450ad5fa2cb4e2b478"; // Device you want to post to -char m2xKey[] = "8ec6719c0ccbd8049494b7404af762f8"; // Your M2X API Key or Master API Key - -const char *hstreamName = "humidity"; -const char *tstreamName = "temp"; -const char *accelstreamNames[] = { "accelX", "accelY", "accelZ" }; - +//startup SMS, disabled by default +//#define STARTUP_SMS WNCInterface eth; +WNCSms sms; Client client; M2XStreamClient m2xClient(&client, m2xKey); TimeService timeService(&m2xClient); @@ -36,6 +34,7 @@ DigitalOut led_blue(LED_BLUE); K64F_Sensors_t SENSOR_DATA = {}; +bool bStop = false; //******************************************************************************************************************************************** //* Set the RGB LED's Color @@ -95,6 +94,11 @@ SetLedColor(7); break; } + case 'S': + { //Stop + bStop = true; + break; + } default: { return false; @@ -114,21 +118,30 @@ } void on_command_found(const char* id, const char* name, int index, void *context) { - pc.printf(">>Found a command, index: %d" CRLF, index); - pc.printf(">>ID: %s" CRLF ">>Name: %s" CRLF, id, name); + pc.printf("\t|Found a command, index: %d" CRLF, index); + pc.printf("\t|ID: %s" CRLF "\t|Name: %s" CRLF, id, name); ExecuteCommand(name); m2xClient.markCommandProcessed(deviceId, id, on_fill_data, NULL); - pc.printf(">>Command confirmed" CRLF, id, name); + pc.printf("\t|Command confirmed" CRLF, id, name); } +void on_msg_rcvd( WNCSmsMsg& msg ) { + pc.printf(YEL "SMS received from %s" CRLF, msg.number.c_str()); + pc.printf("|Timestamp: %s %s" CRLF, msg.date.c_str(), msg.time.c_str()); + pc.printf("|Text: '%s'" CRLF, msg.msg.c_str()); + pc.printf(WHT); + ExecuteCommand(msg.msg.c_str()); +} int main() { char timestamp[25]; int length = 25; int response; - - pc.baud(115200); - pc.printf("M2X StarterKit demo: initializng the network" CRLF); + + ExecuteCommand("Red"); + + pc.baud(115200); + pc.printf("M2X StarterKit demo: initializing the network" CRLF); response = eth.init(); pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); if( !response ) { @@ -136,14 +149,22 @@ NVIC_SystemReset(); while(1); } - + + response = sms.init(1, on_msg_rcvd); + pc.printf("SMS interface %s initialized (%02X)." CRLF, response?"IS NOT":"IS", response); + if (!response) { + pc.printf("SMS number is %s" CRLF , sms.getSMSNbr()); + }; + response = eth.connect(); pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); + + ExecuteCommand("Yellow"); pc.printf("Initialize the sensors" CRLF); sensors_init(); read_sensors(); - + pc.printf(WHT "initialize the M2X time service" CRLF); if (!m2x_status_is_success(timeService.init())) pc.printf("Cannot initialize time service!" CRLF); @@ -152,41 +173,58 @@ pc.printf("Current timestamp: %s" CRLF, timestamp); } - pc.printf("Query for possible commands using this device..." CRLF); + ExecuteCommand("Green"); + +#ifdef STARTUP_SMS + response = sms.send("5277", "IoT StarterKit is now running!"); + pc.printf("Startup SMS %s sent." CRLF, response ? "was" : "NOT"); +#endif + + pc.printf("Query for pending commands ..." CRLF); response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); pc.printf("listCommands response code: %d" CRLF, response); - while (true) { + while (!bStop) { // read sensor values read_sensors(); // post the humidity value - pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.Humidity); - response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.Humidity); + pc.printf("Post updateStreamValue (humidity = %.2f)..." CRLF, SENSOR_DATA.Humidity); + response = m2xClient.updateStreamValue(deviceId, hStreamName, SENSOR_DATA.Humidity); pc.printf("Post response code: %d" CRLF, response); // post the temp value - pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.Temperature); - response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.Temperature); + pc.printf("Post updateStreamValue (temp = %.2f)..." CRLF, SENSOR_DATA.Temperature); + response = m2xClient.updateStreamValue(deviceId, tStreamName, SENSOR_DATA.Temperature); pc.printf("Post response code: %d" CRLF, response); + // post accelerometer values pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature); - response = m2xClient.postDeviceUpdate(deviceId, 3, accelstreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ}); + response = m2xClient.postDeviceUpdate(deviceId, 3, accelStreamNames, (float []){SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ}); pc.printf("Post response code: %d" CRLF, response); timeService.getTimestamp(timestamp, &length); - pc.printf("%s waiting for 60 seconds... " CRLF CRLF CRLF, timestamp); + pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls); - for (short idx=0; idx < 6; idx++) { - // wait 10 seconds - delay(10 * 1000); + // now poll for pending commands + for (short idx=0; idx < commandPolls; idx++) { + // wait commandDelay seconds + delay(commandDelay * 1000); - // and then query fo commands - pc.printf("Query for possible commands using this device..." CRLF); + // and then query for commands + pc.printf("\tQuery for pending commands ..." CRLF); response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); - pc.printf("listCommands response code: %d" CRLF, response); + pc.printf("\tlistCommands response code: %d" CRLF, response); } - - } + }; + + pc.printf("Done sending data. Still accepting SMS commands." CRLF); + bStop = false; + while (!bStop) { + delay(1000); + }; + + pc.printf("- - - - - - - THE END - - - - - - - " CRLF); + NVIC_SystemReset(); }