2

I need to receive data from sensors on the websites located on my PC (hosted with XAMPP).

When I was first using AsyncWebServer library and hosting them on ESP32 everything was working with a combination of those two:

response = String(digitalRead(button1)) + "," +String(digitalRead(button2)) + "," +
                String(digitalRead(button3)) + "," +String(digitalRead(button4)) + "," +
                String(angleX) + "," + String(angleY) + "," +
                String(sensor1value);

server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request){ request->send(200, "text/plain", response); });

and:

setInterval(function ( ) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
            var Answer = this.responseText;
            console.log(Answer);
            let values = Answer.split(',');
            document.getElementById("led1").style.backgroundColor = (values[0] == 0) ? 'green' : 'red';
          }
        };
    xhttp.open("GET", "/state", true); 
    xhttp.send();
  }, 100 ) ;

JS script was linked on dashboard.html page and changed the appearance of elements on it according to the values.

Now I need the same result with HTTPClient

String serverName = "http://192.168.0.20/glove/state.html";

/response = String(digitalRead(button1)) + "," + String(digitalRead(button2));/

response = "?button1=" + String(digitalRead(button1)) + "&button2=" + String(digitalRead(button2));

/response = "button1=" + String(digitalRead(button1)) + "&button2=" + String(digitalRead(button2)); /

WiFiClient client;
HTTPClient http; 

//String serverPath = serverName + response;
String serverPath = serverName;  
//Serial.println(serverPath);
http.begin(client,serverPath);

http.addHeader("Content-Type","application/x-www-form-urlencoded");
//http.addHeader("Content-Type","text-plain");
//http.POST(response);
int httpCode = http.GET();
//Serial.println(response);

//int httpCode = http.sendRequest("POST",response);
Serial.println(httpCode);

But I can't get it to work (was trying different combinations with GET and POST methods).

Nick Gammon
  • 38,901
  • 13
  • 69
  • 125
CaramelTr
  • 21
  • 1

0 Answers0