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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "select-demo.h"
00002  
00003 #if DEMO == MAIN
00004 
00005 #include "mbed.h"
00006 #include "easy-connect.h"
00007 #include "http_request.h"
00008 
00009 #include "LM75B.h"
00010 
00011 LM75B tmp(p28,p27);
00012 
00013 
00014  
00015 void dump_response(HttpResponse* res) {
00016     printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
00017  
00018     printf("Headers:\n");
00019     for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
00020         printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
00021     }
00022     printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
00023 }
00024  
00025 int main() {
00026     
00027     // Connect to the network (see mbed_app.json for the connectivity method used)
00028     NetworkInterface* network = easy_connect(true);
00029     if (!network) {
00030         printf("Cannot connect to the network, see serial output");
00031         return 1;
00032     }
00033  
00034     // Create a TCP socket
00035     printf("\n----- Setting up TCP connection -----\n");
00036  while(tmp.read()<=30){
00037     TCPSocket* socket = new TCPSocket();
00038     nsapi_error_t open_result = socket->open(network);
00039     if (open_result != 0) {
00040         printf("Opening TCPSocket failed... %d\n", open_result);
00041         return 1;
00042     }
00043  
00044     nsapi_error_t connect_result = socket->connect("demo.thingsboard.io", 80);
00045     if (connect_result != 0) {
00046         printf("Connecting over TCPSocket failed... %d\n", connect_result);
00047         return 1;
00048     }
00049  
00050     printf("Connected over TCP to demo.thingsboard.io:80\n");
00051  
00052     /*/ Do a GET request to httpbin.org
00053     {
00054         HttpRequest* get_req = new HttpRequest(socket, HTTP_GET, "http://httpbin.org/status/418");
00055  
00056         // By default the body is automatically parsed and stored in a string, this is memory heavy.
00057         // To receive chunked response, pass in a callback as third parameter to 'send'.
00058         HttpResponse* get_res = get_req->send();
00059         if (!get_res) {
00060             printf("HttpRequest failed (error code %d)\n", get_req->get_error());
00061             return 1;
00062         }
00063  
00064         printf("\n----- HTTP GET response -----\n");
00065         dump_response(get_res);
00066  
00067         delete get_req;
00068     }
00069  
00070     *//// POST request to httpbin.org
00071     
00072     {
00073         HttpRequest* post_req = new HttpRequest(socket, HTTP_POST, "http://demo.thingsboard.io/api/v1/aq8Xr1qkEzo1ANb4VQHa/telemetry");
00074         post_req->set_header("Content-Type", "application/json");
00075         
00076         
00077         char body[256];
00078         //body[0] = 0;
00079         sprintf(body,"{\"temperature\":%2f, \"active\": false}",tmp.read());
00080         //printf(buffer);
00081         //const char body[] = "{\"hello\":\"world\"}";
00082         
00083         HttpResponse* post_res = post_req->send(body, strlen(body));
00084         
00085         if (!post_res) {
00086             printf("HttpRequest failed (error code %d)\n", post_req->get_error());
00087             return 1;
00088         }
00089         
00090  
00091         printf("\n----- HTTP POST response -----\n");
00092         
00093         dump_response(post_res);
00094  
00095         delete post_req;
00096     }
00097  
00098     delete socket;
00099  
00100     wait(5);
00101     }
00102 }
00103 
00104 #endif