![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
I added functionality to get the RSSI, BER, and Cell Neighbor for reporting connection issues to M2X
Dependencies: mbed FXOS8700CQ mbed-rtos WNCInterface M2XStreamClient-JMF jsonlite
Diff: main.cpp
- Revision:
- 8:b82d330e10e9
- Parent:
- 7:721eb6bb68d3
- Child:
- 9:3f5dfac96ac1
--- a/main.cpp Thu Apr 06 03:39:06 2017 +0000 +++ b/main.cpp Wed Apr 26 00:05:32 2017 +0000 @@ -35,6 +35,12 @@ K64F_Sensors_t SENSOR_DATA = {}; bool bStop = false; +bool bM2XConfigured; + +Ticker WatchdogTicker; +int watchdogTicks = 0; +bool bWatchdogOn = false; +unsigned char lastLedColor = 0; //******************************************************************************************************************************************** //* Set the RGB LED's Color @@ -46,8 +52,48 @@ led_red = !(ucColor & 0x1); //bit 0 led_green = !(ucColor & 0x2); //bit 1 led_blue = !(ucColor & 0x4); //bit 2 + + lastLedColor = ucColor; } //SetLedColor() +struct Watchdog { + Watchdog() { + bWatchdogOn = true; + }; + + ~Watchdog() { + bWatchdogOn = false; + }; +}; + +#define WATCHDOG struct Watchdog aWatchdog; + +void watchdog_check() +{ + // watchdog function is run every 1/4th of a second + if(bWatchdogOn) { + watchdogTicks++; + + // blink LED while watchdog is running + if (watchdogTicks % 2 == 1) { + led_red = led_green = led_blue = 1; + } else { + SetLedColor(lastLedColor); + }; + } else { + watchdogTicks = 0; + SetLedColor(lastLedColor); + } + + // reset if the watchod is on for more than one minute + if(watchdogTicks > 60 * 4) NVIC_SystemReset(); +} + + +bool power_on() { + return (RCM->SRS0 & RCM_SRS0_POR_MASK); +} + bool ExecuteCommand(const char* Command) { @@ -80,8 +126,17 @@ break; } case 'M': - { //Magenta - SetLedColor(5); + { //Magenta or M2X + char Key[33], Device[33]; + + int count = sscanf(Command, "M2X:%32s:%32s", Key, Device); + if (count == 2) { + pc.printf("Got key %s and device %s" CRLF, Key, Device); + strncpy(deviceId, Device, 32); + strncpy(m2xKey, Key, 32); + bM2XConfigured = true; + } else + SetLedColor(5); break; } case 'T': @@ -133,21 +188,22 @@ ExecuteCommand(msg.msg.c_str()); } + + int main() { char timestamp[25]; int length = 25; int response; ExecuteCommand("Red"); - + pc.baud(115200); pc.printf("M2X StarterKit demo: initializing the network" CRLF); - response = eth.init(); + response = eth.init("m2m.com.attz"); pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); if( !response ) { - pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF); + pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF CRLF); NVIC_SystemReset(); - while(1); } response = sms.init(1, on_msg_rcvd); @@ -164,15 +220,32 @@ pc.printf("Initialize the sensors" CRLF); sensors_init(); read_sensors(); + + bM2XConfigured = *deviceId && *m2xKey; + if (!bM2XConfigured) { + pc.printf(RED "Waiting for SMS configuration" CRLF); + + while (!bM2XConfigured) { + ExecuteCommand("Red"); + delay(1000); + ExecuteCommand("Yellow"); + delay(1000); + } + }; + + // set up watchdog ticker running every quarter of a second + WatchdogTicker.attach(watchdog_check, 0.25); - pc.printf(WHT "initialize the M2X time service" CRLF); - if (!m2x_status_is_success(timeService.init())) - pc.printf("Cannot initialize time service!" CRLF); - else { - timeService.getTimestamp(timestamp, &length); - pc.printf("Current timestamp: %s" CRLF, timestamp); - } - + { WATCHDOG + pc.printf(WHT "initialize the M2X time service" CRLF); + if (!m2x_status_is_success(timeService.init())) + pc.printf("Cannot initialize time service!" CRLF); + else { + timeService.getTimestamp(timestamp, &length); + pc.printf("Current timestamp: %s" CRLF, timestamp); + }; + }; + ExecuteCommand("Green"); #ifdef STARTUP_SMS @@ -180,31 +253,35 @@ 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); - + { WATCHDOG + 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 (!bStop) { // read sensor values read_sensors(); - // post the humidity value - 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 = %.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}); - pc.printf("Post response code: %d" CRLF, response); - - timeService.getTimestamp(timestamp, &length); - pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls); + { WATCHDOG + // post the humidity value + 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 = %.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}); + pc.printf("Post response code: %d" CRLF, response); + + timeService.getTimestamp(timestamp, &length); + pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls); + }; // now poll for pending commands for (short idx=0; idx < commandPolls; idx++) { @@ -212,9 +289,11 @@ delay(commandDelay * 1000); // and then query for commands - pc.printf("\tQuery for pending commands ..." CRLF); - response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); - pc.printf("\tlistCommands response code: %d" CRLF, response); + { WATCHDOG + pc.printf("\tQuery for pending commands ..." CRLF); + response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); + pc.printf("\tlistCommands response code: %d" CRLF, response); + }; } }; @@ -226,5 +305,4 @@ pc.printf("- - - - - - - THE END - - - - - - - " CRLF); NVIC_SystemReset(); -} - +} \ No newline at end of file