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() {
  while (!Serial) {

  Serial.println("Start ethernet");

    Serial.println("Could not find temp/humidity sensor!");
  sendData("light", "off");

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


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));
  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;

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

  if (client.connected()) {

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.


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


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); 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){ 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") );


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.