GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
Trumple
Date:
Mon Dec 15 15:50:20 2014 +0000
Revision:
9:f0030295ae9b
Parent:
8:fd531fe7637b
Parent:
7:5ae1d99d2b69
Child:
10:13176e7bd4c8
Merge master branch

Who changed what in which revision?

UserRevisionLine numberNew 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 8:fd531fe7637b 7 #include <map>
Trumple 0:fcab3b154e49 8
Trumple 0:fcab3b154e49 9 #define DEBUG
Trumple 0:fcab3b154e49 10
Trumple 0:fcab3b154e49 11 time_t lastPollTime = 0;
Trumple 0:fcab3b154e49 12 time_t pollInterval = 30;
Trumple 2:1cbb20dd1733 13
Trumple 0:fcab3b154e49 14
Trumple 0:fcab3b154e49 15 bool isBasenode = false;
Trumple 2:1cbb20dd1733 16 http h = http();
Trumple 1:27b35752c5d0 17
Trumple 8:fd531fe7637b 18 sensorinterface* sif;
Trumple 8:fd531fe7637b 19
Trumple 1:27b35752c5d0 20 char localAddress[8];
Trumple 2:1cbb20dd1733 21 char baseNodeAddress[8];
Trumple 1:27b35752c5d0 22 bool networkParametersUpdated = false;
Trumple 1:27b35752c5d0 23 bool networkParametersTimedOut = false;
Trumple 0:fcab3b154e49 24
Trumple 0:fcab3b154e49 25 Serial pc(USBTX, USBRX);
Trumple 1:27b35752c5d0 26 snail xbee = snail();
Trumple 1:27b35752c5d0 27 Ticker networkParametersTimeout;
Trumple 0:fcab3b154e49 28
jakehodges 6:92ddc620a78f 29 queue<snail::sensordata> messageQueue;
jakehodges 6:92ddc620a78f 30
Trumple 2:1cbb20dd1733 31 void handleJoinNetworkReply(snail::message& message)
Trumple 1:27b35752c5d0 32 {
Trumple 2:1cbb20dd1733 33 pc.printf("[MAIN] Got join network reply...\r\n");
Trumple 2:1cbb20dd1733 34 snail::joinnetworkreply& reply = static_cast<snail::joinnetworkreply&>(message);
Trumple 2:1cbb20dd1733 35 set_time(reply.timestamp);
Trumple 2:1cbb20dd1733 36 memcpy(baseNodeAddress, reply.baseNodeAddress, sizeof(baseNodeAddress));
Trumple 2:1cbb20dd1733 37
Trumple 1:27b35752c5d0 38 networkParametersUpdated = true;
Trumple 1:27b35752c5d0 39 }
Trumple 1:27b35752c5d0 40
Trumple 2:1cbb20dd1733 41 void handleJoinNetworkRequest(snail::message& message)
Trumple 0:fcab3b154e49 42 {
Trumple 2:1cbb20dd1733 43 pc.printf("[MAIN] Got join network request...\r\n");
Trumple 1:27b35752c5d0 44 if (isBasenode)
Trumple 1:27b35752c5d0 45 {
Trumple 2:1cbb20dd1733 46 snail::joinnetworkrequest& request = static_cast<snail::joinnetworkrequest&>(message);
Trumple 2:1cbb20dd1733 47
Trumple 2:1cbb20dd1733 48 //TODO: Send sensors data to server
Trumple 2:1cbb20dd1733 49 snail::joinnetworkreply reply(request.source, time(NULL), localAddress);
Trumple 1:27b35752c5d0 50 xbee.send(reply, sizeof(reply));
Trumple 1:27b35752c5d0 51 }
Trumple 1:27b35752c5d0 52 }
Trumple 1:27b35752c5d0 53
Trumple 2:1cbb20dd1733 54 void handleSensorData(snail::message& message)
Trumple 2:1cbb20dd1733 55 {
Trumple 2:1cbb20dd1733 56 pc.printf("[MAIN] Got sensor data...\r\n");
Trumple 2:1cbb20dd1733 57 snail::sensordata& d = static_cast<snail::sensordata&>(message);
Trumple 2:1cbb20dd1733 58
Trumple 4:0bab12a0cc9a 59 int value = 0;
Trumple 4:0bab12a0cc9a 60 value += d.payload[0];
Trumple 4:0bab12a0cc9a 61 value = value << 8;
Trumple 4:0bab12a0cc9a 62 value += d.payload[1];
Trumple 4:0bab12a0cc9a 63
Trumple 4:0bab12a0cc9a 64 MbedJSONValue j;
Trumple 4:0bab12a0cc9a 65 j["timestamp"] = (int)time(NULL);
Trumple 4:0bab12a0cc9a 66 j["value"] = value;
Trumple 4:0bab12a0cc9a 67 pc.printf(h.post("178.62.84.55", 8888, "/field/testfield/testnode/testsensor/", j.serialize()).c_str());
Trumple 2:1cbb20dd1733 68 }
Trumple 2:1cbb20dd1733 69
Trumple 1:27b35752c5d0 70 void handleNetworkParametersTimeout()
Trumple 1:27b35752c5d0 71 {
Trumple 1:27b35752c5d0 72 networkParametersTimedOut = true;
Trumple 1:27b35752c5d0 73 }
Trumple 1:27b35752c5d0 74
Trumple 1:27b35752c5d0 75 void getLocalAddress()
Trumple 1:27b35752c5d0 76 {
Trumple 2:1cbb20dd1733 77 pc.printf("[MAIN] Requesting lower address bits...\r\n");
Trumple 2:1cbb20dd1733 78 xbee.ATCommand("SL");
Trumple 2:1cbb20dd1733 79
Trumple 2:1cbb20dd1733 80 while (!xbee.isATCommandResponseWaiting())
Trumple 2:1cbb20dd1733 81 xbee.processIncomingData();
Trumple 2:1cbb20dd1733 82
Trumple 2:1cbb20dd1733 83 snail::atcommandresponse SLr = xbee.getATCommandResponse();
Trumple 2:1cbb20dd1733 84
Trumple 2:1cbb20dd1733 85 pc.printf("[MAIN] Requesting upper address bits...\r\n");
Trumple 2:1cbb20dd1733 86 xbee.ATCommand("SH");
Trumple 2:1cbb20dd1733 87
Trumple 2:1cbb20dd1733 88 while (!xbee.isATCommandResponseWaiting())
Trumple 2:1cbb20dd1733 89 xbee.processIncomingData();
Trumple 2:1cbb20dd1733 90
Trumple 2:1cbb20dd1733 91 snail::atcommandresponse SHr = xbee.getATCommandResponse();
Trumple 2:1cbb20dd1733 92
Trumple 2:1cbb20dd1733 93 string address(SHr.response);
Trumple 2:1cbb20dd1733 94 address += SLr.response;
Trumple 2:1cbb20dd1733 95
Trumple 2:1cbb20dd1733 96 pc.printf("[MAIN] Got local address: ");
Trumple 2:1cbb20dd1733 97
Trumple 2:1cbb20dd1733 98 for (int i = 0; i < address.size(); i++)
Trumple 2:1cbb20dd1733 99 pc.printf("%.2X", address[i]);
Trumple 2:1cbb20dd1733 100
Trumple 2:1cbb20dd1733 101 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 102
Trumple 2:1cbb20dd1733 103 memcpy(localAddress, address.c_str(), sizeof(localAddress));
Trumple 1:27b35752c5d0 104 }
Trumple 1:27b35752c5d0 105
Trumple 1:27b35752c5d0 106 void getNetworkParameters()
Trumple 1:27b35752c5d0 107 {
Trumple 1:27b35752c5d0 108 #ifdef DEBUG
Trumple 2:1cbb20dd1733 109 pc.printf("[MAIN] Requesting to join network...\r\n");
Trumple 1:27b35752c5d0 110 #endif
Trumple 0:fcab3b154e49 111
Trumple 8:fd531fe7637b 112 //prepare for response from basenode
Trumple 2:1cbb20dd1733 113 xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REPLY, handleJoinNetworkReply);
Trumple 2:1cbb20dd1733 114
Trumple 8:fd531fe7637b 115 //handle retrying after timeout
Trumple 2:1cbb20dd1733 116 networkParametersTimeout.attach(&handleNetworkParametersTimeout, 30.0);
Trumple 1:27b35752c5d0 117
Trumple 8:fd531fe7637b 118 //generate list of sensors attached
Trumple 8:fd531fe7637b 119 snail::joinnetworkrequest::sensor localSensors[32];
Trumple 8:fd531fe7637b 120 map<char, sensor>::iterator cs = sif->sensors.begin();
Trumple 8:fd531fe7637b 121 int i = 0;
Trumple 8:fd531fe7637b 122
Trumple 8:fd531fe7637b 123 //either loop until we reach the end of the sensors or until we hit the max
Trumple 8:fd531fe7637b 124 while (cs != sif->sensors.end() && i < 32)
Trumple 8:fd531fe7637b 125 {
Trumple 8:fd531fe7637b 126 localSensors[i].type = cs->second.type;
Trumple 8:fd531fe7637b 127 localSensors[i].id = cs->first;
Trumple 8:fd531fe7637b 128 i++;
Trumple 8:fd531fe7637b 129 cs++;
Trumple 8:fd531fe7637b 130 }
Trumple 8:fd531fe7637b 131
Trumple 8:fd531fe7637b 132 #ifdef DEBUG
Trumple 8:fd531fe7637b 133 pc.printf("[MAIN] Informing basenode of %i attached devices\r\n", i);
Trumple 8:fd531fe7637b 134 #endif
Trumple 8:fd531fe7637b 135
Trumple 8:fd531fe7637b 136 //construct joinnetworkrequest, including the number of sensors attached
Trumple 8:fd531fe7637b 137 snail::joinnetworkrequest request(localSensors, i);
Trumple 1:27b35752c5d0 138
Trumple 1:27b35752c5d0 139 xbee.send(request, sizeof(request));
Trumple 0:fcab3b154e49 140
Trumple 1:27b35752c5d0 141 while(!networkParametersUpdated)
Trumple 1:27b35752c5d0 142 {
Trumple 1:27b35752c5d0 143 if (networkParametersTimedOut)
Trumple 1:27b35752c5d0 144 {
Trumple 1:27b35752c5d0 145 #ifdef DEBUG
Trumple 1:27b35752c5d0 146 pc.printf("[MAIN] Timed out, retrying...\r\n");
Trumple 1:27b35752c5d0 147 #endif
Trumple 1:27b35752c5d0 148 xbee.send(request, sizeof(request));
Trumple 1:27b35752c5d0 149 networkParametersTimedOut = false;
Trumple 1:27b35752c5d0 150 }
Trumple 2:1cbb20dd1733 151 xbee.processIncomingData();
Trumple 1:27b35752c5d0 152 }
Trumple 0:fcab3b154e49 153
Trumple 1:27b35752c5d0 154 #ifdef DEBUG
Trumple 2:1cbb20dd1733 155 pc.printf("[MAIN] Got network parameters. Time: %i, base node address: ", time(NULL));
Trumple 2:1cbb20dd1733 156
Trumple 2:1cbb20dd1733 157 for (int i = 0; i < sizeof(baseNodeAddress); i++)
Trumple 2:1cbb20dd1733 158 pc.printf("%.2X", baseNodeAddress[i]);
Trumple 2:1cbb20dd1733 159 pc.printf("\r\n");
Trumple 1:27b35752c5d0 160 #endif
Trumple 8:fd531fe7637b 161 //no longer need to handle timeout
Trumple 1:27b35752c5d0 162 networkParametersTimeout.detach();
Trumple 0:fcab3b154e49 163 }
Trumple 0:fcab3b154e49 164
Trumple 0:fcab3b154e49 165 int main()
Trumple 0:fcab3b154e49 166 {
Trumple 0:fcab3b154e49 167 #ifdef DEBUG
Trumple 0:fcab3b154e49 168 pc.printf("[MAIN] Starting up...\r\n");
Trumple 2:1cbb20dd1733 169 pc.printf(" . .\r\n");
Trumple 2:1cbb20dd1733 170 pc.printf(" / `. .' \\\r\n");
Trumple 2:1cbb20dd1733 171 pc.printf(" .---. < > < > .---.\r\n");
Trumple 2:1cbb20dd1733 172 pc.printf(" | \\ \\ - ~ ~ - / / |\r\n");
Trumple 2:1cbb20dd1733 173 pc.printf(" ~-..-~ ~-..-~\r\n");
Trumple 2:1cbb20dd1733 174 pc.printf(" \\~~~\\.' `./~~~/\r\n");
Trumple 2:1cbb20dd1733 175 pc.printf(" .-~~^-. \\__/ \\__/\r\n");
Trumple 2:1cbb20dd1733 176 pc.printf(" .' O \\ / / \\ \\\r\n");
Trumple 2:1cbb20dd1733 177 pc.printf("(_____, `._.' | } \\/~~~/\r\n");
Trumple 2:1cbb20dd1733 178 pc.printf(" `----. / } | / \\__/\r\n");
Trumple 2:1cbb20dd1733 179 pc.printf(" `-. | / | / `. ,~~|\r\n");
Trumple 2:1cbb20dd1733 180 pc.printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
Trumple 2:1cbb20dd1733 181 pc.printf(" | / | / ~-. `-. _||_||_\r\n");
Trumple 2:1cbb20dd1733 182 pc.printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n");
Trumple 2:1cbb20dd1733 183 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 184 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 185 pc.printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
Trumple 2:1cbb20dd1733 186 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 187 pc.printf("\r\n");
Trumple 0:fcab3b154e49 188 #endif
Trumple 1:27b35752c5d0 189 //sdcard sd = sdcard();
Trumple 0:fcab3b154e49 190
Trumple 0:fcab3b154e49 191 //TODO: read basenode pin
Trumple 0:fcab3b154e49 192 #ifdef DEBUG
Trumple 0:fcab3b154e49 193 pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
Trumple 0:fcab3b154e49 194 #endif
Trumple 0:fcab3b154e49 195
Trumple 0:fcab3b154e49 196 //TODO: load configuration from SD
Trumple 0:fcab3b154e49 197
Trumple 2:1cbb20dd1733 198 getLocalAddress();
Trumple 2:1cbb20dd1733 199
Trumple 0:fcab3b154e49 200 if (isBasenode)
Trumple 1:27b35752c5d0 201 {
Trumple 1:27b35752c5d0 202 getLocalAddress();
Trumple 2:1cbb20dd1733 203 h.connect();
Trumple 2:1cbb20dd1733 204
Trumple 2:1cbb20dd1733 205 string timestampStr = h.get("time.bitnode.co.uk", 80, "/");
Trumple 2:1cbb20dd1733 206 time_t timestamp = atoi(timestampStr.c_str());
Trumple 2:1cbb20dd1733 207
Trumple 1:27b35752c5d0 208 set_time(timestamp);
Trumple 1:27b35752c5d0 209
Trumple 2:1cbb20dd1733 210 pc.printf("[MAIN] Got time: %i\r\n", timestamp);
Trumple 2:1cbb20dd1733 211
Trumple 2:1cbb20dd1733 212 xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REQUEST, handleJoinNetworkRequest);
Trumple 2:1cbb20dd1733 213 xbee.registerMessageCallback(MESSAGE_SENSOR_DATA, handleSensorData);
Trumple 1:27b35752c5d0 214
Trumple 0:fcab3b154e49 215 while(1)
Trumple 0:fcab3b154e49 216 {
Trumple 0:fcab3b154e49 217 #ifdef DEBUG
Trumple 0:fcab3b154e49 218 pc.printf("[MAIN] Basenode is idle\r\n");
Trumple 0:fcab3b154e49 219 #endif
Trumple 0:fcab3b154e49 220
Trumple 2:1cbb20dd1733 221 wait(1);
Trumple 2:1cbb20dd1733 222 xbee.processIncomingData();
Trumple 0:fcab3b154e49 223 }
Trumple 1:27b35752c5d0 224 }
Trumple 0:fcab3b154e49 225 else
Trumple 8:fd531fe7637b 226 {
Trumple 1:27b35752c5d0 227 sensorinterface sensors = sensorinterface();
Trumple 8:fd531fe7637b 228 sif = &sensors;
Trumple 1:27b35752c5d0 229 getNetworkParameters();
Trumple 1:27b35752c5d0 230
Trumple 0:fcab3b154e49 231 while(1)
Trumple 0:fcab3b154e49 232 {
Trumple 2:1cbb20dd1733 233 xbee.processIncomingData();
jakehodges 6:92ddc620a78f 234
jakehodges 6:92ddc620a78f 235 //TODO: if message queue exceeds MAX_QUEUE SIZE, write older readings to SD card.
jakehodges 6:92ddc620a78f 236
jakehodges 6:92ddc620a78f 237 //if xbee is awake send contents of message queue
jakehodges 6:92ddc620a78f 238 if (xbee.isCTS())
jakehodges 6:92ddc620a78f 239 {
jakehodges 6:92ddc620a78f 240 while(!messageQueue.empty())
jakehodges 6:92ddc620a78f 241 {
jakehodges 6:92ddc620a78f 242 snail::sensordata message = messageQueue.front();
jakehodges 6:92ddc620a78f 243 xbee.send(message, sizeof(message));
jakehodges 6:92ddc620a78f 244 messageQueue.pop();
jakehodges 6:92ddc620a78f 245 }
jakehodges 6:92ddc620a78f 246 }
Trumple 0:fcab3b154e49 247
Trumple 4:0bab12a0cc9a 248 //check if it's time to poll
Trumple 2:1cbb20dd1733 249 if (time(NULL) - lastPollTime > pollInterval)
Trumple 0:fcab3b154e49 250 {
Trumple 0:fcab3b154e49 251 #ifdef DEBUG
Trumple 0:fcab3b154e49 252 pc.printf("[MAIN] Requesting data...\r\n");
Trumple 0:fcab3b154e49 253 #endif
Trumple 0:fcab3b154e49 254 sensors.requestData();
Trumple 0:fcab3b154e49 255 lastPollTime = time(NULL);
Trumple 0:fcab3b154e49 256 }
Trumple 0:fcab3b154e49 257
Trumple 0:fcab3b154e49 258 //if there is data waiting for us...
Trumple 0:fcab3b154e49 259 if (sensors.isDataWaiting())
Trumple 0:fcab3b154e49 260 {
Trumple 0:fcab3b154e49 261 #ifdef DEBUG
Trumple 0:fcab3b154e49 262 pc.printf("[MAIN] Data waiting, reading data...\r\n");
Trumple 0:fcab3b154e49 263 #endif
Trumple 1:27b35752c5d0 264
Trumple 0:fcab3b154e49 265 d_reply data = sensors.readData();
Trumple 1:27b35752c5d0 266
Trumple 0:fcab3b154e49 267 #ifdef DEBUG
Trumple 4:0bab12a0cc9a 268 pc.printf("[MAIN] Got data: ");
Trumple 2:1cbb20dd1733 269 for (int i = 0; i < data.readings.size(); i++)
jakehodges 3:71b090ec5c69 270 pc.printf("0x%.2X|", data.readings[i]);
Trumple 4:0bab12a0cc9a 271 pc.printf("\r\n");
Trumple 0:fcab3b154e49 272 #endif
Trumple 1:27b35752c5d0 273
Trumple 4:0bab12a0cc9a 274 string readings_str(data.readings.begin(), data.readings.end());
Trumple 4:0bab12a0cc9a 275 char readings[sizeof(snail::sensordata().payload)];
Trumple 4:0bab12a0cc9a 276 memcpy(readings, readings_str.c_str(), sizeof(readings));
Trumple 4:0bab12a0cc9a 277
Trumple 4:0bab12a0cc9a 278 snail::sensordata message(baseNodeAddress, data.type, data.type, time(NULL), readings, data.readings.size());
jakehodges 6:92ddc620a78f 279 messageQueue.push(message);
Trumple 4:0bab12a0cc9a 280
Trumple 0:fcab3b154e49 281 //log
Trumple 1:27b35752c5d0 282 //sd.write(static_cast<long int>(time(NULL)), data);
Trumple 0:fcab3b154e49 283 }
Trumple 0:fcab3b154e49 284 }
Trumple 1:27b35752c5d0 285 }
Trumple 0:fcab3b154e49 286 }