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@84:3c8ceb12b773, 2015-03-06 (annotated)
- Committer:
- xinlei
- Date:
- Fri Mar 06 11:10:48 2015 +0000
- Revision:
- 84:3c8ceb12b773
- Parent:
- 82:ca7430f50b2b
- Child:
- 85:5dc5a50e4b06
DisplayInfo: added second line for storing signal quality.
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" |
xinlei | 72:c5709ae7b193 | 6 | #include "logging.h" |
Cumulocity | 41:804f6a0bda26 | 7 | |
xinlei | 76:b07effe83fb8 | 8 | #define THRESHOLD_PERCENT_TEMP 0.02 // Percentage cut-off for avoiding sending similar temperature 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_TEMP 900 |
xinlei | 71:063c45e99578 | 11 | |
xinlei | 73:313975bfec96 | 12 | TemperatureMeasurement::TemperatureMeasurement(AbstractSmartRest& client, SmartRestTemplate& tpl, long& deviceId, LM75B& sensor, |
xinlei | 82:ca7430f50b2b | 13 | DeviceIO& io, DeviceInfo& deviceInfo, DisplayInfo& displayInfo) : |
Cumulocity | 41:804f6a0bda26 | 14 | _client(client), |
Cumulocity | 41:804f6a0bda26 | 15 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 16 | _deviceId(deviceId), |
xinlei | 71:063c45e99578 | 17 | _sensor(sensor), |
xinlei | 73:313975bfec96 | 18 | oldValue(0), |
xinlei | 73:313975bfec96 | 19 | _io(io), |
xinlei | 73:313975bfec96 | 20 | _deviceInfo(deviceInfo), |
xinlei | 82:ca7430f50b2b | 21 | _displayInfo(displayInfo) |
Cumulocity | 41:804f6a0bda26 | 22 | { |
Cumulocity | 41:804f6a0bda26 | 23 | _init = false; |
xinlei | 71:063c45e99578 | 24 | sendingTimer.start(); |
Cumulocity | 41:804f6a0bda26 | 25 | } |
Cumulocity | 41:804f6a0bda26 | 26 | |
Cumulocity | 41:804f6a0bda26 | 27 | bool TemperatureMeasurement::init() |
Cumulocity | 41:804f6a0bda26 | 28 | { |
Cumulocity | 41:804f6a0bda26 | 29 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 30 | return false; |
Cumulocity | 41:804f6a0bda26 | 31 | |
Cumulocity | 41:804f6a0bda26 | 32 | // Insert measurement |
Cumulocity | 41:804f6a0bda26 | 33 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
Cumulocity | 41:804f6a0bda26 | 34 | 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 | 35 | return false; |
Cumulocity | 41:804f6a0bda26 | 36 | |
Cumulocity | 41:804f6a0bda26 | 37 | _open = _sensor.open(); |
Cumulocity | 41:804f6a0bda26 | 38 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 39 | return true; |
Cumulocity | 41:804f6a0bda26 | 40 | } |
Cumulocity | 41:804f6a0bda26 | 41 | |
Cumulocity | 41:804f6a0bda26 | 42 | bool TemperatureMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 43 | { |
Cumulocity | 41:804f6a0bda26 | 44 | if (!_open) |
Cumulocity | 41:804f6a0bda26 | 45 | return false; |
Cumulocity | 41:804f6a0bda26 | 46 | |
xinlei | 71:063c45e99578 | 47 | float data = 0; |
xinlei | 71:063c45e99578 | 48 | data = _sensor.temp(); |
xinlei | 77:f6717e4eccc4 | 49 | |
xinlei | 71:063c45e99578 | 50 | if (abs(oldValue-data) <= abs(oldValue)*THRESHOLD_PERCENT_TEMP) { |
xinlei | 71:063c45e99578 | 51 | if (sendingTimer.read() < TIME_LIMIT_TEMP) { |
xinlei | 84:3c8ceb12b773 | 52 | _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine()); |
xinlei | 73:313975bfec96 | 53 | aDebug("Similar temperature readings found, no sending!\r\n"); |
xinlei | 71:063c45e99578 | 54 | return true; |
xinlei | 71:063c45e99578 | 55 | } else { |
xinlei | 77:f6717e4eccc4 | 56 | aDebug("Temperature sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read()); |
xinlei | 71:063c45e99578 | 57 | } |
xinlei | 71:063c45e99578 | 58 | } |
xinlei | 71:063c45e99578 | 59 | |
xinlei | 73:313975bfec96 | 60 | char status[25] = {0}; |
xinlei | 82:ca7430f50b2b | 61 | snprintf(status, 25, "Sending Temp %.2f", data); |
xinlei | 84:3c8ceb12b773 | 62 | _io.lcdPrint(_displayInfo.getFirstLine(), _displayInfo.getSecondLine(), status); |
Cumulocity | 41:804f6a0bda26 | 63 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 64 | IntegerValue msgId(105); |
Cumulocity | 41:804f6a0bda26 | 65 | IntegerValue devId(_deviceId); |
xinlei | 71:063c45e99578 | 66 | FloatValue temperature(data, 1); |
Cumulocity | 41:804f6a0bda26 | 67 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(temperature))) |
Cumulocity | 41:804f6a0bda26 | 68 | return false; |
Cumulocity | 41:804f6a0bda26 | 69 | |
xinlei | 73:313975bfec96 | 70 | float t_start = sendingTimer.read(); |
Cumulocity | 41:804f6a0bda26 | 71 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 76:b07effe83fb8 | 72 | aWarning("Sending temperature readings failed.\r\n"); |
Cumulocity | 41:804f6a0bda26 | 73 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 74 | return false; |
Cumulocity | 41:804f6a0bda26 | 75 | } |
xinlei | 73:313975bfec96 | 76 | float t_end = sendingTimer.read(); |
Cumulocity | 41:804f6a0bda26 | 77 | _client.stop(); |
xinlei | 74:ca3001991fdc | 78 | aInfo("Temperature readings sent in %.1f.\r\n", t_end-t_start); |
xinlei | 71:063c45e99578 | 79 | oldValue = data; |
xinlei | 71:063c45e99578 | 80 | sendingTimer.reset(); |
Cumulocity | 41:804f6a0bda26 | 81 | return true; |
Cumulocity | 41:804f6a0bda26 | 82 | } |