GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Diff: main.cpp
- Revision:
- 1:27b35752c5d0
- Parent:
- 0:fcab3b154e49
- Child:
- 2:1cbb20dd1733
--- a/main.cpp Tue Nov 11 17:33:41 2014 +0000 +++ b/main.cpp Tue Nov 18 18:28:52 2014 +0000 @@ -2,28 +2,82 @@ #include "snail.h" #include "sensorinterface.h" #include "sdcard.h" +#include "http.h" #define DEBUG time_t lastPollTime = 0; time_t pollInterval = 30; +time_t timestamp = 0; bool isBasenode = false; + +char localAddress[8]; +char baseAddress[8]; +bool networkParametersUpdated = false; +bool networkParametersTimedOut = false; Serial pc(USBTX, USBRX); +snail xbee = snail(); +Ticker networkParametersTimeout; -char* getBasenodeAddress(snail* xbee) +void handleBaseAddressReply(snail::message& message) +{ + snail::baseaddressreply& reply = static_cast<snail::baseaddressreply&>(message); + memcpy(baseAddress, reply.baseAddress, sizeof(baseAddress)); + //TODO: Get timestamp + networkParametersUpdated = true; +} + +void handleBaseAddressRequest(snail::message& message) { - snail::baseaddressrequest request; - xbee->send(request); + if (isBasenode) + { + snail::baseaddressreply reply(snail::broadcastAddress, localAddress); + xbee.send(reply, sizeof(reply)); + } +} + +void handleNetworkParametersTimeout() +{ + networkParametersTimedOut = true; +} + +void getLocalAddress() +{ + //TODO: get local address from xbee +} + +void getNetworkParameters() +{ + #ifdef DEBUG + pc.printf("[MAIN] Requesting time and base address...\r\n"); + #endif - //TODO: only continue if type is base address reply - while(!xbee->isMessageWaiting()) - {} + xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REPLY, handleBaseAddressReply); + + networkParametersTimeout.attach(&handleNetworkParametersTimeout, 10.0); + + snail::baseaddressrequest request; + xbee.send(request, sizeof(request)); - //snail::message reply = xbee->readMessage(); + while(!networkParametersUpdated) + { + if (networkParametersTimedOut) + { + #ifdef DEBUG + pc.printf("[MAIN] Timed out, retrying...\r\n"); + #endif + xbee.send(request, sizeof(request)); + networkParametersTimedOut = false; + } + xbee.readMessage(); + } - return ""; + #ifdef DEBUG + pc.printf("[MAIN] Got network parameters\r\n"); + #endif + networkParametersTimeout.detach(); } int main() @@ -51,28 +105,24 @@ printf("\r\n"); #endif - sensorinterface sensors = sensorinterface(); - sdcard sd = sdcard(); - snail xbee = snail(); + //sdcard sd = sdcard(); //TODO: read basenode pin #ifdef DEBUG pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode); #endif - time_t timestamp = 0; - //if (isBasenode) - //TODO: get timestamp from API - //TODO: be ready to broadcast as the basenode - //else - //TODO: request timestamp from base node - //TODO: find out who basenode is - - set_time(timestamp); - //TODO: load configuration from SD if (isBasenode) + { + getLocalAddress(); + http h = http(); + //TODO: get timestamp from API + set_time(timestamp); + + xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REQUEST, handleBaseAddressRequest); + while(1) { #ifdef DEBUG @@ -81,10 +131,17 @@ wait(5); } + } else + { + sensorinterface sensors = sensorinterface(); + getNetworkParameters(); + set_time(timestamp); + while(1) { - //if xbee interrupt has woken us up + xbee.readMessage(); + //TODO: if xbee interrupt has woken us up //transmit 10 latest readings //check if it's time to poll TODO: add check to see if sensorinterface is ready @@ -103,14 +160,17 @@ #ifdef DEBUG pc.printf("[MAIN] Data waiting, reading data...\r\n"); #endif + d_reply data = sensors.readData(); + #ifdef DEBUG for (int i = 0; i < data.readings.size(); i++) pc.printf("0x%.4X|", data.readings[i]); #endif + //log - sd.write(static_cast<long int>(time(NULL)), data); + //sd.write(static_cast<long int>(time(NULL)), data); } - } + } } \ No newline at end of file