Time from RTC DS3231 substituted by UTC time both in serial monitor and dashboard in Arduino IOT Cloud

Hi,,
after fifteen months spent trying to learn how to use Arduino and ESp32 ( I'm an old newbie, 73 y old) I just subscribed a plan on Arduino IOT Cloud.
I started with some very basic code just trying to collect data from a BME280 sensor and time from a RTC DS3231 ( since I was able with this mCU and sensor to build a datalogger with deepsleep)
Here is the code

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/574c711a-04be-4b20-8972-1e34d8a4cc3b 

  Arduino IoT Cloud Variables description

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

  float hum;
  float temp;

  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 <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; 

#include <RTClib.h>
RTC_DS3231 rtc; 

void gettimeStamp() {
    DateTime now = rtc.now();

char timeStamp[9];
 sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());

  Serial.println(timeStamp);
}

void setup() {
  // Initialize serial and wait for port to open:
   //initializing Serial monitor
  Serial.begin(115200);
    while(!Serial);
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud

  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  delay(4000);

  /*
     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();
  
  Serial.println("Initialize DS3231");

// setup for the RTC
  while(!Serial); 
  delay(1000);
 if(! rtc.begin()) {
      Serial.println("Couldn't find RTC");
      while (1);
    }
    else {
      // following line sets the RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
    }
   
  bme.begin(0x76);  

  temp = bme.readTemperature();
  hum= bme.readHumidity();
    }

void loop() {
  ArduinoCloud.update();
 
  // Your code here 
  temp = bme.readTemperature();
  hum= bme.readHumidity();
  
  gettimeStamp();
  
  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.println( "°C");
  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.println("%");
   DateTime now = rtc.now();

char timeStamp[9];
 sprintf(timeStamp, "%02i:%02i:%02i", now.hour(), now.minute(), now.second());

  Serial.println(timeStamp);
 delay(180000);
  }

However, even if in the dashboard I linked the variable time to timeStamp I still get the UTC time and not the time from RTC ( which is just one hour late in my timezone) both in the serial monitor and in the dashboard.
Furthermore I don't understand how the dashboard data from sensor are updated because it seems that only time ( even if with one hour difference is updated) while the temp and hum values show a great lag and even refreshing the dashboard shows no effect on data.
I googled in search of solutions but I was not able to find one.
I'm really very grateful if you could help me.
Thx in advance for your help.

Does an example sketch for the DS3231 work ?

  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  else {
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

You need to stop resetting the RTC time whenever the sketch runs.

Hi,
Thanks for your prompt answer
When I use deepsleep with esp32 usually after compiling the sketch I comment out the line you point at so that when it awakes the correct time is displayed and I've used this also in this case but at no avail.
I really don't know if in this case it's useful or not. I think that since there is no deepsleep with esp32 reset maybe it's useless

Hi,
Thanks for your help.
The code works just fine out of Arduino IOT Cloud.
Probably I need to use NTP time as I've done with dataloggers when a reliable wifi was available but I'm afraid not to be able to adapt the code I used to IoT cloud
Do you have any suggestion and/or any example I could use?
Thanks

Hi,

Where you able to solve the issue? I have a similar problem. I built a clock that is connected to Arduino cloud and uses RTCZero. I know that this starts the internal clock of the Arduinon Nano 33IOT and will lag within some ours (it is running 20min behind in one day). For this reason I tried to add a DS3231, but without succes.

At this moment I am using the:
int hours_alarm;
int minutes_alarm;
int seconds_alarm;
to set trigger some lights to go off at a certain time. But when I add the code for the DS3231 it somehow interferes with the time of the cloud.

Any suggestions? Thanks in advance.

Hi,
no I left aside Arduino IOT Cloud since I met several problems when trying to adapt my codes to the cloud.
Furthermore I've found that this issue ( Arduino Cloud) still seems not so attractive to forumers as it is clearly demonstrated by the fact that several months have elapsed without any further answer to my question from forum.
Have a nice day

Thanks for your update. I found a solution for my cloud connected clock. It was running late 20 min after one day, so i thought I needed to add a RTC, but this was messing up my code. Apparently I only needed to add the line:

TimeService.setSyncInterval(600);

This syncs the time to keep on track.
Maybe you can add this as wel.

Have a nice evening.

Hi, thx for your suggestion. I'll try it. Where did you find this way of solving the issue ?

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