thingspark example
Dependencies: MbedJSONValue mbed-http HTS221
Diff: source/main-http.cpp
- Revision:
- 36:3c86e52c9e75
- Parent:
- 31:66704f6f17c5
- Child:
- 37:e809fd407dc9
--- a/source/main-http.cpp Fri Jan 04 13:32:26 2019 +0100
+++ b/source/main-http.cpp Mon Oct 21 06:49:02 2019 +0000
@@ -6,6 +6,14 @@
#include "http_request.h"
#include "network-helper.h"
#include "mbed_mem_trace.h"
+#include "HTS221Sensor.h"
+
+static DevI2C devI2c(PB_11,PB_10);
+static HTS221Sensor hum_temp(&devI2c);
+//theKsystemsURL = "https://api.theksystem.com/update?apiKey=9Q8vOz1lguaV6Ou4&field1=21"
+char* theKsystemsURL = "http://api.theksystem.com";
+char* theKsystemsAPIkey = "9Q8vOz1lguaV6Ou4";
+char urlBuffer[256];
void dump_response(HttpResponse* res) {
printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
@@ -17,7 +25,19 @@
printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
+
int main() {
+ unsigned int updateCnt=0;
+ float hum_val=12, temp_val=34;
+
+ printf("DISCO-L457VG-IOT01A WiFi + TheKsystem example\r\n");
+
+#ifdef MBED_MAJOR_VERSION
+ printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
+#endif
+
+ printf("<< =========== Network check.. =========== >>\r\n");
+
// Connect to the network with the default networking interface
// if you use WiFi: see mbed_app.json for the credentials
NetworkInterface* network = connect_to_default_network_interface();
@@ -26,12 +46,31 @@
return 1;
}
+ printf("Successfully access to network\n\n");
+
+ printf("MAC: %s\n", network->get_mac_address());
+ printf("IP: %s\n", network->get_ip_address());
+ printf("Netmask: %s\n", network->get_netmask());
+ printf("Gateway: %s\n", network->get_gateway());
+
+ printf("<< =========== Sensor check.. =========== >>\r\n");
+ unsigned int a=0;
+ hum_temp.init(NULL);
+ hum_temp.enable();
+
// Do a GET request to httpbin.org
- {
+ while(1){
// By default the body is automatically parsed and stored in a buffer, this is memory heavy.
// To receive chunked response, pass in a callback as last parameter to the constructor.
- HttpRequest* get_req = new HttpRequest(network, HTTP_GET, "http://httpbin.org/status/418");
+ hum_temp.get_humidity(&hum_val);
+ hum_temp.get_temperature(&temp_val);
+ printf("HT221 hum: %.1f %, temp: %.1f C\n", hum_val, temp_val);
+ sprintf(urlBuffer, "%s/update?apiKey=%s&field1=%.1f&field2=%.1f", theKsystemsURL, theKsystemsAPIkey, hum_val, temp_val);
+
+ printf("[DEBUG] %s\n", urlBuffer);
+ HttpRequest* get_req = new HttpRequest(network, HTTP_GET, urlBuffer);
+
HttpResponse* get_res = get_req->send();
if (!get_res) {
printf("HttpRequest failed (error code %d)\n", get_req->get_error());
@@ -42,8 +81,10 @@
dump_response(get_res);
delete get_req;
+ printf("\nUpdate count %d (Every 10sec, data update to theK server)\n",updateCnt++);
+ wait(10);
}
-
+#if 0
// POST request to httpbin.org
{
HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post");
@@ -62,7 +103,7 @@
delete post_req;
}
-
+#endif
wait(osWaitForever);
}