ESP8266 simple program

Hello, i am making a very simple program using ESP8266, arduino libraries, coding in c. Anyway my program will read temperature/humidity of 2 sensors, turn on the heater and fan depending on those values and post them on server.

The way program is now done is that it reads the values, posts them on server and then makes a 1 second delay. I dont want to stress the sensors to much (can reading to much be bad for them ?) and would be ok with 10 second delay, reading temp every 10 seconds is fine since it doesnt change that fast. But the problem is if i make this delay, it also delays this piece of code:

server.handleClient();

What that means in practice is that basicly if i access server to check temperatures, i will have to wait up to 10 seconds because of the delay. Now my questions are this:

  1. is there any downside to reading sensor values every 1 second as oposed to every 10 seconds. Can this wear out sensors, esp8266 or something else ?

  2. if the answer to 1) is yes, then i would need a piece of code that will only delay function that reads sensors but NOT delay the server.handleClient(); call, so my server will be accessible without delay

i would need a piece of code that will only delay function that reads sensors but NOT delay the server.handleClient(); call, so my server will be accessible without delay

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and look at the BlinkWithoutDelay example in the IDE.

Sensors are not generally stressed by being used.

Using delay() in any sketch is generally bad for responsiveness. Using the "Blink Without Delay" (millis()) technique to limit data rate allow your sketch to be more responsive.

Thank you, i will look into that :slight_smile:

If it is just that calling server.handleClient(); nees to be called to keep the server responsive, you could switch to ESPAsyncWebServer or you could simply do something like this

uint32_t now = millis();
while (millis()-now < WAITTIME) server.handleClient();

instead of your delay(WAITTIME); though learning how to use millis() to create ‘non-blocking code’ is a very important programming skill.