GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
Trumple
Date:
Mon Dec 15 15:34:52 2014 +0000
Revision:
8:fd531fe7637b
Parent:
5:ffef0c7a70a1
Child:
9:f0030295ae9b
Include local I2C devices when requesting to join the network

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