Deep Slumber, codename ISA, is a program made for the arm MBED during Hack The Burgh 2018 that analyses light, temperature, humidity and CO2 levels in a room. It uploads this information onto an SQL server through a rest API, providing the necessary environment for data processing. Further improvements we hope to provide are the regulation of said parameters by wifi connection to electric heaters, wifi enabled controllable lightbulbs and other iot gadgets as well as a website that will provide recommendations for sleep cycle improvements.

Dependencies:   C12832 CCS811 Sht31 TSL2561

Fork of ARM_HACK_THE_BURGH by Carey Williams

Revision:
59:22dd28696d59
Parent:
58:44e839410957
Child:
60:49746f10fef0
--- a/main.cpp	Sat Mar 10 17:48:37 2018 +0000
+++ b/main.cpp	Sat Mar 10 19:47:43 2018 +0000
@@ -20,6 +20,7 @@
 #include "Sht31.h"
 #include "CCS811.h"
 #include "TSL2561.h"
+#include <cstdlib>
 #include <string>
 using std::string;
 
@@ -104,24 +105,44 @@
     return count;
 }
 
-void http_demo(NetworkInterface *net)
+void http_demo(NetworkInterface *net, string ip, string port, string body)
 {
     TCPSocket socket;
     nsapi_error_t response;
 
-    printf("Sending HTTP request to www.arm.com...\n");
+    printf("Sending HTTP request to %s:%s...\n", ip.data(), port.data());
 
     // Open a socket on the network interface, and create a TCP connection to www.arm.com
     socket.open(net);
-    response = socket.connect("www.arm.com", 80);
+    response = socket.connect(ip.data(), std::atoi(port.data()));
     if(0 != response) {
         printf("Error connecting: %d\n", response);
         socket.close();
         return;
     }
-
+    
+    int body_size = strlen(body.data());
+    char body_size_s [8];
+    sprintf (body_size_s, "%d", body_size);
+    string body_size_string = body_size_s;
+    
     // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
+    string stringbuffer = "";
+    stringbuffer += "POST /data HTTP/1.1\r\n";
+    stringbuffer += "Host: " + ip + "\r\n";
+    stringbuffer += "Content-Type: application/json\r\n";
+    stringbuffer += "Content-Length: " + body_size_string + "\r\n";
+    stringbuffer += "\r\n";
+    stringbuffer += body;
+    //POST /data HTTP/1.1
+    //Host: www.arm.com
+    //Content-Type: application/json
+    //Content-Length: + body_size
+    //
+    //+ body
+    
+    const char* sbuffer = stringbuffer.data();
+    
     nsapi_size_t size = strlen(sbuffer);
     response = 0;
     while(size) {
@@ -155,7 +176,6 @@
 
 int main()
 {
-    /*
     int count = 0;
 
     pc.printf("WiFi example\n\n");
@@ -179,12 +199,6 @@
     printf("Netmask: %s\n", wifi.get_netmask());
     printf("Gateway: %s\n", wifi.get_gateway());
     printf("RSSI: %d\n\n", wifi.get_rssi());
-
-    http_demo(&wifi);
-
-    wifi.disconnect();
-
-    printf("\nDone\n");
     
     ccs811.init();
     tsl2561.begin();
@@ -193,6 +207,9 @@
     
     */
     
+    string ip = "";
+    string port = "80";
+    
     while(1) {
         
         string json = "{\n";
@@ -204,8 +221,7 @@
         //printf("VIS: %d\n", x);
         
         char buffer [8];
-        int n;
-        n = sprintf (buffer, "%d", x);
+        sprintf (buffer, "%d", x);
         
         json += "\t \"Light\" : ";
         json += buffer;
@@ -218,7 +234,7 @@
         
         //printf("eCO2:%dppm\n", eco2);
         
-        n = sprintf (buffer, "%d", eco2);
+        sprintf (buffer, "%d", eco2);
         
         json += "\t \"eCO2\"  : ";
         json += buffer;
@@ -232,20 +248,27 @@
         //printf("TEMP:%3.2fC\n", t);
         //printf("HUM:%3.2f%%\n", h);
         
-        n = sprintf (buffer, "%3.2f", t);
+        sprintf (buffer, "%3.2f", t);
         
         json += "\t \"Tempr\" : ";
         json += buffer;
         json += ",\n";
         
-        n = sprintf (buffer, "%3.2f", h);
+        sprintf (buffer, "%3.2f", h);
         
         json += "\t \"Humid\" : ";
         json += buffer;
         json += ",\n";
         
         json += "}\n\n";
-        printf("%s", json);
+        printf("%s", json.data());
+        
+        http_demo(&wifi, ip, port, json);
+        
         wait(1);
     }
+    
+    wifi.disconnect();
+
+    printf("\nDone\n");
 }