thingspark example
Dependencies: MbedJSONValue mbed-http HTS221
Diff: source/main-https.cpp
- Revision:
- 38:7734b54de565
- Parent:
- 34:7da6cfc032fc
- Child:
- 39:9856f09a1333
--- a/source/main-https.cpp Mon Oct 21 11:56:25 2019 +0000
+++ b/source/main-https.cpp Sat Dec 28 07:53:23 2019 +0000
@@ -8,6 +8,16 @@
#include "mbed_trace.h"
#include "https_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);
+
+char* thingspark_URL = "https://api.thingspark.kr";
+char* thingspark_APIkey = "1yUxh3Dh77DUQyOt";
+char urlBuffer[256];
+
/* List of trusted root CA certificates
* currently two: Amazon, the CA for os.mbed.com and Let's Encrypt, the CA for httpbin.org
@@ -72,15 +82,59 @@
printf("\nBody (%lu bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
}
-int main() {
+int main() {
+ unsigned int updateCnt=0;
+ float hum_val=12, temp_val=34;
+
NetworkInterface* network = connect_to_default_network_interface();
if (!network) {
printf("Cannot connect to the network, see serial output\n");
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");
+
+ hum_temp.init(NULL);
+ hum_temp.enable();
+
+ mbed_trace_init();
+
+ // Do a GET request to thingspark.kr
+ 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.
+
+ 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", thingspark_URL, thingspark_APIkey, hum_val, temp_val);
+
+ printf("[DEBUG] %s\n", urlBuffer);
+
+ HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, urlBuffer);
- mbed_trace_init();
+ HttpResponse* get_res = get_req->send();
+ if (!get_res) {
+ printf("HttpRequest failed (error code %d)\n", get_req->get_error());
+ return 1;
+ }
+ printf("\n----- HTTPS GET response -----\n");
+ dump_response(get_res);
+
+ delete get_req;
+ printf("\nUpdate count %d (Every 30sec, data update to thingspark server)\n", updateCnt++);
+ wait(30);
+ }
+
+#if 0
// GET request to os.mbed.com
{
printf("\n----- HTTPS GET request -----\n");
@@ -118,6 +172,7 @@
delete post_req;
}
+#endif
wait(osWaitForever);
}