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
program.cpp
- Committer:
- vwochnik
- Date:
- 2014-02-16
- Revision:
- 13:4045b6ac448d
- Parent:
- 12:beb64aa0da86
- Child:
- 15:0ccf0f530a05
File content as of revision 13:4045b6ac448d:
#include "common.h"
#include "io.h"
#include "StaticData.h"
#include "ComposedRecord.h"
#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);
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
// Usage: 100,<SERIAL/NR>
"10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n"
// get device id from identity
// Response: 200,<DEVICE/ID>
"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_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"
// Insert global ID
// Usage: 102,<DEVICE/ID>,<SERIAL/NR>
"10,102,POST,/identity/globalIds/%%/externalIds,application/vnd.com.nsn.cumulocity.externalId+json,application/vnd.com.nsn.cumulocity.externalId+json,%%,UNSIGNED STRING,\"{\"\"type\"\":\"\"c8y_Serial\"\",\"\"externalId\"\":\"\"%%\"\"}\"\r\n"
// Update IMEI, CellId and iccid
// Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID>
"10,103,PUT,/inventory/managedObjects/%%,application/vnd.com.nsn.cumulocity.managedObject+json,application/vnd.com.nsn.cumulocity.managedObject+json,%%,UNSIGNED STRING STRING STRING,\"{\"\"c8y_Mobile\"\":{\"\"imei\"\":\"\"%%\"\",\"\"cellId\"\":\"\"%%\"\",\"\"iccid\"\":\"\"%%\"\"}}\"\r\n"
// 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.8");
int program(void)
{
long deviceId; Timer timer;
puts("Hello!");
puts("Bootstrapping");
if (client.bootstrap(srtpl) != SMARTREST_SUCCESS) {
puts("Bootstrapping failed.");
return 2;
}
puts("Starting action...");
if ((deviceId = existing()) == 0) {
deviceId = create();
if (deviceId == 0)
return 1;
if (!identify(deviceId))
return 2;
}
if (!update(deviceId))
return 3;
printf("Device ID: %ld\r\n", deviceId);
timer.start();
while (true) {
timer.reset();
loop(deviceId);
// block remaining number of seconds
while (timer.read() < interval);
}
}
long existing()
{
ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
ParsedRecord received;
puts("Checking for device existance...");
newMoRec.add(IntegerValue(100)).add(CharValue(imei()));
if (client.send(newMoRec) != SMARTREST_SUCCESS) {
puts("Send failed.");
client.stop();
return 0;
}
if (client.receive(received) != SMARTREST_SUCCESS) {
puts("No device found.");
client.stop();
return 0;
}
if (received.values() == 0) {
puts("Received no values.");
client.stop();
return 0;
}
if (received.value(0).integerValue() == 50) {
client.stop();
return 0;
}
if (received.value(0).integerValue() != 200) {
puts("Bad response.");
client.stop();
return 0;
}
client.stop();
return received.value(2).integerValue();
}
long create()
{
ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
ParsedRecord received;
puts("Creating device...");
newMoRec.add(IntegerValue(101)).add(CharValue(imei()));
if (client.send(newMoRec) != SMARTREST_SUCCESS) {
puts("Send failed.");
client.stop();
return 0;
}
if (client.receive(received) != SMARTREST_SUCCESS) {
puts("No device found.");
client.stop();
return 0;
}
if (received.values() != 3) {
puts("Bad received data.");
client.stop();
return 0;
}
if (received.value(0).integerValue() != 201) {
puts("Bad received data.");
client.stop();
return 0;
}
client.stop();
return received.value(2).integerValue();
}
bool identify(long deviceId)
{
ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
ParsedRecord received;
puts("Adding global identifier...");
newMoRec.add(IntegerValue(102)).add(IntegerValue(deviceId)).add(CharValue(imei()));
if (client.send(newMoRec) != SMARTREST_SUCCESS) {
puts("Sending failed.");
client.stop();
return false;
}
if (client.receive(received) != SMARTREST_SUCCESS) {
puts("Failed.");
client.stop();
return false;
}
if (received.values() != 3) {
puts("Received bad data.");
client.stop();
return false;
}
if (received.value(0).integerValue() != 200) {
puts("Received bad data.");
client.stop();
return false;
}
client.stop();
return true;
}
bool update(long deviceId)
{
ComposedRecord newMoRec(true); // set copy=true b/c tmp objects
ParsedRecord received;
puts("Updating device data...");
newMoRec.add(IntegerValue(103)).add(IntegerValue(deviceId)).add(CharValue(imei())).add(CharValue(cellId())).add(CharValue(iccid()));
if (client.send(newMoRec) != SMARTREST_SUCCESS) {
puts("Send failed.");
client.stop();
return false;
}
if (client.receive(received) != SMARTREST_SUCCESS) {
puts("Update failed.");
client.stop();
return false;
}
if (received.values() != 3) {
puts("Bad received data.");
client.stop();
return false;
}
if (received.value(0).integerValue() != 201) {
puts("Bad received data.");
client.stop();
return false;
}
client.stop();
return true;
}
void loop(long deviceId)
{
Aggregator aggr(5, false, true);
signalMeasurement(deviceId, aggr);
temperatureMeasurement(deviceId, aggr);
countMeasurement(deviceId, aggr);
motionMeasurement(deviceId, aggr);
if (client.send(aggr) != SMARTREST_SUCCESS) {
puts("Send failed.");
}
client.stop();
}
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);
}
