In messenger no message after 5 minutes

Hello,

I'm having a strange occurrence with the messenger widget.
If I add a delay of 2 seconds I do get a message in the messenger widget and if I set it to 5 minutes I don't get it.

I have a button that I can use to turn on all 3 lights in my IoT Home Lights. Later that evening I use the same button to switch off the lights again with a delay of 5 minutes.

I do not understand. Is the delay in this the fault or am I doing something wrong.
Please help here.
Thanks.

Bert

The code

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/92d2541e-52f7-4066-ba52-4dabdc9e833c

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  String msg1;
  int light_Value;
  int offset_low;
  int slider_Hour;
  int slider_Minute;
  bool alles;
  bool automatisch;
  bool boven_de_bank;
  bool schommel_en_radio;
  bool staande_lamp;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"
#include <Arduino_ConnectionHandler.h>
#include <RTClib.h>

#define LDR_PIN 36   // ESP32 pin GIOP36 (ADC0)
#define Staand  25   // Look at esp32 Board , and Look Pin diagram, Here We Using GPIO Number
#define Bank    33   // Look at esp32 Board , and Look Pin diagram, Here We Using GPIO Number
#define Radio   32   // Look at esp32 Board , and Look Pin diagram, Here We Using GPIO Number

int Hour, Min;
int flag1 = 0;
char str[5];
int analogValue;
int Slider_uur;
int Slider_minuut;
int Offset_low;

DateTime now;
RTC_DS3231 rtc;

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);


  pinMode(Staand, OUTPUT);  // Staande lamp
  pinMode(Bank, OUTPUT);    // Boven de bank
  pinMode(Radio, OUTPUT);   // Schommelstoel & radio

  digitalWrite(Staand, HIGH); // Relay OFF
  digitalWrite(Bank, HIGH);   // Relay OFF
  digitalWrite(Radio, HIGH);  // Relay OFF

  if (! rtc.begin())
  {
    Serial.println(" RTC Module not Present");
    while (1);
  }
  if (rtc.lostPower())
  {
    Serial.println("RTC power failure, reset the time!");
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  now = rtc.now();
  Hour = (now.hour());
  Min = (now.minute());

  Serial.println();

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
  */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  // Your code here

  now = rtc.now();
  delay(1000);
  light_Value = analogRead(LDR_PIN);
  Testen_op_lampen_uit();
}


