Change conditions automatically after 24 hours using millis

Hello, i have a problem with my arduino program, I need help. In my arduino program, it will send temperature and humidity data to the server every 30 seconds and in this program will be changed the condition (“Temperature Limit”) automatically after 24 hours using millis.

I tested the program on September 30, 2018, at 10:30 AM. The program runs smoothly and can send data to the server every 30 seconds. But on 1 October 2018 at 6:42 AM (after 20 hours 12 minutes), the Arduino program such as the “stack” cannot send data and when I try to remove the Ethernet cable there is no reaction on Arduino (the words “Not Connected” should appear on the LCD).

I need help, how can I send data every 30 seconds and can change conditions automatically every 24 hours ? thank you

#include <SPI.h>
#include <Ethernet.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F, 16, 2);

int ldrPin = 7;
int val = 0;
int led= 6; 

/*Is button pressed ?*/
int Pressed1 = 0;
int Pressed2 = 0;


/*Button*/
/*5v = pin 1
key 1 = pin 3
key 2 = pin 2
*/
int buttonPin1 = 31; //2;
int buttonPin2 = 33; //3;


/*time*/
unsigned long SendTime = 0UL;
unsigned long intervalSend = 30UL * 1000UL;
unsigned long intervalChange = 24UL * 60UL * 60UL * 1000UL;
unsigned long ChangeTime = 0UL;

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 };
IPAddress ip(192, 168, 1, 106);
IPAddress dnServer(192, 168, 1, 80); //IP router
IPAddress gateway(192, 168, 1, 80);
IPAddress subnet(255, 255, 255, 0);

char server[] = "pratamaiot.club"; 
EthernetClient client;

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

void setup() {
  Serial.begin(9600);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(ldrPin, INPUT);
  pinMode(led, OUTPUT);

  lcd.begin();
  lcd.setCursor(0,0);
  lcd.print("Konfigurasi IP ");
  delay(500);

  if (Ethernet.begin(mac) == 0) { 
    Serial.println("Failed to configure Ethernet using DHCP");
    Ethernet.begin(mac, ip, dnServer, gateway, subnet);
  }
  
  dht.begin(); 
  delay(500);
  Serial.println("Starting...");
  lcd.setCursor(0,0);
  lcd.print("Choose Limit ");
}

void loop() {
  tombol1();
  tombol2();
}

/*Tombol 1*/
void tombol1() {
  if(Pressed1 == 0) {
    if (digitalRead(buttonPin1) == HIGH) {
      lcd.setCursor(0,0);
      lcd.print("Limit 45");
      lcd.print((char)223);
      lcd.print("C ");
      Serial.println("Limit 1");
      Pressed1 = 1;
      Pressed2 = 0;
 

      
    }
  } else {
     float hum = dht.readHumidity(); //Reading the humidity and storing in hum
    float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
    delay (100);
   
    if ( temp > 45.00){
      digitalWrite(led, HIGH);
      }
      if ( temp < 45.00){
      digitalWrite(led, LOW);
      }
      int val = digitalRead(ldrPin);
      delay(100);

     unsigned long currentMillis = millis();
     if(currentMillis - SendTime>=intervalSend) {
        SendTime = currentMillis;
     if (client.connect(server, 80)) { 
      lcd.setCursor(0,1);
      lcd.print("Connected ");
      Serial.println("connected");
      delay(100);
          if(val == HIGH ){
              client.print("GET /insert/data.php?");
              client.print("temperature=");
              client.print(temp);
              client.print("&humidity=");
              client.print(hum);
              client.print("&batas_suhu=45");
              client.print("&led=MATI");
              client.println(" HTTP/1.1");
              client.print("Host: ");
              client.println(server);
              client.println("Connection: close");
              client.println();
              Serial.print("Temperature= ");
              Serial.println(temp);
              Serial.print("Humidity= ");
              Serial.println(hum);
              Serial.print("Batas Suhu=45");
              Serial.println("LED=MATI");
              
              client.stop(); }
           else {
              client.print("GET /insert/data.php?");
              client.print("temperature=");
              client.print(temp);
              client.print("&humidity=");
              client.print(hum);
              client.print("&batas_suhu=45");
              client.print("&led=HIDUP");
              client.println(" HTTP/1.1");
              client.print("Host: ");
              client.println(server);
              client.println("Connection: close");
              client.println();
              Serial.print("Temperature= ");
              Serial.println(temp);
              Serial.print("Humidity= ");
              Serial.println(hum);
              Serial.print("Batas Suhu=45");
              Serial.println("LED=HIDUP");
              
              client.stop();
            }
      }else {
        // if you didn't get a connection to the server:
        lcd.setCursor(0,1);
        lcd.print("Not Connect ");
        Serial.println("connection failed");
        }
     }
      unsigned long changeMillis = millis();
     if(gantiMillis - ChangeTime>=intervalChange) {
        ChangeTime = changeMillis;
        lcd.setCursor(0,0);
        lcd.print("Limit 50");
        lcd.print((char)223);
        lcd.print("C ");
     Serial.println("Limit 2");
      Pressed1 = 0;
      Pressed2 = 1;
   

     }
  } 
  }
