thingspark example
Dependencies: MbedJSONValue mbed-http HTS221
source/main-http.cpp@39:9856f09a1333, 2019-12-29 (annotated)
- Committer:
- master_k1
- Date:
- Sun Dec 29 03:14:52 2019 +0000
- Revision:
- 39:9856f09a1333
- Parent:
- 38:7734b54de565
change api key
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jan Jongboom |
0:85fdc69bc10c | 1 | #include "select-demo.h" |
Jan Jongboom |
0:85fdc69bc10c | 2 | |
Jan Jongboom |
0:85fdc69bc10c | 3 | #if DEMO == DEMO_HTTP |
Jan Jongboom |
0:85fdc69bc10c | 4 | |
Jan Jongboom |
0:85fdc69bc10c | 5 | #include "mbed.h" |
Jan Jongboom |
0:85fdc69bc10c | 6 | #include "http_request.h" |
Jan Jongboom |
30:4825e4f38844 | 7 | #include "network-helper.h" |
Jan Jongboom |
31:66704f6f17c5 | 8 | #include "mbed_mem_trace.h" |
Daniel_Lee | 36:3c86e52c9e75 | 9 | #include "HTS221Sensor.h" |
Daniel_Lee | 36:3c86e52c9e75 | 10 | |
Daniel_Lee | 36:3c86e52c9e75 | 11 | static DevI2C devI2c(PB_11,PB_10); |
Daniel_Lee | 36:3c86e52c9e75 | 12 | static HTS221Sensor hum_temp(&devI2c); |
master_k1 | 38:7734b54de565 | 13 | |
master_k1 | 37:e809fd407dc9 | 14 | char* thingspark_URL = "http://api.thingspark.kr"; |
master_k1 | 39:9856f09a1333 | 15 | char* thingspark_APIkey = "CHANGE API KEY"; |
Daniel_Lee | 36:3c86e52c9e75 | 16 | char urlBuffer[256]; |
Jan Jongboom |
0:85fdc69bc10c | 17 | |
Jan Jongboom |
0:85fdc69bc10c | 18 | void dump_response(HttpResponse* res) { |
Jan Jongboom |
0:85fdc69bc10c | 19 | printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 20 | |
Jan Jongboom |
0:85fdc69bc10c | 21 | printf("Headers:\n"); |
Jan Jongboom |
0:85fdc69bc10c | 22 | for (size_t ix = 0; ix < res->get_headers_length(); ix++) { |
Jan Jongboom |
7:5d32909f77de | 23 | printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 24 | } |
Jan Jongboom |
5:42f713b19337 | 25 | printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); |
Jan Jongboom |
0:85fdc69bc10c | 26 | } |
Jan Jongboom |
0:85fdc69bc10c | 27 | |
Daniel_Lee | 36:3c86e52c9e75 | 28 | |
Jan Jongboom |
0:85fdc69bc10c | 29 | int main() { |
Daniel_Lee | 36:3c86e52c9e75 | 30 | unsigned int updateCnt=0; |
Daniel_Lee | 36:3c86e52c9e75 | 31 | float hum_val=12, temp_val=34; |
Daniel_Lee | 36:3c86e52c9e75 | 32 | |
master_k1 | 37:e809fd407dc9 | 33 | printf("DISCO-L457VG-IOT01A WiFi + thingspark example\r\n"); |
Daniel_Lee | 36:3c86e52c9e75 | 34 | |
Daniel_Lee | 36:3c86e52c9e75 | 35 | #ifdef MBED_MAJOR_VERSION |
Daniel_Lee | 36:3c86e52c9e75 | 36 | printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION); |
Daniel_Lee | 36:3c86e52c9e75 | 37 | #endif |
Daniel_Lee | 36:3c86e52c9e75 | 38 | |
Daniel_Lee | 36:3c86e52c9e75 | 39 | printf("<< =========== Network check.. =========== >>\r\n"); |
Daniel_Lee | 36:3c86e52c9e75 | 40 | |
Jan Jongboom |
30:4825e4f38844 | 41 | // Connect to the network with the default networking interface |
Jan Jongboom |
30:4825e4f38844 | 42 | // if you use WiFi: see mbed_app.json for the credentials |
Jan Jongboom |
30:4825e4f38844 | 43 | NetworkInterface* network = connect_to_default_network_interface(); |
Jan Jongboom |
0:85fdc69bc10c | 44 | if (!network) { |
Jan Jongboom |
30:4825e4f38844 | 45 | printf("Cannot connect to the network, see serial output\n"); |
Jan Jongboom |
0:85fdc69bc10c | 46 | return 1; |
Jan Jongboom |
0:85fdc69bc10c | 47 | } |
Jan Jongboom |
0:85fdc69bc10c | 48 | |
Daniel_Lee | 36:3c86e52c9e75 | 49 | printf("Successfully access to network\n\n"); |
Daniel_Lee | 36:3c86e52c9e75 | 50 | |
Daniel_Lee | 36:3c86e52c9e75 | 51 | printf("MAC: %s\n", network->get_mac_address()); |
Daniel_Lee | 36:3c86e52c9e75 | 52 | printf("IP: %s\n", network->get_ip_address()); |
Daniel_Lee | 36:3c86e52c9e75 | 53 | printf("Netmask: %s\n", network->get_netmask()); |
Daniel_Lee | 36:3c86e52c9e75 | 54 | printf("Gateway: %s\n", network->get_gateway()); |
Daniel_Lee | 36:3c86e52c9e75 | 55 | |
Daniel_Lee | 36:3c86e52c9e75 | 56 | printf("<< =========== Sensor check.. =========== >>\r\n"); |
master_k1 | 37:e809fd407dc9 | 57 | |
Daniel_Lee | 36:3c86e52c9e75 | 58 | hum_temp.init(NULL); |
Daniel_Lee | 36:3c86e52c9e75 | 59 | hum_temp.enable(); |
Daniel_Lee | 36:3c86e52c9e75 | 60 | |
master_k1 | 37:e809fd407dc9 | 61 | // Do a GET request to thingspark.kr |
Daniel_Lee | 36:3c86e52c9e75 | 62 | while(1){ |
Jan Jongboom |
14:3c173847e681 | 63 | // By default the body is automatically parsed and stored in a buffer, this is memory heavy. |
Jan Jongboom |
14:3c173847e681 | 64 | // To receive chunked response, pass in a callback as last parameter to the constructor. |
Jan Jongboom |
0:85fdc69bc10c | 65 | |
Daniel_Lee | 36:3c86e52c9e75 | 66 | hum_temp.get_humidity(&hum_val); |
Daniel_Lee | 36:3c86e52c9e75 | 67 | hum_temp.get_temperature(&temp_val); |
Daniel_Lee | 36:3c86e52c9e75 | 68 | printf("HT221 hum: %.1f %, temp: %.1f C\n", hum_val, temp_val); |
master_k1 | 37:e809fd407dc9 | 69 | sprintf(urlBuffer, "%s/update?apiKey=%s&field1=%.1f&field2=%.1f", thingspark_URL, thingspark_APIkey, hum_val, temp_val); |
Daniel_Lee | 36:3c86e52c9e75 | 70 | |
Daniel_Lee | 36:3c86e52c9e75 | 71 | printf("[DEBUG] %s\n", urlBuffer); |
Daniel_Lee | 36:3c86e52c9e75 | 72 | HttpRequest* get_req = new HttpRequest(network, HTTP_GET, urlBuffer); |
Daniel_Lee | 36:3c86e52c9e75 | 73 | |
Jan Jongboom |
5:42f713b19337 | 74 | HttpResponse* get_res = get_req->send(); |
Jan Jongboom |
5:42f713b19337 | 75 | if (!get_res) { |
Jan Jongboom |
5:42f713b19337 | 76 | printf("HttpRequest failed (error code %d)\n", get_req->get_error()); |
Jan Jongboom |
5:42f713b19337 | 77 | return 1; |
Jan Jongboom |
5:42f713b19337 | 78 | } |
Jan Jongboom |
5:42f713b19337 | 79 | |
Jan Jongboom |
5:42f713b19337 | 80 | printf("\n----- HTTP GET response -----\n"); |
Jan Jongboom |
5:42f713b19337 | 81 | dump_response(get_res); |
Jan Jongboom |
5:42f713b19337 | 82 | |
Jan Jongboom |
5:42f713b19337 | 83 | delete get_req; |
master_k1 | 37:e809fd407dc9 | 84 | printf("\nUpdate count %d (Every 30sec, data update to thingspark server)\n",updateCnt++); |
master_k1 | 37:e809fd407dc9 | 85 | wait(30); |
Jan Jongboom |
0:85fdc69bc10c | 86 | } |
Jan Jongboom |
0:85fdc69bc10c | 87 | |
Jan Jongboom |
28:9bccd981a393 | 88 | wait(osWaitForever); |
Jan Jongboom |
0:85fdc69bc10c | 89 | } |
Jan Jongboom |
0:85fdc69bc10c | 90 | |
Jan Jongboom |
0:85fdc69bc10c | 91 | #endif |