delay function unexpected behaviour for longer delay times

I’m using an Arduino Uno Wifi Rev2 for my boat sensor project. The aim is to have 5 sensors data sent to my webserver every 5 hours. Inbetween, the board does not have to do anything, so I plan to use the delay() function. Precision in the actual delay is not very important, it just needs to be somewhere around 4 to 6 hours.

I have researched several topics on this forum with regard to using delay with longer times (and hence quite large millisecond numbers), but so far none of them worked. In practice, even when using 18 mio millisecs, de actual delay time was never longer than 1h15min.

Several options were tested , each time with a variable defined upfront and used further down in the sketch as delay(delayTime)

  1. const unsigned long delayTime = 18000000
  2. const unsigned long delayTime =18000000UL
  3. const unsigned long delayTime=5UL60UL60UL*1000UL
  4. and even simply delayTime=18000000

Actual delays vary between about 10 mins and max about 1h15.

Just to be sure: here is the full sketch in its latest version:

// DHT sensor library - Version: Latest 
// Settings for DHT11
#include <DHT.h>
#include <DHT_U.h>
#define Type DHT11
int sensePin=2;

DHT HT (sensePin,Type);
float humidity;
float tempC;
float tempF;
int setTime=500;
const unsigned long delayTime=4UL*60UL*60UL*1000UL;

// Settings for water sensor
#define SIGNAL_PIN A0
int valueWater = 0; // variable to store the sensor value

// Settings for voltage sensors
int offset =0;
float voltmotor;
float voltagemotor;
float voltserv1;
float voltageserv1;
float voltserv2;
float voltageserv2;
float current;

//Settings for wifi
#include <WiFiNINA.h>
char ssid[] = "************";
char pass[] = "************";
int status = WL_IDLE_STATUS;
char server[] = "secondrival.ddns.net";
WiFiClient client;

void setup() {
  //Setup for Wifi connection
    Serial.begin(9600);

  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);
    status = WiFi.begin(ssid, pass);
    delay(10000);
  }

  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  IPAddress ip = WiFi.localIP();
  IPAddress gateway = WiFi.gatewayIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
  
  // Setup for DHT11:
  Serial.begin(9600);
  HT.begin();
  delay(setTime);
}

void loop() {
  // Read the DHT11 sensor data:
  humidity=HT.readHumidity();
  tempC=HT.readTemperature();

  // Read the water sensor data:
  valueWater = analogRead(SIGNAL_PIN);

  // Read the motor voltage sensor data
  voltmotor = analogRead(A0);// read the input
  voltagemotor = map(voltmotor,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltagemotor /=100;// divide by 100 to get the decimal values

  // Read the service batteries voltage sensor data
  voltserv1 = analogRead(A1);// read the input
  voltageserv1 = map(voltserv1,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv1 /=100;// divide by 100 to get the decimal values
  voltserv2 = analogRead(A2);// read the input
  voltageserv2 = map(voltserv2,0,1023, 0, 2500) + offset;// map 0-1023 to 0-2500 and add correction offset
  voltageserv2 /=100;// divide by 100 to get the decimal values

  // Print all data to serial monitor
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" % ");
  Serial.print(" Temperature: ");
  Serial.print(tempC);
  Serial.print(" C ");
  Serial.print(" Water: ");
  Serial.println(valueWater);
  Serial.print("VoltageMotor: ");
  Serial.print(voltagemotor);//print the voltagemotor
  Serial.println("V");
  Serial.print("VoltageServ1: ");
  Serial.print(voltageserv1);
  Serial.println("V");
  Serial.print("VoltageServ2: ");
  Serial.print(voltageserv2);
  Serial.println("V");

  Sending_To_phpmyadmindatabase(); 
  delay(5000); // interval before sending to webserver

}

 void Sending_To_phpmyadmindatabase() {  //CONNECTING WITH MYSQL
 
   if (client.connect(server, 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /post.php?humidity=");
    client.print("GET /post.php?humidity=");
    client.print(humidity);
    client.print("&temperature=");
    client.print(tempC);
    client.print("&water=");
    client.print(valueWater);
    client.print("&voltagemotor=");
    client.print(voltagemotor);
    client.print("&voltageserv1=");
    client.print(voltageserv1);
    client.print("&voltageserv2=");
    client.print(voltageserv2);   
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: *********.ddns.net");
    client.println("Connection: close");
    client.println();
    } 
  
    else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    }
   
  //Delay for defined time before next reading
  delay(delayTime);
}

I'd guess that the Uno is restarting for some reason. Can you see that from your serial output?

I'm using an Arduino Uno Wifi Rev2 for my boat sensor project.

Where is the Arduino powered from ?
If it is on a boat then is anything else electrical running ?

UKHeliBob:
Where is the Arduino powered from ?
If it is on a boat then is anything else electrical running ?

It's not on the boat yet, I'm trying to finalize this at home first. This behaviour occurs with different power sources: USB from laptop, 9V battery, as well as a with a fully loaded 5V 2A powerbank

wildbill:
I'd guess that the Uno is restarting for some reason. Can you see that from your serial output?

Not that I can tell: just connecting, serial printing the sensor data, posting to webserver and then disconnecting from the selected wifi-network. Any specific statements I need to look for?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.