Issue Migrating "Arduino+WiFiEsp.h" to "ESP8266-12E+ESP8266WebServer.h" - SOLVED

I used an Arduino Mega2560 and an ESP-01 to develop a driveway gate monitor and web controller, and am now migrating it over to an ESP8266-12E board. In doing so, I changed from the WiFiEsp.h library, to the ESP8266WebServer library ... and a statement I was using to define a RingBuffer won't compile. I think this is because the RingBuffer statement was calling a function in the WiFiEsp library?

Considering the client response is very small, I don't think I really need a ring buffer anyway, so I changed it to a string. But now when I connect with a client, the response is never received and the client times out.

I've tried adjusting the code with the limited knowledge I have, but can't seem to get it to function.

I've placed the code here, but removed several areas due to it being over 9000 characters. I've attached the ino file with the full code as well though.

Right now, the ESP8266-12E board is just sitting on my breadboard with only a USB cable connected to my PC. So no schematic is relevant at this time.

When the board boots up, I get successful messages in Serial Monitor, so it looks like everything is booting and connecting fine. It appears I just have something off in the client handling.

*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Status:
*WM: 6
*WM: Using last saved values, should be faster
*WM: Connection result: 
*WM: 3
*WM: IP Address:
*WM: 192.168.0.177
<Controller is ready>
*WM: freeing allocated params!

I've commented out the lines I used with the Arduino and ESP-01, and added the lines I think I need for the 12E board directly underneath the commented out lines.

Any assistance is greatly appreciated!

//#include "WiFiEsp.h"
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>

// ********************************************************* DEFINITIONS 
// PIN DEFINITIONS -----------------------------------------------


// VARIABLE DEFINITIONS -----------------------------------------------
// Network Config  -----------------------------------------------
  WiFiServer server(80);                // Define web server name and port
  String buf = "";                      // Define string to hold web response from client
//  RingBuffer buf(8);                  // use a ring buffer to increase speed and reduce memory allocation

// Variables for Gate Monitoring and Control ------------------------------------


// Variables for Tracking Elapsed Time of Gate Status ---------------------------


// Variables for Monitoring Gate Controller's 12v Battery -----------------------

  
// ********************************************************* INITIALIZE 
void setup() {
// Initialize Serial and WiFi -------------------------------
  Serial.begin(115200);                                 // initialize serial for debugging
  WiFiManager wifiManager;                              // Connect to previous WiFi, if found
  wifiManager.autoConnect("MySetupAP");                 // If no previsous WiFi connection found, start configuration AP
  Serial.println("<Controller is ready>");              // Success message printed to serial monitor
  server.begin();                                       // Start the web server  
}

// ********************************************************* MAIN LOOP 
void loop() {
  ReadGateSensors();                //Call function to read the current state of the gate reed sensors
  SetGateStatus();                  //Determine and set the gate status
  HandleClient();                   //Handle any requests coming in over WiFi
  ReadBatteryVoltage();             //Check the voltage of the gate controller's 12v battery  
  if (CommandReceived == 1) {       //If a WiFi command has been received...
    ProcessGateCommand();           //... then call the function to process the command
    }  
}

// *********************************************************  FUNCTION DEFINITIONS

// Handle Client Connections ------------------------------------------------------------
void HandleClient() {                         
  WiFiClient client = server.available();               // listen for incoming clients
    if (!client) {                                      // If no client ...
    return;                                             // ... Return  
  }
//    buf.init();                                         // initialize the circular buffer
    while (client.connected()) {                        // loop while the client's connected
      if (client.available()) {                         // if there's data to read from the client
        char c = client.read();                         // read a byte
//        buf.push(c);                                    // push it to the ring buffer
        buf += c;

        if (buf.endsWith("\r\n\r\n")) {                 // If two newline characters in a row
          sendHttpResponse(client);                     // Call HTTP response function
          break;                                        // Exit function
        }
        
        if (buf.endsWith("GET /O")) {                   // If data from client has "GET /O"
          CommandReceived = 1;                          // Set Command Received to 1
          OpenGate = 1;                                 // Set OpenGate to 1, so gate will open when ProcessGateCommand() is called
        }
        else if (buf.endsWith("GET /S")) {              // If data from client has "GET /S"
          CommandReceived = 1;                          // Set Command Received to 1
          StopGate = 1;                                 // Set StopGate to 1, so gate will stop when ProcessGateCommand() is called
        }
        else if (buf.endsWith("GET /C")) {              // If data from client has "GET /C"
          CommandReceived = 1;                          // Set Command Received to 1
          CloseGate = 1;                                // Set CloseGate to 1, so gate will close when ProcessGateCommand() is called
        }
      }
    }
   client.stop();                                       // Close the connection
   buf = "";
}

