Initial setup for TheKsystem.

Dependencies:   mbed-http HTS221

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main-http.cpp Source File

main-http.cpp

00001 #include "select-demo.h"
00002 
00003 #if DEMO == DEMO_HTTP
00004 
00005 #include "mbed.h"
00006 #include "http_request.h"
00007 #include "network-helper.h"
00008 #include "mbed_mem_trace.h"
00009 #include "HTS221Sensor.h"
00010 
00011 static DevI2C devI2c(PB_11,PB_10);
00012 static HTS221Sensor hum_temp(&devI2c);
00013 //theKsystemsURL = "https://api.theksystem.com/update?apiKey=9Q8vOz1lguaV6Ou4&field1=21"
00014 char* theKsystemsURL = "http://api.theksystem.com";
00015 char* theKsystemsAPIkey = "9Q8vOz1lguaV6Ou4";
00016 char urlBuffer[256];
00017 
00018 void dump_response(HttpResponse* res) {
00019     printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00020 
00021     printf("Headers:\n");
00022     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00023         printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00024     }
00025     printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00026 }
00027 
00028 
00029 int main() {
00030     unsigned int updateCnt=0;
00031     float hum_val=12, temp_val=34;
00032     
00033     printf("DISCO-L457VG-IOT01A WiFi + TheKsystem example\r\n");
00034 
00035 #ifdef MBED_MAJOR_VERSION
00036     printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
00037 #endif
00038 
00039     printf("<< =========== Network check.. =========== >>\r\n");
00040 
00041     // Connect to the network with the default networking interface
00042     // if you use WiFi: see mbed_app.json for the credentials
00043     NetworkInterface* network = connect_to_default_network_interface();
00044     if (!network) {
00045         printf("Cannot connect to the network, see serial output\n");
00046         return 1;
00047     }
00048 
00049     printf("Successfully access to network\n\n");
00050 
00051     printf("MAC: %s\n", network->get_mac_address());
00052     printf("IP: %s\n", network->get_ip_address());
00053     printf("Netmask: %s\n", network->get_netmask());
00054     printf("Gateway: %s\n", network->get_gateway());
00055 
00056     printf("<< =========== Sensor check.. =========== >>\r\n");
00057     unsigned int a=0;
00058     hum_temp.init(NULL);
00059     hum_temp.enable();
00060 
00061     // Do a GET request to httpbin.org
00062     while(1){
00063         // By default the body is automatically parsed and stored in a buffer, this is memory heavy.
00064         // To receive chunked response, pass in a callback as last parameter to the constructor.
00065 
00066         hum_temp.get_humidity(&hum_val);
00067         hum_temp.get_temperature(&temp_val);
00068         printf("HT221 hum: %.1f %, temp: %.1f C\n", hum_val, temp_val);
00069         sprintf(urlBuffer, "%s/update?apiKey=%s&field1=%.1f&field2=%.1f", theKsystemsURL, theKsystemsAPIkey, hum_val, temp_val);
00070 
00071         printf("[DEBUG] %s\n", urlBuffer);
00072         HttpRequest* get_req = new HttpRequest(network, HTTP_GET, urlBuffer);
00073         
00074         HttpResponse* get_res = get_req->send();
00075         if (!get_res) {
00076             printf("HttpRequest failed (error code %d)\n", get_req->get_error());
00077             return 1;
00078         }
00079 
00080         printf("\n----- HTTP GET response -----\n");
00081         dump_response(get_res);
00082 
00083         delete get_req;
00084         printf("\nUpdate count %d (Every 10sec, data update to theK server)\n",updateCnt++);
00085         wait(10);
00086     }
00087 #if 0
00088     // POST request to httpbin.org
00089     {
00090         HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
00091         post_req->set_header("Content-Type", "application/json");
00092 
00093         const char body[] = "{\"hello\":\"world\"}";
00094 
00095         HttpResponse* post_res = post_req->send(body, strlen(body));
00096         if (!post_res) {
00097             printf("HttpRequest failed (error code %d)\n", post_req->get_error());
00098             return 1;
00099         }
00100 
00101         printf("\n----- HTTP POST response -----\n");
00102         dump_response(post_res);
00103 
00104         delete post_req;
00105     }
00106 #endif
00107     wait(osWaitForever);
00108 }
00109 
00110 #endif