GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Revision:
10:13176e7bd4c8
Parent:
9:f0030295ae9b
Child:
15:1d98f45717bc
--- a/main.cpp	Mon Dec 15 15:50:20 2014 +0000
+++ b/main.cpp	Tue Dec 23 21:28:40 2014 +0000
@@ -13,8 +13,9 @@
 
 
 bool isBasenode = false;
+string fieldID = "testfield";
+
 http h = http();
-
 sensorinterface* sif;
 
 char localAddress[8];
@@ -28,6 +29,19 @@
 
 queue<snail::sensordata> messageQueue;
 
+string addressToString(char address[8])
+{
+    string address_str;
+    for(int i = 0; i < 8; i++)
+    {
+        char addressBuffer[2];
+        sprintf(addressBuffer, "%.2X", address[i]);
+        address_str += string(addressBuffer, 2);
+    }
+    
+    return address_str;
+}
+
 void handleJoinNetworkReply(snail::message& message)
 {
     pc.printf("[MAIN] Got join network reply...\r\n");
@@ -45,7 +59,25 @@
     {
         snail::joinnetworkrequest& request = static_cast<snail::joinnetworkrequest&>(message);
         
-        //TODO: Send sensors data to server
+        //report connected sensors to server
+        MbedJSONValue j;
+        j["sensors"];
+        for (int i = 0; i < request.sensorCount; i++)
+        {
+            char sensorIDBuffer[3];
+            sprintf(sensorIDBuffer, "%i", request.sensors[i].id);
+            char fBuffer[3];
+            sprintf(fBuffer, "%i", request.sensors[i].type);
+            j["sensors"][i]["sensorID"] = string(sensorIDBuffer);
+            j["sensors"][i]["sensortype"] = string(fBuffer);
+        }
+        
+        string address = addressToString(request.source);
+        #ifdef DEBUG
+            pc.printf( ("[MAIN] POSTing startup information: " + j.serialize() + "\r\n").c_str() );
+        #endif
+        h.post("178.62.84.55", 8888, "/field/" + fieldID + "/" + address + "/startup/", j.serialize()).c_str();
+        
         snail::joinnetworkreply reply(request.source, time(NULL), localAddress);
         xbee.send(reply, sizeof(reply));
     }
@@ -61,10 +93,18 @@
     value = value << 8;
     value += d.payload[1];
     
+    char sensorID[3];
+    sprintf(sensorID, "%i", d.i2cAddress);
+    
     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());
+    
+    #ifdef DEBUG
+        pc.printf( ("[MAIN] POSTing sensor reading: " + j.serialize() + "\r\n").c_str() );
+    #endif
+    
+    h.post("178.62.84.55", 8888, "/field/" + fieldID + "/" + addressToString(d.source) + "/" + sensorID + "/", j.serialize()).c_str();
 }
 
 void handleNetworkParametersTimeout()
@@ -275,7 +315,9 @@
                 char readings[sizeof(snail::sensordata().payload)];
                 memcpy(readings, readings_str.c_str(), sizeof(readings));
                 
+                //TODO: Get real I2C address
                 snail::sensordata message(baseNodeAddress, data.type, data.type, time(NULL), readings, data.readings.size());
+                
                 messageQueue.push(message);
                 
                 //log