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:
- 4:08979e323c6e
- Parent:
- 2:eb0768c06c1b
- Child:
- 5:8099493f2c35
diff -r 94a2e1a54aac -r 08979e323c6e main.cpp --- a/main.cpp Thu Nov 17 18:40:25 2016 +0000 +++ b/main.cpp Wed Apr 05 04:53:25 2017 +0000 @@ -12,159 +12,205 @@ #include <jsonlite.h> #include "M2XStreamClient.h" +#include "sensors.h" + #define CRLF "\n\r" -char deviceId[] = "e83cdd8645ab1a7c0c480156efbf78f6"; // Device you want to post to -char m2xKey[] = "4d7e1da7f05c3fa4d5426419891a254d"; // Your M2X API Key or Master API Key +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 = "temperature"; -const char *streamNames[] = { tstreamName, hstreamName }; -char name[] = "Wake Forest"; // Name of current location of datasource - -int counts[] = { 2, 1 }; -const char *ats[] = { "2016-09-09T02:05:14.692Z", - "2016-09-09T02:05:14.700Z", - "2016-09-09T02:05:14.692Z" }; -double values[] = { 10.9, 11.2, 6.1 }; - -char fromTime[]= "1969-12-31T19:00:01.000Z"; // yyyy-mm-ddTHH:MM:SS.SSSZ -char endTime[25]; - -double latitude = 33.007872; // You could read these values from a GPS but -double longitude = -96.751614; // for now, will just hardcode them -double elevation = 697.00; +const char *tstreamName = "temp"; WNCInterface eth; Client client; M2XStreamClient m2xClient(&client, m2xKey); TimeService timeService(&m2xClient); -MODSERIAL pc(USBTX,USBRX,256,256); + +I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used +Serial pc(USBTX, USBRX); // tx, rx +DigitalOut led_green(LED_GREEN); +DigitalOut led_red(LED_RED); +DigitalOut led_blue(LED_BLUE); + +K64F_Sensors_t SENSOR_DATA = +{ + .Temperature = "0", + .Humidity = "0", + .AccelX = "0", + .AccelY = "0", + .AccelZ = "0", + .MagnetometerX = "0", + .MagnetometerY = "0", + .MagnetometerZ = "0", + .AmbientLightVis = "0", + .AmbientLightIr = "0", + .UVindex = "0", + .Proximity = "0", + .Temperature_Si7020 = "0", + .Humidity_Si7020 = "0", + .Virtual_Sensor1 = "0", + .Virtual_Sensor2 = "0", + .Virtual_Sensor3 = "0", + .Virtual_Sensor4 = "0", + .Virtual_Sensor5 = "0", + .Virtual_Sensor6 = "0", + .Virtual_Sensor7 = "0", + .Virtual_Sensor8 = "0", + .GPS_Satellites = "0", + .GPS_Latitude = "0", + .GPS_Longitude = "0", + .GPS_Altitude = "0", + .GPS_Speed = "0", + .GPS_Course = "0" +}; + +//******************************************************************************************************************************************** +//* Set the RGB LED's Color +//* 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 + led_blue = !(ucColor & 0x4); //bit 2 +} //SetLedColor() + -void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { - pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type); - pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value); +bool ExecuteCommand(const char* Command) +{ + char cLedColor = *Command; + switch(cLedColor) + { + case 'O': + { //Off + SetLedColor(0); + break; + } + case 'R': + { //Red + SetLedColor(1); + break; + } + case 'G': + { //Green + SetLedColor(2); + break; + } + case 'Y': + { //Yellow + SetLedColor(3); + break; + } + case 'B': + { //Blue + SetLedColor(4); + break; + } + case 'M': + { //Magenta + SetLedColor(5); + break; + } + case 'T': + { //Turquoise + SetLedColor(6); + break; + } + case 'W': + { //White + SetLedColor(7); + break; + } + default: + { + return false; + } + } //switch(cLedColor) + return true; } -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\n Name: %s" CRLF, id, name); +void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { + pc.printf(">>Found a data point, index: %d type: %d" CRLF, index, type); + pc.printf(">>At: %s" CRLF " Value: %s" CRLF, at, value); +} + +void on_fill_data(Print *print, void *context) { + // no data to fill } -void on_location_found(const char* name, - double latitude, - double longitude, - double elevation, - const char* timestamp, - int index, - void* context) { - pc.printf(">>Found a location, index: %d" CRLF, index); - pc.printf(">>Name: %s" CRLF ">>Latitude: %lf" CRLF ">>Longitude: %lf" CRLF, name, latitude, longitude); - pc.printf(">>Elevation: %lf" CRLF ">>Timestamp: %s" CRLF, elevation, timestamp); +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); + ExecuteCommand(name); + m2xClient.markCommandProcessed(deviceId, id, on_fill_data, NULL); + pc.printf(">>Command confirmed" CRLF, id, name); } + int main() { - char timestamp[25]; - int length = 25; - char amb_temp[6]; - char amb_humd[6]; - int response, cnt=1; - double temp=0.00; //we will just increment these 0.01 each time through the loop - double humid=0.00; //we will just increment these 1 each time through the loop wrapping at 100 - - pc.baud(115200); - pc.printf("Start m2x-demo-all by initializng the network" CRLF); - response = eth.init(); - pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); - if( !response ) { - pc.printf(" - - - - - - - ALL DONE - - - - - - - " CRLF); - while(1); - } + char timestamp[25]; + int length = 25; + int response; + + pc.baud(115200); + pc.printf("M2X StarterKit demo: initializng the network" CRLF); + response = eth.init(); + pc.printf("WNC Module %s initialized (%02X)." CRLF, response?"IS":"IS NOT", response); + if( !response ) { + pc.printf(" - - - - - - - SYSTEM RESET - - - - - - - " CRLF); + NVIC_SystemReset(); + while(1); + } - response = eth.connect(); - pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); - - pc.printf("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); - strcpy(endTime,timestamp); - } - - pc.printf("Now delete all existing values" CRLF); - // Delete values - pc.printf("Delete humidity values..." CRLF); - response = m2xClient.deleteValues(deviceId,hstreamName, fromTime, endTime); - pc.printf("Delete response code: %d" CRLF, response); - - pc.printf("Delete temp values..." CRLF); - response = m2xClient.deleteValues(deviceId,tstreamName, fromTime, endTime); - pc.printf("Delete response code: %d" CRLF, response); - - pc.printf("Delete location values..." CRLF); - response = m2xClient.deleteLocations(deviceId, fromTime, endTime); - pc.printf("Delete response code: %d" CRLF, response); - - pc.printf("Query for possible commands using this device..." CRLF); - response = m2xClient.listCommands(deviceId, on_command_found, NULL); - pc.printf("listCommands response code: %d" CRLF, response); + response = eth.connect(); + pc.printf("IP Address: %s " CRLF CRLF, eth.getIPAddress()); + + 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); + else { + timeService.getTimestamp(timestamp, &length); + pc.printf("Current timestamp: %s" CRLF, timestamp); + } + + pc.printf("Query for possible commands using this device..." CRLF); + response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); + pc.printf("listCommands response code: %d" CRLF, response); - while (true) { - // read temp -- for now, just use a fixed temp, but will need to read the HTS221 - // and put it into a 6 byte string formatted as "%0.2f" - sprintf(amb_temp,"%0.2f",temp); - sprintf(amb_humd,"%0.2f",humid); - temp += .01; - humid += 1.0; - humid = fmod(humid,100.0); - pc.printf("cnt=%d\r\n",cnt++); - // post the humidity value - pc.printf("Post updateStreamValue (humidity)..." CRLF); - response = m2xClient.updateStreamValue(deviceId, "humidity", humid); - pc.printf("Post response code: %d" CRLF, response); - - // post the temp value - pc.printf("Post updateStreamValue (temp)..." CRLF); - response = m2xClient.updateStreamValue(deviceId, "temperature", temp); - pc.printf("Post response code: %d" CRLF, response); - - // read temperature - pc.printf("listStreamValues (temp)..." CRLF); - response = m2xClient.listStreamValues(deviceId, tstreamName, on_data_point_found, NULL); - pc.printf("listStreamValues response code: %d" CRLF, response); - if (response == -1) while (true) ; + while (true) { + // read sensor values + read_sensors(); - // read temperature - pc.printf("listStreamValues (humid)..." CRLF); - response = m2xClient.listStreamValues(deviceId, hstreamName, on_data_point_found, NULL); - pc.printf("listStreamValues response code: %d" CRLF, response); - if (response == -1) while (true) ; + // post the humidity value + pc.printf("Post updateStreamValue (humidity = %f)..." CRLF, SENSOR_DATA.fHumidity); + response = m2xClient.updateStreamValue(deviceId, hstreamName, SENSOR_DATA.fHumidity); + pc.printf("Post response code: %d" CRLF, response); + + // post the temp value + pc.printf("Post updateStreamValue (temp = %f)..." CRLF, SENSOR_DATA.fTemperature); + response = m2xClient.updateStreamValue(deviceId, tstreamName, SENSOR_DATA.fTemperature); + pc.printf("Post response code: %d" CRLF, response); + + timeService.getTimestamp(timestamp, &length); + pc.printf("%s waiting for 60 seconds... " CRLF CRLF CRLF, timestamp); + + for (short idx=0; idx < 6; idx++) { + // wait 10 seconds + delay(10 * 1000); - // update location - pc.printf("updateLocation..." CRLF); - response = m2xClient.updateLocation(deviceId, name, latitude, longitude, elevation); - pc.printf("updateLocation response code: %d" CRLF, response); - if (response == -1) while (true) ; - - // read location - pc.printf("readLocation..." CRLF); - int response = m2xClient.readLocation(deviceId, on_location_found, NULL); - pc.printf("readLocation response code: %d" CRLF, response); + // and then query fo commands + pc.printf("Query for possible commands using this device..." CRLF); + response = m2xClient.listCommands(deviceId, on_command_found, NULL, "status=pending"); + pc.printf("listCommands response code: %d" CRLF, response); + } - pc.printf("PostDeviceUpdates..." CRLF); - response = m2xClient.postDeviceUpdates(deviceId, 2, streamNames, counts, ats, values); - pc.printf("Post response code: %d" CRLF, response); - - timeService.getTimestamp(timestamp, &length); - pc.printf("Thats all folks, got to wait 60 seconds... (%s)" CRLF CRLF CRLF, timestamp); - - // wait 60 secs and then loop - delay(6000); - - - } + } }