/*Tombol 2*/
void tombol2() {
  if(Pressed2 == 0) {
    if (digitalRead(buttonPin2) == HIGH) {
      lcd.setCursor(0,0);
      lcd.print("Limit 50");
      lcd.print((char)223);
      lcd.print("C ");
      Serial.println("Limit 2");
      Pressed1 = 0;
      Pressed2 = 1;

     
    }
  } else {
     float hum = dht.readHumidity(); //Reading the humidity and storing in hum
    float temp = dht.readTemperature(); //Reading the temperature as Celsius and storing in temp
    delay (100);
   
    if ( temp > 50.00){
      digitalWrite(led, HIGH);
      }
      if ( temp < 50.00){
      digitalWrite(led, LOW);
      }
      int val = digitalRead(ldrPin);
      delay(100);

     unsigned long currentMillis = millis();
     if(currentMillis - SendTime>=intervalSend) {
        SendTime = currentMillis;
     if (client.connect(server, 80)) { 
      lcd.setCursor(0,1);
      lcd.print("Connected ");
      Serial.println("connected");
      delay(100);
          if(val == HIGH ){
              client.print("GET /insert/data.php?");
              client.print("temperature=");
              client.print(temp);
              client.print("&humidity=");
              client.print(hum);
              client.print("&batas_suhu=50");
              client.print("&led=MATI");
              client.println(" HTTP/1.1");
              client.print("Host: ");
              client.println(server);
              client.println("Connection: close");
              client.println();
              Serial.print("Temperature= ");
              Serial.println(temp);
              Serial.print("Humidity= ");
              Serial.println(hum);
              Serial.print("Batas Suhu=50");
              Serial.println("LED=MATI");
              
              client.stop(); }
           else {
              client.print("GET /insert/data.php?");
              client.print("temperature=");
              client.print(temp);
              client.print("&humidity=");
              client.print(hum);
              client.print("&batas_suhu=50");
              client.print("&led=HIDUP");
              client.println(" HTTP/1.1");
              client.print("Host: ");
              client.println(server);
              client.println("Connection: close");
              client.println();
              Serial.print("Temperature= ");
              Serial.println(temp);
              Serial.print("Humidity= ");
              Serial.println(hum);
              Serial.print("Batas Suhu=50");
              Serial.println("LED=HIDUP");
              
              client.stop();
            }
      }else {
        // if you didn't get a connection to the server:
        lcd.setCursor(0,1);
      lcd.print("Not Connect ");
        Serial.println("connection failed");
        }
     
     } 
}

senddata_change.ino (13.9 KB)

Your English is quite good. Kenapa pakai bahasa Indonesia untuk program?

Karena supaya lebih mudah dipahami. I will change using English

Tidak apa2. What board are you using? Also link to the DHT library.

I see nothing in your code wrt to timing that would cause it to stop when it did, so the problem is with something else. I cannot compile your code as I get errors with the DHT library.

I use Arduino Mega 2560 and this dht library that i use https://github.com/adafruit/DHT-sensor-library

before, I tried the interval every 2 hours will change conditions and succeed

Still can’t compile as now I’m missing <Adafruit_Sensor.h> which I will not look for. I don’t want my setup cluttered with Adafruit libraries.

I don’t believe the problem though, is with the library either. If it works once, it should work always and forever unless something changes. This could be a memory leak which you can only really test by running it again and seeing if it stops at the same time. The other possibilities are with the power connection. There are many influencing factors that can disrupt operations, especially with a breadboard wired test assembly. The short hookup wires we use make perfect antennas for cell phone signals and all other kinds of interference.

Run the test again and see what happens.

Your code doesn't compile. Looks like you stripped it to make it fit but not sure. You can attach your complete code in case it was too big.

The code seems to be missing a } and buttonPin3 and gantiMillis() are not declared.

