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