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
measurement/AnalogMeasurement.cpp@76:b07effe83fb8, 2015-02-25 (annotated)
- Committer:
- xinlei
- Date:
- Wed Feb 25 10:06:11 2015 +0000
- Revision:
- 76:b07effe83fb8
- Parent:
- 74:ca3001991fdc
- Child:
- 77:f6717e4eccc4
watchdog: added watchdog;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 47:89ae46d5c466 | 1 | #include "AnalogMeasurement.h" |
Cumulocity | 47:89ae46d5c466 | 2 | #include "ComposedRecord.h" |
Cumulocity | 47:89ae46d5c466 | 3 | #include "CharValue.h" |
Cumulocity | 47:89ae46d5c466 | 4 | #include "IntegerValue.h" |
Cumulocity | 47:89ae46d5c466 | 5 | #include "FloatValue.h" |
xinlei | 72:c5709ae7b193 | 6 | #include "logging.h" |
Cumulocity | 47:89ae46d5c466 | 7 | |
xinlei | 76:b07effe83fb8 | 8 | #define THRESHOLD_PERCENT_ANA 0.03 // Percentage cut-off for avoiding sending similar analog sensor data. |
xinlei | 73:313975bfec96 | 9 | // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds). |
xinlei | 73:313975bfec96 | 10 | #define TIME_LIMIT_ANA 900 |
xinlei | 71:063c45e99578 | 11 | |
xinlei | 73:313975bfec96 | 12 | AnalogMeasurement::AnalogMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, AnalogIn& analog1, |
xinlei | 73:313975bfec96 | 13 | AnalogIn& analog2, DeviceIO& io, DeviceInfo& deviceInfo, DeviceBootstrap& bootstrap) : |
Cumulocity | 47:89ae46d5c466 | 14 | _client(client), |
Cumulocity | 47:89ae46d5c466 | 15 | _tpl(tpl), |
Cumulocity | 47:89ae46d5c466 | 16 | _deviceId(deviceId), |
Cumulocity | 47:89ae46d5c466 | 17 | _analog1(analog1), |
xinlei | 73:313975bfec96 | 18 | _analog2(analog2), |
xinlei | 73:313975bfec96 | 19 | _io(io), |
xinlei | 73:313975bfec96 | 20 | _deviceInfo(deviceInfo), |
xinlei | 73:313975bfec96 | 21 | _bootstrap(bootstrap) |
Cumulocity | 47:89ae46d5c466 | 22 | { |
Cumulocity | 47:89ae46d5c466 | 23 | _init = false; |
xinlei | 71:063c45e99578 | 24 | oldValues[0] = 0; |
xinlei | 71:063c45e99578 | 25 | oldValues[1] = 0; |
xinlei | 71:063c45e99578 | 26 | sendingTimer.start(); |
Cumulocity | 47:89ae46d5c466 | 27 | } |
Cumulocity | 47:89ae46d5c466 | 28 | |
Cumulocity | 47:89ae46d5c466 | 29 | bool AnalogMeasurement::init() |
Cumulocity | 47:89ae46d5c466 | 30 | { |
Cumulocity | 47:89ae46d5c466 | 31 | if (_init) |
Cumulocity | 47:89ae46d5c466 | 32 | return false; |
Cumulocity | 47:89ae46d5c466 | 33 | |
Cumulocity | 47:89ae46d5c466 | 34 | // Insert measurement |
Cumulocity | 47:89ae46d5c466 | 35 | // USAGE: 107,<DEVICE/ID>,<ANALOG1>,<ANALOG2> |
Cumulocity | 47:89ae46d5c466 | 36 | if (!_tpl.add("10,107,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_AnalogMeasurement\"\",\"\"c8y_AnalogMeasurement\"\":{\"\"A1\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"},\"\"A2\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"A\"\"}}}\"\r\n")) |
Cumulocity | 47:89ae46d5c466 | 37 | return false; |
Cumulocity | 47:89ae46d5c466 | 38 | |
Cumulocity | 47:89ae46d5c466 | 39 | _init = true; |
Cumulocity | 47:89ae46d5c466 | 40 | return true; |
Cumulocity | 47:89ae46d5c466 | 41 | } |
Cumulocity | 47:89ae46d5c466 | 42 | |
Cumulocity | 47:89ae46d5c466 | 43 | bool AnalogMeasurement::run() |
Cumulocity | 47:89ae46d5c466 | 44 | { |
xinlei | 71:063c45e99578 | 45 | float data[2] = {0, 0}; |
xinlei | 71:063c45e99578 | 46 | data[0] = _analog1.read()*100; |
xinlei | 71:063c45e99578 | 47 | data[1] = _analog2.read()*100; |
xinlei | 73:313975bfec96 | 48 | char tenant[25] = {0}; |
xinlei | 74:ca3001991fdc | 49 | const char* user= _bootstrap.username(); |
xinlei | 74:ca3001991fdc | 50 | int len = strchr(user, '/')-user+1+8; // 8: length of "Tenant: "; |
xinlei | 74:ca3001991fdc | 51 | len = len <= 25 ? len : 25; |
xinlei | 74:ca3001991fdc | 52 | snprintf(tenant, len, "Tenant: %s", user); |
xinlei | 73:313975bfec96 | 53 | char signal[25] = {0}; |
xinlei | 74:ca3001991fdc | 54 | if (_deviceInfo.signalQuality()) |
xinlei | 74:ca3001991fdc | 55 | snprintf(signal, 25, "Network: %d dBm", _deviceInfo.signalQuality()->rssi); |
xinlei | 74:ca3001991fdc | 56 | else |
xinlei | 74:ca3001991fdc | 57 | strncpy(signal, "Network: ? dBm", 25); |
xinlei | 71:063c45e99578 | 58 | if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA && |
xinlei | 71:063c45e99578 | 59 | abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) { |
xinlei | 71:063c45e99578 | 60 | if (sendingTimer.read() < TIME_LIMIT_ANA) { |
xinlei | 74:ca3001991fdc | 61 | _io.lcdPrint(tenant, signal); |
xinlei | 73:313975bfec96 | 62 | aDebug("Similar analog readings found, no sending!\r\n"); |
xinlei | 71:063c45e99578 | 63 | return true; |
xinlei | 71:063c45e99578 | 64 | } else { |
xinlei | 73:313975bfec96 | 65 | aDebug("Sending timer of analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 66 | } |
xinlei | 71:063c45e99578 | 67 | } |
xinlei | 73:313975bfec96 | 68 | |
xinlei | 73:313975bfec96 | 69 | char status[25] = {0}; |
xinlei | 74:ca3001991fdc | 70 | snprintf(status, 25, "Sending Poti %.1f,%.1f", data[0], data[1]); |
xinlei | 73:313975bfec96 | 71 | _io.lcdPrint(tenant, signal, status); |
Cumulocity | 47:89ae46d5c466 | 72 | ComposedRecord record; |
Cumulocity | 47:89ae46d5c466 | 73 | IntegerValue msgId(107); |
Cumulocity | 47:89ae46d5c466 | 74 | IntegerValue devId(_deviceId); |
xinlei | 71:063c45e99578 | 75 | FloatValue analog1(data[0], 1); |
xinlei | 71:063c45e99578 | 76 | FloatValue analog2(data[1], 1); |
Cumulocity | 47:89ae46d5c466 | 77 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2))) |
Cumulocity | 47:89ae46d5c466 | 78 | return false; |
Cumulocity | 47:89ae46d5c466 | 79 | |
xinlei | 73:313975bfec96 | 80 | float t_start = sendingTimer.read(); |
Cumulocity | 47:89ae46d5c466 | 81 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 76:b07effe83fb8 | 82 | aWarning("Sending analog readings failed."); |
Cumulocity | 47:89ae46d5c466 | 83 | _client.stop(); |
Cumulocity | 47:89ae46d5c466 | 84 | return false; |
Cumulocity | 47:89ae46d5c466 | 85 | } |
xinlei | 73:313975bfec96 | 86 | float t_end = sendingTimer.read(); |
Cumulocity | 47:89ae46d5c466 | 87 | _client.stop(); |
xinlei | 74:ca3001991fdc | 88 | aInfo("Analog readings sent in %.1f.\r\n", t_end-t_start); |
xinlei | 71:063c45e99578 | 89 | oldValues[0] = data[0]; |
xinlei | 71:063c45e99578 | 90 | oldValues[1] = data[1]; |
xinlei | 71:063c45e99578 | 91 | sendingTimer.reset(); |
Cumulocity | 47:89ae46d5c466 | 92 | return true; |
Cumulocity | 47:89ae46d5c466 | 93 | } |