Initial commit
Dependencies: ConfigFile FXOS8700CQ M2XStreamClient-JMF MODSERIAL SDFileSystem WNCInterface jsonlite mbed-rtos mbed
Fork of StarterKit_M2X_DevLab by
Diff: main.cpp
- Revision:
- 14:6034f6896f22
- Parent:
- 9:3f5dfac96ac1
- Child:
- 15:c63a080c6814
--- a/main.cpp Mon Dec 11 20:02:13 2017 +0000 +++ b/main.cpp Thu Apr 05 03:17:45 2018 +0000 @@ -5,6 +5,7 @@ #include "mbed.h" #include "WNCInterface.h" +#include "WncUDPSocket.h" #include "IOTSMS.h" #define MBED_PLATFORM @@ -14,6 +15,9 @@ #include "M2XStreamClient.h" #include "sensors.h" + +#include "SDFileSystem.h" +#include "ConfigFile.h" #include "config_me.h" #define CRLF "\n\r" @@ -22,15 +26,22 @@ //#define STARTUP_SMS //command processing enabled by default -#define COMMANDS_ENABLED +//#define COMMANDS_ENABLED //update all streams in one command, disabled by default -//#define SINGLE_UPDATE +#define SINGLE_UPDATE + +//send data to AMOC +#define AMOC_ENABLED + +// SD card access (MOSI, MISO, SCK, CS) +SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); +Configuration conf; WNCInterface eth; WNCSms sms; Client client; -M2XStreamClient m2xClient(&client, m2xKey); +M2XStreamClient m2xClient(&client, conf.m2xKey); TimeService timeService(&m2xClient); I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used @@ -44,7 +55,6 @@ bool bStop = false; bool bSendDataNow = false; bool bSendSMS = false; -bool bM2XConfigured; Ticker WatchdogTicker; int watchdogTicks = 0; @@ -54,6 +64,8 @@ InterruptIn btn3(SW3); InterruptIn btn2(SW2); + + #ifdef SINGLE_UPDATE const char* allStreamNames[] = { hStreamName, tStreamName, accelStreamNames[0], accelStreamNames[1], accelStreamNames[2] }; #endif @@ -148,9 +160,9 @@ 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; + strncpy(conf.deviceId, Device, 32); + strncpy(conf.m2xKey, Key, 32); + conf.m2xConfigured = true; } else SetLedColor(5); break; @@ -171,8 +183,8 @@ if ((Delay > 5) && (Delay < 300) && (Polls > 0) && (Polls < 100)) { bSendDataNow = true; - commandDelay = Delay; - commandPolls = Polls; + conf.commandDelay = Delay; + conf.commandPolls = Polls; pc.printf("done" CRLF); } else pc.printf("discarding invalid parameters!" CRLF); @@ -208,7 +220,7 @@ 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); + m2xClient.markCommandProcessed(conf.deviceId, id, on_fill_data, NULL); pc.printf("\t|Command confirmed" CRLF, id, name); } @@ -261,6 +273,25 @@ return changed; } + + +#define MAX_AMOC 16+(7*5)+11+12 +void run_amoc() { + pc.printf("Sending data to AMOC." CRLF); + WncEndpoint AmocEndpoint; + WncUDPSocket AmocSocket; + char buff[MAX_AMOC]; + + if (!AmocEndpoint.set_address(conf.udpHost, conf.udpPort)) { + pc.printf("Cannot resolve AMOC endpoint." CRLF); + return; + }; + + snprintf(buff, MAX_AMOC, "'%s',%.2f,%.2f,%.2f,%.2f,%.2f,%2.6f,%3.6f", conf.devIMEI, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX, SENSOR_DATA.AccelY, SENSOR_DATA.AccelZ, conf.locLat, conf.locLong); + + int resp = AmocSocket.sendTo(AmocEndpoint, buff, strlen(buff)); + pc.printf("%d bytes sent to AMOC" CRLF , resp); +} int main() { @@ -287,6 +318,7 @@ response = eth.connect(); pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); + pc.printf("MAC Address: %s " CRLF CRLF, eth.getMACAddress()); ExecuteCommand("Yellow"); @@ -294,11 +326,10 @@ sensors_init(); read_sensors(); - bM2XConfigured = *deviceId && *m2xKey; - if (!bM2XConfigured) { + if (!conf.m2xConfigured) { pc.printf(RED "Waiting for SMS configuration" CRLF); - while (!bM2XConfigured) { + while (!conf.m2xConfigured) { ExecuteCommand("Red"); delay(1000); ExecuteCommand("Yellow"); @@ -308,8 +339,9 @@ // set up watchdog ticker running every quarter of a second WatchdogTicker.attach(watchdog_check, 0.25); - - { //WATCHDOG + + if (conf.enableM2X) + { WATCHDOG pc.printf(WHT "initialize the M2X time service" CRLF); if (!m2x_status_is_success(timeService.init())) pc.printf("Cannot initialize time service!" CRLF); @@ -329,21 +361,25 @@ pc.printf("Startup SMS %s sent." CRLF, response ? "was" : "NOT"); #endif -#ifdef COMMANDS_ENABLED + if (conf.enableCommands) { WATCHDOG pc.printf("Query for pending commands ..." CRLF); - response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); + response = m2xClient.listCommands(conf.deviceId, on_command_found, NULL, "status=pending"); pc.printf("listCommands response code: %d" CRLF, response); }; -#endif + while (!bStop) { // read sensor values read_sensors(); + if (conf.enableUDP) + run_amoc(); + + if (conf.enableM2X) { WATCHDOG #ifndef SINGLE_UPDATE - // post the humidity value + // 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); @@ -360,19 +396,19 @@ #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}); + response = m2xClient.postDeviceUpdate(conf.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); + pc.printf("%s waiting for %d seconds... " CRLF , timestamp, conf.commandDelay * conf.commandPolls); }; // 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++) { + for (short idx=0; idx < conf.commandPolls; idx++) { // wait commandDelay seconds - for (short delays=0; delays < commandDelay; delays++) { + for (short delays=0; delays < conf.commandDelay; delays++) { delay(1000); // if the buttons were pressed process them immediately process_buttons(); @@ -382,14 +418,13 @@ break; }; -#ifdef COMMANDS_ENABLED // and then query for commands + if (conf.enableCommands) { WATCHDOG pc.printf("\tQuery for pending commands ..." CRLF); - response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); + response = m2xClient.listCommands(conf.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) {