Change Item state with a nodemcu ESP8266

  • Platform information:
    • Hardware: Raspberry 3B
    • OS: what OS is used and which version
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 2.5.1

Hello,
I’m trying to change some states of several items via ResApi with a http-post request postet from a nodemcu. So when I post the http request, I get the response code 400 but the item state doesn’t change.
Can someone help me to solve the issue?

My code:

http.begin(“http://” SERVER_IP “:8089/rest/items/Switch”);
http.addHeader(“text/plain”, “application/json”);
int httpCode = http.POST(“OFF”);

The command in the RestAPI Documentation of OpenHab looks like:

curl -X POST --header “Content-Type: text/plain” --header “Accept: application/json” -d “OFF” “http://SERVER_IP :8089/rest/items/Switch”

400 is a Bad Request, which means it has failed because the request is somehow incorrect.

I don’t know the answer, but the API adds two headers, whilst you’re only adding one header which seems to be a mixture of the two? Why not add the two headers as done exactly by the rest API?

If you can’t get this to work, a work around might be to move to using MQTT instead. Not only would that make your ESP more generic and not strictly tied openHAB, it tends to be more energy efficient and has a bunch of nice features like disconnection reporting and the like built in.

Note, most of the ESP firmware like Tasmota and ESPEasy support MQTT out of the box. I run ESPEasy on most of my NodeMCUs.

1 Like

Thank you for the answers.
The hint with two headers helped me. I’ve changed the request to the following code snippet and now it works.

Code:
http.addHeader(“Content-Type”,“text/plain”);
http.addHeader(“Accept”,“application/json”);

Regards,
ugene