GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
jakehodges
Date:
Mon Dec 15 00:19:48 2014 +0000
Revision:
3:71b090ec5c69
Parent:
2:1cbb20dd1733
Child:
4:0bab12a0cc9a
Added functionality to vary reading size depending on sensor type.

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
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 2:1cbb20dd1733 54 // MbedJSONValue j;
Trumple 2:1cbb20dd1733 55 // j["timestamp"] = (int)time(NULL);
Trumple 2:1cbb20dd1733 56 // j["value"] = value;
Trumple 2:1cbb20dd1733 57 // pc.printf(h.post("178.62.84.55", 8888, "/field/testfield/testnode/testsensor/", j.serialize()).c_str());
Trumple 2:1cbb20dd1733 58 }
Trumple 2:1cbb20dd1733 59
Trumple 1:27b35752c5d0 60 void handleNetworkParametersTimeout()
Trumple 1:27b35752c5d0 61 {
Trumple 1:27b35752c5d0 62 networkParametersTimedOut = true;
Trumple 1:27b35752c5d0 63 }
Trumple 1:27b35752c5d0 64
Trumple 1:27b35752c5d0 65 void getLocalAddress()
Trumple 1:27b35752c5d0 66 {
Trumple 2:1cbb20dd1733 67 pc.printf("[MAIN] Requesting lower address bits...\r\n");
Trumple 2:1cbb20dd1733 68 xbee.ATCommand("SL");
Trumple 2:1cbb20dd1733 69
Trumple 2:1cbb20dd1733 70 while (!xbee.isATCommandResponseWaiting())
Trumple 2:1cbb20dd1733 71 xbee.processIncomingData();
Trumple 2:1cbb20dd1733 72
Trumple 2:1cbb20dd1733 73 snail::atcommandresponse SLr = xbee.getATCommandResponse();
Trumple 2:1cbb20dd1733 74
Trumple 2:1cbb20dd1733 75 pc.printf("[MAIN] Requesting upper address bits...\r\n");
Trumple 2:1cbb20dd1733 76 xbee.ATCommand("SH");
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 SHr = xbee.getATCommandResponse();
Trumple 2:1cbb20dd1733 82
Trumple 2:1cbb20dd1733 83 string address(SHr.response);
Trumple 2:1cbb20dd1733 84 address += SLr.response;
Trumple 2:1cbb20dd1733 85
Trumple 2:1cbb20dd1733 86 pc.printf("[MAIN] Got local address: ");
Trumple 2:1cbb20dd1733 87
Trumple 2:1cbb20dd1733 88 for (int i = 0; i < address.size(); i++)
Trumple 2:1cbb20dd1733 89 pc.printf("%.2X", address[i]);
Trumple 2:1cbb20dd1733 90
Trumple 2:1cbb20dd1733 91 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 92
Trumple 2:1cbb20dd1733 93 memcpy(localAddress, address.c_str(), sizeof(localAddress));
Trumple 1:27b35752c5d0 94 }
Trumple 1:27b35752c5d0 95
Trumple 1:27b35752c5d0 96 void getNetworkParameters()
Trumple 1:27b35752c5d0 97 {
Trumple 1:27b35752c5d0 98 #ifdef DEBUG
Trumple 2:1cbb20dd1733 99 pc.printf("[MAIN] Requesting to join network...\r\n");
Trumple 1:27b35752c5d0 100 #endif
Trumple 0:fcab3b154e49 101
Trumple 2:1cbb20dd1733 102 xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REPLY, handleJoinNetworkReply);
Trumple 2:1cbb20dd1733 103
Trumple 2:1cbb20dd1733 104 networkParametersTimeout.attach(&handleNetworkParametersTimeout, 30.0);
Trumple 1:27b35752c5d0 105
Trumple 2:1cbb20dd1733 106 snail::joinnetworkrequest::sensor sensors[255];
Trumple 2:1cbb20dd1733 107 snail::joinnetworkrequest request(sensors);
Trumple 1:27b35752c5d0 108
Trumple 1:27b35752c5d0 109 xbee.send(request, sizeof(request));
Trumple 0:fcab3b154e49 110
Trumple 1:27b35752c5d0 111 while(!networkParametersUpdated)
Trumple 1:27b35752c5d0 112 {
Trumple 1:27b35752c5d0 113 if (networkParametersTimedOut)
Trumple 1:27b35752c5d0 114 {
Trumple 1:27b35752c5d0 115 #ifdef DEBUG
Trumple 1:27b35752c5d0 116 pc.printf("[MAIN] Timed out, retrying...\r\n");
Trumple 1:27b35752c5d0 117 #endif
Trumple 1:27b35752c5d0 118 xbee.send(request, sizeof(request));
Trumple 1:27b35752c5d0 119 networkParametersTimedOut = false;
Trumple 1:27b35752c5d0 120 }
Trumple 2:1cbb20dd1733 121 xbee.processIncomingData();
Trumple 1:27b35752c5d0 122 }
Trumple 0:fcab3b154e49 123
Trumple 1:27b35752c5d0 124 #ifdef DEBUG
Trumple 2:1cbb20dd1733 125 pc.printf("[MAIN] Got network parameters. Time: %i, base node address: ", time(NULL));
Trumple 2:1cbb20dd1733 126
Trumple 2:1cbb20dd1733 127 for (int i = 0; i < sizeof(baseNodeAddress); i++)
Trumple 2:1cbb20dd1733 128 pc.printf("%.2X", baseNodeAddress[i]);
Trumple 2:1cbb20dd1733 129 pc.printf("\r\n");
Trumple 1:27b35752c5d0 130 #endif
Trumple 1:27b35752c5d0 131 networkParametersTimeout.detach();
Trumple 0:fcab3b154e49 132 }
Trumple 0:fcab3b154e49 133
Trumple 0:fcab3b154e49 134 int main()
Trumple 0:fcab3b154e49 135 {
Trumple 0:fcab3b154e49 136 #ifdef DEBUG
Trumple 0:fcab3b154e49 137 pc.printf("[MAIN] Starting up...\r\n");
Trumple 2:1cbb20dd1733 138 pc.printf(" . .\r\n");
Trumple 2:1cbb20dd1733 139 pc.printf(" / `. .' \\\r\n");
Trumple 2:1cbb20dd1733 140 pc.printf(" .---. < > < > .---.\r\n");
Trumple 2:1cbb20dd1733 141 pc.printf(" | \\ \\ - ~ ~ - / / |\r\n");
Trumple 2:1cbb20dd1733 142 pc.printf(" ~-..-~ ~-..-~\r\n");
Trumple 2:1cbb20dd1733 143 pc.printf(" \\~~~\\.' `./~~~/\r\n");
Trumple 2:1cbb20dd1733 144 pc.printf(" .-~~^-. \\__/ \\__/\r\n");
Trumple 2:1cbb20dd1733 145 pc.printf(" .' O \\ / / \\ \\\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(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
Trumple 2:1cbb20dd1733 150 pc.printf(" | / | / ~-. `-. _||_||_\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(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
Trumple 2:1cbb20dd1733 155 pc.printf("\r\n");
Trumple 2:1cbb20dd1733 156 pc.printf("\r\n");
Trumple 0:fcab3b154e49 157 #endif
Trumple 1:27b35752c5d0 158 //sdcard sd = sdcard();
Trumple 0:fcab3b154e49 159
Trumple 0:fcab3b154e49 160 //TODO: read basenode pin
Trumple 0:fcab3b154e49 161 #ifdef DEBUG
Trumple 0:fcab3b154e49 162 pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode);
Trumple 0:fcab3b154e49 163 #endif
Trumple 0:fcab3b154e49 164
Trumple 0:fcab3b154e49 165 //TODO: load configuration from SD
Trumple 0:fcab3b154e49 166
Trumple 2:1cbb20dd1733 167 getLocalAddress();
Trumple 2:1cbb20dd1733 168
Trumple 0:fcab3b154e49 169 if (isBasenode)
Trumple 1:27b35752c5d0 170 {
Trumple 1:27b35752c5d0 171 getLocalAddress();
Trumple 2:1cbb20dd1733 172 h.connect();
Trumple 2:1cbb20dd1733 173
Trumple 2:1cbb20dd1733 174 string timestampStr = h.get("time.bitnode.co.uk", 80, "/");
Trumple 2:1cbb20dd1733 175 time_t timestamp = atoi(timestampStr.c_str());
Trumple 2:1cbb20dd1733 176
Trumple 1:27b35752c5d0 177 set_time(timestamp);
Trumple 1:27b35752c5d0 178
Trumple 2:1cbb20dd1733 179 pc.printf("[MAIN] Got time: %i\r\n", timestamp);
Trumple 2:1cbb20dd1733 180
Trumple 2:1cbb20dd1733 181 xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REQUEST, handleJoinNetworkRequest);
Trumple 2:1cbb20dd1733 182 xbee.registerMessageCallback(MESSAGE_SENSOR_DATA, handleSensorData);
Trumple 1:27b35752c5d0 183
Trumple 0:fcab3b154e49 184 while(1)
Trumple 0:fcab3b154e49 185 {
Trumple 0:fcab3b154e49 186 #ifdef DEBUG
Trumple 0:fcab3b154e49 187 pc.printf("[MAIN] Basenode is idle\r\n");
Trumple 0:fcab3b154e49 188 #endif
Trumple 0:fcab3b154e49 189
Trumple 2:1cbb20dd1733 190 wait(1);
Trumple 2:1cbb20dd1733 191 xbee.processIncomingData();
Trumple 0:fcab3b154e49 192 }
Trumple 1:27b35752c5d0 193 }
Trumple 0:fcab3b154e49 194 else
Trumple 1:27b35752c5d0 195 {
Trumple 1:27b35752c5d0 196 sensorinterface sensors = sensorinterface();
Trumple 1:27b35752c5d0 197 getNetworkParameters();
Trumple 1:27b35752c5d0 198
Trumple 0:fcab3b154e49 199 while(1)
Trumple 0:fcab3b154e49 200 {
Trumple 2:1cbb20dd1733 201 xbee.processIncomingData();
Trumple 1:27b35752c5d0 202 //TODO: if xbee interrupt has woken us up
Trumple 0:fcab3b154e49 203 //transmit 10 latest readings
Trumple 0:fcab3b154e49 204
Trumple 0:fcab3b154e49 205 //check if it's time to poll TODO: add check to see if sensorinterface is ready
Trumple 2:1cbb20dd1733 206 if (time(NULL) - lastPollTime > pollInterval)
Trumple 0:fcab3b154e49 207 {
Trumple 0:fcab3b154e49 208 #ifdef DEBUG
Trumple 0:fcab3b154e49 209 pc.printf("[MAIN] Requesting data...\r\n");
Trumple 0:fcab3b154e49 210 #endif
Trumple 0:fcab3b154e49 211 sensors.requestData();
Trumple 0:fcab3b154e49 212 lastPollTime = time(NULL);
Trumple 0:fcab3b154e49 213 }
Trumple 0:fcab3b154e49 214
Trumple 0:fcab3b154e49 215 //if there is data waiting for us...
Trumple 0:fcab3b154e49 216 if (sensors.isDataWaiting())
Trumple 0:fcab3b154e49 217 {
Trumple 0:fcab3b154e49 218 #ifdef DEBUG
Trumple 0:fcab3b154e49 219 pc.printf("[MAIN] Data waiting, reading data...\r\n");
Trumple 0:fcab3b154e49 220 #endif
Trumple 1:27b35752c5d0 221
Trumple 0:fcab3b154e49 222 d_reply data = sensors.readData();
Trumple 1:27b35752c5d0 223
Trumple 0:fcab3b154e49 224 #ifdef DEBUG
Trumple 2:1cbb20dd1733 225 for (int i = 0; i < data.readings.size(); i++)
jakehodges 3:71b090ec5c69 226 pc.printf("0x%.2X|", data.readings[i]);
Trumple 0:fcab3b154e49 227 #endif
Trumple 1:27b35752c5d0 228
Trumple 0:fcab3b154e49 229 //log
Trumple 1:27b35752c5d0 230 //sd.write(static_cast<long int>(time(NULL)), data);
Trumple 0:fcab3b154e49 231 }
Trumple 0:fcab3b154e49 232 }
Trumple 1:27b35752c5d0 233 }
Trumple 0:fcab3b154e49 234 }