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@100:47ea098f8a47, 2015-05-07 (annotated)
- Committer:
- xinlei
- Date:
- Thu May 07 13:56:19 2015 +0000
- Revision:
- 100:47ea098f8a47
- Parent:
- 96:5dfdc8568e9f
prepare for v2.1rc3.
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 | 96:5dfdc8568e9f | 6 | #include "LCDDisplay.h" |
xinlei | 72:c5709ae7b193 | 7 | #include "logging.h" |
Cumulocity | 41:804f6a0bda26 | 8 | |
xinlei | 76:b07effe83fb8 | 9 | #define THRESHOLD_PERCENT_TEMP 0.02 // Percentage cut-off for avoiding sending similar temperature sensor data. |
xinlei | 73:313975bfec96 | 10 | // Time interval for forcing a sending even if analog sensor readings are constantly similar (in seconds). |
xinlei | 73:313975bfec96 | 11 | #define TIME_LIMIT_TEMP 900 |
xinlei | 71:063c45e99578 | 12 | |
xinlei | 96:5dfdc8568e9f | 13 | TemperatureMeasurement::TemperatureMeasurement(AbstractSmartRest& client, |
xinlei | 100:47ea098f8a47 | 14 | SmartRestTemplate& tpl, DeviceInfo& deviceInfo) : |
xinlei | 91:423177e8a401 | 15 | _tpl(tpl), |
Cumulocity | 41:804f6a0bda26 | 16 | _client(client), |
xinlei | 91:423177e8a401 | 17 | _sensor(D14,D15), |
xinlei | 73:313975bfec96 | 18 | oldValue(0), |
xinlei | 96:5dfdc8568e9f | 19 | _deviceInfo(deviceInfo) |
Cumulocity | 41:804f6a0bda26 | 20 | { |
Cumulocity | 41:804f6a0bda26 | 21 | _init = false; |
xinlei | 94:61d44636f020 | 22 | t_start = time(NULL); |
Cumulocity | 41:804f6a0bda26 | 23 | } |
Cumulocity | 41:804f6a0bda26 | 24 | |
Cumulocity | 41:804f6a0bda26 | 25 | bool TemperatureMeasurement::init() |
Cumulocity | 41:804f6a0bda26 | 26 | { |
Cumulocity | 41:804f6a0bda26 | 27 | if (_init) |
Cumulocity | 41:804f6a0bda26 | 28 | return false; |
Cumulocity | 41:804f6a0bda26 | 29 | |
Cumulocity | 41:804f6a0bda26 | 30 | // Insert measurement |
Cumulocity | 41:804f6a0bda26 | 31 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
Cumulocity | 41:804f6a0bda26 | 32 | 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 | 33 | return false; |
Cumulocity | 41:804f6a0bda26 | 34 | |
Cumulocity | 41:804f6a0bda26 | 35 | _open = _sensor.open(); |
Cumulocity | 41:804f6a0bda26 | 36 | _init = true; |
Cumulocity | 41:804f6a0bda26 | 37 | return true; |
Cumulocity | 41:804f6a0bda26 | 38 | } |
Cumulocity | 41:804f6a0bda26 | 39 | |
Cumulocity | 41:804f6a0bda26 | 40 | bool TemperatureMeasurement::run() |
Cumulocity | 41:804f6a0bda26 | 41 | { |
Cumulocity | 41:804f6a0bda26 | 42 | if (!_open) |
Cumulocity | 41:804f6a0bda26 | 43 | return false; |
Cumulocity | 41:804f6a0bda26 | 44 | |
xinlei | 86:b49c4cfecc43 | 45 | float data = _sensor.temp(); |
xinlei | 77:f6717e4eccc4 | 46 | |
xinlei | 71:063c45e99578 | 47 | if (abs(oldValue-data) <= abs(oldValue)*THRESHOLD_PERCENT_TEMP) { |
xinlei | 94:61d44636f020 | 48 | time_t t_interval = time(NULL) - t_start; |
xinlei | 94:61d44636f020 | 49 | if (t_interval < TIME_LIMIT_TEMP) { |
xinlei | 96:5dfdc8568e9f | 50 | if (strcmp(LCDDisplay::inst().getThirdLine(), "")) { |
xinlei | 96:5dfdc8568e9f | 51 | LCDDisplay::inst().setThirdLine(""); |
xinlei | 85:5dc5a50e4b06 | 52 | } |
xinlei | 71:063c45e99578 | 53 | return true; |
xinlei | 71:063c45e99578 | 54 | } else { |
xinlei | 94:61d44636f020 | 55 | aDebug("Temp: Timeout at %d s.\n", t_interval); |
xinlei | 71:063c45e99578 | 56 | } |
xinlei | 71:063c45e99578 | 57 | } |
xinlei | 71:063c45e99578 | 58 | |
xinlei | 73:313975bfec96 | 59 | char status[25] = {0}; |
xinlei | 93:0acd11870c6a | 60 | snprintf(status, 25, "Send Temp %.2f", data); |
xinlei | 96:5dfdc8568e9f | 61 | LCDDisplay::inst().setThirdLine(status); |
Cumulocity | 41:804f6a0bda26 | 62 | ComposedRecord record; |
Cumulocity | 41:804f6a0bda26 | 63 | IntegerValue msgId(105); |
xinlei | 100:47ea098f8a47 | 64 | IntegerValue devId(deviceID); |
xinlei | 71:063c45e99578 | 65 | FloatValue temperature(data, 1); |
Cumulocity | 41:804f6a0bda26 | 66 | if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(temperature))) |
Cumulocity | 41:804f6a0bda26 | 67 | return false; |
Cumulocity | 41:804f6a0bda26 | 68 | |
xinlei | 94:61d44636f020 | 69 | time_t t0 = time(NULL); |
Cumulocity | 41:804f6a0bda26 | 70 | if (_client.send(record) != SMARTREST_SUCCESS) { |
xinlei | 93:0acd11870c6a | 71 | aWarning("Temp: Send failed.\n"); |
Cumulocity | 41:804f6a0bda26 | 72 | _client.stop(); |
Cumulocity | 41:804f6a0bda26 | 73 | return false; |
Cumulocity | 41:804f6a0bda26 | 74 | } |
xinlei | 94:61d44636f020 | 75 | time_t t1 = time(NULL); |
Cumulocity | 41:804f6a0bda26 | 76 | _client.stop(); |
xinlei | 94:61d44636f020 | 77 | aDebug("Temp: Sent in %d s.\n", t1-t0); |
xinlei | 71:063c45e99578 | 78 | oldValue = data; |
xinlei | 94:61d44636f020 | 79 | t_start = time(NULL); |
Cumulocity | 41:804f6a0bda26 | 80 | return true; |
Cumulocity | 41:804f6a0bda26 | 81 | } |