GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Files at this revision

API Documentation at this revision

Comitter:
Trumple
Date:
Tue Nov 18 18:28:52 2014 +0000
Parent:
0:fcab3b154e49
Child:
2:1cbb20dd1733
Commit message:
Initial commit

Changed in this revision

EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
http.cpp Show annotated file Show diff for this revision Revisions of this file
http.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
sensorinterface.cpp Show annotated file Show diff for this revision Revisions of this file
snail.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Tue Nov 18 18:28:52 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#5887ae6c0c2c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http.cpp	Tue Nov 18 18:28:52 2014 +0000
@@ -0,0 +1,107 @@
+#include "mbed.h"
+#include "http.h"
+#include <string>
+#include <vector>
+#include <sstream>
+
+http::http()
+{
+    #ifdef DEBUG
+        printf("[HTTP] Ethernet connecting...\r\n");
+    #endif
+    
+    eth.init();
+    eth.connect();
+    
+    #ifdef DEBUG
+        printf("[HTTP] Ethernet connected, IP: %s\r\n", eth.getIPAddress());
+    #endif
+}
+
+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());
+    #endif
+    
+    TCPSocketConnection sock;
+    sock.connect(address.c_str(), port);
+    
+    #ifdef DEBUG
+        printf("[HTTP] Connected to endpoint...\r\n");
+    #endif
+    
+    string httpget = "GET " + url + " HTTP/1.1";
+    httpget += "\nHost: " + address + "\n\n";
+    
+    //get a writable char* (I.E. not const char* returned by c_str), put it into a vector 
+    vector<char> writable(httpget.begin(), httpget.end());
+    writable.push_back('\0');
+    
+    sock.send_all(&writable[0], writable.size()-1);
+    
+    string message = this->receiveFromSock(sock, replyTimeout);
+      
+    sock.close();
+    
+    return this->parse(message);
+}
+
+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());
+    #endif
+    
+    TCPSocketConnection sock;
+    sock.connect(address.c_str(), port);
+    
+    #ifdef DEBUG
+        printf("[HTTP] Connected to endpoint...\r\n");
+    #endif
+    
+    stringstream contentLength;
+    contentLength << jsonPayload.size();
+    string contentLengthStr = contentLength.str();
+    
+    string httppost = "POST " + url + " HTTP/1.1";
+    httppost += "\nHost: " + address;
+    httppost += "\nContent-Type: application/json";
+    httppost += "\nContent-Length: " + contentLengthStr;
+    httppost += "\n\n" + jsonPayload;
+    
+    //to get a writable char* (I.E. not const char* returned by string.c_str), put it into a vector 
+    vector<char> writable(httppost.begin(), httppost.end());
+    writable.push_back('\0');
+    
+    sock.send_all(&writable[0], writable.size()-1);
+    
+    string message = this->receiveFromSock(sock, replyTimeout);
+    
+    sock.close();
+    
+    return this->parse(message);
+}
+
+string http::receiveFromSock(TCPSocketConnection sock, int replyTimeout)
+{
+    char buffer[1024];
+    int receiveByteCount;
+    string message;
+    
+    while (true)
+    {
+        receiveByteCount = sock.receive(buffer, sizeof(buffer)-1);//spare a byte for null termination byte
+        if (receiveByteCount <= 0)
+            break;
+        buffer[receiveByteCount] = '\0';
+        message += buffer;
+    }
+    
+    return message;
+}
+
+string http::parse(string httpReply)
+{
+    return httpReply;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http.h	Tue Nov 18 18:28:52 2014 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include <string>
+
+#define DEBUG
+
+class http
+{
+    public:
+        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();
+    
+    private:
+        EthernetInterface eth;
+        string receiveFromSock(TCPSocketConnection sock, int replyTimeout);
+};
\ No newline at end of file
--- 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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Nov 18 18:28:52 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#318e02f48146
--- a/sensorinterface.cpp	Tue Nov 11 17:33:41 2014 +0000
+++ b/sensorinterface.cpp	Tue Nov 18 18:28:52 2014 +0000
@@ -8,6 +8,10 @@
     
     char buffer[255];
     
+    #ifdef DEBUG
+        printf("[SIF] Scanning for devices...\r\n");
+    #endif
+    
     for (char i=1; i<=254; i=i+2)
     {
         //last bit determines read/write not actual address
@@ -31,13 +35,22 @@
 
 void sensorinterface::requestData()
 {
-    currentSensor = sensors.begin();
-    
-    #ifdef DEBUG
-        printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
-    #endif
-    
-    sendRequest(currentSensor->first);
+    if (sensors.size() > 0)
+    {
+        currentSensor = sensors.begin();
+        
+        #ifdef DEBUG
+            printf("[SIF] Beginning device query chain from 0x%.2X\r\n", currentSensor->first);
+        #endif
+        
+        sendRequest(currentSensor->first);
+    }
+    else
+    {
+        #ifdef DEBUG
+            printf("[SIF] Data requested, but no devices present\r\n");
+        #endif
+    }
 }
 
 void sensorinterface::readyTrigger()
--- a/snail.lib	Tue Nov 11 17:33:41 2014 +0000
+++ b/snail.lib	Tue Nov 18 18:28:52 2014 +0000
@@ -1,1 +1,1 @@
-http://developer.mbed.org/users/Trumple/code/snail/#0b79538c5e5a
+http://developer.mbed.org/users/Trumple/code/snail/#125dca84093e