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
Diff: measurement/AnalogMeasurement.cpp
- Revision:
- 71:063c45e99578
- Parent:
- 56:4d6e34f1589d
- Child:
- 72:c5709ae7b193
--- 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; }