• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

[SOLVED] ESP32/ESP8266 Web Server Drops Connection/Crashes

Q&A Forum › Category: ESP32 › [SOLVED] ESP32/ESP8266 Web Server Drops Connection/Crashes
0 Vote Up Vote Down
Rui Santos Staff asked 5 years ago

If you’ve followed one of the web server projects in the “Learn ESP32 with Arduino IDE” course and your web server stops working (drops connection/crashes) on Google Chrome, here’s how to solve that problem.

Open this link for the new web server code: https://gist.github.com/RuiSantosdotme/d5a9d2536c1470008a850b5e01261fb5

Basically, you’ll need to add a timeout timer in any web server code presented in the course to fix this issue. Here’s the exact lines that you need to insert in any web server example.

Before the setup() (take a look at lines 27 to 32), add the following to define these three variables:

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0; 
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

Add inside the loop() and after the if(client) { (line 62 and 63):

currentTime = millis();
previousTime = currentTime;

Modify the while(client.connected()) { to look like this (line 66 and 67):

while (client.connected() && currentTime - previousTime <= timeoutTime) {
currentTime = millis();

That’s it. This problem should be solved by implementing these few lines of code in any web server presented in the course.

Note: this problem only happens in Google Chrome and I think it’s because the web browser leaves a connection open (it’s not a problem with the code, but on how the Chrome web browser works).

With this new code, the ESP32/ESP8266 automatically creates a timeout to close any connection after 2 seconds.

I hope this solves the problem and let me know if it works for you.

Learn more about the ESP32 or ESP8266 GPIOs below:

  • ESP32 Pinout and GPIOs Reference Guide
  • ESP8266 Pinout and GPIOs Reference Guide

Regards,

Rui

Question Tags: ESP32
1 Answers
0 Vote Up Vote Down
Michael Mummert answered 3 years ago

Thank you for that, it also happens when you log in from an Android phone.
 
Mike

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] SMART HOME with RPi, ESP32, and ESP8266 (version 1.2) March 8, 2023
  • [eBook Updated] ESP32-CAM Projects (version 1.3) January 7, 2023

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2023 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.