Switch with HTTP

Hi

I want to connect a switch to the my LPC1768 and when I press this switch , I will send a response to my rest server after getting the request.

I’m using interrupts but when I press the switch I got this error:

++ MbedOS Error Info ++ Error Status: 0x80020115 Code: 277 Module: 2 Error Message: Mutex lock failed Location: 0x1280F Error Value: 0xFFFFFFFA Current Thread: rtx_idle Id: 0x1000037C Entry: 0x14ABB StackSize: 0x200 StackMem: 0x10000408 SP: 0x10007EE8 For more info, visit: mbedos-error MbedOS Error Info

My program as follow :

  1. include “select-demo.h”

  2. if DEMO == DEMO_HTTP

  3. include “mbed.h”

  4. include “http_request.h”

  5. include “network-helper.h”

  6. include “mbed_mem_trace.h”

DigitalOut myled(LED1); InterruptIn pb(p8);

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()); }

void pb_hit_interrupt (NetworkInterface* network) { POST request to httpbin.org

HttpRequest* post_req = new HttpRequest(network, HTTP_POST, "http://192.168.0.57:5500/status"); post_req->set_header(“Content-Type”, “application/json”);

const char body = “{"hello":"world"}”;

HttpResponse* post_res;

post_res = post_req->send(body, strlen(body));

if (!post_res) { printf(“HttpRequest failed (error code %d)\n”, post_req->get_error()); return 1; }

printf(“\n - HTTP POST response - \n”); dump_response(post_res);

delete post_req; }

int main() {

pb.mode(PullUp);

char buf[10]; Connect to the network with the default networking interface if you use WiFi: see mbed_app.json for the credentials NetworkInterface network = connect_to_default_network_interface(); if (!network) { printf(“Cannot connect to the network, see serial output\n”); return 1;*

}

pb.fall(&pb_hit_interrupt, network);

reafing the switch

button_state = pb.read(); button_state = !button_state;

printf(“button state: %d\n”, button_state); myled = button_state;

sprintf(buf, “%d”, button_state);

Do a GET request to httpbin.org { By default the body is automatically parsed and stored in a buffer, this is memory heavy. To receive chunked response, pass in a callback as last parameter to the constructor. HttpRequest get_req = new HttpRequest(network, HTTP_GET, "http://192.168.0.57:5500/status");*

HttpResponse* get_res = get_req->send(); 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; }

wait(osWaitForever); }

  1. endif

Please help.

Thank you in advance

Nada

When you press the button you end up in an ISR (interrupt context). You’re not allowed to call HTTP operations from an interrupt context. Thus, you need to signal back from the ISR to your main thread and do the operation there.

You can do this manually through a Semaphore, or by using the EventQueue. More information and some patterns are in this blog post.