Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue mbed-http HTS221
source/main-http.cpp@36:3c86e52c9e75, 2019-10-21 (annotated)
- Committer:
- Daniel_Lee
- Date:
- Mon Oct 21 06:49:02 2019 +0000
- Revision:
- 36:3c86e52c9e75
- Parent:
- 31:66704f6f17c5
- Child:
- 37:e809fd407dc9
Initial setup for TheKsystem.
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); | 
| Daniel_Lee | 36:3c86e52c9e75 | 13 | //theKsystemsURL = "https://api.theksystem.com/update?apiKey=9Q8vOz1lguaV6Ou4&field1=21" | 
| Daniel_Lee | 36:3c86e52c9e75 | 14 | char* theKsystemsURL = "http://api.theksystem.com"; | 
| Daniel_Lee | 36:3c86e52c9e75 | 15 | char* theKsystemsAPIkey = "9Q8vOz1lguaV6Ou4"; | 
| 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 | |
| Daniel_Lee | 36:3c86e52c9e75 | 33 | printf("DISCO-L457VG-IOT01A WiFi + TheKsystem 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"); | 
| Daniel_Lee | 36:3c86e52c9e75 | 57 | unsigned int a=0; | 
| Daniel_Lee | 36:3c86e52c9e75 | 58 | hum_temp.init(NULL); | 
| Daniel_Lee | 36:3c86e52c9e75 | 59 | hum_temp.enable(); | 
| Daniel_Lee | 36:3c86e52c9e75 | 60 | |
| Jan Jongboom | 0:85fdc69bc10c | 61 | // Do a GET request to httpbin.org | 
| 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); | 
| Daniel_Lee | 36:3c86e52c9e75 | 69 | sprintf(urlBuffer, "%s/update?apiKey=%s&field1=%.1f&field2=%.1f", theKsystemsURL, theKsystemsAPIkey, 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; | 
| Daniel_Lee | 36:3c86e52c9e75 | 84 | printf("\nUpdate count %d (Every 10sec, data update to theK server)\n",updateCnt++); | 
| Daniel_Lee | 36:3c86e52c9e75 | 85 | wait(10); | 
| Jan Jongboom | 0:85fdc69bc10c | 86 | } | 
| Daniel_Lee | 36:3c86e52c9e75 | 87 | #if 0 | 
| Jan Jongboom | 0:85fdc69bc10c | 88 | // POST request to httpbin.org | 
| Jan Jongboom | 5:42f713b19337 | 89 | { | 
| Jan Jongboom | 5:42f713b19337 | 90 | HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://httpbin.org/post"); | 
| Jan Jongboom | 5:42f713b19337 | 91 | post_req->set_header("Content-Type", "application/json"); | 
| Jan Jongboom | 0:85fdc69bc10c | 92 | |
| Jan Jongboom | 5:42f713b19337 | 93 | const char body[] = "{\"hello\":\"world\"}"; | 
| Jan Jongboom | 0:85fdc69bc10c | 94 | |
| Jan Jongboom | 5:42f713b19337 | 95 | HttpResponse* post_res = post_req->send(body, strlen(body)); | 
| Jan Jongboom | 5:42f713b19337 | 96 | if (!post_res) { | 
| Jan Jongboom | 5:42f713b19337 | 97 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); | 
| Jan Jongboom | 5:42f713b19337 | 98 | return 1; | 
| Jan Jongboom | 5:42f713b19337 | 99 | } | 
| Jan Jongboom | 5:42f713b19337 | 100 | |
| Jan Jongboom | 5:42f713b19337 | 101 | printf("\n----- HTTP POST response -----\n"); | 
| Jan Jongboom | 5:42f713b19337 | 102 | dump_response(post_res); | 
| Jan Jongboom | 5:42f713b19337 | 103 | |
| Jan Jongboom | 5:42f713b19337 | 104 | delete post_req; | 
| Jan Jongboom | 0:85fdc69bc10c | 105 | } | 
| Daniel_Lee | 36:3c86e52c9e75 | 106 | #endif | 
| Jan Jongboom | 28:9bccd981a393 | 107 | wait(osWaitForever); | 
| Jan Jongboom | 0:85fdc69bc10c | 108 | } | 
| Jan Jongboom | 0:85fdc69bc10c | 109 | |
| Jan Jongboom | 0:85fdc69bc10c | 110 | #endif |