Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: C027_Support C12832 LM75B MMA7660 MbedSmartRest mbed-rtos mbed
Fork of MbedSmartRestMain by
Diff: program.cpp
- Revision:
- 13:4045b6ac448d
- Parent:
- 12:beb64aa0da86
- Child:
- 15:0ccf0f530a05
diff -r beb64aa0da86 -r 4045b6ac448d program.cpp
--- a/program.cpp Fri Feb 14 12:28:41 2014 +0000
+++ b/program.cpp Sun Feb 16 17:44:15 2014 +0000
@@ -5,13 +5,17 @@
#include "CharValue.h"
#include "IntegerValue.h"
#include "FloatValue.h"
+#include "Aggregator.h"
long existing();
long create();
bool identify(long deviceId);
bool update(long deviceId);
void loop(long deviceId);
-bool measurement(long deviceId, double rssi, int ber);
+void signalMeasurement(long deviceId, Aggregator& aggr);
+void temperatureMeasurement(long deviceId, Aggregator& aggr);
+void countMeasurement(long deviceId, Aggregator& aggr);
+void motionMeasurement(long deviceId, Aggregator& aggr);
StaticData srtpl(
// get device by identity
@@ -22,7 +26,7 @@
"11,200,\"$.managedObject\",,\"$.id\"\r\n"
// Create device
// Usage: 101,<SERIAL/NR>
-"10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{}}\"\r\n"
+"10,101,POST,/inventory/managedObjects,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,STRING,\"{\"\"name\"\":\"\"Mbed Test Device\"\",\"\"type\"\":\"\"com_ublox_C027_REV-A\"\",\"\"c8y_Hardware\"\":{\"\"revision\"\":\"\"1\"\",\"\"model\"\":\"\"Ublox C027\"\",\"\"serialNumber\"\":\"\"%%\"\"},\"\"c8y_SupportedMeasurements\"\":[\"\"c8y_SignalStrength\"\",\"\"c8y_TemperatureMeasurement\"\",\"\"c8y_CountMeasurement\"\",\"\"c8y_MotionMeasurement\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{}}\"\r\n"
// Get device id
// Response: 201,<DEVICE/ID>
"11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n"
@@ -35,11 +39,19 @@
// Insert measurement
// USAGE: 104,<DEVICE/ID>,<RSSI>,<BER>
"10,104,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_SignalStrength\"\",\"\"c8y_SignalStrength\"\":{\"\"rssi\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"dBm\"\"},\"\"ber\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"}}}\"\r\n"
+// Insert measurement
+// USAGE: 105,<DEVICE/ID>,<TEMPERATURE>
+"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"
+// Insert measurement
+// USAGE: 105,<DEVICE/ID>,<COUNTER>
+"10,106,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_CountMeasurement\"\",\"c8y_CountMeasurement\":{\"n\":{\"value\":%%,\"unit\":\"\"}}}\"\r\n"
+// Insert measurement
+// USAGE: 105,<DEVICE/ID>,<X>,<Y>,<Z>
+"10,107,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED NUMBER NUMBER NUMBER,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_MotionMeasurement\"\",\"\"c8y_MotionMeasurement\"\":{\"\"x\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"},\"\"y\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"},\"\"z\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"m^2/s\"\"}}}\"\r\n"
);
float interval = 120.0; // send measurements every two minutes
-MbedSmartRest client("http://developer.cumulocity.com/s", "vaillant/admin", "klanpi", "com_u-blox_C027_REV-A_0.7");
-//MbedSmartRest client("http://nocore.info:8888/", "vaillant/admin", "klanpi", "com_cumulocity_MbedTestDevice_2.0");
+MbedSmartRest client("http://developer.cumulocity.com/s", "vaillant/admin", "klanpi", "com_u-blox_C027_REV-A_0.8");
int program(void)
{
@@ -233,25 +245,66 @@
void loop(long deviceId)
{
- sigq_t *signal = signalQuality();
- if ((signal->rssi != 0) && (signal->ber != 0))
- measurement(deviceId, signal->rssi, signal->ber);
-}
+ Aggregator aggr(5, false, true);
-bool measurement(long deviceId, double rssi, int ber)
-{
- ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
-
- puts("Creating measurement...");
+ signalMeasurement(deviceId, aggr);
+ temperatureMeasurement(deviceId, aggr);
+ countMeasurement(deviceId, aggr);
+ motionMeasurement(deviceId, aggr);
- newMoRec.add(IntegerValue(104)).add(IntegerValue(deviceId)).add(FloatValue(rssi, 0)).add(IntegerValue(ber));
-
- if (client.send(newMoRec) != SMARTREST_SUCCESS) {
+ if (client.send(aggr) != SMARTREST_SUCCESS) {
puts("Send failed.");
- client.stop();
- return false;
}
client.stop();
- return true;
+}
+
+void signalMeasurement(long deviceId, Aggregator& aggr)
+{
+ sigq_t *sq = signalQuality();
+
+ if ((sq->rssi == 0) || (sq->ber == 0))
+ return;
+
+ ComposedRecord measurement;
+ IntegerValue msgId(104);
+ IntegerValue devId(deviceId);
+ FloatValue rssi(sq->rssi, 0);
+ IntegerValue ber(sq->ber);
+ measurement.add(msgId).add(devId).add(rssi).add(ber);
+ aggr.add(measurement);
}
+
+void temperatureMeasurement(long deviceId, Aggregator& aggr)
+{
+ ComposedRecord measurement;
+ IntegerValue msgId(105);
+ IntegerValue devId(deviceId);
+ FloatValue temp(temperature(), 1);
+ measurement.add(msgId).add(devId).add(temp);
+ aggr.add(measurement);
+}
+
+void countMeasurement(long deviceId, Aggregator& aggr)
+{
+ ComposedRecord measurement;
+ IntegerValue msgId(106);
+ IntegerValue devId(deviceId);
+ IntegerValue n(counter());
+ measurement.add(msgId).add(devId).add(n);
+ aggr.add(measurement);
+}
+
+void motionMeasurement(long deviceId, Aggregator& aggr)
+{
+ acceleration_t acc = acceleration();
+ ComposedRecord measurement;
+ IntegerValue msgId(107);
+ IntegerValue devId(deviceId);
+ FloatValue x(acc.x, 2);
+ FloatValue y(acc.y, 2);
+ FloatValue z(acc.z, 2);
+ measurement.add(msgId).add(devId).add(x).add(y).add(z);
+ aggr.add(measurement);
+}
+
