s00500 / ESPUI

A simple web user interface library for ESP32 and ESP8266
https://valencia.lbsfilm.at/midterm-presentation/
Other
923 stars 174 forks source link

HTTP GET Request #120

Closed Marcel199765 closed 3 years ago

Marcel199765 commented 3 years ago

Hi Guys,

I have problems with a simple HTTP GET Request: When I press a Button on the ESPUI, I will switch an wifi-relay (Shelly). But therefore I need to send the Request: "http://192.168.2.224/relay/0?turn=on". I don't understand why it is not working, because without the ESPUI everythink is working well. I think the AsyncWebSocket used in ESPUI is not compatibel with the simple HTTP Client and WiFiClient?

Can someone help me with this issue? It would be nice if someone can give me a hint or tell me what I'm doing wrong ;-)

Working Code on ESP8266 without ESPUI:


void httpSendRequest(String adress){

    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    if (http.begin(client, adress)) {  // HTTP

      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
           Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
}

The same function responses "connection failed" when I implement this together with ESPUI. After two days of searching the Web and trying different things, I gived up...

Best regards

P.S. The ESPUI-Library is very awesome! I Love it!

s00500 commented 3 years ago

can you maybe post the non working code ? Finding the problem in working code is apparently not so easy

nodoubtman commented 3 years ago

Hi Guys,

I have problems with a simple HTTP GET Request: When I press a Button on the ESPUI, I will switch an wifi-relay (Shelly). But therefore I need to send the Request: "http://192.168.2.224/relay/0?turn=on". I don't understand why it is not working, because without the ESPUI everythink is working well. I think the AsyncWebSocket used in ESPUI is not compatibel with the simple HTTP Client and WiFiClient?

Can someone help me with this issue? It would be nice if someone can give me a hint or tell me what I'm doing wrong ;-)

Working Code on ESP8266 without ESPUI:


void httpSendRequest(String adress){

    WiFiClient client;

    HTTPClient http;

    Serial.print("[HTTP] begin...\n");
    if (http.begin(client, adress)) {  // HTTP

      Serial.print("[HTTP] GET...\n");
      // start connection and send HTTP header
      int httpCode = http.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTP] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = http.getString();
           Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
      }

      http.end();
    } else {
      Serial.printf("[HTTP} Unable to connect\n");
    }
}

The same function responses "connection failed" when I implement this together with ESPUI. After two days of searching the Web and trying different things, I gived up...

Best regards

P.S. The ESPUI-Library is very awesome! I Love it!

Of course you can. If you have Node-RED, you install the node : WebSocket

for the websocket config: you put this url: let's say your ip adress is 192.168.0.134, yout put:

ws://192.168.0.134/ws

and for the value 1 you can send: {"type":105,"value":"1","id":1,"color":6}

and for off you can send: {"type":105,"value":"0","id":1,"color":6}

it may differs, but you can listen to the websocket message... in node-red

hope it helps :o)

Marc.

and for the switch you take websocket out

mbellot1 commented 3 years ago

I'm not sure if I should tag on to this issue or start a new one since it appears I am having a very similar problem with ESPUI and the Arduino libraries ESP8266HTTPClient, and WiFiClient.

I'm trying to control Tasmota modules with a NodeMCU-12E, using both a switches connected to the module and via the ESPUI web interface.

The Tasmota looks similar to the Shelly in the original post, the relay is turned ON/OFF via a specific URL. I have created a function to encode the URL based on three inputs (IP address of the Tasmota, Relay number and whether the relay should be commanded ON or OFF).

Using the real switches the function gets called and the Tasmota responds as expected. Using the ESPUI button to call the same function with the same inputs I get an HTTP error -1. The debug output shows the same url being generated by the function, so I'm at a loss as to why one works and the other fails.

The key value and button ID are different because ESPUI does not allow numbering of controls. It seems to be sequential from first to last, which can be a real pain if you have to re-order the UI.

This a sample serial (debug) output from the NodeMCU board.

Key value:16 Module: 1 - ON IP Addr: 192.168.0.31 Output: 1 http://192.168.0.31/cm?cmnd=Power1+ON HTTP Response code: 200 {"POWER1":"ON"}

Button DOWN 19 Module: 1 - ON IP Addr: 192.168.0.31 Output: 1 http://192.168.0.31/cm?cmnd=Power1+ON HTTP Response code: -1

nodoubtman commented 3 years ago

You should use MQTT for this :)

Have a good evening :) Marc.

mbellot1 commented 3 years ago

Marc - I do not want to use MQTT for this particular project.

Any thoughts on the actual problem?

orithena commented 3 years ago

@mbellot1 I'm having a hunch that ESPUI's answer to its frontend is still hogging the ESP's network output buffer or something like that. Could you try running your request to the Tasmota in the next loop() iteration?

mbellot1 commented 3 years ago

orithena - great minds... I was coming back to report success.

I ended up adding a global variable (button_pressed) and assigned it the same value as the hardware button based on the ESPUI button sender->id inside the button callback function.

Inside loop() I added a check for button_pressed to call the keyhandler and magically everything started working.

Moral of the story, it looks like you can't do non-ESPUI network actions inside the ESPUI callback functions.

Thanks for the thoughtful input!