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