![](/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:
- 9:3f5dfac96ac1
- Parent:
- 8:b82d330e10e9
- Child:
- 12:0dfd0129f75d
--- a/main.cpp Wed Apr 26 00:05:32 2017 +0000 +++ b/main.cpp Wed Oct 25 14:56:01 2017 +0000 @@ -21,6 +21,12 @@ //startup SMS, disabled by default //#define STARTUP_SMS +//command processing enabled by default +#define COMMANDS_ENABLED + +//update all streams in one command, disabled by default +//#define SINGLE_UPDATE + WNCInterface eth; WNCSms sms; Client client; @@ -34,7 +40,10 @@ DigitalOut led_blue(LED_BLUE); K64F_Sensors_t SENSOR_DATA = {}; +K64F_Sensors_t OLD_SENSOR_DATA = {}; bool bStop = false; +bool bSendDataNow = false; +bool bSendSMS = false; bool bM2XConfigured; Ticker WatchdogTicker; @@ -42,6 +51,13 @@ bool bWatchdogOn = false; unsigned char lastLedColor = 0; +InterruptIn btn3(SW3); +InterruptIn btn2(SW2); + +#ifdef SINGLE_UPDATE +const char* allStreamNames[] = { hStreamName, tStreamName, accelStreamNames[0], accelStreamNames[1], accelStreamNames[2] }; +#endif + //******************************************************************************************************************************************** //* Set the RGB LED's Color //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) @@ -85,8 +101,8 @@ SetLedColor(lastLedColor); } - // reset if the watchod is on for more than one minute - if(watchdogTicks > 60 * 4) NVIC_SystemReset(); + // reset if the watchod is on for more than 10 seconds + if(watchdogTicks > 30 * 4) NVIC_SystemReset(); } @@ -146,7 +162,22 @@ } case 'W': { //White - SetLedColor(7); + int Delay = 0, Polls = 0; + + pc.printf("Processing WAIT command" CRLF); + int count = sscanf(Command, "WAIT:%d:%d", Delay, Polls); + if (count == 2) { + pc.printf("Reconfiguring wait loop to %d delay and %d polls ...", Delay, Polls); + if ((Delay > 5) && (Delay < 300) + && (Polls > 0) && (Polls < 100)) { + bSendDataNow = true; + commandDelay = Delay; + commandPolls = Polls; + pc.printf("done" CRLF); + } else + pc.printf("discarding invalid parameters!" CRLF); + } else + SetLedColor(7); break; } case 'S': @@ -172,6 +203,7 @@ // no data to fill } + void on_command_found(const char* id, const char* name, int index, void *context) { pc.printf("\t|Found a command, index: %d" CRLF, index); pc.printf("\t|ID: %s" CRLF "\t|Name: %s" CRLF, id, name); @@ -180,6 +212,7 @@ 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()); @@ -189,16 +222,56 @@ } +void button_2_pressed() { + bSendSMS = true; +} + + +void button_3_pressed() { + bSendDataNow = true; +} + + +void process_buttons() { + char smsText[100]; + + if (bSendSMS) { + bSendSMS = false; + + snprintf(smsText, 100, "Last temperature was %.2f", SENSOR_DATA.Temperature); + int response = sms.send("5277", smsText); + pc.printf(YEL "Button SMS %s sent." CRLF, response ? "was" : "NOT"); + pc.printf(WHT); + }; +} + +bool check_accelerometer_change() { + read_sensors(); + + float diffX, diffY, diffZ; + diffX = abs(SENSOR_DATA.AccelX - OLD_SENSOR_DATA.AccelX); + diffY = abs(SENSOR_DATA.AccelY - OLD_SENSOR_DATA.AccelY); + diffZ = abs(SENSOR_DATA.AccelZ - OLD_SENSOR_DATA.AccelZ); + + bool changed = (diffX > 0.5) || (diffY > 0.5) || (diffZ > 0.5); + if (changed) { + bSendDataNow = true; + pc.printf("Accelerometer changed, sending data immediately." CRLF); + }; + + return changed; +} + int main() { char timestamp[25]; int length = 25; int response; - + ExecuteCommand("Red"); - + pc.baud(115200); - pc.printf("M2X StarterKit demo: initializing the network" CRLF); + pc.printf("M2X StarterKit demo (compiled " __DATE__ ", " __TIME__ "): initializing the network" CRLF); response = eth.init("m2m.com.attz"); pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); if( !response ) { @@ -236,7 +309,7 @@ // set up watchdog ticker running every quarter of a second WatchdogTicker.attach(watchdog_check, 0.25); - { WATCHDOG + { //WATCHDOG pc.printf(WHT "initialize the M2X time service" CRLF); if (!m2x_status_is_success(timeService.init())) pc.printf("Cannot initialize time service!" CRLF); @@ -245,25 +318,31 @@ pc.printf("Current timestamp: %s" CRLF, timestamp); }; }; + + btn2.fall(&button_2_pressed); + btn3.fall(&button_3_pressed); 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 - + +#ifdef COMMANDS_ENABLED { 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); }; +#endif while (!bStop) { // read sensor values read_sensors(); { WATCHDOG +#ifndef SINGLE_UPDATE // post the humidity value pc.printf("Post updateStreamValue (humidity = %.2f)..." CRLF, SENSOR_DATA.Humidity); response = m2xClient.updateStreamValue(deviceId, hStreamName, SENSOR_DATA.Humidity); @@ -275,25 +354,48 @@ pc.printf("Post response code: %d" CRLF, response); // post accelerometer values - pc.printf("Post postDeviceUpdate (accelerometer)..." CRLF, SENSOR_DATA.Temperature); + pc.printf("Post postDeviceUpdate (accelerometer [%.2f,%.2f,%.2f])..." CRLF, 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); - +#else + // post all values at one + pc.printf("Post all stream values [%.2f,%.2f,%.2f,%.2f,%.2f])..." CRLF, SENSOR_DATA.Humidity, SENSOR_DATA.Temperature, SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ); + response = m2xClient.postDeviceUpdate(deviceId, 5, allStreamNames, (float []){SENSOR_DATA.Humidity, SENSOR_DATA.Temperature, SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ}); + pc.printf("Post response code: %d" CRLF, response); +#endif timeService.getTimestamp(timestamp, &length); pc.printf("%s waiting for %d seconds... " CRLF , timestamp, commandDelay * commandPolls); }; - // now poll for pending commands + // save old sensor data, we will use them to check for accelerometer change + OLD_SENSOR_DATA = SENSOR_DATA; + // sleep loop, check for accelerometer changes and pending commands for (short idx=0; idx < commandPolls; idx++) { // wait commandDelay seconds - delay(commandDelay * 1000); - + for (short delays=0; delays < commandDelay; delays++) { + delay(1000); + // if the buttons were pressed process them immediately + process_buttons(); + check_accelerometer_change(); + // button 3 skips wait and sends data right away + if (bSendDataNow || bStop) + break; + }; + +#ifdef COMMANDS_ENABLED // and then query for commands { 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); - }; + }; +#endif + + //if button 3 was pressed skip the wait and send data right away + if (bSendDataNow || bStop) { + bSendDataNow = false; + break; + }; } };