/*
  Since StaandeLamp is READ_WRITE variable, onStaandeLampChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onStaandeLampChange()  {
  // Add your code here to act upon StaandeLamp change

  if (staande_lamp) {
    digitalWrite(Staand, LOW); // ON
    msg1 = "Staande lamp is aan";
  }
  else {
    digitalWrite(Staand, HIGH); // OFF
    msg1 = "Staande lamp is uit";
  }
}

/*
  Since BovenDeBank is READ_WRITE variable, onBovenDeBankChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onBovenDeBankChange()  {
  // Add your code here to act upon BovenDeBank change
  if (boven_de_bank) {
    digitalWrite(Bank, LOW); // ON
    msg1 = "Boven de bank lamp aan";
  }
  else {
    digitalWrite(Bank, HIGH); // OFF
    msg1 = "Boven de bank lamp uit";
  }
}

/*
  Since SchommelEnRadio is READ_WRITE variable, onSchommelEnRadioChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSchommelEnRadioChange()  {
  // Add your code here to act upon SchommelEnRadio change

  if (schommel_en_radio) {
    digitalWrite(Radio, LOW); // ON
    msg1 = "Schommel en radio lamp aan";
  }
  else {
    digitalWrite(Radio, HIGH); // OFF
    msg1 = "Schommel en radio lamp uit";
  }
}

/*
  Since Alles is READ_WRITE variable, onAllesChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAllesChange()  {
  // Add your code here to act upon Alles change
  if (alles) {
    digitalWrite(Staand, LOW); //Everything ON
    digitalWrite(Bank, LOW);
    digitalWrite(Radio, LOW);
    msg1 = "Alles aan"; 
  }
  if(!alles) {
    delay(300000); // Real life 5 minutes
    //delay(2000);   // For testing 2 seconds
    digitalWrite(Staand, HIGH); // Everything OFF
    digitalWrite(Bank, HIGH);
    digitalWrite(Radio, HIGH);
    msg1 = "Alles uit over 5 minuten";
  }
}


/*
  Since Automatisch is READ_WRITE variable, onAutomatischChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onAutomatischChange()  {

  if (!automatisch) {
    msg1 = "Automatisch verlichting is uitgeschakeld";
    //Serial.print("Automatisch verlichting is uitgeschakeld ");
  }
  
    // Bepalen of de verlichting aan moet
  if (automatisch && flag1 == 0) {
    msg1 = "Automatisch verlichting is ingeschakeld. Wachten op schemering.";
    Serial.print("Automatisch verlichting is ingeschakeld. Wachten op schemering.");
    }
  }

/*
  Since LightValue is READ_WRITE variable, onLightValueChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLightValueChange()  {
  // Add your code here to act upon LightValue change
}


/*
  Since SliderHour is READ_WRITE variable, onSliderHourChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSliderHourChange()  {
  // Add your code here to act upon SliderHour change
  Slider_uur = slider_Hour;
}

/*
  Since SliderMinute is READ_WRITE variable, onSliderMinuteChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSliderMinuteChange()  {
  // Add your code here to act upon SliderMinute change
  Slider_minuut = slider_Minute;
}

// Rounded HH:mm format
char * hoursToString(double h, char *str)
{
  int m = int(round(h * 60));
  int hr = (m / 60) % 24;
  int mn = m % 60;
  str[0] = (hr / 10) % 10 + '0';
  str[1] = (hr % 10) + '0';
  str[2] = ':';
  str[3] = (mn / 10) % 10 + '0';
  str[4] = (mn % 10) + '0';
  str[5] = '\0';
  return str;
}

/*
  Since Msg1 is READ_WRITE variable, onMsg1Change() is
  executed every time a new value is received from IoT Cloud.
*/
void onMsg1Change()  {
  // Add your code here to act upon Msg1 change
}

void Testen_op_lampen_uit(){
  if (automatisch && flag1 == 0) {
  light_Value = analogRead(LDR_PIN);
  Serial.print("Lichtwaarde = "); Serial.println(light_Value);  // the raw analog reading
  
  if (light_Value < 1100) {
      //Serial.println("Verlichting is aan, waarde lager dan 1100");
      digitalWrite(Staand, LOW); // ON
      digitalWrite(Bank, LOW);
      digitalWrite(Radio, LOW);
      msg1 = "Automatische verlichting is aan";
      msg1 = "flag1 1";
      flag1 = 1;
      delay(2000);
      }
  }
  // Bepalen of de verlichting uit moet gaan

  if (automatisch && now.hour() == Slider_uur && now.minute() == Slider_minuut && flag1 == 1) {
    delay(2000);
    digitalWrite(Staand, HIGH); // OFF
    digitalWrite(Bank, HIGH);
    digitalWrite(Radio, HIGH);
    msg1 = "Automatische verlichting is uit ";
    flag1 = 2;
    msg1 = "flag1 2";
  }

  // Bepalen wanneer het weer licht genoeg is om de volgende cycles te starten
  if (automatisch && flag1 == 2) {
    light_Value = analogRead(LDR_PIN);

    if (light_Value > 1400) {
      flag1 = 0;
      delay(2000);
      msg1 = "flag1 3";
    }
  }
}

Hi, you should avoid using delay() in your Cloud sketches, since delays do block the loop function and can cause the connection to the Cloud to drop.

Please have a look here:
https://docs.arduino.cc/arduino-cloud/cloud-interface/variables/#cloud-updates

Instead of using delay() you could use millis() and compare the current time with a specified interval.
Here is another useful tutorial:
https://www.norwegiancreations.com/2017/09/arduino-tutorial-using-millis-instead-of-delay/

Let me know if it works.

Hello c_sarnataro_arduino,

I changed the delay's in millis() and it is working now.

Thank so much.

Regards Bert

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