simple http request (temperature data) from mbed lpc1768 with application board to thingsboard device/dashboard

Dependencies:   LM75B mbed-http

Fork of http-example by sandbox

Committer:
daklowprofile
Date:
Fri Jun 29 03:19:58 2018 +0000
Revision:
31:cba9d9dea21a
Parent:
30:6b0698841e48
don't change anything

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daklowprofile 30:6b0698841e48 1 #include "select-demo.h"
daklowprofile 30:6b0698841e48 2
daklowprofile 30:6b0698841e48 3 #if DEMO == MAIN
daklowprofile 30:6b0698841e48 4
daklowprofile 30:6b0698841e48 5 #include "mbed.h"
daklowprofile 30:6b0698841e48 6 #include "easy-connect.h"
daklowprofile 30:6b0698841e48 7 #include "http_request.h"
daklowprofile 30:6b0698841e48 8
daklowprofile 30:6b0698841e48 9 #include "LM75B.h"
daklowprofile 30:6b0698841e48 10
daklowprofile 30:6b0698841e48 11 LM75B tmp(p28,p27);
daklowprofile 30:6b0698841e48 12
daklowprofile 30:6b0698841e48 13
daklowprofile 30:6b0698841e48 14
daklowprofile 30:6b0698841e48 15 void dump_response(HttpResponse* res) {
daklowprofile 30:6b0698841e48 16 printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
daklowprofile 30:6b0698841e48 17
daklowprofile 30:6b0698841e48 18 printf("Headers:\n");
daklowprofile 30:6b0698841e48 19 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
daklowprofile 30:6b0698841e48 20 printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
daklowprofile 30:6b0698841e48 21 }
daklowprofile 30:6b0698841e48 22 printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
daklowprofile 30:6b0698841e48 23 }
daklowprofile 30:6b0698841e48 24
daklowprofile 30:6b0698841e48 25 int main() {
daklowprofile 30:6b0698841e48 26
daklowprofile 30:6b0698841e48 27 // Connect to the network (see mbed_app.json for the connectivity method used)
daklowprofile 30:6b0698841e48 28 NetworkInterface* network = easy_connect(true);
daklowprofile 30:6b0698841e48 29 if (!network) {
daklowprofile 30:6b0698841e48 30 printf("Cannot connect to the network, see serial output");
daklowprofile 30:6b0698841e48 31 return 1;
daklowprofile 30:6b0698841e48 32 }
daklowprofile 30:6b0698841e48 33
daklowprofile 30:6b0698841e48 34 // Create a TCP socket
daklowprofile 30:6b0698841e48 35 printf("\n----- Setting up TCP connection -----\n");
daklowprofile 30:6b0698841e48 36 while(tmp.read()<=30){
daklowprofile 30:6b0698841e48 37 TCPSocket* socket = new TCPSocket();
daklowprofile 30:6b0698841e48 38 nsapi_error_t open_result = socket->open(network);
daklowprofile 30:6b0698841e48 39 if (open_result != 0) {
daklowprofile 30:6b0698841e48 40 printf("Opening TCPSocket failed... %d\n", open_result);
daklowprofile 30:6b0698841e48 41 return 1;
daklowprofile 30:6b0698841e48 42 }
daklowprofile 30:6b0698841e48 43
daklowprofile 30:6b0698841e48 44 nsapi_error_t connect_result = socket->connect("demo.thingsboard.io", 80);
daklowprofile 30:6b0698841e48 45 if (connect_result != 0) {
daklowprofile 30:6b0698841e48 46 printf("Connecting over TCPSocket failed... %d\n", connect_result);
daklowprofile 30:6b0698841e48 47 return 1;
daklowprofile 30:6b0698841e48 48 }
daklowprofile 30:6b0698841e48 49
daklowprofile 30:6b0698841e48 50 printf("Connected over TCP to demo.thingsboard.io:80\n");
daklowprofile 30:6b0698841e48 51
daklowprofile 30:6b0698841e48 52 /*/ Do a GET request to httpbin.org
daklowprofile 30:6b0698841e48 53 {
daklowprofile 30:6b0698841e48 54 HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");
daklowprofile 30:6b0698841e48 55
daklowprofile 30:6b0698841e48 56 // By default the body is automatically parsed and stored in a string, this is memory heavy.
daklowprofile 30:6b0698841e48 57 // To receive chunked response, pass in a callback as third parameter to 'send'.
daklowprofile 30:6b0698841e48 58 HttpResponse* get_res = get_req->send();
daklowprofile 30:6b0698841e48 59 if (!get_res) {
daklowprofile 30:6b0698841e48 60 printf("HttpRequest failed (error code %d)\n", get_req->get_error());
daklowprofile 30:6b0698841e48 61 return 1;
daklowprofile 30:6b0698841e48 62 }
daklowprofile 30:6b0698841e48 63
daklowprofile 30:6b0698841e48 64 printf("\n----- HTTP GET response -----\n");
daklowprofile 30:6b0698841e48 65 dump_response(get_res);
daklowprofile 30:6b0698841e48 66
daklowprofile 30:6b0698841e48 67 delete get_req;
daklowprofile 30:6b0698841e48 68 }
daklowprofile 30:6b0698841e48 69
daklowprofile 30:6b0698841e48 70 *//// POST request to httpbin.org
daklowprofile 30:6b0698841e48 71
daklowprofile 30:6b0698841e48 72 {
daklowprofile 30:6b0698841e48 73 HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://demo.thingsboard.io/api/v1/aq8Xr1qkEzo1ANb4VQHa/telemetry");
daklowprofile 30:6b0698841e48 74 post_req->set_header("Content-Type", "application/json");
daklowprofile 30:6b0698841e48 75
daklowprofile 30:6b0698841e48 76
daklowprofile 30:6b0698841e48 77 char body[256];
daklowprofile 30:6b0698841e48 78 //body[0] = 0;
daklowprofile 30:6b0698841e48 79 sprintf(body,"{\"temperature\":%2f, \"active\": false}",tmp.read());
daklowprofile 30:6b0698841e48 80 //printf(buffer);
daklowprofile 30:6b0698841e48 81 //const char body[] = "{\"hello\":\"world\"}";
daklowprofile 30:6b0698841e48 82
daklowprofile 30:6b0698841e48 83 HttpResponse* post_res = post_req->send(body, strlen(body));
daklowprofile 30:6b0698841e48 84
daklowprofile 30:6b0698841e48 85 if (!post_res) {
daklowprofile 30:6b0698841e48 86 printf("HttpRequest failed (error code %d)\n", post_req->get_error());
daklowprofile 30:6b0698841e48 87 return 1;
daklowprofile 30:6b0698841e48 88 }
daklowprofile 30:6b0698841e48 89
daklowprofile 30:6b0698841e48 90
daklowprofile 30:6b0698841e48 91 printf("\n----- HTTP POST response -----\n");
daklowprofile 30:6b0698841e48 92
daklowprofile 30:6b0698841e48 93 dump_response(post_res);
daklowprofile 30:6b0698841e48 94
daklowprofile 30:6b0698841e48 95 delete post_req;
daklowprofile 30:6b0698841e48 96 }
daklowprofile 30:6b0698841e48 97
daklowprofile 30:6b0698841e48 98 delete socket;
daklowprofile 30:6b0698841e48 99
daklowprofile 30:6b0698841e48 100 wait(5);
daklowprofile 30:6b0698841e48 101 }
daklowprofile 30:6b0698841e48 102 }
daklowprofile 30:6b0698841e48 103
daklowprofile 30:6b0698841e48 104 #endif