GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
main.cpp@27:61e67ab47da5, 2015-01-28 (annotated)
- Committer:
- Trumple
- Date:
- Wed Jan 28 20:40:15 2015 +0000
- Revision:
- 27:61e67ab47da5
- Parent:
- 26:172b585fbb10
- Child:
- 28:9c8d472c44b7
Add check for presence of Ethernet connection to determine node type
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 | 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 | |
jakehodges | 23:b57a47c7862a | 15 | bool isBasenode = false; |
Trumple | 10:13176e7bd4c8 | 16 | string fieldID = "testfield"; |
Trumple | 10:13176e7bd4c8 | 17 | |
Trumple | 2:1cbb20dd1733 | 18 | http h = http(); |
Trumple | 8:fd531fe7637b | 19 | sensorinterface* sif; |
Trumple | 8:fd531fe7637b | 20 | |
Trumple | 1:27b35752c5d0 | 21 | char localAddress[8]; |
Trumple | 2:1cbb20dd1733 | 22 | char baseNodeAddress[8]; |
Trumple | 1:27b35752c5d0 | 23 | bool networkParametersUpdated = false; |
Trumple | 1:27b35752c5d0 | 24 | bool networkParametersTimedOut = false; |
Trumple | 0:fcab3b154e49 | 25 | |
Trumple | 0:fcab3b154e49 | 26 | Serial pc(USBTX, USBRX); |
Trumple | 1:27b35752c5d0 | 27 | snail xbee = snail(); |
Trumple | 1:27b35752c5d0 | 28 | Ticker networkParametersTimeout; |
Trumple | 0:fcab3b154e49 | 29 | |
jakehodges | 6:92ddc620a78f | 30 | queue<snail::sensordata> messageQueue; |
jakehodges | 6:92ddc620a78f | 31 | |
Trumple | 25:fe273397ebb2 | 32 | //commissioning button |
Trumple | 25:fe273397ebb2 | 33 | DigitalOut commissioningOutput(p30); |
Trumple | 25:fe273397ebb2 | 34 | InterruptIn commissioningInput(p16); |
Trumple | 25:fe273397ebb2 | 35 | |
Trumple | 10:13176e7bd4c8 | 36 | string addressToString(char address[8]) |
Trumple | 10:13176e7bd4c8 | 37 | { |
Trumple | 10:13176e7bd4c8 | 38 | string address_str; |
Trumple | 10:13176e7bd4c8 | 39 | for(int i = 0; i < 8; i++) |
Trumple | 10:13176e7bd4c8 | 40 | { |
Trumple | 10:13176e7bd4c8 | 41 | char addressBuffer[2]; |
Trumple | 10:13176e7bd4c8 | 42 | sprintf(addressBuffer, "%.2X", address[i]); |
Trumple | 10:13176e7bd4c8 | 43 | address_str += string(addressBuffer, 2); |
Trumple | 10:13176e7bd4c8 | 44 | } |
Trumple | 10:13176e7bd4c8 | 45 | |
Trumple | 10:13176e7bd4c8 | 46 | return address_str; |
Trumple | 10:13176e7bd4c8 | 47 | } |
Trumple | 10:13176e7bd4c8 | 48 | |
Trumple | 2:1cbb20dd1733 | 49 | void handleJoinNetworkReply(snail::message& message) |
Trumple | 1:27b35752c5d0 | 50 | { |
Trumple | 2:1cbb20dd1733 | 51 | pc.printf("[MAIN] Got join network reply...\r\n"); |
Trumple | 2:1cbb20dd1733 | 52 | snail::joinnetworkreply& reply = static_cast<snail::joinnetworkreply&>(message); |
Trumple | 2:1cbb20dd1733 | 53 | set_time(reply.timestamp); |
Trumple | 2:1cbb20dd1733 | 54 | memcpy(baseNodeAddress, reply.baseNodeAddress, sizeof(baseNodeAddress)); |
Trumple | 2:1cbb20dd1733 | 55 | |
Trumple | 1:27b35752c5d0 | 56 | networkParametersUpdated = true; |
Trumple | 1:27b35752c5d0 | 57 | } |
Trumple | 1:27b35752c5d0 | 58 | |
Trumple | 2:1cbb20dd1733 | 59 | void handleJoinNetworkRequest(snail::message& message) |
Trumple | 0:fcab3b154e49 | 60 | { |
Trumple | 2:1cbb20dd1733 | 61 | pc.printf("[MAIN] Got join network request...\r\n"); |
Trumple | 1:27b35752c5d0 | 62 | if (isBasenode) |
Trumple | 1:27b35752c5d0 | 63 | { |
Trumple | 2:1cbb20dd1733 | 64 | snail::joinnetworkrequest& request = static_cast<snail::joinnetworkrequest&>(message); |
Trumple | 2:1cbb20dd1733 | 65 | |
Trumple | 10:13176e7bd4c8 | 66 | //report connected sensors to server |
Trumple | 10:13176e7bd4c8 | 67 | MbedJSONValue j; |
Trumple | 10:13176e7bd4c8 | 68 | j["sensors"]; |
Trumple | 10:13176e7bd4c8 | 69 | for (int i = 0; i < request.sensorCount; i++) |
Trumple | 10:13176e7bd4c8 | 70 | { |
Trumple | 10:13176e7bd4c8 | 71 | char sensorIDBuffer[3]; |
Trumple | 10:13176e7bd4c8 | 72 | sprintf(sensorIDBuffer, "%i", request.sensors[i].id); |
Trumple | 10:13176e7bd4c8 | 73 | char fBuffer[3]; |
Trumple | 10:13176e7bd4c8 | 74 | sprintf(fBuffer, "%i", request.sensors[i].type); |
Trumple | 10:13176e7bd4c8 | 75 | j["sensors"][i]["sensorID"] = string(sensorIDBuffer); |
Trumple | 10:13176e7bd4c8 | 76 | j["sensors"][i]["sensortype"] = string(fBuffer); |
Trumple | 10:13176e7bd4c8 | 77 | } |
Trumple | 10:13176e7bd4c8 | 78 | |
Trumple | 10:13176e7bd4c8 | 79 | string address = addressToString(request.source); |
Trumple | 10:13176e7bd4c8 | 80 | #ifdef DEBUG |
Trumple | 10:13176e7bd4c8 | 81 | pc.printf( ("[MAIN] POSTing startup information: " + j.serialize() + "\r\n").c_str() ); |
Trumple | 10:13176e7bd4c8 | 82 | #endif |
Trumple | 10:13176e7bd4c8 | 83 | h.post("178.62.84.55", 8888, "/field/" + fieldID + "/" + address + "/startup/", j.serialize()).c_str(); |
Trumple | 10:13176e7bd4c8 | 84 | |
Trumple | 2:1cbb20dd1733 | 85 | snail::joinnetworkreply reply(request.source, time(NULL), localAddress); |
Trumple | 1:27b35752c5d0 | 86 | xbee.send(reply, sizeof(reply)); |
Trumple | 1:27b35752c5d0 | 87 | } |
Trumple | 1:27b35752c5d0 | 88 | } |
Trumple | 1:27b35752c5d0 | 89 | |
Trumple | 2:1cbb20dd1733 | 90 | void handleSensorData(snail::message& message) |
Trumple | 2:1cbb20dd1733 | 91 | { |
Trumple | 2:1cbb20dd1733 | 92 | pc.printf("[MAIN] Got sensor data...\r\n"); |
Trumple | 2:1cbb20dd1733 | 93 | snail::sensordata& d = static_cast<snail::sensordata&>(message); |
Trumple | 2:1cbb20dd1733 | 94 | |
Trumple | 10:13176e7bd4c8 | 95 | char sensorID[3]; |
Trumple | 10:13176e7bd4c8 | 96 | sprintf(sensorID, "%i", d.i2cAddress); |
Trumple | 10:13176e7bd4c8 | 97 | |
Trumple | 4:0bab12a0cc9a | 98 | MbedJSONValue j; |
Trumple | 4:0bab12a0cc9a | 99 | j["timestamp"] = (int)time(NULL); |
jakehodges | 23:b57a47c7862a | 100 | |
jakehodges | 23:b57a47c7862a | 101 | for (int i = 0; i < d.payloadSize; i++) |
jakehodges | 23:b57a47c7862a | 102 | j["value"][i] = d.payload[i]; |
Trumple | 10:13176e7bd4c8 | 103 | |
Trumple | 10:13176e7bd4c8 | 104 | #ifdef DEBUG |
Trumple | 10:13176e7bd4c8 | 105 | pc.printf( ("[MAIN] POSTing sensor reading: " + j.serialize() + "\r\n").c_str() ); |
Trumple | 10:13176e7bd4c8 | 106 | #endif |
Trumple | 10:13176e7bd4c8 | 107 | |
Trumple | 10:13176e7bd4c8 | 108 | h.post("178.62.84.55", 8888, "/field/" + fieldID + "/" + addressToString(d.source) + "/" + sensorID + "/", j.serialize()).c_str(); |
Trumple | 2:1cbb20dd1733 | 109 | } |
Trumple | 2:1cbb20dd1733 | 110 | |
Trumple | 1:27b35752c5d0 | 111 | void handleNetworkParametersTimeout() |
Trumple | 1:27b35752c5d0 | 112 | { |
Trumple | 1:27b35752c5d0 | 113 | networkParametersTimedOut = true; |
Trumple | 1:27b35752c5d0 | 114 | } |
Trumple | 1:27b35752c5d0 | 115 | |
Trumple | 1:27b35752c5d0 | 116 | void getLocalAddress() |
Trumple | 1:27b35752c5d0 | 117 | { |
Trumple | 2:1cbb20dd1733 | 118 | pc.printf("[MAIN] Requesting lower address bits...\r\n"); |
Trumple | 2:1cbb20dd1733 | 119 | xbee.ATCommand("SL"); |
Trumple | 2:1cbb20dd1733 | 120 | |
Trumple | 2:1cbb20dd1733 | 121 | while (!xbee.isATCommandResponseWaiting()) |
Trumple | 2:1cbb20dd1733 | 122 | xbee.processIncomingData(); |
Trumple | 2:1cbb20dd1733 | 123 | |
Trumple | 2:1cbb20dd1733 | 124 | snail::atcommandresponse SLr = xbee.getATCommandResponse(); |
Trumple | 2:1cbb20dd1733 | 125 | |
Trumple | 2:1cbb20dd1733 | 126 | pc.printf("[MAIN] Requesting upper address bits...\r\n"); |
Trumple | 2:1cbb20dd1733 | 127 | xbee.ATCommand("SH"); |
Trumple | 2:1cbb20dd1733 | 128 | |
Trumple | 2:1cbb20dd1733 | 129 | while (!xbee.isATCommandResponseWaiting()) |
Trumple | 2:1cbb20dd1733 | 130 | xbee.processIncomingData(); |
Trumple | 2:1cbb20dd1733 | 131 | |
Trumple | 2:1cbb20dd1733 | 132 | snail::atcommandresponse SHr = xbee.getATCommandResponse(); |
Trumple | 2:1cbb20dd1733 | 133 | |
Trumple | 2:1cbb20dd1733 | 134 | string address(SHr.response); |
Trumple | 2:1cbb20dd1733 | 135 | address += SLr.response; |
Trumple | 2:1cbb20dd1733 | 136 | |
Trumple | 2:1cbb20dd1733 | 137 | pc.printf("[MAIN] Got local address: "); |
Trumple | 2:1cbb20dd1733 | 138 | |
Trumple | 2:1cbb20dd1733 | 139 | for (int i = 0; i < address.size(); i++) |
Trumple | 2:1cbb20dd1733 | 140 | pc.printf("%.2X", address[i]); |
Trumple | 2:1cbb20dd1733 | 141 | |
Trumple | 2:1cbb20dd1733 | 142 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 143 | |
Trumple | 2:1cbb20dd1733 | 144 | memcpy(localAddress, address.c_str(), sizeof(localAddress)); |
Trumple | 1:27b35752c5d0 | 145 | } |
Trumple | 1:27b35752c5d0 | 146 | |
Trumple | 1:27b35752c5d0 | 147 | void getNetworkParameters() |
Trumple | 1:27b35752c5d0 | 148 | { |
Trumple | 1:27b35752c5d0 | 149 | #ifdef DEBUG |
Trumple | 2:1cbb20dd1733 | 150 | pc.printf("[MAIN] Requesting to join network...\r\n"); |
Trumple | 1:27b35752c5d0 | 151 | #endif |
Trumple | 0:fcab3b154e49 | 152 | |
Trumple | 8:fd531fe7637b | 153 | //prepare for response from basenode |
Trumple | 2:1cbb20dd1733 | 154 | xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REPLY, handleJoinNetworkReply); |
Trumple | 2:1cbb20dd1733 | 155 | |
Trumple | 8:fd531fe7637b | 156 | //handle retrying after timeout |
Trumple | 2:1cbb20dd1733 | 157 | networkParametersTimeout.attach(&handleNetworkParametersTimeout, 30.0); |
Trumple | 1:27b35752c5d0 | 158 | |
Trumple | 8:fd531fe7637b | 159 | //generate list of sensors attached |
Trumple | 8:fd531fe7637b | 160 | snail::joinnetworkrequest::sensor localSensors[32]; |
Trumple | 8:fd531fe7637b | 161 | map<char, sensor>::iterator cs = sif->sensors.begin(); |
Trumple | 8:fd531fe7637b | 162 | int i = 0; |
Trumple | 8:fd531fe7637b | 163 | |
Trumple | 8:fd531fe7637b | 164 | //either loop until we reach the end of the sensors or until we hit the max |
Trumple | 8:fd531fe7637b | 165 | while (cs != sif->sensors.end() && i < 32) |
Trumple | 8:fd531fe7637b | 166 | { |
Trumple | 8:fd531fe7637b | 167 | localSensors[i].type = cs->second.type; |
jakehodges | 23:b57a47c7862a | 168 | localSensors[i].id = cs->second.type; |
Trumple | 8:fd531fe7637b | 169 | i++; |
Trumple | 8:fd531fe7637b | 170 | cs++; |
Trumple | 8:fd531fe7637b | 171 | } |
Trumple | 8:fd531fe7637b | 172 | |
Trumple | 8:fd531fe7637b | 173 | #ifdef DEBUG |
Trumple | 8:fd531fe7637b | 174 | pc.printf("[MAIN] Informing basenode of %i attached devices\r\n", i); |
Trumple | 8:fd531fe7637b | 175 | #endif |
Trumple | 8:fd531fe7637b | 176 | |
Trumple | 8:fd531fe7637b | 177 | //construct joinnetworkrequest, including the number of sensors attached |
Trumple | 8:fd531fe7637b | 178 | snail::joinnetworkrequest request(localSensors, i); |
Trumple | 1:27b35752c5d0 | 179 | |
Trumple | 1:27b35752c5d0 | 180 | xbee.send(request, sizeof(request)); |
Trumple | 0:fcab3b154e49 | 181 | |
Trumple | 1:27b35752c5d0 | 182 | while(!networkParametersUpdated) |
Trumple | 1:27b35752c5d0 | 183 | { |
Trumple | 1:27b35752c5d0 | 184 | if (networkParametersTimedOut) |
Trumple | 1:27b35752c5d0 | 185 | { |
Trumple | 1:27b35752c5d0 | 186 | #ifdef DEBUG |
Trumple | 1:27b35752c5d0 | 187 | pc.printf("[MAIN] Timed out, retrying...\r\n"); |
Trumple | 1:27b35752c5d0 | 188 | #endif |
Trumple | 1:27b35752c5d0 | 189 | xbee.send(request, sizeof(request)); |
Trumple | 1:27b35752c5d0 | 190 | networkParametersTimedOut = false; |
Trumple | 1:27b35752c5d0 | 191 | } |
Trumple | 2:1cbb20dd1733 | 192 | xbee.processIncomingData(); |
Trumple | 1:27b35752c5d0 | 193 | } |
Trumple | 0:fcab3b154e49 | 194 | |
Trumple | 1:27b35752c5d0 | 195 | #ifdef DEBUG |
Trumple | 2:1cbb20dd1733 | 196 | pc.printf("[MAIN] Got network parameters. Time: %i, base node address: ", time(NULL)); |
Trumple | 2:1cbb20dd1733 | 197 | |
Trumple | 2:1cbb20dd1733 | 198 | for (int i = 0; i < sizeof(baseNodeAddress); i++) |
Trumple | 2:1cbb20dd1733 | 199 | pc.printf("%.2X", baseNodeAddress[i]); |
Trumple | 2:1cbb20dd1733 | 200 | pc.printf("\r\n"); |
Trumple | 1:27b35752c5d0 | 201 | #endif |
Trumple | 8:fd531fe7637b | 202 | //no longer need to handle timeout |
Trumple | 1:27b35752c5d0 | 203 | networkParametersTimeout.detach(); |
Trumple | 0:fcab3b154e49 | 204 | } |
Trumple | 0:fcab3b154e49 | 205 | |
Trumple | 25:fe273397ebb2 | 206 | void commissioningFall() |
Trumple | 25:fe273397ebb2 | 207 | { |
Trumple | 25:fe273397ebb2 | 208 | commissioningOutput = 0; |
Trumple | 25:fe273397ebb2 | 209 | } |
Trumple | 25:fe273397ebb2 | 210 | |
Trumple | 25:fe273397ebb2 | 211 | void commissioningRise() |
Trumple | 25:fe273397ebb2 | 212 | { |
Trumple | 25:fe273397ebb2 | 213 | commissioningOutput = 1; |
Trumple | 25:fe273397ebb2 | 214 | } |
Trumple | 25:fe273397ebb2 | 215 | |
Trumple | 0:fcab3b154e49 | 216 | int main() |
Trumple | 0:fcab3b154e49 | 217 | { |
Trumple | 0:fcab3b154e49 | 218 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 219 | pc.printf("[MAIN] Starting up...\r\n"); |
Trumple | 2:1cbb20dd1733 | 220 | pc.printf(" . .\r\n"); |
Trumple | 2:1cbb20dd1733 | 221 | pc.printf(" / `. .' \\\r\n"); |
Trumple | 2:1cbb20dd1733 | 222 | pc.printf(" .---. < > < > .---.\r\n"); |
Trumple | 2:1cbb20dd1733 | 223 | pc.printf(" | \\ \\ - ~ ~ - / / |\r\n"); |
Trumple | 2:1cbb20dd1733 | 224 | pc.printf(" ~-..-~ ~-..-~\r\n"); |
Trumple | 2:1cbb20dd1733 | 225 | pc.printf(" \\~~~\\.' `./~~~/\r\n"); |
Trumple | 2:1cbb20dd1733 | 226 | pc.printf(" .-~~^-. \\__/ \\__/\r\n"); |
Trumple | 2:1cbb20dd1733 | 227 | pc.printf(" .' O \\ / / \\ \\\r\n"); |
Trumple | 2:1cbb20dd1733 | 228 | pc.printf("(_____, `._.' | } \\/~~~/\r\n"); |
Trumple | 2:1cbb20dd1733 | 229 | pc.printf(" `----. / } | / \\__/\r\n"); |
Trumple | 2:1cbb20dd1733 | 230 | pc.printf(" `-. | / | / `. ,~~|\r\n"); |
Trumple | 2:1cbb20dd1733 | 231 | pc.printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n"); |
Trumple | 2:1cbb20dd1733 | 232 | pc.printf(" | / | / ~-. `-. _||_||_\r\n"); |
Trumple | 2:1cbb20dd1733 | 233 | pc.printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n"); |
Trumple | 2:1cbb20dd1733 | 234 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 235 | pc.printf("\r\n"); |
Trumple | 26:172b585fbb10 | 236 | pc.printf(" Sensorsaurus | Team 24 GDP | Southampton | 2015\r\n"); |
Trumple | 2:1cbb20dd1733 | 237 | pc.printf("\r\n"); |
Trumple | 2:1cbb20dd1733 | 238 | pc.printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 239 | #endif |
jakehodges | 23:b57a47c7862a | 240 | sdcard sd = sdcard(); |
Trumple | 27:61e67ab47da5 | 241 | h.connect(); |
Trumple | 0:fcab3b154e49 | 242 | |
Trumple | 25:fe273397ebb2 | 243 | //commissioning button |
Trumple | 25:fe273397ebb2 | 244 | commissioningInput.fall(commissioningFall); |
Trumple | 25:fe273397ebb2 | 245 | commissioningInput.rise(commissioningRise); |
Trumple | 25:fe273397ebb2 | 246 | |
Trumple | 25:fe273397ebb2 | 247 | //check if local node is basenode |
Trumple | 27:61e67ab47da5 | 248 | isBasenode = h.isEthernetConnected(); |
Trumple | 0:fcab3b154e49 | 249 | #ifdef DEBUG |
Trumple | 25:fe273397ebb2 | 250 | pc.printf("[MAIN] Basenode status: %i\r\n", isBasenode); |
Trumple | 0:fcab3b154e49 | 251 | #endif |
Trumple | 0:fcab3b154e49 | 252 | |
Trumple | 2:1cbb20dd1733 | 253 | getLocalAddress(); |
Trumple | 2:1cbb20dd1733 | 254 | |
Trumple | 0:fcab3b154e49 | 255 | if (isBasenode) |
Trumple | 27:61e67ab47da5 | 256 | { |
Trumple | 2:1cbb20dd1733 | 257 | string timestampStr = h.get("time.bitnode.co.uk", 80, "/"); |
Trumple | 2:1cbb20dd1733 | 258 | time_t timestamp = atoi(timestampStr.c_str()); |
Trumple | 2:1cbb20dd1733 | 259 | |
Trumple | 1:27b35752c5d0 | 260 | set_time(timestamp); |
Trumple | 1:27b35752c5d0 | 261 | |
Trumple | 2:1cbb20dd1733 | 262 | pc.printf("[MAIN] Got time: %i\r\n", timestamp); |
Trumple | 2:1cbb20dd1733 | 263 | |
Trumple | 2:1cbb20dd1733 | 264 | xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REQUEST, handleJoinNetworkRequest); |
Trumple | 2:1cbb20dd1733 | 265 | xbee.registerMessageCallback(MESSAGE_SENSOR_DATA, handleSensorData); |
Trumple | 1:27b35752c5d0 | 266 | |
Trumple | 0:fcab3b154e49 | 267 | while(1) |
Trumple | 0:fcab3b154e49 | 268 | { |
Trumple | 0:fcab3b154e49 | 269 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 270 | pc.printf("[MAIN] Basenode is idle\r\n"); |
Trumple | 0:fcab3b154e49 | 271 | #endif |
Trumple | 0:fcab3b154e49 | 272 | |
Trumple | 2:1cbb20dd1733 | 273 | wait(1); |
Trumple | 2:1cbb20dd1733 | 274 | xbee.processIncomingData(); |
Trumple | 0:fcab3b154e49 | 275 | } |
Trumple | 1:27b35752c5d0 | 276 | } |
Trumple | 0:fcab3b154e49 | 277 | else |
Trumple | 8:fd531fe7637b | 278 | { |
Trumple | 1:27b35752c5d0 | 279 | sensorinterface sensors = sensorinterface(); |
Trumple | 8:fd531fe7637b | 280 | sif = &sensors; |
Trumple | 1:27b35752c5d0 | 281 | getNetworkParameters(); |
Trumple | 1:27b35752c5d0 | 282 | |
Trumple | 0:fcab3b154e49 | 283 | while(1) |
Trumple | 0:fcab3b154e49 | 284 | { |
Trumple | 2:1cbb20dd1733 | 285 | xbee.processIncomingData(); |
jakehodges | 6:92ddc620a78f | 286 | |
jakehodges | 6:92ddc620a78f | 287 | //if xbee is awake send contents of message queue |
jakehodges | 6:92ddc620a78f | 288 | if (xbee.isCTS()) |
jakehodges | 6:92ddc620a78f | 289 | { |
jakehodges | 6:92ddc620a78f | 290 | while(!messageQueue.empty()) |
jakehodges | 6:92ddc620a78f | 291 | { |
jakehodges | 6:92ddc620a78f | 292 | snail::sensordata message = messageQueue.front(); |
jakehodges | 6:92ddc620a78f | 293 | xbee.send(message, sizeof(message)); |
jakehodges | 6:92ddc620a78f | 294 | messageQueue.pop(); |
jakehodges | 6:92ddc620a78f | 295 | } |
jakehodges | 6:92ddc620a78f | 296 | } |
Trumple | 0:fcab3b154e49 | 297 | |
Trumple | 4:0bab12a0cc9a | 298 | //check if it's time to poll |
Trumple | 2:1cbb20dd1733 | 299 | if (time(NULL) - lastPollTime > pollInterval) |
Trumple | 0:fcab3b154e49 | 300 | { |
Trumple | 0:fcab3b154e49 | 301 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 302 | pc.printf("[MAIN] Requesting data...\r\n"); |
Trumple | 0:fcab3b154e49 | 303 | #endif |
Trumple | 0:fcab3b154e49 | 304 | sensors.requestData(); |
Trumple | 0:fcab3b154e49 | 305 | lastPollTime = time(NULL); |
Trumple | 0:fcab3b154e49 | 306 | } |
Trumple | 0:fcab3b154e49 | 307 | |
Trumple | 0:fcab3b154e49 | 308 | //if there is data waiting for us... |
Trumple | 0:fcab3b154e49 | 309 | if (sensors.isDataWaiting()) |
Trumple | 0:fcab3b154e49 | 310 | { |
Trumple | 0:fcab3b154e49 | 311 | #ifdef DEBUG |
Trumple | 0:fcab3b154e49 | 312 | pc.printf("[MAIN] Data waiting, reading data...\r\n"); |
Trumple | 0:fcab3b154e49 | 313 | #endif |
Trumple | 1:27b35752c5d0 | 314 | |
Trumple | 0:fcab3b154e49 | 315 | d_reply data = sensors.readData(); |
Trumple | 1:27b35752c5d0 | 316 | |
Trumple | 0:fcab3b154e49 | 317 | #ifdef DEBUG |
Trumple | 4:0bab12a0cc9a | 318 | pc.printf("[MAIN] Got data: "); |
Trumple | 2:1cbb20dd1733 | 319 | for (int i = 0; i < data.readings.size(); i++) |
Trumple | 21:a32666afce7a | 320 | pc.printf("0x%.4X|", data.readings[i]); |
Trumple | 4:0bab12a0cc9a | 321 | pc.printf("\r\n"); |
Trumple | 0:fcab3b154e49 | 322 | #endif |
Trumple | 1:27b35752c5d0 | 323 | |
jakehodges | 23:b57a47c7862a | 324 | int readings[16]; |
jakehodges | 23:b57a47c7862a | 325 | for (int i = 0; i < data.readings.size(); i++) |
jakehodges | 23:b57a47c7862a | 326 | readings[i] = data.readings[i]; |
Trumple | 4:0bab12a0cc9a | 327 | |
jakehodges | 23:b57a47c7862a | 328 | snail::sensordata message(baseNodeAddress, data.type, data.type, time(NULL), readings, data.readings.size()); |
Trumple | 10:13176e7bd4c8 | 329 | |
jakehodges | 6:92ddc620a78f | 330 | messageQueue.push(message); |
Trumple | 4:0bab12a0cc9a | 331 | |
Trumple | 0:fcab3b154e49 | 332 | //log |
jakehodges | 23:b57a47c7862a | 333 | sd.write(static_cast<long int>(time(NULL)), data); |
Trumple | 0:fcab3b154e49 | 334 | } |
Trumple | 0:fcab3b154e49 | 335 | } |
Trumple | 1:27b35752c5d0 | 336 | } |
Trumple | 0:fcab3b154e49 | 337 | } |