GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
main.cpp@5:ffef0c7a70a1, 2014-12-15 (annotated)
- Committer:
- Trumple
- Date:
- Mon Dec 15 13:02:05 2014 +0000
- Revision:
- 5:ffef0c7a70a1
- Parent:
- 4:0bab12a0cc9a
- Child:
- 7:5ae1d99d2b69
- Child:
- 8:fd531fe7637b
Update SNAIL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Trumple | 0:fcab3b154e49 | 1 | #include "mbed.h" |
Trumple | 0:fcab3b154e49 | 2 | #include "snail.h" |
Trumple | 0:fcab3b154e49 | 3 | #include "sensorinterface.h" |
Trumple | 0:fcab3b154e49 | 4 | #include "sdcard.h" |
Trumple | 1:27b35752c5d0 | 5 | #include "http.h" |
Trumple | 2:1cbb20dd1733 | 6 | #include "MbedJSONValue.h" |
Trumple | 0:fcab3b154e49 | 7 | |
Trumple | 0:fcab3b154e49 | 8 | #define DEBUG |
Trumple | 0:fcab3b154e49 | 9 | |
Trumple | 0:fcab3b154e49 | 10 | time_t lastPollTime = 0; |
Trumple | 0:fcab3b154e49 | 11 | time_t pollInterval = 30; |
Trumple | 2:1cbb20dd1733 | 12 | |
Trumple | 0:fcab3b154e49 | 13 | |
Trumple | 0:fcab3b154e49 | 14 | bool isBasenode = false; |
Trumple | 2:1cbb20dd1733 | 15 | http h = http(); |
Trumple | 1:27b35752c5d0 | 16 | |
Trumple | 1:27b35752c5d0 | 17 | char localAddress[8]; |
Trumple | 2:1cbb20dd1733 | 18 | char baseNodeAddress[8]; |
Trumple | 1:27b35752c5d0 | 19 | bool networkParametersUpdated = false; |
Trumple | 1:27b35752c5d0 | 20 | bool networkParametersTimedOut = false; |
Trumple | 0:fcab3b154e49 | 21 | |
Trumple | 0:fcab3b154e49 | 22 | Serial pc(USBTX, USBRX); |
Trumple | 1:27b35752c5d0 | 23 | snail xbee = snail(); |
Trumple | 1:27b35752c5d0 | 24 | Ticker networkParametersTimeout; |
Trumple | 0:fcab3b154e49 | 25 | |
Trumple | 2:1cbb20dd1733 | 26 | void handleJoinNetworkReply(snail::message& message) |
Trumple | 1:27b35752c5d0 | 27 | { |
Trumple | 2:1cbb20dd1733 | 28 | pc.printf("[MAIN] Got join network reply...\r\n"); |
Trumple | 2:1cbb20dd1733 | 29 | snail::joinnetworkreply& reply = static_cast<snail::joinnetworkreply&>(message); |
Trumple | 2:1cbb20dd1733 | 30 | set_time(reply.timestamp); |
Trumple | 2:1cbb20dd1733 | 31 | memcpy(baseNodeAddress, reply.baseNodeAddress, sizeof(baseNodeAddress)); |
Trumple | 2:1cbb20dd1733 | 32 | |
Trumple | 1:27b35752c5d0 | 33 | networkParametersUpdated = true; |
Trumple | 1:27b35752c5d0 | 34 | } |
Trumple | 1:27b35752c5d0 | 35 | |
Trumple | 2:1cbb20dd1733 | 36 | void handleJoinNetworkRequest(snail::message& message) |
Trumple | 0:fcab3b154e49 | 37 | { |
Trumple | 2:1cbb20dd1733 | 38 | pc.printf("[MAIN] Got join network request...\r\n"); |
Trumple | 1:27b35752c5d0 | 39 | if (isBasenode) |
Trumple | 1:27b35752c5d0 | 40 | { |
Trumple | 2:1cbb20dd1733 | 41 | snail::joinnetworkrequest& request = static_cast<snail::joinnetworkrequest&>(message); |
Trumple | 2:1cbb20dd1733 | 42 | |
Trumple | 2:1cbb20dd1733 | 43 | //TODO: Send sensors data to server |
Trumple | 2:1cbb20dd1733 | 44 | snail::joinnetworkreply reply(request.source, time(NULL), localAddress); |
Trumple | 1:27b35752c5d0 | 45 | xbee.send(reply, sizeof(reply)); |
Trumple | 1:27b35752c5d0 | 46 | } |
Trumple | 1:27b35752c5d0 | 47 | } |
Trumple | 1:27b35752c5d0 | 48 | |
Trumple | 2:1cbb20dd1733 | 49 | void handleSensorData(snail::message& message) |
Trumple | 2:1cbb20dd1733 | 50 | { |
Trumple | 2:1cbb20dd1733 | 51 | pc.printf("[MAIN] Got sensor data...\r\n"); |
Trumple | 2:1cbb20dd1733 | 52 | snail::sensordata& d = static_cast<snail::sensordata&>(message); |
Trumple | 2:1cbb20dd1733 | 53 | |
Trumple | 4:0bab12a0cc9a | 54 | int value = 0; |
Trumple | 4:0bab12a0cc9a | 55 | value += d.payload[0]; |
Trumple | 4:0bab12a0cc9a | 56 | value = value << 8; |
Trumple | 4:0bab12a0cc9a | 57 | value += d.payload[1]; |
Trumple | 4:0bab12a0cc9a | 58 | |
Trumple | 4:0bab12a0cc9a | 59 | MbedJSONValue j; |
Trumple | 4:0bab12a0cc9a | 60 | j["timestamp"] = (int)time(NULL); |
Trumple | 4:0bab12a0cc9a | 61 | j["value"] = value; |
Trumple | 4:0bab12a0cc9a | 62 | pc.printf(h.post("178.62.84.55", 8888, "/field/testfield/testnode/testsensor/", j.serialize()).c_str()); |
Trumple | 2:1cbb20dd1733 | 63 | } |
Trumple | 2:1cbb20dd1733 | 64 | |
Trumple | 1:27b35752c5d0 | 65 | void handleNetworkParametersTimeout() |
Trumple | 1:27b35752c5d0 | 66 | { |
Trumple | 1:27b35752c5d0 | 67 | networkParametersTimedOut = true; |
Trumple | 1:27b35752c5d0 | 68 | } |
Trumple | 1:27b35752c5d0 | 69 | |
Trumple | 1:27b35752c5d0 | 70 | void getLocalAddress() |
Trumple | 1:27b35752c5d0 | 71 | { |
Trumple | 2:1cbb20dd1733 | 72 | pc.printf("[MAIN] Requesting lower address bits...\r\n"); |
Trumple | 2:1cbb20dd1733 | 73 | xbee.ATCommand("SL"); |
Trumple | 2:1cbb20dd1733 | 74 | |
Trumple | 2:1cbb20dd1733 | 75 | while (!xbee.isATCommandResponseWaiting()) |
Trumple | 2:1cbb20dd1733 | 76 | xbee.processIncomingData(); |
Trumple | 2:1cbb20dd1733 | 77 | |
Trumple | 2:1cbb20dd1733 | 78 | snail::atcommandresponse SLr = xbee.getATCommandResponse(); |
Trumple | 2:1cbb20dd1733 | 79 | |
Trumple | 2:1cbb20dd1733 | 80 | pc.printf("[MAIN] Requesting upper address bits...\r\n"); |
Trumple | 2:1cbb20dd1733 | 81 | xbee.ATCommand("SH"); |
Trumple | 2:1cbb20dd1733 | 82 | |
Trumple | 2:1cbb20dd1733 | 83 | while (!xbee.isATCommandResponseWaiting()) |
Trumple | 2:1cbb20dd1733 | 84 | xbee.processIncomingData(); |
Trumple | 2:1cbb20dd1733 | 85 | |
Trumple | 2:1cbb20dd1733 | 86 | snail::atcommandresponse SHr = xbee.getATCommandResponse(); |
Trumple | 2:1cbb20dd1733 | 87 | |
Trumple | 2:1cbb20dd1733 | 88 | string address(SHr.response); |
Trumple | 2:1cbb20dd1733 | 89 | address += SLr.response; |
Trumple | 2:1cbb20dd1733 | 90 | |
Trumple | 2:1cbb20dd1733 | 91 | pc.printf("[MAIN] Got local address: "); |
Trumple | 2:1cbb20dd1733 | 92 | |
Trumple | 2:1cbb20dd1733 | 93 | for (int i = 0; i < address.size(); i++) |
Trumple | 2:1cbb20dd1733 | 94 | pc.printf("%.2X", address[i]); |
Trumple | 2:1cbb20dd1733 | 95 | |
Trumple | 2:1cbb20dd1733 | 96 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 97 | |
Trumple | 2:1cbb20dd1733 | 98 | memcpy(localAddress, address.c_str(), sizeof(localAddress)); |
Trumple | 1:27b35752c5d0 | 99 | } |
Trumple | 1:27b35752c5d0 | 100 | |
Trumple | 1:27b35752c5d0 | 101 | void getNetworkParameters() |
Trumple | 1:27b35752c5d0 | 102 | { |
Trumple | 1:27b35752c5d0 | 103 | #ifdef DEBUG |
Trumple | 2:1cbb20dd1733 | 104 | pc.printf("[MAIN] Requesting to join network...\r\n"); |
Trumple | 1:27b35752c5d0 | 105 | #endif |
Trumple | 0:fcab3b154e49 | 106 | |
Trumple | 2:1cbb20dd1733 | 107 | xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REPLY, handleJoinNetworkReply); |
Trumple | 2:1cbb20dd1733 | 108 | |
Trumple | 2:1cbb20dd1733 | 109 | networkParametersTimeout.attach(&handleNetworkParametersTimeout, 30.0); |
Trumple | 1:27b35752c5d0 | 110 | |
Trumple | 5:ffef0c7a70a1 | 111 | snail::joinnetworkrequest::sensor sensors[32]; |
Trumple | 5:ffef0c7a70a1 | 112 | snail::joinnetworkrequest request(sensors, 0); |
Trumple | 1:27b35752c5d0 | 113 | |
Trumple | 1:27b35752c5d0 | 114 | xbee.send(request, sizeof(request)); |
Trumple | 0:fcab3b154e49 | 115 | |
Trumple | 1:27b35752c5d0 | 116 | while(!networkParametersUpdated) |
Trumple | 1:27b35752c5d0 | 117 | { |
Trumple | 1:27b35752c5d0 | 118 | if (networkParametersTimedOut) |
Trumple | 1:27b35752c5d0 | 119 | { |
Trumple | 1:27b35752c5d0 | 120 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 121 | pc.printf("[MAIN] Timed out, retrying...\r\n"); |
Trumple | 1:27b35752c5d0 | 122 | #endif |
Trumple | 1:27b35752c5d0 | 123 | xbee.send(request, sizeof(request)); |
Trumple | 1:27b35752c5d0 | 124 | networkParametersTimedOut = false; |
Trumple | 1:27b35752c5d0 | 125 | } |
Trumple | 2:1cbb20dd1733 | 126 | xbee.processIncomingData(); |
Trumple | 1:27b35752c5d0 | 127 | } |
Trumple | 0:fcab3b154e49 | 128 | |
Trumple | 1:27b35752c5d0 | 129 | #ifdef DEBUG |
Trumple | 2:1cbb20dd1733 | 130 | pc.printf("[MAIN] Got network parameters. Time: %i, base node address: ", time(NULL)); |
Trumple | 2:1cbb20dd1733 | 131 | |
Trumple | 2:1cbb20dd1733 | 132 | for (int i = 0; i < sizeof(baseNodeAddress); i++) |
Trumple | 2:1cbb20dd1733 | 133 | pc.printf("%.2X", baseNodeAddress[i]); |
Trumple | 2:1cbb20dd1733 | 134 | pc.printf("\r\n"); |
Trumple | 1:27b35752c5d0 | 135 | #endif |
Trumple | 1:27b35752c5d0 | 136 | networkParametersTimeout.detach(); |
Trumple | 0:fcab3b154e49 | 137 | } |
Trumple | 0:fcab3b154e49 | 138 | |
Trumple | 0:fcab3b154e49 | 139 | int main() |
Trumple | 0:fcab3b154e49 | 140 | { |
Trumple | 0:fcab3b154e49 | 141 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 142 | pc.printf("[MAIN] Starting up...\r\n"); |
Trumple | 2:1cbb20dd1733 | 143 | pc.printf(" . .\r\n"); |
Trumple | 2:1cbb20dd1733 | 144 | pc.printf(" / `. .' \\\r\n"); |
Trumple | 2:1cbb20dd1733 | 145 | pc.printf(" .---. < > < > .---.\r\n"); |
Trumple | 2:1cbb20dd1733 | 146 | pc.printf(" | \\ \\ - ~ ~ - / / |\r\n"); |
Trumple | 2:1cbb20dd1733 | 147 | pc.printf(" ~-..-~ ~-..-~\r\n"); |
Trumple | 2:1cbb20dd1733 | 148 | pc.printf(" \\~~~\\.' `./~~~/\r\n"); |
Trumple | 2:1cbb20dd1733 | 149 | pc.printf(" .-~~^-. \\__/ \\__/\r\n"); |
Trumple | 2:1cbb20dd1733 | 150 | pc.printf(" .' O \\ / / \\ \\\r\n"); |
Trumple | 2:1cbb20dd1733 | 151 | pc.printf("(_____, `._.' | } \\/~~~/\r\n"); |
Trumple | 2:1cbb20dd1733 | 152 | pc.printf(" `----. / } | / \\__/\r\n"); |
Trumple | 2:1cbb20dd1733 | 153 | pc.printf(" `-. | / | / `. ,~~|\r\n"); |
Trumple | 2:1cbb20dd1733 | 154 | pc.printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n"); |
Trumple | 2:1cbb20dd1733 | 155 | pc.printf(" | / | / ~-. `-. _||_||_\r\n"); |
Trumple | 2:1cbb20dd1733 | 156 | pc.printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n"); |
Trumple | 2:1cbb20dd1733 | 157 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 158 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 159 | pc.printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n"); |
Trumple | 2:1cbb20dd1733 | 160 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 161 | pc.printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 162 | #endif |
Trumple | 1:27b35752c5d0 | 163 | //sdcard sd = sdcard(); |
Trumple | 0:fcab3b154e49 | 164 | |
Trumple | 0:fcab3b154e49 | 165 | //TODO: read basenode pin |
Trumple | 0:fcab3b154e49 | 166 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 167 | pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode); |
Trumple | 0:fcab3b154e49 | 168 | #endif |
Trumple | 0:fcab3b154e49 | 169 | |
Trumple | 0:fcab3b154e49 | 170 | //TODO: load configuration from SD |
Trumple | 0:fcab3b154e49 | 171 | |
Trumple | 2:1cbb20dd1733 | 172 | getLocalAddress(); |
Trumple | 2:1cbb20dd1733 | 173 | |
Trumple | 0:fcab3b154e49 | 174 | if (isBasenode) |
Trumple | 1:27b35752c5d0 | 175 | { |
Trumple | 1:27b35752c5d0 | 176 | getLocalAddress(); |
Trumple | 2:1cbb20dd1733 | 177 | h.connect(); |
Trumple | 2:1cbb20dd1733 | 178 | |
Trumple | 2:1cbb20dd1733 | 179 | string timestampStr = h.get("time.bitnode.co.uk", 80, "/"); |
Trumple | 2:1cbb20dd1733 | 180 | time_t timestamp = atoi(timestampStr.c_str()); |
Trumple | 2:1cbb20dd1733 | 181 | |
Trumple | 1:27b35752c5d0 | 182 | set_time(timestamp); |
Trumple | 1:27b35752c5d0 | 183 | |
Trumple | 2:1cbb20dd1733 | 184 | pc.printf("[MAIN] Got time: %i\r\n", timestamp); |
Trumple | 2:1cbb20dd1733 | 185 | |
Trumple | 2:1cbb20dd1733 | 186 | xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REQUEST, handleJoinNetworkRequest); |
Trumple | 2:1cbb20dd1733 | 187 | xbee.registerMessageCallback(MESSAGE_SENSOR_DATA, handleSensorData); |
Trumple | 1:27b35752c5d0 | 188 | |
Trumple | 0:fcab3b154e49 | 189 | while(1) |
Trumple | 0:fcab3b154e49 | 190 | { |
Trumple | 0:fcab3b154e49 | 191 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 192 | pc.printf("[MAIN] Basenode is idle\r\n"); |
Trumple | 0:fcab3b154e49 | 193 | #endif |
Trumple | 0:fcab3b154e49 | 194 | |
Trumple | 2:1cbb20dd1733 | 195 | wait(1); |
Trumple | 2:1cbb20dd1733 | 196 | xbee.processIncomingData(); |
Trumple | 0:fcab3b154e49 | 197 | } |
Trumple | 1:27b35752c5d0 | 198 | } |
Trumple | 0:fcab3b154e49 | 199 | else |
Trumple | 1:27b35752c5d0 | 200 | { |
Trumple | 1:27b35752c5d0 | 201 | sensorinterface sensors = sensorinterface(); |
Trumple | 1:27b35752c5d0 | 202 | getNetworkParameters(); |
Trumple | 1:27b35752c5d0 | 203 | |
Trumple | 0:fcab3b154e49 | 204 | while(1) |
Trumple | 0:fcab3b154e49 | 205 | { |
Trumple | 2:1cbb20dd1733 | 206 | xbee.processIncomingData(); |
Trumple | 1:27b35752c5d0 | 207 | //TODO: if xbee interrupt has woken us up |
Trumple | 0:fcab3b154e49 | 208 | //transmit 10 latest readings |
Trumple | 0:fcab3b154e49 | 209 | |
Trumple | 4:0bab12a0cc9a | 210 | //check if it's time to poll |
Trumple | 2:1cbb20dd1733 | 211 | if (time(NULL) - lastPollTime > pollInterval) |
Trumple | 0:fcab3b154e49 | 212 | { |
Trumple | 0:fcab3b154e49 | 213 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 214 | pc.printf("[MAIN] Requesting data...\r\n"); |
Trumple | 0:fcab3b154e49 | 215 | #endif |
Trumple | 0:fcab3b154e49 | 216 | sensors.requestData(); |
Trumple | 0:fcab3b154e49 | 217 | lastPollTime = time(NULL); |
Trumple | 0:fcab3b154e49 | 218 | } |
Trumple | 0:fcab3b154e49 | 219 | |
Trumple | 0:fcab3b154e49 | 220 | //if there is data waiting for us... |
Trumple | 0:fcab3b154e49 | 221 | if (sensors.isDataWaiting()) |
Trumple | 0:fcab3b154e49 | 222 | { |
Trumple | 0:fcab3b154e49 | 223 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 224 | pc.printf("[MAIN] Data waiting, reading data...\r\n"); |
Trumple | 0:fcab3b154e49 | 225 | #endif |
Trumple | 1:27b35752c5d0 | 226 | |
Trumple | 0:fcab3b154e49 | 227 | d_reply data = sensors.readData(); |
Trumple | 1:27b35752c5d0 | 228 | |
Trumple | 0:fcab3b154e49 | 229 | #ifdef DEBUG |
Trumple | 4:0bab12a0cc9a | 230 | pc.printf("[MAIN] Got data: "); |
Trumple | 2:1cbb20dd1733 | 231 | for (int i = 0; i < data.readings.size(); i++) |
jakehodges | 3:71b090ec5c69 | 232 | pc.printf("0x%.2X|", data.readings[i]); |
Trumple | 4:0bab12a0cc9a | 233 | pc.printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 234 | #endif |
Trumple | 1:27b35752c5d0 | 235 | |
Trumple | 4:0bab12a0cc9a | 236 | string readings_str(data.readings.begin(), data.readings.end()); |
Trumple | 4:0bab12a0cc9a | 237 | char readings[sizeof(snail::sensordata().payload)]; |
Trumple | 4:0bab12a0cc9a | 238 | memcpy(readings, readings_str.c_str(), sizeof(readings)); |
Trumple | 4:0bab12a0cc9a | 239 | |
Trumple | 4:0bab12a0cc9a | 240 | snail::sensordata message(baseNodeAddress, data.type, data.type, time(NULL), readings, data.readings.size()); |
Trumple | 4:0bab12a0cc9a | 241 | |
Trumple | 4:0bab12a0cc9a | 242 | xbee.send(message, sizeof(message)); |
Trumple | 4:0bab12a0cc9a | 243 | |
Trumple | 0:fcab3b154e49 | 244 | //log |
Trumple | 1:27b35752c5d0 | 245 | //sd.write(static_cast<long int>(time(NULL)), data); |
Trumple | 0:fcab3b154e49 | 246 | } |
Trumple | 0:fcab3b154e49 | 247 | } |
Trumple | 1:27b35752c5d0 | 248 | } |
Trumple | 0:fcab3b154e49 | 249 | } |