// SEND HTTP REQUEST TO THE CLIENT ------------------------------------------------------------
//void sendHttpResponse(WiFiEspClient client) {
void sendHttpResponse(WiFiClient client) {
  client.println("HTTP/1.1 200 OK");
  client.println("Content-type:text/html");
  client.println("Connnection: close");
  client.println();
  client.println("<!DOCTYPE html>");                     
  client.println("<html>");
  client.println("<head>");
  client.println("<title>Gate Monitor</title>");
//  client.println("<meta http-equiv=\"refresh\" content=\"1\" URL=\"//192.168.0.158:80/\">");  //Refresh the client every one second
  client.println("<style type=\"text/css\">");
  client.println(".tftable {width:950px;border-collapse: collapse; table-layout: fixed;}");
  client.println(".tftable tr {background-color:#d4e3e5;}");
  client.println(".tftable td {font-size:40px;}");
  client.println("h1 {font-size:45px;}");
  client.println("h2 {font-size: 45px; Line-Height:0px;}");
  client.println("h4 {Line-Height:0px;}");
  client.println("</style>");
  client.println("</head>");
  client.println("<body>");  
  client.println("<form method=\"get\" action=\"/O\"><button type=\"submit\" style=\"background-color:blue; border-color:blue; color:white\">Open Gate</button></form>
");
  client.println("<form method=\"get\" action=\"/S\"><button type=\"submit\" style=\"background-color:red; border-color:black; color:white\">Stop Gate</button></form>
");
  client.println("<form method=\"get\" action=\"/C\"><button type=\"submit\" style=\"background-color:blue; border-color:red; color:white\">Close Gate</button></form>
");
  client.println("<button onclick=\"window.location.href = 'http://192.168.0.158';\">Refresh Timer</button>");
  client.print("<h1 height=\"0.8\">Gate Status: " + (GateStatus));
  client.print("<h4 height=\"0.8\">For: ");
  if (Hours != 0) {
      client.print(Hours);
      client.print(" hrs ");
      }
  if (Minutes != 0) {
      client.print(Minutes);
      client.print(" mins ");
      }
  client.print(Seconds);
  client.println(" secs</h4>");

  client.print("<h4>Battery Voltage: ");
  client.print(vin);
  client.println("v</h4>");
  
  client.println("</body>");
  client.println("</html>");
  client.println();
}

sketch_Apr3a.ino (16.5 KB)

Ok, I figured this out.

Turns out it was an analogRead function, when reading A0 (the only ADC pin on the ESP8266-12E).

Apparently, if you read the analog pin too frequently, it'll cause the WiFi to hang.

So where "Battery_PIN = A0;" my code was like this:

// Check Gate Controller's Battery Voltage Level  --------------------------------------------------------
void ReadBatteryVoltage() {
   if (v == 1000) {                                                             //If v has reached 1000 cycles, meaning 1000 voltage readings of the gate controller's 12v battery have been taken
    vin = volt / v;                                                             //Set voltage variable (vin) to the average of the 1000 readings
    v = 0;                                                                      //reset v to 0
    volt = 0;                                                                   //reset volt to 0
   }
   v++;                                                                         //increment v counter by 1
   volt += ((analogRead(Battery_PIN) * 3.3) / 1024.0) / (R2/(R1+R2));           //Read the gate controller's 12v battery voltage, adjust for the voltage divider, add it to current value of volt
}

I changed it to this, and it works now:

// Check Gate Controller's Battery Voltage Level  --------------------------------------------------------
void ReadBatteryVoltage() {
  if (v == 100) {                                                             //If v has reached 1000 cycles, meaning 1000 voltage readings of the gate controller's 12v battery have been taken
   vin = volt / v;                                                             //Set voltage variable (vin) to the average of the 1000 readings
   v = 0;                                                                      //reset v to 0
   volt = 0;                                                                   //reset volt to 0
  }
  if (t == 100) {
    volt += ((analogRead(Battery_PIN) * 3.3) / 1024.0) / (R2/(R1+R2));           //Read the gate controller's 12v battery voltage, adjust for the voltage divider, add it to current value of volt
    v++;
    t = 0;
  }
  t++;
}