Created
August 30, 2017 10:11
-
-
Save janjongboom/7a99a56866d41adcfb9ea1b07af75cd1 to your computer and use it in GitHub Desktop.
Ubidots
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mbed.h" | |
#include "easy-connect.h" | |
#include "http_request.h" | |
Serial pc(USBTX, USBRX); | |
void dump_response(HttpResponse* res) { | |
printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str()); | |
printf("Headers:\n"); | |
for (size_t ix = 0; ix < res->get_headers_length(); ix++) { | |
printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str()); | |
} | |
printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str()); | |
} | |
int main() { | |
pc.baud(115200); | |
// Replace this line by your network connectivity method | |
NetworkInterface* network = easy_connect(true); | |
if (!network) { | |
printf("Cannot connect to the network, see serial output"); | |
return 1; | |
} | |
{ | |
HttpRequest* get_req = new HttpRequest(network, HTTP_POST, "http://things.ubidots.com/api/v1.6/variables/5997efbdc03f971ca757920a/values"); | |
get_req->set_header("Content-Type", "application/json"); | |
get_req->set_header("X-Auth-Token", "A1E-B0I6Hzcqxxtl0ecHPOrApvvkoABQE8"); | |
const char* buffer = "{\"value\": 98}"; | |
HttpResponse* get_res = get_req->send(buffer, strlen(buffer)); | |
if (!get_res) { | |
printf("HttpRequest failed (error code %d)\n", get_req->get_error()); | |
return 1; | |
} | |
printf("\n----- HTTP GET response -----\n"); | |
dump_response(get_res); | |
delete get_req; | |
} | |
Thread::wait(osWaitForever); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment