Bug. Scheduler variable not syncing after connection

I think that there is a bug with the Arduino IOT Cloud Scheduler widget not syncing after board restart. When the board and dashboard scheduler widget are both true, disconnect power to the board, reconnect board power. Now the board variable is false. The board and cloud are out of sync.

Here is a simple sketch connected to a dashboard with a switch widget and a scheduler widget.

/*
  Sketch generated by the Arduino IoT Cloud Thing "Untitled"
  https://create.arduino.cc/cloud/things/09de5b68-998a-4200-a40a-ba7d32a2026e

  Arduino IoT Cloud Variables description

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

  CloudSchedule mySchedule;
  bool mySwitch;

  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"

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);

  // 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();

}

void onMySwitchChange()  {
  if (mySwitch) {
    Serial.print("MySwitch state is ");
    Serial.println("true");
  } else {
    Serial.print("MySwitch state is ");
    Serial.println("false");
  }
}

void onMyScheduleChange()  {
  if (mySchedule.isActive()) {
    Serial.print("MySchedule state is ");
    Serial.println("true");
} else {
Serial.print("MySchedule state is ");
Serial.println("false");
}
}
  1. I set the dashboard widgets to false (switch off and no active schedule), then boot to nano. In the serial monitor (below I get the boot messages and widgets showing as false (correctly). Then you can see that I have tried changing the widgets a couple of times and left them BOTH as true. I disconnected power to the nano, then plugged it in again. (Without changing anything on the dashboard). You can see the boot messages followed by widget states which are switch true (correct) and scheduler false (incorrect).
  2. In this (related) post A variable that updates on change and periodically? - #6 by ubidefeo , ubidefeo from the Arduino team states that "despite the property update being "on change", at every successful connection between IoT Cloud and your board there is a "Sync" event which syncs your board's variable with the Cloud property".

So restart sync works for the switch widget but not for scheduler.

Board is Arduino nano 33 IOT

***** Arduino IoT Cloud - configuration info *****
Device ID: 30786b15-d67f-49ad-9769-8a84c518096d
MQTT Broker: mqtts-sa.iot.arduino.cc:8883
WiFi.status(): 0
Current WiFi Firmware: 1.4.4
Connected to "WiFi_OliveNet-429C9D"
Connected to Arduino IoT Cloud
Thing ID: 09de5b68-998a-4200-a40a-ba7d32a2026e
MySchedule state is false
MySwitch state is false
MySwitch state is true
MySchedule state is false
MySchedule state is true
***** Arduino IoT Cloud - configuration info *****
Device ID: 30786b15-d67f-49ad-9769-8a84c518096d
MQTT Broker: mqtts-sa.iot.arduino.cc:8883
WiFi.status(): 0
Current WiFi Firmware: 1.4.4
Connected to "WiFi_OliveNet-429C9D"
ArduinoIoTCloudTCP::handle_ConnectMqttBroker could not connect to mqtts-sa.iot.arduino.cc:8883
ArduinoIoTCloudTCP::handle_ConnectMqttBroker 1 connection attempt at tick time 14668
Connected to Arduino IoT Cloud
Thing ID: 09de5b68-998a-4200-a40a-ba7d32a2026e
MySchedule state is false
MySwitch state is true

Hi @ianlilburne thanks for your message. The fact is the scheduler widget depends on other two hidden properties tz_offset and tz_dst_unitl to compute the localtime. Until this two variables are not received by the board the scheduler will report an inactive state.

The onMyScheduleChange() callback is hitted before tz_offset and tz_dst_unitl are received by the board so inside that callback the resulting state is false.

If you want to check schedule status after connection is more safe to use the sync callback. For an example see ArduinoIoTCloud/ArduinoIoTCloud-Callbacks.ino at 9c6d5d741ea936009f0e538deb33bfc655ca02e3 · arduino-libraries/ArduinoIoTCloud · GitHub

Many thanks for that. Yes solved.
I found it helpful to set debug to level 4 to see the 'tz_offset' and 'tz_dst_until' variables in the serial monitor. I also changed the cloud scheduler variable update to 'periodic' to then fetch the dashboard value to complete the variable sync.

//in setup
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(4);
  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);  

//then the function exactly per the github example
  void doThisOnSync(){
  /* add your custom code here */
  Serial.println("Thing Properties synchronised");

2 Likes

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