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 Vincent Wochnik

Committer:
xinlei
Date:
Tue Feb 17 16:31:30 2015 +0000
Revision:
73:313975bfec96
Parent:
72:c5709ae7b193
Child:
74:ca3001991fdc
measurements: displaying concrete information on LCD display, timing of sending operation.

Who changed what in which revision?

UserRevisionLine numberNew 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 71:063c45e99578 8 #define THRESHOLD_PERCENT_ANA 0.05 // 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 73:313975bfec96 49 snprintf(tenant, 25, "Tenant: %s", _bootstrap.username());
xinlei 73:313975bfec96 50 char signal[25] = {0};
xinlei 73:313975bfec96 51 snprintf(signal, 25, "Network: %d dBm", _deviceInfo.signalQuality()->rssi);
xinlei 71:063c45e99578 52 if (abs(oldValues[0]-data[0]) <= abs(oldValues[0])*THRESHOLD_PERCENT_ANA &&
xinlei 71:063c45e99578 53 abs(oldValues[1]-data[1]) <= abs(oldValues[1])*THRESHOLD_PERCENT_ANA) {
xinlei 71:063c45e99578 54 if (sendingTimer.read() < TIME_LIMIT_ANA) {
xinlei 73:313975bfec96 55 aDebug("Similar analog readings found, no sending!\r\n");
xinlei 73:313975bfec96 56 _io.lcdPrint(tenant, signal);
xinlei 71:063c45e99578 57 return true;
xinlei 71:063c45e99578 58 } else {
xinlei 73:313975bfec96 59 aDebug("Sending timer of analog sensor timed out at %f s, a sending is forced.\r\n", sendingTimer.read());
xinlei 71:063c45e99578 60 }
xinlei 71:063c45e99578 61 }
xinlei 73:313975bfec96 62
xinlei 73:313975bfec96 63 char status[25] = {0};
xinlei 73:313975bfec96 64 snprintf(status, 25, "Sending Ana %.1f,%.1f", data[0], data[1]);
xinlei 73:313975bfec96 65 _io.lcdPrint(tenant, signal, status);
Cumulocity 47:89ae46d5c466 66 ComposedRecord record;
Cumulocity 47:89ae46d5c466 67 IntegerValue msgId(107);
Cumulocity 47:89ae46d5c466 68 IntegerValue devId(_deviceId);
xinlei 71:063c45e99578 69 FloatValue analog1(data[0], 1);
xinlei 71:063c45e99578 70 FloatValue analog2(data[1], 1);
Cumulocity 47:89ae46d5c466 71 if ((!record.add(msgId)) || (!record.add(devId)) || (!record.add(analog1)) || (!record.add(analog2)))
Cumulocity 47:89ae46d5c466 72 return false;
Cumulocity 47:89ae46d5c466 73
xinlei 73:313975bfec96 74 float t_start = sendingTimer.read();
Cumulocity 47:89ae46d5c466 75 if (_client.send(record) != SMARTREST_SUCCESS) {
xinlei 72:c5709ae7b193 76 aError("Signal measurement failed.");
Cumulocity 47:89ae46d5c466 77 _client.stop();
Cumulocity 47:89ae46d5c466 78 return false;
Cumulocity 47:89ae46d5c466 79 }
xinlei 73:313975bfec96 80 float t_end = sendingTimer.read();
Cumulocity 47:89ae46d5c466 81 _client.stop();
xinlei 73:313975bfec96 82 aInfo("Acceleration readings sent in %.1f.\r\n", t_end-t_start);
xinlei 71:063c45e99578 83 oldValues[0] = data[0];
xinlei 71:063c45e99578 84 oldValues[1] = data[1];
xinlei 71:063c45e99578 85 sendingTimer.reset();
Cumulocity 47:89ae46d5c466 86 return true;
Cumulocity 47:89ae46d5c466 87 }