Official reference client implementation for Cumulocity SmartREST on u-blox C027.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Revision 71:063c45e99578, committed 2015-02-16
- Comitter:
- xinlei
- Date:
- Mon Feb 16 09:17:30 2015 +0000
- Parent:
- 70:f489ca11f254
- Child:
- 72:c5709ae7b193
- Commit message:
- Various minor bug fixes
Changed in this revision
--- a/DeviceBootstrap.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/DeviceBootstrap.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -64,7 +64,7 @@ if (_client.setAuthorization(DEVICE_BOOTSTRAP_USERNAME, DEVICE_BOOTSTRAP_PASSWORD) != SMARTREST_SUCCESS) return false; - _io.lcdPrint("BOOTSTRAP", _deviceInfo.imei()); + _io.lcdPrint("Bootstrap", _deviceInfo.imei()); tries = 255; do { @@ -99,12 +99,12 @@ recvdRecord.value(4).characterValue(), recvdRecord.value(5).characterValue()); - _io.lcdPrint("BOOTSTRAP SUCCESSFUL", _username, _password); + _io.lcdPrint("Bootstrap Success", _username, _password); return true; } while (--tries > 0); - _io.lcdPrint("BOOTSTRAP FAILURE"); + _io.lcdPrint("Bootstrap Failure"); return false; }
--- a/MbedAgent.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/MbedAgent.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -23,18 +23,44 @@ bool MbedAgent::init() { - if ((!_integration.init()) || - (!_configurationSynchronization.init()) || - (!_signalQualityMeasurement.init()) || - (!_temperatureMeasurement.init()) || - (!_accelerationMeasurement.init()) || - (!_analogMeasurement.init()) || - (!_locationUpdate.init()) || - (!_operationSupport.init())) { - puts("Initialization failed."); - return false; + bool flag = true; + if (!_integration.init()) { + puts("Init Device Integration failed."); + flag = false; + } + if (!_configurationSynchronization.init()) { + puts("Init Configuration Synchronization failed."); + flag = false; + } + if (!_signalQualityMeasurement.init()) { + puts("Init Signal Quality Measurement failed."); + flag = false; + } + if (!_temperatureMeasurement.init()) { + puts("Init Temperature Measurement failed."); + flag = false; } - return true; + if (!_accelerationMeasurement.init()) { + puts("Init Acceleration Measurement failed."); + flag = false; + } + if (!_analogMeasurement.init()) { + puts("Init Analog Measurement failed."); + flag = false; + } + if (!_locationUpdate.init()) { + puts("Init Location Measurement failed."); + flag = false; + } + if (!_operationSupport.init()) { + puts("Init Operation Support failed."); + flag = false; + } + if (flag) + puts("T"); + else + puts("F"); + return flag; } bool MbedAgent::run() @@ -45,7 +71,7 @@ Thread::wait(5000); - _io.lcdPrint("INTEGRATION"); + _io.lcdPrint("Connect to Cloud"); if (!_integration.integrate()) { return false; } @@ -56,7 +82,7 @@ char status[60]; snprintf(status, sizeof(status), "ID: %ld", _deviceId); - _io.lcdPrint("INTEGRATED", status); + _io.lcdPrint("Connected", status); loop();
--- a/MbedSmartRest.lib Tue Feb 10 20:52:13 2015 +0000 +++ b/MbedSmartRest.lib Mon Feb 16 09:17:30 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/Cumulocity/code/MbedSmartRest/#aba98ad2ac1b +http://mbed.org/users/Cumulocity/code/MbedSmartRest/#56478403e340
--- a/io/DeviceFeedback.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/io/DeviceFeedback.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -9,8 +9,14 @@ DeviceFeedback::DeviceFeedback(PwmOut speaker) : _speaker(speaker), - _thread(DeviceFeedback::thread_func, this) + _thread(DeviceFeedback::thread_func, this), + shieldLEDRed(D5), + shieldLEDBlue(D8), + shieldLEDGreen(D9) { + shieldLEDRed = 255; + shieldLEDBlue = 255; + shieldLEDGreen = 255; } void DeviceFeedback::beepSuccess() @@ -25,11 +31,17 @@ void DeviceFeedback::closeRelay() { + shieldLEDRed = 255; + shieldLEDBlue = 255; + shieldLEDGreen = 0; sendMessage(MSG_CLOSE_RELAY); } void DeviceFeedback::openRelay() { + shieldLEDRed = 255; + shieldLEDBlue = 255; + shieldLEDGreen = 255; sendMessage(MSG_OPEN_RELAY); }
--- a/io/DeviceFeedback.h Tue Feb 10 20:52:13 2015 +0000 +++ b/io/DeviceFeedback.h Mon Feb 16 09:17:30 2015 +0000 @@ -23,6 +23,13 @@ private: PwmOut _speaker; Thread _thread; + + /* LED lights on the application shield, consists of three colors: Red, Blue, Green. + * Note: value 0 means fully on for a color, 255 means off. + */ + DigitalOut shieldLEDRed; + DigitalOut shieldLEDBlue; + DigitalOut shieldLEDGreen; Mail<uint8_t, 16> _mail; };
--- a/main.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/main.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -31,44 +31,42 @@ MDMRtos<MDMSerial> mdm; GPSI2C gps; + DeviceIO io(gps); //mdm.setDebug(4); - + io.lcdPrint("Device Init"); if (!mdm.init(SIM_PIN, &devStatus)) status = 1; else if (!gps.init()) status = 2; - DeviceIO io(gps); - switch (status) { case 1: - io.lcdPrint("MODEM INIT FAILURE", "CHECK SIM"); + io.lcdPrint("Modem Init Failure", "Check SIM"); break; case 2: - io.lcdPrint("GPS INIT FAILURE"); + io.lcdPrint("GPS Init Failure"); break; } if (status != 0) goto error; - io.lcdPrint("DEVICE INIT"); - io.lcdPrint("REGISTER NETWORK", "IMEI", devStatus.imei); + io.lcdPrint("Register Network", "IMEI", devStatus.imei); if (!mdm.registerNet()) { - io.lcdPrint("NETWORK REG ERROR"); + io.lcdPrint("Network Reg Error"); goto error; } - io.lcdPrint("JOIN NETWORK"); + io.lcdPrint("Join Network"); #ifdef SIM_APN if (mdm.join(SIM_APN, SIM_USER, SIM_PASS) == NOIP) { #else if (mdm.join() == NOIP) { #endif - io.lcdPrint("NETWORK JOIN FAILURE"); + io.lcdPrint("Network join failure"); goto error; } @@ -78,31 +76,37 @@ DeviceMemory deviceMemory(mdm); if (io.resetButtonPressed()) { - if (deviceMemory.resetPlatformCredentials()) - io.deviceFeedback().beepSuccess(); - else - io.deviceFeedback().beepFailure(); - Thread::wait(1000); + io.lcdPrint("Factory Reset"); + if (deviceMemory.resetPlatformCredentials()) { +// io.deviceFeedback().beepSuccess(); + io.lcdPrint("Reset Success"); + } + else { +// io.deviceFeedback().beepFailure(); + io.lcdPrint("Reset Failure"); + } +// Thread::wait(1000); return; } MbedAgent agent(io, mdm, deviceInfo, deviceMemory); - io.lcdPrint("AGENT INIT"); + io.lcdPrint("Agent Init"); if (!agent.init()) { - io.lcdPrint("AGENT INIT FAILURE"); + io.lcdPrint("Agent Init Failure"); goto error; } tries = 3; do { - io.lcdPrint("AGENT RUN"); +// io.lcdPrint("AGENT RUN"); + puts("R"); if (agent.run()) break; } while (--tries > 0); if (tries == 0) { - io.lcdPrint("AGENT RUN FAILURE"); + io.lcdPrint("Agent Run Failure"); goto error; } }
--- a/measurement/AccelerationMeasurement.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/AccelerationMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -3,6 +3,10 @@ #include "CharValue.h" #include "IntegerValue.h" #include "FloatValue.h" +#include "MbedClient.h" + +#define THRESHOLD_PERCENT_ACCE 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. +#define TIME_LIMIT_ACCE 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar. (in seconds) AccelerationMeasurement::AccelerationMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, MMA7660& sensor) : _client(client), @@ -11,6 +15,10 @@ _sensor(sensor) { _init = false; + oldValues[0] = 0; + oldValues[1] = 0; + oldValues[2] = 0; + sendingTimer.start(); } bool AccelerationMeasurement::init() @@ -30,13 +38,24 @@ bool AccelerationMeasurement::run() { - float data[3] = { 0.0, 0.0, 0.0 }; - if (!_test) return false; - _sensor.readData(data); - + float data[3] = { 0.0, 0.0, 0.0 }; + _sensor.readData(data); +// printf("Acceleration sensor readings: [Old] %.2f, %.2f, %.2f\r\n", oldValues[0], oldValues[1], oldValues[2]); +// printf(" [New] %.2f, %.2f, %.2f\r\n", data[0], data[1], data[2]); + if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ACCE && + abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ACCE && + abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_ACCE) { + if (sendingTimer.read() < TIME_LIMIT_ACCE) { + printf("Similar acceleration readings found, no sending!\r\n"); + return true; + } else { + printf("Sending timer of acceleration sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + } + } + ComposedRecord record; IntegerValue msgId(106); IntegerValue devId(_deviceId); @@ -52,5 +71,10 @@ return false; } _client.stop(); + oldValues[0] = data[0]; + oldValues[1] = data[1]; + oldValues[2] = data[2]; + sendingTimer.reset(); + printf("Acceleration readings sent.\r\n"); return true; }
--- a/measurement/AccelerationMeasurement.h Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/AccelerationMeasurement.h Mon Feb 16 09:17:30 2015 +0000 @@ -19,6 +19,8 @@ SmartRestTemplate& _tpl; AbstractSmartRest& _client; MMA7660& _sensor; + float oldValues[3]; + Timer sendingTimer; }; #endif \ No newline at end of file
--- a/measurement/AnalogMeasurement.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/AnalogMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -4,6 +4,9 @@ #include "IntegerValue.h" #include "FloatValue.h" +#define THRESHOLD_PERCENT_ANA 0.05 // Percentage cut-off for avoiding sending similar analog sensor data. +#define TIME_LIMIT_ANA 900 // Time interval for forcing a sending even if analog sensor readings are constantly similar. + AnalogMeasurement::AnalogMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, AnalogIn& analog1, AnalogIn& analog2) : _client(client), _tpl(tpl), @@ -12,6 +15,9 @@ _analog2(analog2) { _init = false; + oldValues[0] = 0; + oldValues[1] = 0; + sendingTimer.start(); } bool AnalogMeasurement::init() @@ -30,11 +36,24 @@ bool AnalogMeasurement::run() { + float data[2] = {0, 0}; + data[0] = _analog1.read()*100; + data[1] = _analog2.read()*100; + if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA && + abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) { + if (sendingTimer.read() < TIME_LIMIT_ANA) { + printf("Similar analog readings found, no sending!\r\n"); + return true; + } else { + printf("Sending timer of analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + } + } + ComposedRecord record; IntegerValue msgId(107); IntegerValue devId(_deviceId); - FloatValue analog1(_analog1.read(), 1); - FloatValue analog2(_analog2.read(), 1); + FloatValue analog1(data[0], 1); + FloatValue analog2(data[1], 1); if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2))) return false; @@ -44,5 +63,9 @@ return false; } _client.stop(); + oldValues[0] = data[0]; + oldValues[1] = data[1]; + sendingTimer.reset(); + printf("Analog readings sent.\r\n"); return true; }
--- a/measurement/AnalogMeasurement.h Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/AnalogMeasurement.h Mon Feb 16 09:17:30 2015 +0000 @@ -20,6 +20,8 @@ AbstractSmartRest& _client; AnalogIn& _analog1; AnalogIn& _analog2; + float oldValues[2]; + Timer sendingTimer; }; #endif \ No newline at end of file
--- a/measurement/LocationUpdate.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/LocationUpdate.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -5,6 +5,9 @@ #include "IntegerValue.h" #include "FloatValue.h" +#define THRESHOLD_PERCENT_LOC 0.05 // Percentage cut-off for avoiding sending similar acceleration sensor data. +#define TIME_LIMIT_LOC 900 // Time interval for forcing a sending even if acceleration sensor readings are constantly similar. + LocationUpdate::LocationUpdate(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, GPSTracker& gpsTracker) : _client(client), _tpl(tpl), @@ -12,6 +15,10 @@ _gpsTracker(gpsTracker) { _init = false; + oldValues[0] = 0; + oldValues[1] = 0; + oldValues[2] = 0; + sendingTimer.start(); } bool LocationUpdate::init() @@ -41,9 +48,22 @@ puts("No GPS data available."); return true; } + float data[3] = { 0.0, 0.0, 0.0 }; + data[0] = position.altitude; + data[1] = position.latitude; + data[2] = position.longitude; + if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_LOC && + abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_LOC && + abs(oldValues[2]-data[2]) <= abs(oldValues[2])*THRESHOLD_PERCENT_LOC) { + if (sendingTimer.read() < TIME_LIMIT_LOC) { + printf("Similar location readings found, no sending!\r\n"); + return true; + } else { + printf("Sending timer of location sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + } + } puts("Starting measurement sending."); - Aggregator aggregator; ComposedRecord record1, record2; IntegerValue msgId1(108); @@ -67,5 +87,10 @@ } _client.stop(); + oldValues[0] = data[0]; + oldValues[1] = data[1]; + oldValues[2] = data[2]; + sendingTimer.reset(); + printf("Location readings sent.\r\n"); return true; }
--- a/measurement/LocationUpdate.h Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/LocationUpdate.h Mon Feb 16 09:17:30 2015 +0000 @@ -19,6 +19,8 @@ SmartRestTemplate& _tpl; AbstractSmartRest& _client; GPSTracker& _gpsTracker; + float oldValues[3]; + Timer sendingTimer; }; #endif \ No newline at end of file
--- a/measurement/SignalQualityMeasurement.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/SignalQualityMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -4,6 +4,9 @@ #include "IntegerValue.h" #include "FloatValue.h" +#define THRESHOLD_PERCENT_SIG 0.05 // Percentage cut-off for avoiding sending similar signal sensor data. +#define TIME_LIMIT_SIG 900 // Time interval for forcing a sending even if signal sensor readings are constantly similar. + SignalQualityMeasurement::SignalQualityMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, DeviceInfo& deviceInfo) : _client(client), _tpl(tpl), @@ -11,6 +14,9 @@ _deviceInfo(deviceInfo) { _init = false; + oldValues[0] = 0; + oldValues[1] = 0; + sendingTimer.start(); } bool SignalQualityMeasurement::init() @@ -34,6 +40,19 @@ if ((signalQuality = _deviceInfo.signalQuality()) == NULL) return false; + float data[2] = {0, 0}; + data[0] = signalQuality->rssi; + data[1] = signalQuality->ber; + if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_SIG && + abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_SIG) { + if (sendingTimer.read() < TIME_LIMIT_SIG) { + printf("Similar signal readings found, no sending!\r\n"); + return true; + } else { + printf("Sending timer of signal sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + } + } + ComposedRecord record; IntegerValue msgId(104); IntegerValue devId(_deviceId); @@ -47,6 +66,11 @@ _client.stop(); return false; } + _client.stop(); + oldValues[0] = data[0]; + oldValues[1] = data[1]; + sendingTimer.reset(); + printf("Signal quality measurement sent.\r\n"); return true; }
--- a/measurement/SignalQualityMeasurement.h Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/SignalQualityMeasurement.h Mon Feb 16 09:17:30 2015 +0000 @@ -19,6 +19,8 @@ SmartRestTemplate& _tpl; AbstractSmartRest& _client; DeviceInfo& _deviceInfo; + float oldValues[2]; + Timer sendingTimer; }; #endif \ No newline at end of file
--- a/measurement/TemperatureMeasurement.cpp Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/TemperatureMeasurement.cpp Mon Feb 16 09:17:30 2015 +0000 @@ -4,13 +4,18 @@ #include "IntegerValue.h" #include "FloatValue.h" +#define THRESHOLD_PERCENT_TEMP 0.05 // Percentage cut-off for avoiding sending similar temperature sensor data. +#define TIME_LIMIT_TEMP 900 // Time interval for forcing a sending even if analog sensor readings are constantly similar. + TemperatureMeasurement::TemperatureMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, LM75B& sensor) : _client(client), _tpl(tpl), _deviceId(deviceId), - _sensor(sensor) + _sensor(sensor), + oldValue(0) { _init = false; + sendingTimer.start(); } bool TemperatureMeasurement::init() @@ -33,10 +38,21 @@ if (!_open) return false; + float data = 0; + data = _sensor.temp(); + if (abs(oldValue-data) <= abs(oldValue)*THRESHOLD_PERCENT_TEMP) { + if (sendingTimer.read() < TIME_LIMIT_TEMP) { + printf("Similar temperature readings found, no sending!\r\n"); + return true; + } else { + printf("Sending timer of temperature sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); + } + } + ComposedRecord record; IntegerValue msgId(105); IntegerValue devId(_deviceId); - FloatValue temperature(_sensor.temp(), 1); + FloatValue temperature(data, 1); if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(temperature))) return false; @@ -46,5 +62,8 @@ return false; } _client.stop(); + oldValue = data; + sendingTimer.reset(); + printf("Temperature readings sent.\r\n"); return true; }
--- a/measurement/TemperatureMeasurement.h Tue Feb 10 20:52:13 2015 +0000 +++ b/measurement/TemperatureMeasurement.h Mon Feb 16 09:17:30 2015 +0000 @@ -19,6 +19,8 @@ SmartRestTemplate& _tpl; AbstractSmartRest& _client; LM75B& _sensor; + float oldValue; + Timer sendingTimer; }; #endif \ No newline at end of file