GDP group 24 node core
Dependencies: EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue
Revision 2:1cbb20dd1733, committed 2014-12-14
- Comitter:
- Trumple
- Date:
- Sun Dec 14 22:28:24 2014 +0000
- Parent:
- 1:27b35752c5d0
- Child:
- 3:71b090ec5c69
- Commit message:
- Implement getLocalAddress, fix getNetworkParameters, implement timestamp acquisition, implement HTTP response parsing
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Sun Dec 14 22:28:24 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/samux/code/MbedJSONValue/#10a99cdf7846
--- a/http.cpp Tue Nov 18 18:28:52 2014 +0000
+++ b/http.cpp Sun Dec 14 22:28:24 2014 +0000
@@ -2,9 +2,8 @@
#include "http.h"
#include <string>
#include <vector>
-#include <sstream>
-http::http()
+void http::connect()
{
#ifdef DEBUG
printf("[HTTP] Ethernet connecting...\r\n");
@@ -21,7 +20,7 @@
string http::get(string address, int port, string url, int replyTimeout)
{
#ifdef DEBUG
- printf("[HTTP] Sending GET request to %s%s\r\n", address.c_str(), url.c_str());
+ printf("[HTTP] Sending GET request to %s:%i%s\r\n", address.c_str(), port, url.c_str());
#endif
TCPSocketConnection sock;
@@ -50,7 +49,7 @@
string http::post(string address, int port, string url, string jsonPayload, int replyTimeout)
{
#ifdef DEBUG
- printf("[HTTP] Sending POST request to %s%s\r\n", address.c_str(), url.c_str());
+ printf("[HTTP] Sending POST request to %s:%i%s\r\n", address.c_str(), port, url.c_str());
#endif
TCPSocketConnection sock;
@@ -60,14 +59,15 @@
printf("[HTTP] Connected to endpoint...\r\n");
#endif
- stringstream contentLength;
- contentLength << jsonPayload.size();
- string contentLengthStr = contentLength.str();
+ char buffer[20];
+ sprintf(buffer, "%i", jsonPayload.size());
+ string contentLengthStr = string(buffer);
string httppost = "POST " + url + " HTTP/1.1";
httppost += "\nHost: " + address;
httppost += "\nContent-Type: application/json";
httppost += "\nContent-Length: " + contentLengthStr;
+ httppost += "\nAuthorization: Key 1";
httppost += "\n\n" + jsonPayload;
//to get a writable char* (I.E. not const char* returned by string.c_str), put it into a vector
@@ -103,5 +103,16 @@
string http::parse(string httpReply)
{
- return httpReply;
+ int payloadBegin = httpReply.find("\r\n\r\n");
+
+ if (payloadBegin > -1)
+ {
+ string payload(httpReply.begin() + payloadBegin + 4, httpReply.end());
+
+ return payload;
+ }
+ else
+ {
+ return "";
+ }
}
\ No newline at end of file
--- a/http.h Tue Nov 18 18:28:52 2014 +0000
+++ b/http.h Sun Dec 14 22:28:24 2014 +0000
@@ -10,8 +10,7 @@
string get(string address, int port, string url, int replyTimeout = 20);
string post(string address, int port, string url, string jsonPayload, int replyTimeout = 20);
string parse(string httpReply);
-
- http();
+ void connect();
private:
EthernetInterface eth;
--- a/main.cpp Tue Nov 18 18:28:52 2014 +0000
+++ b/main.cpp Sun Dec 14 22:28:24 2014 +0000
@@ -3,17 +3,19 @@
#include "sensorinterface.h"
#include "sdcard.h"
#include "http.h"
+#include "MbedJSONValue.h"
#define DEBUG
time_t lastPollTime = 0;
time_t pollInterval = 30;
-time_t timestamp = 0;
+
bool isBasenode = false;
+http h = http();
char localAddress[8];
-char baseAddress[8];
+char baseNodeAddress[8];
bool networkParametersUpdated = false;
bool networkParametersTimedOut = false;
@@ -21,23 +23,40 @@
snail xbee = snail();
Ticker networkParametersTimeout;
-void handleBaseAddressReply(snail::message& message)
+void handleJoinNetworkReply(snail::message& message)
{
- snail::baseaddressreply& reply = static_cast<snail::baseaddressreply&>(message);
- memcpy(baseAddress, reply.baseAddress, sizeof(baseAddress));
- //TODO: Get timestamp
+ pc.printf("[MAIN] Got join network reply...\r\n");
+ snail::joinnetworkreply& reply = static_cast<snail::joinnetworkreply&>(message);
+ set_time(reply.timestamp);
+ memcpy(baseNodeAddress, reply.baseNodeAddress, sizeof(baseNodeAddress));
+
networkParametersUpdated = true;
}
-void handleBaseAddressRequest(snail::message& message)
+void handleJoinNetworkRequest(snail::message& message)
{
+ pc.printf("[MAIN] Got join network request...\r\n");
if (isBasenode)
{
- snail::baseaddressreply reply(snail::broadcastAddress, localAddress);
+ snail::joinnetworkrequest& request = static_cast<snail::joinnetworkrequest&>(message);
+
+ //TODO: Send sensors data to server
+ snail::joinnetworkreply reply(request.source, time(NULL), localAddress);
xbee.send(reply, sizeof(reply));
}
}
+void handleSensorData(snail::message& message)
+{
+ pc.printf("[MAIN] Got sensor data...\r\n");
+ snail::sensordata& d = static_cast<snail::sensordata&>(message);
+
+// MbedJSONValue j;
+// j["timestamp"] = (int)time(NULL);
+// j["value"] = value;
+// pc.printf(h.post("178.62.84.55", 8888, "/field/testfield/testnode/testsensor/", j.serialize()).c_str());
+}
+
void handleNetworkParametersTimeout()
{
networkParametersTimedOut = true;
@@ -45,20 +64,48 @@
void getLocalAddress()
{
- //TODO: get local address from xbee
+ pc.printf("[MAIN] Requesting lower address bits...\r\n");
+ xbee.ATCommand("SL");
+
+ while (!xbee.isATCommandResponseWaiting())
+ xbee.processIncomingData();
+
+ snail::atcommandresponse SLr = xbee.getATCommandResponse();
+
+ pc.printf("[MAIN] Requesting upper address bits...\r\n");
+ xbee.ATCommand("SH");
+
+ while (!xbee.isATCommandResponseWaiting())
+ xbee.processIncomingData();
+
+ snail::atcommandresponse SHr = xbee.getATCommandResponse();
+
+ string address(SHr.response);
+ address += SLr.response;
+
+ pc.printf("[MAIN] Got local address: ");
+
+ for (int i = 0; i < address.size(); i++)
+ pc.printf("%.2X", address[i]);
+
+ pc.printf("\r\n");
+
+ memcpy(localAddress, address.c_str(), sizeof(localAddress));
}
void getNetworkParameters()
{
#ifdef DEBUG
- pc.printf("[MAIN] Requesting time and base address...\r\n");
+ pc.printf("[MAIN] Requesting to join network...\r\n");
#endif
- xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REPLY, handleBaseAddressReply);
+ xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REPLY, handleJoinNetworkReply);
+
+ networkParametersTimeout.attach(&handleNetworkParametersTimeout, 30.0);
- networkParametersTimeout.attach(&handleNetworkParametersTimeout, 10.0);
+ snail::joinnetworkrequest::sensor sensors[255];
+ snail::joinnetworkrequest request(sensors);
- snail::baseaddressrequest request;
xbee.send(request, sizeof(request));
while(!networkParametersUpdated)
@@ -71,11 +118,15 @@
xbee.send(request, sizeof(request));
networkParametersTimedOut = false;
}
- xbee.readMessage();
+ xbee.processIncomingData();
}
#ifdef DEBUG
- pc.printf("[MAIN] Got network parameters\r\n");
+ pc.printf("[MAIN] Got network parameters. Time: %i, base node address: ", time(NULL));
+
+ for (int i = 0; i < sizeof(baseNodeAddress); i++)
+ pc.printf("%.2X", baseNodeAddress[i]);
+ pc.printf("\r\n");
#endif
networkParametersTimeout.detach();
}
@@ -84,27 +135,26 @@
{
#ifdef DEBUG
pc.printf("[MAIN] Starting up...\r\n");
- printf(" . .\r\n");
- printf(" / `. .' \\\r\n");
- printf(" .---. < > < > .---.\r\n");
- printf(" | \\ \\ - ~ ~ - / / |\r\n");
- printf(" ~-..-~ ~-..-~\r\n");
- printf(" \\~~~\\.' `./~~~/\r\n");
- printf(" .-~~^-. \\__/ \\__/\r\n");
- printf(" .' O \\ / / \\ \\\r\n");
- printf("(_____, `._.' | } \\/~~~/\r\n");
- printf(" `----. / } | / \\__/\r\n");
- printf(" `-. | / | / `. ,~~|\r\n");
- printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
- printf(" | / | / ~-. `-. _||_||_\r\n");
- printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n");
- printf("\r\n");
- printf("\r\n");
- printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
- printf("\r\n");
- printf("\r\n");
+ pc.printf(" . .\r\n");
+ pc.printf(" / `. .' \\\r\n");
+ pc.printf(" .---. < > < > .---.\r\n");
+ pc.printf(" | \\ \\ - ~ ~ - / / |\r\n");
+ pc.printf(" ~-..-~ ~-..-~\r\n");
+ pc.printf(" \\~~~\\.' `./~~~/\r\n");
+ pc.printf(" .-~~^-. \\__/ \\__/\r\n");
+ pc.printf(" .' O \\ / / \\ \\\r\n");
+ pc.printf("(_____, `._.' | } \\/~~~/\r\n");
+ pc.printf(" `----. / } | / \\__/\r\n");
+ pc.printf(" `-. | / | / `. ,~~|\r\n");
+ pc.printf(" ~-.__| /_ - ~ ^| /- _ `..-' f: f:\r\n");
+ pc.printf(" | / | / ~-. `-. _||_||_\r\n");
+ pc.printf(" |_____| |_____| ~ - . _ _ _ _ _>\r\n");
+ pc.printf("\r\n");
+ pc.printf("\r\n");
+ pc.printf(" Sensorsaurus | Team 24 GDP | Southampton | 2014\r\n");
+ pc.printf("\r\n");
+ pc.printf("\r\n");
#endif
-
//sdcard sd = sdcard();
//TODO: read basenode pin
@@ -114,14 +164,22 @@
//TODO: load configuration from SD
+ getLocalAddress();
+
if (isBasenode)
{
getLocalAddress();
- http h = http();
- //TODO: get timestamp from API
+ h.connect();
+
+ string timestampStr = h.get("time.bitnode.co.uk", 80, "/");
+ time_t timestamp = atoi(timestampStr.c_str());
+
set_time(timestamp);
- xbee.registerMessageCallback(MESSAGE_BASE_ADDRESS_REQUEST, handleBaseAddressRequest);
+ pc.printf("[MAIN] Got time: %i\r\n", timestamp);
+
+ xbee.registerMessageCallback(MESSAGE_JOIN_NETWORK_REQUEST, handleJoinNetworkRequest);
+ xbee.registerMessageCallback(MESSAGE_SENSOR_DATA, handleSensorData);
while(1)
{
@@ -129,23 +187,23 @@
pc.printf("[MAIN] Basenode is idle\r\n");
#endif
- wait(5);
+ wait(1);
+ xbee.processIncomingData();
}
}
else
{
sensorinterface sensors = sensorinterface();
getNetworkParameters();
- set_time(timestamp);
while(1)
{
- xbee.readMessage();
+ xbee.processIncomingData();
//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
- if (time(NULL) - lastPollTime > 10)
+ if (time(NULL) - lastPollTime > pollInterval)
{
#ifdef DEBUG
pc.printf("[MAIN] Requesting data...\r\n");
@@ -164,8 +222,8 @@
d_reply data = sensors.readData();
#ifdef DEBUG
- for (int i = 0; i < data.readings.size(); i++)
- pc.printf("0x%.4X|", data.readings[i]);
+ for (int i = 0; i < data.readings.size(); i++)
+ pc.printf("0x%.4X|", data.readings[i]);
#endif
//log
--- a/mbed-rtos.lib Tue Nov 18 18:28:52 2014 +0000 +++ b/mbed-rtos.lib Sun Dec 14 22:28:24 2014 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed-rtos/#318e02f48146 +http://mbed.org/users/mbed_official/code/mbed-rtos/#631c0f1008c3
--- a/snail.lib Tue Nov 18 18:28:52 2014 +0000 +++ b/snail.lib Sun Dec 14 22:28:24 2014 +0000 @@ -1,1 +1,1 @@ -http://developer.mbed.org/users/Trumple/code/snail/#125dca84093e +http://developer.mbed.org/users/Trumple/code/snail/#978c7ba07ee5
