GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
main.cpp@1:27b35752c5d0, 2014-11-18 (annotated)
- Committer:
- Trumple
- Date:
- Tue Nov 18 18:28:52 2014 +0000
- Revision:
- 1:27b35752c5d0
- Parent:
- 0:fcab3b154e49
- Child:
- 2:1cbb20dd1733
Initial commit
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:fcab3b154e49 | 6 | |
Trumple | 0:fcab3b154e49 | 7 | #define DEBUG |
Trumple | 0:fcab3b154e49 | 8 | |
Trumple | 0:fcab3b154e49 | 9 | time_t lastPollTime = 0; |
Trumple | 0:fcab3b154e49 | 10 | time_t pollInterval = 30; |
Trumple | 1:27b35752c5d0 | 11 | time_t timestamp = 0; |
Trumple | 0:fcab3b154e49 | 12 | |
Trumple | 0:fcab3b154e49 | 13 | bool isBasenode = false; |
Trumple | 1:27b35752c5d0 | 14 | |
Trumple | 1:27b35752c5d0 | 15 | char localAddress[8]; |
Trumple | 1:27b35752c5d0 | 16 | char baseAddress[8]; |
Trumple | 1:27b35752c5d0 | 17 | bool networkParametersUpdated = false; |
Trumple | 1:27b35752c5d0 | 18 | bool networkParametersTimedOut = false; |
Trumple | 0:fcab3b154e49 | 19 | |
Trumple | 0:fcab3b154e49 | 20 | Serial pc(USBTX, USBRX); |
Trumple | 1:27b35752c5d0 | 21 | snail xbee = snail(); |
Trumple | 1:27b35752c5d0 | 22 | Ticker networkParametersTimeout; |
Trumple | 0:fcab3b154e49 | 23 | |
Trumple | 1:27b35752c5d0 | 24 | void handleBaseAddressReply(snail::message& message) |
Trumple | 1:27b35752c5d0 | 25 | { |
Trumple | 1:27b35752c5d0 | 26 | snail::baseaddressreply& reply = static_cast<snail::baseaddressreply&>(message); |
Trumple | 1:27b35752c5d0 | 27 | memcpy(baseAddress, reply.baseAddress, sizeof(baseAddress)); |
Trumple | 1:27b35752c5d0 | 28 | //TODO: Get timestamp |
Trumple | 1:27b35752c5d0 | 29 | networkParametersUpdated = true; |
Trumple | 1:27b35752c5d0 | 30 | } |
Trumple | 1:27b35752c5d0 | 31 | |
Trumple | 1:27b35752c5d0 | 32 | void handleBaseAddressRequest(snail::message& message) |
Trumple | 0:fcab3b154e49 | 33 | { |
Trumple | 1:27b35752c5d0 | 34 | if (isBasenode) |
Trumple | 1:27b35752c5d0 | 35 | { |
Trumple | 1:27b35752c5d0 | 36 | snail::baseaddressreply reply(snail::broadcastAddress, localAddress); |
Trumple | 1:27b35752c5d0 | 37 | xbee.send(reply, sizeof(reply)); |
Trumple | 1:27b35752c5d0 | 38 | } |
Trumple | 1:27b35752c5d0 | 39 | } |
Trumple | 1:27b35752c5d0 | 40 | |
Trumple | 1:27b35752c5d0 | 41 | void handleNetworkParametersTimeout() |
Trumple | 1:27b35752c5d0 | 42 | { |
Trumple | 1:27b35752c5d0 | 43 | networkParametersTimedOut = true; |
Trumple | 1:27b35752c5d0 | 44 | } |
Trumple | 1:27b35752c5d0 | 45 | |
Trumple | 1:27b35752c5d0 | 46 | void getLocalAddress() |
Trumple | 1:27b35752c5d0 | 47 | { |
Trumple | 1:27b35752c5d0 | 48 | //TODO: get local address from xbee |
Trumple | 1:27b35752c5d0 | 49 | } |
Trumple | 1:27b35752c5d0 | 50 | |
Trumple | 1:27b35752c5d0 | 51 | void getNetworkParameters() |
Trumple | 1:27b35752c5d0 | 52 | { |
Trumple | 1:27b35752c5d0 | 53 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 54 | pc.printf("[MAIN] Requesting time and base address...\r\n"); |
Trumple | 1:27b35752c5d0 | 55 | #endif |
Trumple | 0:fcab3b154e49 | 56 | |
Trumple | 1:27b35752c5d0 | 57 | xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REPLY, handleBaseAddressReply); |
Trumple | 1:27b35752c5d0 | 58 | |
Trumple | 1:27b35752c5d0 | 59 | networkParametersTimeout.attach(&handleNetworkParametersTimeout, 10.0); |
Trumple | 1:27b35752c5d0 | 60 | |
Trumple | 1:27b35752c5d0 | 61 | snail::baseaddressrequest request; |
Trumple | 1:27b35752c5d0 | 62 | xbee.send(request, sizeof(request)); |
Trumple | 0:fcab3b154e49 | 63 | |
Trumple | 1:27b35752c5d0 | 64 | while(!networkParametersUpdated) |
Trumple | 1:27b35752c5d0 | 65 | { |
Trumple | 1:27b35752c5d0 | 66 | if (networkParametersTimedOut) |
Trumple | 1:27b35752c5d0 | 67 | { |
Trumple | 1:27b35752c5d0 | 68 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 69 | pc.printf("[MAIN] Timed out, retrying...\r\n"); |
Trumple | 1:27b35752c5d0 | 70 | #endif |
Trumple | 1:27b35752c5d0 | 71 | xbee.send(request, sizeof(request)); |
Trumple | 1:27b35752c5d0 | 72 | networkParametersTimedOut = false; |
Trumple | 1:27b35752c5d0 | 73 | } |
Trumple | 1:27b35752c5d0 | 74 | xbee.readMessage(); |
Trumple | 1:27b35752c5d0 | 75 | } |
Trumple | 0:fcab3b154e49 | 76 | |
Trumple | 1:27b35752c5d0 | 77 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 78 | pc.printf("[MAIN] Got network parameters\r\n"); |
Trumple | 1:27b35752c5d0 | 79 | #endif |
Trumple | 1:27b35752c5d0 | 80 | networkParametersTimeout.detach(); |
Trumple | 0:fcab3b154e49 | 81 | } |
Trumple | 0:fcab3b154e49 | 82 | |
Trumple | 0:fcab3b154e49 | 83 | int main() |
Trumple | 0:fcab3b154e49 | 84 | { |
Trumple | 0:fcab3b154e49 | 85 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 86 | pc.printf("[MAIN] Starting up...\r\n"); |
Trumple | 0:fcab3b154e49 | 87 | printf(" . .\r\n"); |
Trumple | 0:fcab3b154e49 | 88 | printf(" / `. .' \\\r\n"); |
Trumple | 0:fcab3b154e49 | 89 | printf(" .---. < > < > .---.\r\n"); |
Trumple | 0:fcab3b154e49 | 90 | printf(" | \\ \\ - ~ ~ - / / |\r\n"); |
Trumple | 0:fcab3b154e49 | 91 | printf(" ~-..-~ ~-..-~\r\n"); |
Trumple | 0:fcab3b154e49 | 92 | printf(" \\~~~\\.' `./~~~/\r\n"); |
Trumple | 0:fcab3b154e49 | 93 | printf(" .-~~^-. \\__/ \\__/\r\n"); |
Trumple | 0:fcab3b154e49 | 94 | printf(" .' O \\ / / \\ \\\r\n"); |
Trumple | 0:fcab3b154e49 | 95 | printf("(_____, `._.' | } \\/~~~/\r\n"); |
Trumple | 0:fcab3b154e49 | 96 | printf(" `----. / } | / \\__/\r\n"); |
Trumple | 0:fcab3b154e49 | 97 | printf(" `-. | / | / `. ,~~|\r\n"); |
Trumple | 0:fcab3b154e49 | 98 | printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n"); |
Trumple | 0:fcab3b154e49 | 99 | printf(" | / | / ~-. `-. _||_||_\r\n"); |
Trumple | 0:fcab3b154e49 | 100 | printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n"); |
Trumple | 0:fcab3b154e49 | 101 | printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 102 | printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 103 | printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n"); |
Trumple | 0:fcab3b154e49 | 104 | printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 105 | printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 106 | #endif |
Trumple | 0:fcab3b154e49 | 107 | |
Trumple | 1:27b35752c5d0 | 108 | //sdcard sd = sdcard(); |
Trumple | 0:fcab3b154e49 | 109 | |
Trumple | 0:fcab3b154e49 | 110 | //TODO: read basenode pin |
Trumple | 0:fcab3b154e49 | 111 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 112 | pc.printf("[MAIN] Basenode switch: %i\r\n", isBasenode); |
Trumple | 0:fcab3b154e49 | 113 | #endif |
Trumple | 0:fcab3b154e49 | 114 | |
Trumple | 0:fcab3b154e49 | 115 | //TODO: load configuration from SD |
Trumple | 0:fcab3b154e49 | 116 | |
Trumple | 0:fcab3b154e49 | 117 | if (isBasenode) |
Trumple | 1:27b35752c5d0 | 118 | { |
Trumple | 1:27b35752c5d0 | 119 | getLocalAddress(); |
Trumple | 1:27b35752c5d0 | 120 | http h = http(); |
Trumple | 1:27b35752c5d0 | 121 | //TODO: get timestamp from API |
Trumple | 1:27b35752c5d0 | 122 | set_time(timestamp); |
Trumple | 1:27b35752c5d0 | 123 | |
Trumple | 1:27b35752c5d0 | 124 | xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REQUEST, handleBaseAddressRequest); |
Trumple | 1:27b35752c5d0 | 125 | |
Trumple | 0:fcab3b154e49 | 126 | while(1) |
Trumple | 0:fcab3b154e49 | 127 | { |
Trumple | 0:fcab3b154e49 | 128 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 129 | pc.printf("[MAIN] Basenode is idle\r\n"); |
Trumple | 0:fcab3b154e49 | 130 | #endif |
Trumple | 0:fcab3b154e49 | 131 | |
Trumple | 0:fcab3b154e49 | 132 | wait(5); |
Trumple | 0:fcab3b154e49 | 133 | } |
Trumple | 1:27b35752c5d0 | 134 | } |
Trumple | 0:fcab3b154e49 | 135 | else |
Trumple | 1:27b35752c5d0 | 136 | { |
Trumple | 1:27b35752c5d0 | 137 | sensorinterface sensors = sensorinterface(); |
Trumple | 1:27b35752c5d0 | 138 | getNetworkParameters(); |
Trumple | 1:27b35752c5d0 | 139 | set_time(timestamp); |
Trumple | 1:27b35752c5d0 | 140 | |
Trumple | 0:fcab3b154e49 | 141 | while(1) |
Trumple | 0:fcab3b154e49 | 142 | { |
Trumple | 1:27b35752c5d0 | 143 | xbee.readMessage(); |
Trumple | 1:27b35752c5d0 | 144 | //TODO: if xbee interrupt has woken us up |
Trumple | 0:fcab3b154e49 | 145 | //transmit 10 latest readings |
Trumple | 0:fcab3b154e49 | 146 | |
Trumple | 0:fcab3b154e49 | 147 | //check if it's time to poll TODO: add check to see if sensorinterface is ready |
Trumple | 0:fcab3b154e49 | 148 | if (time(NULL) - lastPollTime > 10) |
Trumple | 0:fcab3b154e49 | 149 | { |
Trumple | 0:fcab3b154e49 | 150 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 151 | pc.printf("[MAIN] Requesting data...\r\n"); |
Trumple | 0:fcab3b154e49 | 152 | #endif |
Trumple | 0:fcab3b154e49 | 153 | sensors.requestData(); |
Trumple | 0:fcab3b154e49 | 154 | lastPollTime = time(NULL); |
Trumple | 0:fcab3b154e49 | 155 | } |
Trumple | 0:fcab3b154e49 | 156 | |
Trumple | 0:fcab3b154e49 | 157 | //if there is data waiting for us... |
Trumple | 0:fcab3b154e49 | 158 | if (sensors.isDataWaiting()) |
Trumple | 0:fcab3b154e49 | 159 | { |
Trumple | 0:fcab3b154e49 | 160 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 161 | pc.printf("[MAIN] Data waiting, reading data...\r\n"); |
Trumple | 0:fcab3b154e49 | 162 | #endif |
Trumple | 1:27b35752c5d0 | 163 | |
Trumple | 0:fcab3b154e49 | 164 | d_reply data = sensors.readData(); |
Trumple | 1:27b35752c5d0 | 165 | |
Trumple | 0:fcab3b154e49 | 166 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 167 | for (int i = 0; i < data.readings.size(); i++) |
Trumple | 0:fcab3b154e49 | 168 | pc.printf("0x%.4X|", data.readings[i]); |
Trumple | 0:fcab3b154e49 | 169 | #endif |
Trumple | 1:27b35752c5d0 | 170 | |
Trumple | 0:fcab3b154e49 | 171 | //log |
Trumple | 1:27b35752c5d0 | 172 | //sd.write(static_cast<long int>(time(NULL)), data); |
Trumple | 0:fcab3b154e49 | 173 | } |
Trumple | 0:fcab3b154e49 | 174 | } |
Trumple | 1:27b35752c5d0 | 175 | } |
Trumple | 0:fcab3b154e49 | 176 | } |