Sinan Vanli / Mbed OS ISA

Dependencies:   C12832 CCS811 Sht31 TSL2561

Fork of ARM_HACK_THE_BURGH by Carey Williams

Files at this revision

API Documentation at this revision

Comitter:
Ottomanslap
Date:
Sat Mar 10 19:47:43 2018 +0000
Parent:
58:44e839410957
Child:
60:49746f10fef0
Commit message:
Should work,; Parametrized http demo; Other stuff too; Proud Daddy; Murray helped me shitlots.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- 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");
 }