Data stops sending after a few minutes.

I have a program that sends environmental data to a webpage. It works fine and sends data for a few minutes. Then it just stops. The Arduino is still on and running, there is just no output. My code is below. If anyone has any suggestions, I would be grateful.

#include <UIPEthernet.h>
#include <Wire.h> 
#include <BH1750.h>
#include <BME280.h>
#include <avr/wdt.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(10,1,110,15); 
static uint32_t tempTimer = millis() + 30000;
EthernetClient client;
BME280 bme;
BH1750 lightMeter;
const int floodSensor1 = A6;
const int floodSensor2 = A7;
int count = 0;
bool leak = false;
bool lightOn = false;

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ;
  }

  Serial.println("Start ethernet");
  Ethernet.begin(mac);
  delay(1000);
  Serial.println("connecting...");

  while(!bme.begin()){
    Serial.println("Could not find temp/humidity sensor!");
    delay(1000);
  }
  
  lightMeter.begin();
  delay(1000);
  sendData("light", "off");

  pinMode(floodSensor1, INPUT);
  pinMode(floodSensor2, INPUT);

  wdt_disable();
  wdt_enable(WDTO_8S);
 
}

void loop() {
  
  if (millis() > tempTimer) {
    tempTimer = millis() + 30000;

    int temp = bme.ReadTemperature(false);
    int hum = bme.ReadHumidity();
    String tempHum = String(temp-3) + ":" + String(hum);
    sendData("tempHum", tempHum);   
  }

  uint16_t lux = lightMeter.readLightLevel();
  if (lux > 50){
    if (!lightOn){
      sendData("light", "on");
    }
    lightOn = true;
  } else {
    if (lightOn){
      sendData("light", "off");
    }
    lightOn = false;
  }
  
  //Serial.println(analogRead(floodSensor1) + String(" - ") + analogRead(floodSensor2));
  //Serial.println(count);
  if (analogRead(floodSensor1) > 100 || analogRead(floodSensor2) > 100) {
    count += 1; 
  } else {
    count = 0;
  }
  
  if (count < 5){
    if (leak){
      sendData("leakDetected", "ok");
    }
    leak = false;
  } else {
    if (!leak) {
      Serial.println("Leak Detected");
      sendData("leakDetected", "leak");
    }
    leak = true;
  }
  
  wdt_reset();
  delay(1000);
}

void sendData(String where, String what){
  if (client.connect(server, 3000)) {
    client.print("POST /");
    client.print(where);
    client.print("/");
    client.print(what);
    client.println(" HTTP/1.1");
    client.println("Host: batman");
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println("2");
    client.println();
    client.println("go");
    client.println();
    //client.stop();
  } 

  delay(1000);
  if (client.connected()) {
      //Serial.println();
      //Serial.println("disconnecting.");
      client.stop();
    }
}

Put a few tests in to see what is happening. EG, flash the LED at certain points to see if the code is being run. Or use serial to write a msg to the terminal.

This way you can see if the code is working as you expect.

Weedpharma

While it won't cause a problem for 49 days this is not the correct way to use millis()

if (millis() > tempTimer) {
    tempTimer = millis() + 30000;

You should always use subtraction which automatically deals with the rollover after 49 days

if (millis() - previousMillis > intervalMillis) {
    previousMillis += intervalMillis;

See Several Things at a Time

...R

With multiple libraries, you are very low on RAM. The String class really cannot be used in this situation and, in general, should never be used in an embedded environment. Use char arrays instead; they're smaller, faster, and will allow your sketch to run forever.

To switch from String to char arrays (aka C strings), change this call:

   String tempHum = String(temp-3) + ":" + String(hum);
    sendData("tempHum", tempHum);

...to this:

    char what[32];                 // array to receive formatted chars
    itoa( temp-3, &what[0], 10 );  // "prints" temp-3 at the beginning of the array
    strcat( what, ":" );           // "appends" the colon char to the array
    uint8_t end = strlen(what);    // how many chars are in the array so far
    itoa( hum, &what[ end ], 10 ); // "prints" hum at the end of the array
    sendData( "tempHum", what );   // pass two arrays to sendData

(itoa function is described here, strcat here.) And change your sendData routine from this:

    void sendData(String where, String what){

...to this:

    void sendData( char *where, char *what ) {

Eliminating String will also save at least 1.6k in your binary sketch size.

You can save a few hundred bytes of RAM by using the F macro in print statements. Change all statements like these:

    client.print("POST /");
    client.println("Host: batman");

to these:

    client.print( F("POST /") );
    client.println( F("Host: batman") );

Cheers,
/dev

Thank you everyone. I have made the suggested changes and will see if it works any better. Could the small amount of dynamic memory I have left cause this problem? There is only 491 bytes available.