Giter Site home page Giter Site logo

Comments (3)

Hieromon avatar Hieromon commented on August 18, 2024

The sketch is wrong. Handle handleClient outside the timer loop. That's the way to use the WebServer library.

from autoconnect.

GeorgeFlorian avatar GeorgeFlorian commented on August 18, 2024

The sketch is wrong. Handle handleClient outside the timer loop. That's the way to use the WebServer library.

In all of your examples you put handleClient or handleRequest inside void loop() {} .

If you know how to fix this sketch please help me. I wouldn't have bothered you, but I couldn't find any tutorial or guide.

from autoconnect.

Hieromon avatar Hieromon commented on August 18, 2024

ESP32 is multicore, but AutoConnect hosts the WebServer instances in the same core, not multitasking. AutoConnect::handleClient internally calls WebServer::handleClient, but it resides in the same thread. Therefore it is the same as ESP8266WebServer::handleClient even if running on ESP32. See the documentation.

In all of your examples you put handleClient or handleRequest inside void loop() {} .

If your sketch intends to respond to another HTTP request as a web server at the same time issuing a GET request every 10 seconds, delay(10000); stalls WebServer process because it runs in the same thread. So WebServer cannot respond to new HTTP requests. It will cause a timeout error on the client (usually a web browser). The outside of the timer loop means that handleClient must be performed under the loop() immediately avoid delay(10000);. Because it is not multitasking. I encourage you to first read all the necessary documentation for ESP32, ESP8266. AutoConnect assumes an understanding of the basic usage of the ESP8266WebServer library.

I don't know your sketch intention, but the sketch below will be helpful. It excludes AutoConnect from your sketch and made it just using the WebServer library. This is not work properly.
So, it is not a problem with AutoConnect. Make sure that you first the confirmation on the WebServer library only and post it.

#include <WiFi.h>
#include <WebServer.h>
#include <HTTPClient.h>

WebServer Server;

void rootPage() {
  char content[] = "Hello, world !";
  Server.send(200, "text/plain", content);
}

void setup () {
  delay(1000);
  Serial.begin(115200);
  Serial.println();

  Server.on("/", rootPage);
  WiFi.mode(WIFI_STA);
  WiFi.begin("YOUR_SSID", "YOUR_PASSWORD");
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
  }
  Serial.println("WiFi Connected: " + WiFi.localIP().toString());
  Server.begin();
}

void loop() {
  HTTPClient http;

  http.begin("http://jsonplaceholder.typicode.com/comments?id=10"); //Specify the URL
  int httpCode = http.GET();
  
  if (httpCode > 0) {
    String payload = http.getString();
    Serial.println(httpCode);
    Serial.println(payload);
  } else {
    Serial.println("Error on HTTP request");
  }
  http.end();

  Server.handleClient();     

  delay(10000);
}

from autoconnect.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.