long non-responsive intervals while serving webpage


I am developing this controller for my pump room. It will monitor the level of my rainwater tank (pressure sensor), the conductivity of my pool water (copper ion disinfection) etc. It displays data on a TFT, shows it on a web page and sends an email, when certain boundary levels are reached. Oh, and it also gets NTP time quite often, as Covid-19 has my RTC shield in its grip…

This is the current scope. Once I have the bugs out, it will monitor several other parameters and it will also include routines to calibrate the various sensors (pH, EC, pressure, ORP, temp etc) and grab environmental data from my Davis Instruments meteorological station, to manage all of my water needs, controlling valves etc. (if you feel that this might be too much for the Mega and I should be looking to something else(RPi?), now would be a good time to speak up too…)

Using a mega 2560, ethernet shield, a 3.5"kuman tft screen with touch, and a little EXO stamp by Atlas Scientific to condition the EC probe.

Overall, the sketch seems to work, displaying data on the screen and webserver, sending emails and getting time . However, when observing the tftscreen while moving the potentiometer simulating the pressure sensor, I notice that it seems to stop sampling/updating, temporarily, when:
1- the unit is supposed to send a email and/or
2- a client is connected to the server.

I realize that there are 3 competing items accessing the internet, webserver, email and NTP. Since I’m new to this, I’m guessing there might be some redundancies in the code and/or incompatible calls.

Feel free to critique any and all parts of this code. I’m eager to learn.

The sketch is divided in Tabs. the tab names are commented in the first line of code… its exceeds the 9000 character limit for the forum, so its attached as a ZIP file.

Thanks for your help.

Pump_Room_Server.zip (9.86 KB)

If your webserver only serves up the single page, you can move your client.stop() command inside your while (client.connected()) loop since you only need to print the page and then close the connection. That will speed up the web server. As for the email, you have to transmit all that data.

Also, you are taking 100 samples and storing the sum in an ‘int’ which will not fit. Analog read can return up to 1023 so that means the sum can be 102300 which exceeds the maximum int value (32767). You need to make sensorValue7 a ‘long int’. Also, that float (sensorValue7) does absolutely nothing.

  for (int sampleCount = 0; sampleCount < samples; sampleCount++) {
    sensorValue7 = sensorValue7 + analogRead(analogChannel);
    float (sensorValue7);
    delay (1);

Thanks blh64;

Got the long int implemented successfully.

However, when I move the client.stop() inside the while (client.connected()) loop, it stops working. the Webpage gives me a couple different errors, depending on where I place the client.stop().

I put comments in the code, where I put it and what I get…

 // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // 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
          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: 5");  // refresh the page automatically every 5 sec
          client.println("<!DOCTYPE HTML>");
          // send pressure value to web page
          client.print("Rain Water: ");
          client.print(pressureValue7, 2);
          client.print(" m3");
        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;
        //////// WHEN I MOVE IT HERE I get a "x.x.x.x didn't send any data" error
      //////// WHEN I MOVE IT HERE I get a "x.x.x.x took too long to respond" error
    // give the web browser time to receive the data
    // close the connection:
    client.stop();   ///////////////////this is the line I'm moving
    Serial.println("client disconnected");

Sorry for the basic questions…

You can stop your client after you serve up your webpage (all those HTML print statements clause)

You can stop your client after you serve up your webpage (all those HTML print statements clause)

Right before the break; statement?

tried that too, not much of a difference.

If I close the web browser and reset the arduino, the tft responds rather quickly, with a slight delay every 5 seconds, while its getting NTP time.

The moment I point a browser to the server address, it works for well for about 30 seconds, then it hangs for 16-17 seconds at a time.

I changed the timing for the NTP time from 5000 to 15000 millis, to see, if this is causing it, but the behaviour remains the same.