![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SmartREST client reference implementation for the u-blox C027 mbed compatible device.
Dependencies: C027 C027_Support mbed mbed-rtos MbedSmartRest LM75B MMA7660 C12832
Fork of MbedSmartRestTest by
program.cpp@19:7bee744fe527, 2014-02-18 (annotated)
- Committer:
- vwochnik
- Date:
- Tue Feb 18 17:17:48 2014 +0000
- Revision:
- 19:7bee744fe527
- Parent:
- 16:94d6eae3195a
- Child:
- 20:ef9cc1b42e9d
fix
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vwochnik | 6:642e7c233e83 | 1 | #include "common.h" |
vwochnik | 12:beb64aa0da86 | 2 | #include "io.h" |
vwochnik | 6:642e7c233e83 | 3 | #include "StaticData.h" |
vwochnik | 6:642e7c233e83 | 4 | #include "ComposedRecord.h" |
vwochnik | 6:642e7c233e83 | 5 | #include "CharValue.h" |
vwochnik | 6:642e7c233e83 | 6 | #include "IntegerValue.h" |
vwochnik | 6:642e7c233e83 | 7 | #include "FloatValue.h" |
vwochnik | 13:4045b6ac448d | 8 | #include "Aggregator.h" |
vwochnik | 6:642e7c233e83 | 9 | |
vwochnik | 8:940c782eec5e | 10 | long existing(); |
vwochnik | 8:940c782eec5e | 11 | long create(); |
vwochnik | 8:940c782eec5e | 12 | bool identify(long deviceId); |
vwochnik | 8:940c782eec5e | 13 | bool update(long deviceId); |
vwochnik | 8:940c782eec5e | 14 | void loop(long deviceId); |
vwochnik | 13:4045b6ac448d | 15 | void signalMeasurement(long deviceId, Aggregator& aggr); |
vwochnik | 13:4045b6ac448d | 16 | void temperatureMeasurement(long deviceId, Aggregator& aggr); |
vwochnik | 19:7bee744fe527 | 17 | void analogMeasurement(long deviceId, Aggregator& aggr); |
vwochnik | 13:4045b6ac448d | 18 | void motionMeasurement(long deviceId, Aggregator& aggr); |
vwochnik | 8:940c782eec5e | 19 | |
vwochnik | 6:642e7c233e83 | 20 | StaticData srtpl( |
vwochnik | 6:642e7c233e83 | 21 | // get device by identity |
vwochnik | 6:642e7c233e83 | 22 | // Usage: 100,<SERIAL/NR> |
vwochnik | 6:642e7c233e83 | 23 | "10,100,GET,/identity/externalIds/c8y_Serial/%%,,application/vnd.com.nsn.cumulocity.externalId+json,%%,STRING,\r\n" |
vwochnik | 6:642e7c233e83 | 24 | // get device id from identity |
vwochnik | 6:642e7c233e83 | 25 | // Response: 200,<DEVICE/ID> |
vwochnik | 6:642e7c233e83 | 26 | "11,200,\"$.managedObject\",,\"$.id\"\r\n" |
vwochnik | 6:642e7c233e83 | 27 | // Create device |
vwochnik | 6:642e7c233e83 | 28 | // Usage: 101,<SERIAL/NR> |
vwochnik | 19:7bee744fe527 | 29 | "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_AnalogMeasurement\"\",\"\"c8y_MotionMeasurement\"\"],\"\"c8y_RequiredAvailability\"\":{ \"\"responseInterval\"\":15},\"\"c8y_IsDevice\"\":{}}\"\r\n" |
vwochnik | 6:642e7c233e83 | 30 | // Get device id |
vwochnik | 6:642e7c233e83 | 31 | // Response: 201,<DEVICE/ID> |
vwochnik | 6:642e7c233e83 | 32 | "11,201,,\"$.c8y_IsDevice\",\"$.id\"\r\n" |
vwochnik | 6:642e7c233e83 | 33 | // Insert global ID |
vwochnik | 6:642e7c233e83 | 34 | // Usage: 102,<DEVICE/ID>,<SERIAL/NR> |
vwochnik | 6:642e7c233e83 | 35 | "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" |
vwochnik | 7:f77afd49c35d | 36 | // Update IMEI, CellId and iccid |
vwochnik | 7:f77afd49c35d | 37 | // Usage: 103,<DEVICE/ID>,<IMEI>,<CELL/ID>,<ICCID> |
vwochnik | 7:f77afd49c35d | 38 | "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" |
vwochnik | 6:642e7c233e83 | 39 | // Insert measurement |
vwochnik | 7:f77afd49c35d | 40 | // USAGE: 104,<DEVICE/ID>,<RSSI>,<BER> |
vwochnik | 7:f77afd49c35d | 41 | "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" |
vwochnik | 13:4045b6ac448d | 42 | // Insert measurement |
vwochnik | 13:4045b6ac448d | 43 | // USAGE: 105,<DEVICE/ID>,<TEMPERATURE> |
vwochnik | 13:4045b6ac448d | 44 | "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" |
vwochnik | 13:4045b6ac448d | 45 | // Insert measurement |
vwochnik | 19:7bee744fe527 | 46 | // USAGE: 105,<DEVICE/ID>,<ANALOG0>,<ANALOG1> |
vwochnik | 19:7bee744fe527 | 47 | "10,106,POST,/measurement/measurements,application/vnd.com.nsn.cumulocity.measurement+json,application/vnd.com.nsn.cumulocity.measurement+json,%%,NOW UNSIGNED UNSIGNED UNSIGNED,\"{\"\"time\"\":\"\"%%\"\",\"\"source\"\":{\"\"id\"\":\"\"%%\"\"},\"\"type\"\":\"\"c8y_AnalogMeasurement\"\",\"\"c8y_AnalogMeasurement\"\":{\"\"analog0\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"},\"\"analog1\"\":{\"\"value\"\":%%,\"\"unit\"\":\"\"%\"\"}}}\"\r\n" |
vwochnik | 13:4045b6ac448d | 48 | // Insert measurement |
vwochnik | 13:4045b6ac448d | 49 | // USAGE: 105,<DEVICE/ID>,<X>,<Y>,<Z> |
vwochnik | 13:4045b6ac448d | 50 | "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" |
vwochnik | 6:642e7c233e83 | 51 | ); |
vwochnik | 6:642e7c233e83 | 52 | |
vwochnik | 8:940c782eec5e | 53 | float interval = 120.0; // send measurements every two minutes |
vwochnik | 19:7bee744fe527 | 54 | MbedSmartRest client("http://developer.cumulocity.com/s", "vaillant/admin", "klanpi", "com_u-blox_C027_REV-A_0.10"); |
vwochnik | 6:642e7c233e83 | 55 | |
vwochnik | 6:642e7c233e83 | 56 | int program(void) |
vwochnik | 6:642e7c233e83 | 57 | { |
vwochnik | 15:0ccf0f530a05 | 58 | long deviceId = 0; Timer timer; |
vwochnik | 8:940c782eec5e | 59 | |
vwochnik | 6:642e7c233e83 | 60 | puts("Hello!"); |
vwochnik | 6:642e7c233e83 | 61 | |
vwochnik | 6:642e7c233e83 | 62 | puts("Bootstrapping"); |
vwochnik | 6:642e7c233e83 | 63 | if (client.bootstrap(srtpl) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 64 | puts("Bootstrapping failed."); |
vwochnik | 6:642e7c233e83 | 65 | return 2; |
vwochnik | 6:642e7c233e83 | 66 | } |
vwochnik | 6:642e7c233e83 | 67 | |
vwochnik | 6:642e7c233e83 | 68 | puts("Starting action..."); |
vwochnik | 6:642e7c233e83 | 69 | |
vwochnik | 6:642e7c233e83 | 70 | if ((deviceId = existing()) == 0) { |
vwochnik | 6:642e7c233e83 | 71 | deviceId = create(); |
vwochnik | 7:f77afd49c35d | 72 | if (deviceId == 0) |
vwochnik | 7:f77afd49c35d | 73 | return 1; |
vwochnik | 7:f77afd49c35d | 74 | if (!identify(deviceId)) |
vwochnik | 7:f77afd49c35d | 75 | return 2; |
vwochnik | 6:642e7c233e83 | 76 | } |
vwochnik | 19:7bee744fe527 | 77 | //if (!update(deviceId)) |
vwochnik | 19:7bee744fe527 | 78 | // return 3; |
vwochnik | 19:7bee744fe527 | 79 | update(deviceId); |
vwochnik | 6:642e7c233e83 | 80 | |
vwochnik | 6:642e7c233e83 | 81 | printf("Device ID: %ld\r\n", deviceId); |
vwochnik | 8:940c782eec5e | 82 | |
vwochnik | 8:940c782eec5e | 83 | timer.start(); |
vwochnik | 7:f77afd49c35d | 84 | while (true) { |
vwochnik | 8:940c782eec5e | 85 | timer.reset(); |
vwochnik | 8:940c782eec5e | 86 | loop(deviceId); |
vwochnik | 8:940c782eec5e | 87 | |
vwochnik | 8:940c782eec5e | 88 | // block remaining number of seconds |
vwochnik | 16:94d6eae3195a | 89 | while (timer.read() < interval) { |
vwochnik | 16:94d6eae3195a | 90 | Thread::yield(); |
vwochnik | 16:94d6eae3195a | 91 | } |
vwochnik | 6:642e7c233e83 | 92 | } |
vwochnik | 6:642e7c233e83 | 93 | } |
vwochnik | 6:642e7c233e83 | 94 | |
vwochnik | 6:642e7c233e83 | 95 | long existing() |
vwochnik | 6:642e7c233e83 | 96 | { |
vwochnik | 6:642e7c233e83 | 97 | ComposedRecord newMoRec(true); // set copy=true b/c tmp objects |
vwochnik | 6:642e7c233e83 | 98 | ParsedRecord received; |
vwochnik | 6:642e7c233e83 | 99 | |
vwochnik | 6:642e7c233e83 | 100 | puts("Checking for device existance..."); |
vwochnik | 6:642e7c233e83 | 101 | |
vwochnik | 12:beb64aa0da86 | 102 | newMoRec.add(IntegerValue(100)).add(CharValue(imei())); |
vwochnik | 6:642e7c233e83 | 103 | |
vwochnik | 6:642e7c233e83 | 104 | if (client.send(newMoRec) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 105 | puts("Send failed."); |
vwochnik | 10:d5cd4a7ada7b | 106 | client.stop(); |
vwochnik | 6:642e7c233e83 | 107 | return 0; |
vwochnik | 6:642e7c233e83 | 108 | } |
vwochnik | 6:642e7c233e83 | 109 | |
vwochnik | 6:642e7c233e83 | 110 | if (client.receive(received) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 111 | puts("No device found."); |
vwochnik | 10:d5cd4a7ada7b | 112 | client.stop(); |
vwochnik | 6:642e7c233e83 | 113 | return 0; |
vwochnik | 6:642e7c233e83 | 114 | } |
vwochnik | 6:642e7c233e83 | 115 | |
vwochnik | 6:642e7c233e83 | 116 | if (received.values() == 0) { |
vwochnik | 6:642e7c233e83 | 117 | puts("Received no values."); |
vwochnik | 10:d5cd4a7ada7b | 118 | client.stop(); |
vwochnik | 6:642e7c233e83 | 119 | return 0; |
vwochnik | 6:642e7c233e83 | 120 | } |
vwochnik | 10:d5cd4a7ada7b | 121 | |
vwochnik | 6:642e7c233e83 | 122 | if (received.value(0).integerValue() == 50) { |
vwochnik | 6:642e7c233e83 | 123 | client.stop(); |
vwochnik | 6:642e7c233e83 | 124 | return 0; |
vwochnik | 6:642e7c233e83 | 125 | } |
vwochnik | 6:642e7c233e83 | 126 | |
vwochnik | 6:642e7c233e83 | 127 | if (received.value(0).integerValue() != 200) { |
vwochnik | 6:642e7c233e83 | 128 | puts("Bad response."); |
vwochnik | 10:d5cd4a7ada7b | 129 | client.stop(); |
vwochnik | 6:642e7c233e83 | 130 | return 0; |
vwochnik | 6:642e7c233e83 | 131 | } |
vwochnik | 6:642e7c233e83 | 132 | |
vwochnik | 6:642e7c233e83 | 133 | client.stop(); |
vwochnik | 6:642e7c233e83 | 134 | return received.value(2).integerValue(); |
vwochnik | 6:642e7c233e83 | 135 | } |
vwochnik | 6:642e7c233e83 | 136 | |
vwochnik | 6:642e7c233e83 | 137 | long create() |
vwochnik | 6:642e7c233e83 | 138 | { |
vwochnik | 6:642e7c233e83 | 139 | ComposedRecord newMoRec(true); // set copy=true b/c tmp objects |
vwochnik | 6:642e7c233e83 | 140 | ParsedRecord received; |
vwochnik | 6:642e7c233e83 | 141 | |
vwochnik | 6:642e7c233e83 | 142 | puts("Creating device..."); |
vwochnik | 6:642e7c233e83 | 143 | |
vwochnik | 12:beb64aa0da86 | 144 | newMoRec.add(IntegerValue(101)).add(CharValue(imei())); |
vwochnik | 6:642e7c233e83 | 145 | |
vwochnik | 6:642e7c233e83 | 146 | if (client.send(newMoRec) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 147 | puts("Send failed."); |
vwochnik | 10:d5cd4a7ada7b | 148 | client.stop(); |
vwochnik | 6:642e7c233e83 | 149 | return 0; |
vwochnik | 6:642e7c233e83 | 150 | } |
vwochnik | 6:642e7c233e83 | 151 | |
vwochnik | 6:642e7c233e83 | 152 | if (client.receive(received) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 153 | puts("No device found."); |
vwochnik | 10:d5cd4a7ada7b | 154 | client.stop(); |
vwochnik | 6:642e7c233e83 | 155 | return 0; |
vwochnik | 6:642e7c233e83 | 156 | } |
vwochnik | 6:642e7c233e83 | 157 | |
vwochnik | 6:642e7c233e83 | 158 | if (received.values() != 3) { |
vwochnik | 6:642e7c233e83 | 159 | puts("Bad received data."); |
vwochnik | 10:d5cd4a7ada7b | 160 | client.stop(); |
vwochnik | 6:642e7c233e83 | 161 | return 0; |
vwochnik | 6:642e7c233e83 | 162 | } |
vwochnik | 6:642e7c233e83 | 163 | |
vwochnik | 6:642e7c233e83 | 164 | if (received.value(0).integerValue() != 201) { |
vwochnik | 6:642e7c233e83 | 165 | puts("Bad received data."); |
vwochnik | 10:d5cd4a7ada7b | 166 | client.stop(); |
vwochnik | 6:642e7c233e83 | 167 | return 0; |
vwochnik | 6:642e7c233e83 | 168 | } |
vwochnik | 6:642e7c233e83 | 169 | |
vwochnik | 6:642e7c233e83 | 170 | client.stop(); |
vwochnik | 6:642e7c233e83 | 171 | return received.value(2).integerValue(); |
vwochnik | 6:642e7c233e83 | 172 | } |
vwochnik | 6:642e7c233e83 | 173 | |
vwochnik | 6:642e7c233e83 | 174 | bool identify(long deviceId) |
vwochnik | 6:642e7c233e83 | 175 | { |
vwochnik | 6:642e7c233e83 | 176 | ComposedRecord newMoRec(true); // set copy=true b/c tmp objects |
vwochnik | 6:642e7c233e83 | 177 | ParsedRecord received; |
vwochnik | 6:642e7c233e83 | 178 | |
vwochnik | 6:642e7c233e83 | 179 | puts("Adding global identifier..."); |
vwochnik | 6:642e7c233e83 | 180 | |
vwochnik | 12:beb64aa0da86 | 181 | newMoRec.add(IntegerValue(102)).add(IntegerValue(deviceId)).add(CharValue(imei())); |
vwochnik | 6:642e7c233e83 | 182 | |
vwochnik | 6:642e7c233e83 | 183 | if (client.send(newMoRec) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 184 | puts("Sending failed."); |
vwochnik | 10:d5cd4a7ada7b | 185 | client.stop(); |
vwochnik | 6:642e7c233e83 | 186 | return false; |
vwochnik | 6:642e7c233e83 | 187 | } |
vwochnik | 6:642e7c233e83 | 188 | |
vwochnik | 6:642e7c233e83 | 189 | if (client.receive(received) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 190 | puts("Failed."); |
vwochnik | 10:d5cd4a7ada7b | 191 | client.stop(); |
vwochnik | 6:642e7c233e83 | 192 | return false; |
vwochnik | 6:642e7c233e83 | 193 | } |
vwochnik | 6:642e7c233e83 | 194 | |
vwochnik | 6:642e7c233e83 | 195 | if (received.values() != 3) { |
vwochnik | 6:642e7c233e83 | 196 | puts("Received bad data."); |
vwochnik | 10:d5cd4a7ada7b | 197 | client.stop(); |
vwochnik | 6:642e7c233e83 | 198 | return false; |
vwochnik | 6:642e7c233e83 | 199 | } |
vwochnik | 6:642e7c233e83 | 200 | |
vwochnik | 6:642e7c233e83 | 201 | if (received.value(0).integerValue() != 200) { |
vwochnik | 6:642e7c233e83 | 202 | puts("Received bad data."); |
vwochnik | 10:d5cd4a7ada7b | 203 | client.stop(); |
vwochnik | 6:642e7c233e83 | 204 | return false; |
vwochnik | 6:642e7c233e83 | 205 | } |
vwochnik | 6:642e7c233e83 | 206 | |
vwochnik | 6:642e7c233e83 | 207 | client.stop(); |
vwochnik | 6:642e7c233e83 | 208 | return true; |
vwochnik | 6:642e7c233e83 | 209 | } |
vwochnik | 6:642e7c233e83 | 210 | |
vwochnik | 7:f77afd49c35d | 211 | bool update(long deviceId) |
vwochnik | 7:f77afd49c35d | 212 | { |
vwochnik | 7:f77afd49c35d | 213 | ComposedRecord newMoRec(true); // set copy=true b/c tmp objects |
vwochnik | 7:f77afd49c35d | 214 | ParsedRecord received; |
vwochnik | 7:f77afd49c35d | 215 | |
vwochnik | 7:f77afd49c35d | 216 | puts("Updating device data..."); |
vwochnik | 7:f77afd49c35d | 217 | |
vwochnik | 7:f77afd49c35d | 218 | newMoRec.add(IntegerValue(103)).add(IntegerValue(deviceId)).add(CharValue(imei())).add(CharValue(cellId())).add(CharValue(iccid())); |
vwochnik | 7:f77afd49c35d | 219 | |
vwochnik | 7:f77afd49c35d | 220 | if (client.send(newMoRec) != SMARTREST_SUCCESS) { |
vwochnik | 7:f77afd49c35d | 221 | puts("Send failed."); |
vwochnik | 10:d5cd4a7ada7b | 222 | client.stop(); |
vwochnik | 7:f77afd49c35d | 223 | return false; |
vwochnik | 7:f77afd49c35d | 224 | } |
vwochnik | 7:f77afd49c35d | 225 | |
vwochnik | 7:f77afd49c35d | 226 | if (client.receive(received) != SMARTREST_SUCCESS) { |
vwochnik | 7:f77afd49c35d | 227 | puts("Update failed."); |
vwochnik | 10:d5cd4a7ada7b | 228 | client.stop(); |
vwochnik | 7:f77afd49c35d | 229 | return false; |
vwochnik | 7:f77afd49c35d | 230 | } |
vwochnik | 7:f77afd49c35d | 231 | |
vwochnik | 7:f77afd49c35d | 232 | if (received.values() != 3) { |
vwochnik | 7:f77afd49c35d | 233 | puts("Bad received data."); |
vwochnik | 10:d5cd4a7ada7b | 234 | client.stop(); |
vwochnik | 7:f77afd49c35d | 235 | return false; |
vwochnik | 7:f77afd49c35d | 236 | } |
vwochnik | 7:f77afd49c35d | 237 | |
vwochnik | 7:f77afd49c35d | 238 | if (received.value(0).integerValue() != 201) { |
vwochnik | 7:f77afd49c35d | 239 | puts("Bad received data."); |
vwochnik | 10:d5cd4a7ada7b | 240 | client.stop(); |
vwochnik | 7:f77afd49c35d | 241 | return false; |
vwochnik | 7:f77afd49c35d | 242 | } |
vwochnik | 7:f77afd49c35d | 243 | |
vwochnik | 7:f77afd49c35d | 244 | client.stop(); |
vwochnik | 7:f77afd49c35d | 245 | |
vwochnik | 7:f77afd49c35d | 246 | return true; |
vwochnik | 7:f77afd49c35d | 247 | } |
vwochnik | 7:f77afd49c35d | 248 | |
vwochnik | 8:940c782eec5e | 249 | void loop(long deviceId) |
vwochnik | 8:940c782eec5e | 250 | { |
vwochnik | 13:4045b6ac448d | 251 | Aggregator aggr(5, false, true); |
vwochnik | 8:940c782eec5e | 252 | |
vwochnik | 13:4045b6ac448d | 253 | signalMeasurement(deviceId, aggr); |
vwochnik | 15:0ccf0f530a05 | 254 | if (client.send(aggr) != SMARTREST_SUCCESS) { |
vwochnik | 15:0ccf0f530a05 | 255 | puts("Send failed."); |
vwochnik | 15:0ccf0f530a05 | 256 | } |
vwochnik | 15:0ccf0f530a05 | 257 | client.stop(); |
vwochnik | 15:0ccf0f530a05 | 258 | aggr.clear(); |
vwochnik | 13:4045b6ac448d | 259 | temperatureMeasurement(deviceId, aggr); |
vwochnik | 19:7bee744fe527 | 260 | analogMeasurement(deviceId, aggr); |
vwochnik | 15:0ccf0f530a05 | 261 | if (client.send(aggr) != SMARTREST_SUCCESS) { |
vwochnik | 15:0ccf0f530a05 | 262 | puts("Send failed."); |
vwochnik | 15:0ccf0f530a05 | 263 | } |
vwochnik | 15:0ccf0f530a05 | 264 | client.stop(); |
vwochnik | 15:0ccf0f530a05 | 265 | aggr.clear(); |
vwochnik | 13:4045b6ac448d | 266 | motionMeasurement(deviceId, aggr); |
vwochnik | 6:642e7c233e83 | 267 | |
vwochnik | 13:4045b6ac448d | 268 | if (client.send(aggr) != SMARTREST_SUCCESS) { |
vwochnik | 6:642e7c233e83 | 269 | puts("Send failed."); |
vwochnik | 6:642e7c233e83 | 270 | } |
vwochnik | 6:642e7c233e83 | 271 | client.stop(); |
vwochnik | 13:4045b6ac448d | 272 | } |
vwochnik | 13:4045b6ac448d | 273 | |
vwochnik | 13:4045b6ac448d | 274 | void signalMeasurement(long deviceId, Aggregator& aggr) |
vwochnik | 13:4045b6ac448d | 275 | { |
vwochnik | 13:4045b6ac448d | 276 | sigq_t *sq = signalQuality(); |
vwochnik | 13:4045b6ac448d | 277 | |
vwochnik | 13:4045b6ac448d | 278 | if ((sq->rssi == 0) || (sq->ber == 0)) |
vwochnik | 13:4045b6ac448d | 279 | return; |
vwochnik | 13:4045b6ac448d | 280 | |
vwochnik | 13:4045b6ac448d | 281 | ComposedRecord measurement; |
vwochnik | 13:4045b6ac448d | 282 | IntegerValue msgId(104); |
vwochnik | 13:4045b6ac448d | 283 | IntegerValue devId(deviceId); |
vwochnik | 13:4045b6ac448d | 284 | FloatValue rssi(sq->rssi, 0); |
vwochnik | 13:4045b6ac448d | 285 | IntegerValue ber(sq->ber); |
vwochnik | 13:4045b6ac448d | 286 | measurement.add(msgId).add(devId).add(rssi).add(ber); |
vwochnik | 13:4045b6ac448d | 287 | aggr.add(measurement); |
vwochnik | 10:d5cd4a7ada7b | 288 | } |
vwochnik | 13:4045b6ac448d | 289 | |
vwochnik | 13:4045b6ac448d | 290 | void temperatureMeasurement(long deviceId, Aggregator& aggr) |
vwochnik | 13:4045b6ac448d | 291 | { |
vwochnik | 13:4045b6ac448d | 292 | ComposedRecord measurement; |
vwochnik | 13:4045b6ac448d | 293 | IntegerValue msgId(105); |
vwochnik | 13:4045b6ac448d | 294 | IntegerValue devId(deviceId); |
vwochnik | 13:4045b6ac448d | 295 | FloatValue temp(temperature(), 1); |
vwochnik | 13:4045b6ac448d | 296 | measurement.add(msgId).add(devId).add(temp); |
vwochnik | 13:4045b6ac448d | 297 | aggr.add(measurement); |
vwochnik | 13:4045b6ac448d | 298 | } |
vwochnik | 13:4045b6ac448d | 299 | |
vwochnik | 19:7bee744fe527 | 300 | void analogMeasurement(long deviceId, Aggregator& aggr) |
vwochnik | 13:4045b6ac448d | 301 | { |
vwochnik | 19:7bee744fe527 | 302 | long analog0 = (long)(potentiometer(0) * 100.0); |
vwochnik | 19:7bee744fe527 | 303 | long analog1 = (long)(potentiometer(1) * 100.0); |
vwochnik | 13:4045b6ac448d | 304 | ComposedRecord measurement; |
vwochnik | 13:4045b6ac448d | 305 | IntegerValue msgId(106); |
vwochnik | 13:4045b6ac448d | 306 | IntegerValue devId(deviceId); |
vwochnik | 19:7bee744fe527 | 307 | IntegerValue an0(analog0); |
vwochnik | 19:7bee744fe527 | 308 | IntegerValue an1(analog1); |
vwochnik | 19:7bee744fe527 | 309 | measurement.add(msgId).add(devId).add(an0).add(an1); |
vwochnik | 13:4045b6ac448d | 310 | aggr.add(measurement); |
vwochnik | 13:4045b6ac448d | 311 | } |
vwochnik | 13:4045b6ac448d | 312 | |
vwochnik | 13:4045b6ac448d | 313 | void motionMeasurement(long deviceId, Aggregator& aggr) |
vwochnik | 13:4045b6ac448d | 314 | { |
vwochnik | 13:4045b6ac448d | 315 | acceleration_t acc = acceleration(); |
vwochnik | 13:4045b6ac448d | 316 | ComposedRecord measurement; |
vwochnik | 13:4045b6ac448d | 317 | IntegerValue msgId(107); |
vwochnik | 13:4045b6ac448d | 318 | IntegerValue devId(deviceId); |
vwochnik | 13:4045b6ac448d | 319 | FloatValue x(acc.x, 2); |
vwochnik | 13:4045b6ac448d | 320 | FloatValue y(acc.y, 2); |
vwochnik | 13:4045b6ac448d | 321 | FloatValue z(acc.z, 2); |
vwochnik | 13:4045b6ac448d | 322 | measurement.add(msgId).add(devId).add(x).add(y).add(z); |
vwochnik | 13:4045b6ac448d | 323 | aggr.add(measurement); |
vwochnik | 13:4045b6ac448d | 324 | } |
vwochnik | 13:4045b6ac448d | 325 |