sterretje: Your code doesn't compile. Looks like you stripped it to make it fit but not sure. You can attach your complete code in case it was too big.

The code seems to be missing a } and buttonPin3 and gantiMillis() are not declared.

yes because my code is too much, I have uploaded my code

You DO have pullup resistors on those pushbutton pins, don't you? :)

outsider: You DO have pullup resistors on those pushbutton pins, don't you? :)

i use a pull down resistor. Can this affect ? I use from this reference https://www.instructables.com/id/Arduino-1x4-Keypad-Membrane/

can anyone help me ??

rpm070: can anyone help me ??

Need to confirm it’s repeateable, does it stop working always after x amount of time. Need to confirm it’s not a power issue / overheating. Need to confirm you get same result with a different Arduino board, Ethernet shield, LCD board.

update of my project :

Previously, I apologize, because it turns out that the code I uploaded to Arduino is not like the one I posted above first time. this is the code that I use when a problem occurs :

/ * time * /
unsigned last time = 0UL;
unsigned long intervalkirim = 1000UL * 30UL;
unsigned long waktuganti = 0UL;
const unsigned long intervalganti = 24UL * 60UL * 60UL * 1000UL;

Now my code has successfully changed "temperature limit" every 24 hours using the code I posted the first time

this is the code that I use when a problem occurs :

Why did you not post all of it ?

UKHeliBob: Why did you not post all of it ?

because only this part is different from the code

const unsigned long interval replace = 24UL * 60UL * 60UL * 1000UL;

This line does not compile

Please post a complete program that compiles

UKHeliBob:

const unsigned long interval replace = 24UL * 60UL * 60UL * 1000UL;

This line does not compile

Please post a complete program that compiles

you can download my complete code from the first post

you can download my complete code from the first post

Does that code have the original global declarations

/ * time * /
unsigned last time = 0UL;
unsigned long intervalkirim = 1000UL * 30UL;
unsigned long waktuganti = 0UL;
const unsigned long intervalganti = 24UL * 60UL * 60UL * 1000UL;

or the revised ones ?

/ * time * /
unsigned last time = 0UL;
unsigned long intervalkirim = 1000UL * 30UL;
unsigned long waktuganti = 0UL;
const unsigned long intervalganti = 24UL * 60UL * 60UL * 1000UL;

or maybe neither of them. The second version does not compile anyway

By the way, I do not appreciate that way that you have changed the code that I commented on after I pointed out a problem with it. That is bad manners.

Yes, I’m sorry, because I use Googletranslate. where “replace” means “ganti”.

Whatever changes you have made and whatever code is not working it would be better for you to post the whole of it in a new post and describe the problems that this version has.