Author Topic: ESP32 Wifi problem.  (Read 1569 times)

0 Members and 1 Guest are viewing this topic.

Offline LindleyTopic starter

  • Regular Contributor
  • *
  • Posts: 240
  • Country: gb
ESP32 Wifi problem.
« on: November 16, 2025, 09:32:39 am »
Hi,

Wifi newbie looking for a bit of guidance please.

Have got this ds18B20 web server code up and running ok and have added the auto refresh to act every 60 seconds.
https://lastminuteengineers.com/multiple-ds18b20-esp32-web-server-tutorial/

Have also added a lcd so we can see the temperatures locally.

The problem is that if we close the PC or Mobys Wifi connection with whole program code hangs and the local display stops.

Have looked around the web and tried a few things but unable to see how to allow the rest of the code to run when the client  is not connected ?
« Last Edit: November 16, 2025, 05:05:57 pm by Lindley »
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 4272
  • Country: us
Re: ESP32 Wifi problem.
« Reply #1 on: November 16, 2025, 11:12:25 am »
Is the "PCs connection" you refer to a serial port connection?

If you never establish a serial connection with the ESP32 it should run just fine -- e.g. power the ESP32 from a USB power bank or power it from one of your PC's USB ports but don't establish a serial connection to it.

Otherwise your problem might be caused by the way the ESP32 responds to the DTR serial signal:

https://stackoverflow.com/questions/76553420/esp32-consistently-restarting-after-closing-serial-connection

https://electronics.stackexchange.com/questions/666808/esp32-program-completely-halts-when-serial-terminal-is-disconnected

Setting up the watchdog timer can help you to recover in case something causes your ESP32 to hang, e.g. see:

https://forum.arduino.cc/t/watchdog-reset-esp32-if-stuck-more-than-120-seconds/1266565
« Last Edit: November 16, 2025, 11:22:24 am by ledtester »
 
The following users thanked this post: Lindley

Offline LindleyTopic starter

  • Regular Contributor
  • *
  • Posts: 240
  • Country: gb
Re: ESP32 Wifi problem.
« Reply #2 on: November 16, 2025, 05:05:39 pm »
Sorry, unintentionally confused things :-[

 No, as in that linked tutorial,  its a direct Browser Wifi connection from a pc or mobile phone, have now modified the question to clarify.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: ESP32 Wifi problem.
« Reply #3 on: November 16, 2025, 10:39:36 pm »
In the example code the ADCs are only read in the OnConnect() handler, you need to move these ADC reads out of this function and into the main loop along with some timing code to read them and update your display at a reasonable rate.  Do not use the delay() function to do this, use the millis() timer and only read the ADCs every ~1 second.  Continue calling the handleClient() function frequently.
 
The following users thanked this post: Lindley

Offline LindleyTopic starter

  • Regular Contributor
  • *
  • Posts: 240
  • Country: gb
Re: ESP32 Wifi problem.
« Reply #4 on: November 17, 2025, 10:47:31 am »
In the example code the ADCs are only read in the OnConnect() handler, you need to move these ADC reads out of this function and into the main loop along with some timing code to read them and update your display at a reasonable rate.  Do not use the delay() function to do this, use the millis() timer and only read the ADCs every ~1 second.  Continue calling the handleClient() function frequently.

Thanks,  the problem is that whatever routines we use, when it comes around to the  server.handleClient();   if the browser is not running or stops running on that port, the whole program just hangs there until its re-connected.

Have looked to see , without success due to our limited knowledge,  if there is some condition within that  server.handleClient(); routine that we can test and so bypass it with a simple IF  when the wifi is off.

The project idea is that we can monitor remotely the workshop temps  but also check them locally via the LCD, but the wifi will be off when we are away.

The sensors are all  Digital, though do wonder why you say avoid the Delay and use Millis ?


Code: [Select]
void  loop()  {

server.handleClient();
}

 
  void handle_OnConnect() {

TempMainLoop();       // call  Temp/lcd routine within wifi
   
  server.send(200, "text/html", SendHTML(tempAIRIN,tempAIROUT,tempR));
 
« Last Edit: November 17, 2025, 10:49:29 am by Lindley »
 

Offline ledtester

  • Super Contributor
  • ***
  • Posts: 4272
  • Country: us
Re: ESP32 Wifi problem.
« Reply #5 on: November 17, 2025, 11:55:12 am »
I would try using the async web server library as described here:

https://randomnerdtutorials.com/esp32-dht11-dht22-temperature-humidity-web-server-arduino-ide/

From that web page:

Quote
Building an asynchronous web server has several advantages as mentioned in the library GitHub page, such as:

* “Handle more than one connection at the same time”;
* “When you send the response, you are immediately ready to handle other connections while the server is taking care of sending the response in the background”;
* “Simple template processing engine to handle templates”;
And much more.

so if one web client behaves badly it shouldn't lock up the entire server.

 
The following users thanked this post: tooki, Lindley

Offline tooki

  • Super Contributor
  • ***
  • Posts: 16141
  • Country: ch
Re: ESP32 Wifi problem.
« Reply #6 on: November 17, 2025, 01:17:09 pm »
Indeed. In my thermometer project (which uses a giant 7-segment LED display, but also has a web server), the loop does the following:
- reads and filters the temperature
- converts to Fahrenheit if necessary
- formats the temperature for the LED display (my thermometer uses per-segment grayscale control, to mimic incandescent filament behavior, so the formatting code is much more complex than usual)
- formats the temperature string for the web server
- updates the display

Meanwhile, the async web server waits until a request comes in, and services it as needed.
Similarly, an interrupt is used to read the physical button (used for switching °C/F and adjusting display brightness).


(Actually, I use a library called TaskManagerIO to schedule the loop tasks at different intervals, so that it’s not trying to read the sensor a million times a second. Before reading the temperature, which does take a noticeable fraction of a second to complete on the DS18B20), I also make sure that the display code isn’t in the middle of updating the display, since a mid-update read causes the fading effect to stutter.)
 
The following users thanked this post: Lindley

Offline LindleyTopic starter

  • Regular Contributor
  • *
  • Posts: 240
  • Country: gb
Re: ESP32 Wifi problem.
« Reply #7 on: November 17, 2025, 04:55:57 pm »
@ledtester and @tooki,

Many thanks,  using the Async routine has fixed the problem for us  :-+

With the Browser closed or even the Routers Wifi switched off, all the program code still continues to run, yet once the browser is restarted it continues to send out the data properly.

@tooki  that TaskManagerIO looks an interesting routine  and we do ensure the sensors, 2 x DS18B20 and 1 x SHT31  have a long time between successive reads.
 
 
The following users thanked this post: tooki


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->