ESP8266 / ADC / Webserver --> after some time the server is unavailable

Hi Community!

I’m new to the whole arduino thing and crafted myself a water-pressure sensor for my deep well (~ 22m)
This is the sensor.

Everything works fine and quite accurat (a calibrated the sensor with a small test station i built), but after some time (i think its about 36-48hours) the webserver page is no more reachable. After reset it works well again for this period of time.

Could it be that the reading is too fast for ADC/ analog pin? Should i increase the “delay” what would be a recommendation?

What the programm should do:

  • read the anlog signal and build an average (after 10 readings)
  • output this data via WIFI on a webserver with a static IP
  • output this data on the serial monitor (just for checking with laptop etc.)
  • output also the WIFI signal strength

And this is my code - PLEASE - I’m no Coder or programmer, I read and copied a lot from different tutorials and other projects and adopted to my project as best as i could

// Library für WiFi-Verbindung
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiClient.h>
#include <SPI.h>

IPAddress staticIP(10, 0, 0, 85);      // statische IP des NodeMCU ESP8266
IPAddress gateway(10, 0, 0, 138);        // IP-Adresse des Router
IPAddress subnet(255, 255, 255, 0);         // Subnetzmaske des Netzwerkes
IPAddress dns(8, 8, 8, 8);            // DNS Server



// Daten des WiFi-Netzwerks
const char* ssid     = "my_SSID";
const char* password = "my_PW";

const char* deviceName = "Brunnen";

// Port des Web Servers auf 80 setzen
WiFiServer server(80);

// Variable für den HTTP Request
String header;

//Glättung
const int numReadings = 10;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average

int inputPin = A0;
int Analog = 0; 

void setup() {
 Serial.begin(115200);

 // initialize all the readings to 0:
 for (int thisReading = 0; thisReading < numReadings; thisReading++) {
   readings[thisReading] = 0;
 }

 // Mit dem WiFi-Netzwerk verbinden
 Serial.print("Connecting to WiFi");
 
 WiFi.disconnect();  //Prevent connecting to wifi based on previous configuration
 
 WiFi.hostname(deviceName);  // DHCP Hostname (useful for finding device for static lease)
 WiFi.config(staticIP, gateway, subnet, dns);
 WiFi.begin(ssid, password);

 WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only)

 //auf Verbindung mit WiFi_Netzwerk warten
 while (WiFi.status() != WL_CONNECTED) {
   delay(500);
   Serial.print(".");
 }
 // Lokale IP-Adresse im Seriellen Monitor ausgeben und Server starten
 Serial.println("");
 Serial.println("WiFi connected");
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 server.begin();

 
}

void loop() {
 // listen for incoming clients
 WiFiClient client = server.available();
 if (client) {
   Serial.println("new client");
   // an http request ends with a blank line
   bool currentLineIsBlank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       Serial.write(c);
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply
       if (c == '\n' && currentLineIsBlank) {
         // send a standard http response header

           // Der Server sendet nun eine Antwort an den Client
         client.println("HTTP/1.1 200 OK");
         client.println("Content-Type: text/html");
         client.println("Connection: close");  // the connection will be closed after completion of the response
         client.println("Refresh: 20");  // refresh the page automatically every 15 sec
         client.println();
         client.println("<!DOCTYPE HTML>");
         client.println("<html>");
           

 // subtract the last reading:
   total = total - readings[readIndex];
 
 // read from the sensor:
   readings[readIndex] = analogRead(inputPin);
 
 // add the reading to the total:
   total = total + readings[readIndex];
 
 // advance to the next position in the array:
   readIndex = readIndex + 1;

 // if we're at the end of the array...
 if (readIndex >= numReadings) {
   // ...wrap around to the beginning:
   readIndex = 0;
 }

 // calculate the average:
 average = total / numReadings;
 
 Serial.print("Indexnummer: ");
 Serial.println(readIndex);

 Analog = analogRead(inputPin);

 Serial.print("Analoger Wert: ");
 Serial.println(Analog);
 
 Serial.print("Summe analoger Werte: ");
 Serial.println(total);

 Serial.print("Durchschnitt: ");
 Serial.println(average); // Ausgabe am seriellen Monitor
 delay(2);        // delay in between reads for stability 

 long rssi = WiFi.RSSI();
 Serial.print("RSSI:");
 Serial.println(rssi);

         // output the value of each analog input pin       
         
         for (int analogChannel = 0; analogChannel < 1; analogChannel++) {
           int sensorReading = analogRead(analogChannel);
           client.print("analog input ");
           client.print(analogChannel);
           client.print(" is ");
           client.println(average);      // send it to the computer as ASCII digits
                       
           delay(2);        // delay in between reads for stability 
           
           client.println("
");

         long rssi = WiFi.RSSI();
         client.println("RSSI:");
         client.print(rssi);
         
         }

         
           
         client.println("</html>");
         break;
       }
       if (c == '\n') {
         // you're starting a new line
         currentLineIsBlank = true;
       } else if (c != '\r') {
         // you've gotten a character on the current line
         currentLineIsBlank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(10);

   // close the connection:
   client.stop();
   Serial.println("client disconnected");
 }
}

Would you help me with your suggestions and/ or instructions to get the webserver stable?

Kind regards
VR1

The problem is analogRead. The esp8266 uses the ADC to evaluate the strength of the WiFi signal. Hard use of analogRead disturbs it and the signal goes down. Use millis() to time analogRead.

    unsigned int tempSensRead() {
    
      const unsigned long MEASURE_INTERVAL = 2L * 1000 * 60; // 2 minutes
      static unsigned long lastMeasureMillis;
      static unsigned int lastValue;
    
      if (millis() - lastMeasureMillis > MEASURE_INTERVAL) {
        lastMeasureMillis = millis();
        lastValue = analogRead(A0);
      }
      return lastValue;
    }

Hi Juraj,

thanks for the fast reply!
ok seems logic that this causes the webserver failure...

just for my understanding - the code you supported
shall be put in the "loop" section?

Does it matter to put in first or last in this section?

Thank you so much in advance.

extraordinary85:
Hi Juraj,

thanks for the fast reply!
ok seems logic that this causes the webserver failure...

just for my understanding - the code you supported
shall be put in the "loop" section?

Does it matter to put in first or last in this section?

Thank you so much in advance.

it is a function as example. it is from one of my projects