check for new client every 2 seconds

Hello

I have an arduino code which is measuring temperature and humidity from DHT 22 sensor and printing those values to lcd display … however, i wanted to make a web server and if client will connect it will print on lcd screen “device under maintenance” and when i wrote it, it just displayed nothing … here is my code :

#include <Ethernet.h>
#include <SPI.h>
#include <SD.h>
#include <LiquidCrystal.h>
#include <DHT.h>
#include <EthernetUdp.h>
#include <TimeLib.h>

#define DHTPIN 3
#define DHTTYPE DHT22
DHT dht (DHTPIN,DHTTYPE);

File webPage;
String HTTP_req;

const int rs = 5, en = 6, d4 = 7, d5 =8, d6 = 9, d7 = 2;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,243,253);
IPAddress subnet(255,255,252,0);
IPAddress gateway(192,168,240,2);
IPAddress dns(192,168,240,55);
IPAddress timeServer(192,168,240,55);

EthernetServer server(80);

const int timeZone = 2;
EthernetUDP Udp;
unsigned int localPort = 8888;

void setup() {  
  Ethernet.begin(mac,ip,dns,gateway,subnet);
  lcd.begin(16,2);
  server.begin();
  lcd.display();
  dht.begin();
  Serial.begin(9600);
  Udp.begin(localPort);
  setSyncProvider(getNtpTime);
}

void loop() {
  
  EthernetClient client = server.available();
  float t = dht.readTemperature();
  float h = dht.readHumidity();


  if (client) {
    lcd.setCursor(0,0);
    lcd.print("  DEVICE UNDER  ");
    lcd.setCursor(0,1);
    lcd.print("  MAINTENANCE  ");
    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(); // read 1 byte (character) from client
        if ( HTTP_req.length() < 80)
          HTTP_req += c;  // save the HTTP request 1 char at a time
        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");
          client.println();
          // send web page
          if (HTTP_req.indexOf("ajaxrefresh") >= 0 ) {
            ajaxRequest(client);  //update the analog values
            break;
          }
          else if (HTTP_req.indexOf("ledstatus") >= 0 ) {
            ledChangeStatus(client); //change the LED state
            break;
          }
          else {
            webPage = SD.open("index.htm");        // open web page file
            if (webPage) {
              while (webPage.available()) {
                client.write(webPage.read()); // send web page to client
              }
              webPage.close();
            }
            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(1);
    // close the connection:
    client.stop();
    HTTP_req = "";
    Serial.println("client disconnected");
  } // end if (client)


  else if ((isnan(h) || isnan(t)) && !client) {
    lcd.setCursor(2,0);
    lcd.print("TIME AND DATE");
    lcd.setCursor(0,1);
    lcd.print("  SENSOR ERROR  ");
  }
  else {
    
    lcd.setCursor(0,0);
    if (second()==1) {
      digitalClockDisplay();
    }
    else {
      lcd.setCursor(0,1);     // Sets cursor on the next line
      lcd.print(h,1);         // Prints the humidity value from the sensor
      lcd.print(" %   ");
      lcd.setCursor(9,1);
      lcd.print(t,1);
      lcd.print(" ");
      lcd.print(char(223));
      lcd.print("C");
    }
  }
}

void digitalClockDisplay() {
  printDigits(hour());
  lcd.print(":");
  printDigits(minute());
  lcd.print("  ");
  lcd.print(day());
  lcd.print(".");
  lcd.print(month());
  lcd.print(".");
  lcd.print(year()); 
}

void printDigits(int digits) {
  lcd.print("");
  if(digits < 10)
    lcd.print('0');
    lcd.print(digits);
}

const int NTP_PACKET_SIZE = 48;
byte packetBuffer[NTP_PACKET_SIZE];

time_t getNtpTime() {
  while (Udp.parsePacket() > 0) ; // discard any previously received packets
  sendNTPpacket(timeServer);
  uint32_t beginWait = millis();
  while (millis() - beginWait < 1500) {
    int size = Udp.parsePacket();
    if (size >= NTP_PACKET_SIZE) {
      Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
      unsigned long secsSince1900;
      // convert four bytes starting at location 40 to a long integer
      secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
      secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
      secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
      secsSince1900 |= (unsigned long)packetBuffer[43];
      return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
    }
  }
  return 0; // return 0 if unable to get the time
}

void sendNTPpacket(IPAddress &address) {
  // set all bytes in the buffer to 0
  memset(packetBuffer, 0, NTP_PACKET_SIZE);
  // Initialize values needed to form NTP request
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  // 8 bytes of zero for Root Delay & Root Dispersion
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;
  // all NTP fields have been given values, now
  // you can send a packet requesting a timestamp:                 
  Udp.beginPacket(address, 123); //NTP requests are to port 123
  Udp.write(packetBuffer, NTP_PACKET_SIZE);
  Udp.endPacket();
}



void ajaxRequest(EthernetClient client)
{
  for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
    int sensorReading = analogRead(analogChannel);
    client.print("analog input ");
    client.print(analogChannel);
    client.print(" is ");
    client.print(sensorReading);
    client.println("
");
  }
}

void ledChangeStatus(EthernetClient client)
{
  int state = digitalRead(LED_BUILTIN);
  Serial.println(state);
  if (state == 1) {
    digitalWrite(LED_BUILTIN, LOW);
    client.print("OFF");
  }
  else {
    digitalWrite(LED_BUILTIN, HIGH);
    client.print("ON");
  }
}

i think there is problem with neverending listening for clients … or in my written if conditions …
can someone please advise me ? thanks

and when i wrote it, it just displayed nothing

Did you try to connect to the server? How? With what results?

PaulS: Did you try to connect to the server? How? With what results?

yes i tried connecting on ip of arduino from chrome and internet explorer but it were just loading with no result ... alongside with opened serial monitor which should display "new client" it did nothing.

  lcd.print("");

Why?

Does your server work without the LCD and DHT sensor and without the NTP stuff?

PaulS:   lcd.print("");

Why?

Does your server work without the LCD and DHT sensor and without the NTP stuff?

no without ... with and i don't understand what do you mean

no without ... with and i don't understand what do you mean

Well, I don't understand this response.

It seems to me that you have thrown a lot of code together without testing that each piece works individually. I think you need to step back and test the web server code, without the LCD connected, and with all the stuff to write to the LCD removed. And without the DHT connected and without all the code to read from the DHT. And without the NTP server stuff.

When you KNOW that the server works, serving up data to clients, then add ONE new feature - the NTP server stuff, the LCD, OR the DHT - but only ONE of them.

If adding that device, and code, allows the new device to work and allows the server to still work, great. Repeat with another feature. If not, there is no point in adding even more hardware and code.

It works now, it prints the values from sensor to lcd, receives time from ntp server but when i try to connect to its ip address, the internet browser (chrome for me) displays “this site is not working” and lower “no response sent” and alongside with opened arduino serial monitor, when i press enter after typing ip address, serial monitor prints “new client” and then imidiatly “client disconnected” ... i suspect that problem might be in conditions.

Code cannot be send because arduino forum blocked me when i wanted to do so ...