GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
jakehodges
Date:
Mon Dec 15 15:05:08 2014 +0000
Revision:
7:5ae1d99d2b69
Parent:
6:92ddc620a78f
Parent:
5:ffef0c7a70a1
Child:
9:f0030295ae9b
Merge 5 and 6

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