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