MKRwifi 1010 - Scheduler drifting

Hello.

I have a MKRWifi 1010 connected to IoT cloud. It is controlling a relay via a cloud scheduler. Unfortunately though the timing is drifting. Once it powers up it is about 1-2 seconds off from GMT, but after about 1hr is around 30 seconds slow. See image attached

What could be causing this? Here is my code:

#include "thingProperties.h"

float Val_LED_Max_Brightness = 0;
bool waterPowerOverride = false;


void setup() {
  // Initialize serial and wait for port to open:
  Serial1.begin(9600);
  pinMode(6, OUTPUT);
  digitalWrite(6, LOW);

  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // 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
  // reply only when you receive data:
  if (waterFeatureSchedule.isActive()) {
    // Serial.println("Water is on");
    waterStatus = true;
    digitalWrite(6, LOW);

  } else { 
    digitalWrite(6, HIGH);
    waterStatus = false;
  }
  
  if(ArduinoCloud.connected()){
     time_read = ArduinoCloud.getLocalTime();
  }
 
}

/*
  Since LEDMaxBrightness is READ_WRITE variable, onLEDMaxBrightnessChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onLEDMaxBrightnessChange()  {
  // Add your code here to act upon LEDMaxBrightness change
  //Serial.write(int(lED_Max_Brightness.getBrightness()));
  if (lED_Max_Brightness.getSwitch()) {
    Val_LED_Max_Brightness = map(lED_Max_Brightness.getBrightness(), 0, 100, 16, 75);
    Serial1.println(Val_LED_Max_Brightness, 0);
  } else {
    Val_LED_Max_Brightness = 16;
    Serial1.println(Val_LED_Max_Brightness, 0);
  }


}



/*
  Since WaterFeature is READ_WRITE variable, onWaterFeatureChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onWaterFeatureChange()  {
  // Add your code here to act upon WaterFeature change
  if (waterFeature == 1) {
    waterPowerOverride = false;
  } else {
    waterPowerOverride = true;
    //Serial.println("Water is off regardless of schedule");
  }
}

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

}

I would recommend using the real time clock. The library is RTCZero.

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