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/TemperatureMeasurement.cpp@71:063c45e99578, 2015-02-16 (annotated)
- Committer:
- xinlei
- Date:
- Mon Feb 16 09:17:30 2015 +0000
- Revision:
- 71:063c45e99578
- Parent:
- 56:4d6e34f1589d
- Child:
- 72:c5709ae7b193
Various minor bug fixes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cumulocity | 41:804f6a0bda26 | 1 | #include "TemperatureMeasurement.h" |
Cumulocity | 41:804f6a0bda26 | 2 | #include "ComposedRecord.h" |
Cumulocity | 41:804f6a0bda26 | 3 | #include "CharValue.h" |
Cumulocity | 41:804f6a0bda26 | 4 | #include "IntegerValue.h" |
Cumulocity | 41:804f6a0bda26 | 5 | #include "FloatValue.h" |
Cumulocity | 41:804f6a0bda26 | 6 | |
xinlei | 71:063c45e99578 | 7 | #define THRESHOLD_PERCENT_TEMP 0.05 // Percentage cut-off for avoiding sending similar temperature sensor data. |
xinlei | 71:063c45e99578 | 8 | #define TIME_LIMIT_TEMP 900 // Time interval for forcing a sending even if analog sensor readings are constantly similar. |
xinlei | 71:063c45e99578 | 9 | |
vwochnik | 55:a0f7295ed6b6 | 10 | TemperatureMeasurement::TemperatureMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, LM75B& sensor) : |
Cumulocity | 41:804f6a0bda26 | 11 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 12 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 13 | _deviceId(deviceId), |
xinlei | 71:063c45e99578 | 14 | _sensor(sensor), |
xinlei | 71:063c45e99578 | 15 | oldValue(0) |
Cumulocity | 41:804f6a0bda26 | 16 | { |
Cumulocity | 41:804f6a0bda26 | 17 | _init = false; |
xinlei | 71:063c45e99578 | 18 | sendingTimer.start(); |
Cumulocity | 41:804f6a0bda26 | 19 | } |
Cumulocity | 41:804f6a0bda26 | 20 | |
Cumulocity | 41:804f6a0bda26 | 21 | bool TemperatureMeasurement::init() |
Cumulocity | 41:804f6a0bda26 | 22 | { |
Cumulocity | 41:804f6a0bda26 | 23 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 24 | return false; |
Cumulocity | 41:804f6a0bda26 | 25 | |
Cumulocity | 41:804f6a0bda26 | 26 | // Insert measurement |
Cumulocity | 41:804f6a0bda26 | 27 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
Cumulocity | 41:804f6a0bda26 | 28 | if (!_tpl.add("10,105,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_TemperatureMeasurement\"\":{\"\"T\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"C\"\"}}}\"\r\n")) |
Cumulocity | 41:804f6a0bda26 | 29 | return false; |
Cumulocity | 41:804f6a0bda26 | 30 | |
Cumulocity | 41:804f6a0bda26 | 31 | _open = _sensor.open(); |
Cumulocity | 41:804f6a0bda26 | 32 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 33 | return true; |
Cumulocity | 41:804f6a0bda26 | 34 | } |
Cumulocity | 41:804f6a0bda26 | 35 | |
Cumulocity | 41:804f6a0bda26 | 36 | bool TemperatureMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 37 | { |
Cumulocity | 41:804f6a0bda26 | 38 | if (!_open) |
Cumulocity | 41:804f6a0bda26 | 39 | return false; |
Cumulocity | 41:804f6a0bda26 | 40 | |
xinlei | 71:063c45e99578 | 41 | float data = 0; |
xinlei | 71:063c45e99578 | 42 | data = _sensor.temp(); |
xinlei | 71:063c45e99578 | 43 | if (abs(oldValue-data) <= abs(oldValue)*THRESHOLD_PERCENT_TEMP) { |
xinlei | 71:063c45e99578 | 44 | if (sendingTimer.read() < TIME_LIMIT_TEMP) { |
xinlei | 71:063c45e99578 | 45 | printf("Similar temperature readings found, no sending!\r\n"); |
xinlei | 71:063c45e99578 | 46 | return true; |
xinlei | 71:063c45e99578 | 47 | } else { |
xinlei | 71:063c45e99578 | 48 | printf("Sending timer of temperature sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 49 | } |
xinlei | 71:063c45e99578 | 50 | } |
xinlei | 71:063c45e99578 | 51 | |
Cumulocity | 41:804f6a0bda26 | 52 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 53 | IntegerValue msgId(105); |
Cumulocity | 41:804f6a0bda26 | 54 | IntegerValue devId(_deviceId); |
xinlei | 71:063c45e99578 | 55 | FloatValue temperature(data, 1); |
Cumulocity | 41:804f6a0bda26 | 56 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(temperature))) |
Cumulocity | 41:804f6a0bda26 | 57 | return false; |
Cumulocity | 41:804f6a0bda26 | 58 | |
Cumulocity | 41:804f6a0bda26 | 59 | if (_client.send(record) != SMARTREST_SUCCESS) { |
Cumulocity | 41:804f6a0bda26 | 60 | puts("Signal measurement failed."); |
Cumulocity | 41:804f6a0bda26 | 61 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 62 | return false; |
Cumulocity | 41:804f6a0bda26 | 63 | } |
Cumulocity | 41:804f6a0bda26 | 64 | _client.stop(); |
xinlei | 71:063c45e99578 | 65 | oldValue = data; |
xinlei | 71:063c45e99578 | 66 | sendingTimer.reset(); |
xinlei | 71:063c45e99578 | 67 | printf("Temperature readings sent.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 68 | return true; |
Cumulocity | 41:804f6a0bda26 | 69 | } |