Skip to content

TimeService update/refactor #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Feb 28, 2023

Conversation

pennam
Copy link
Collaborator

@pennam pennam commented Feb 16, 2023

TCP boards

NTP sync is enabled by default with 24h interval from startup. Interval can be configured calling:

TimeService.setSyncInterval(unsigned long seconds);

To force and trigger a manual sync call:

TimeServicesync();

LoRa boards

At boot EPOCH_AT_COMPILE_TIME is stored inside RTC. To change its value afterwards is possible to call:

TimeSerivce.setTime(unsigned long time )

If a time source is available is possible to configure a custom sync function that will be called periodically (each 24h by default) by the TimeSerivceClass.

TimeService.setSyncFunction(syncTimeFunctionPtr sync_func);

Tests and benchmarks

Board Update time NTP sync time
NANO RP 2040 Connect 80-180ms 250ms :shipit:
MKR WIFi 1010 70-140ms 250ms :shipit:
PORTENTA [ETH] 1ms 250ms :shipit:
PORTENTA [WiFi] 1ms 250ms :shipit:
ESP32 1ms 40ms :shipit:
NICLA VISION 1ms 70ms :shipit:
OPTA [ETH] 1ms 250ms :shipit:
MKR WAN [DS3232] 2-105ms 1ms :shipit:
MKR WAN [ETH] 2-105ms 90ms :shipit:

Tests sketch

/*
  This sketch demonstrates how to exchange data between your board and the Arduino IoT Cloud.

  * Connect a potentiometer (or other analog sensor) to A0.
  * When the potentiometer (or sensor) value changes the data is sent to the Cloud.
  * When you flip the switch in the Cloud dashboard the onboard LED lights gets turned ON or OFF.

  IMPORTANT:
  This sketch works with WiFi, GSM, NB, Ethernet and Lora enabled boards supported by Arduino IoT Cloud.
  On a LoRa board, if it is configured as a class A device (default and preferred option), values from Cloud dashboard are received
  only after a value is sent to Cloud.

  The full list of compatible boards can be found here:
   - https://github.com/arduino-libraries/ArduinoIoTCloud#what
*/

#include "arduino_secrets.h"
#include "thingProperties.h"
#include <TimeLib.h>
#ifdef BOARD_HAS_LORA
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Arduino_NTP.h>
byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
EthernetUDP Udp;

#include <DS3232RTC.h>
DS3232RTC dsRTC;
#endif

#if !defined(LED_BUILTIN)
static int const LED_BUILTIN = 2;
#endif

#ifdef BOARD_HAS_LORA
unsigned long getTimeFrom() {
  // LoRa + Ethernet -> get time from NTP
  return NTPUtils::getTime(Udp);
  // Lora + DS3232 -> get time from external RTC module
  //return dsRTC.get();
}
#endif

int time_to_print = 0;
int update_start = 0;
int update_end = 0;
int update_time = 0;

void printTime(unsigned long const time) {
  if(hour(time) < 10) {
    Serial.print("0");
  }
  Serial.print(hour(time));
  Serial.print(":");
  if(minute(time) < 10) {
    Serial.print("0");
  }
  Serial.print(minute(time));
  Serial.print(":");
  if(second(time) < 10) {
    Serial.print("0");
  }
  Serial.print(second(time));
  Serial.println("");
}

void setup() {
  /* Initialize serial and wait up to 5 seconds for port to open */
  Serial.begin(9600);
  for(unsigned long const serialBeginTime = millis(); !Serial && (millis() - serialBeginTime > 5000); ) { }

  /* Configure LED pin as an output */
  pinMode(LED_BUILTIN, OUTPUT);

#ifdef BOARD_HAS_LORA
  /* Setup ethernet shield */
  Ethernet.init(5);
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // Check for Ethernet hardware present
    if (Ethernet.hardwareStatus() == EthernetNoHardware) {
      Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    } else if (Ethernet.linkStatus() == LinkOFF) {
      Serial.println("Ethernet cable is not connected.");
    }
    // no point in carrying on, so do nothing forevermore:
    while (true) {
      delay(1);
    }
  }

  dsRTC.begin();
#endif

  /* This function takes care of connecting your sketch variables to the ArduinoIoTCloud object */
  initProperties();

  /* Initialize Arduino IoT Cloud library */
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  /* Setup NTP sync timeout */
  TimeService.setSyncInterval(60);

#ifdef BOARD_HAS_LORA
  TimeService.setSyncFunction(getTimeFrom);
  TimeService.setTimeZoneData(3600, 1679792400);
#endif

  ArduinoCloud.addCallback(ArduinoIoTCloudEvent::SYNC, doThisOnSync);

  setDebugMessageLevel(4);
  ArduinoCloud.printDebugInfo();
}

void loop() {

  update_start = millis();
  ArduinoCloud.update();
  update_end = millis();

  update_time = update_end - update_start;

  local = ArduinoCloud.getLocalTime();
  utc = ArduinoCloud.getInternalTime();

  if(millis() - time_to_print > 1000) {
    printTime(local);
    time_to_print = millis();

    Serial.print("Update execution time: ");
    Serial.println(update_time);
  }

#ifndef BOARD_HAS_LORA
  if(calendar.isActive()) {
    Serial.print("Led is Active: ");
    printTime(local);
  }
#endif
}

#ifndef BOARD_HAS_LORA
/*
 * 'onCalendarChange' is called when the "led" property of your Thing changes
 */
void onCalendarChange() {
  Serial.println("calendar changed:");

  Schedule schedule = calendar.getValue();
  Serial.print("calendar starts: ");
  printTime(schedule.frm);
}
#endif

void doThisOnSync(){
  /* add your custom code here */
  Serial.println("Thing Properties synchronised");
#ifndef BOARD_HAS_LORA
  Schedule schedule = calendar.getValue();
  Serial.print("led frm: ");
  Serial.println(schedule.frm);
  Serial.print("led to: ");
  Serial.println(schedule.to);
  Serial.print("led len: ");
  Serial.println(schedule.len);
  Serial.print("led msk: ");
  Serial.println(schedule.msk);
#endif
}

@github-actions
Copy link

Memory usage change @ 3c06977

Board flash % RAM for global variables %
arduino:mbed_nano:nanorp2040connect 🔺 +132 - +149 0.0 - 0.0 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_nicla:nicla_vision 🔺 +80 - +176 0.0 - +0.01 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_opta:opta 🔺 +144 - +176 +0.02 - +0.02 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_portenta:envie_m7 🔺 +80 - +144 +0.01 - +0.02 🔺 +8 - +8 0.0 - 0.0
arduino:samd:mkr1000 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrgsm1400 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrnb1500 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrwan1300 🔺 +192 - +192 +0.07 - +0.07 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrwifi1010 🔺 +152 - +176 +0.06 - +0.07 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:nano_33_iot 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
esp32:esp32:esp32 🔺 +1040 - +7080 +0.08 - +0.54 🔺 +108 - +276 +0.03 - +0.08
esp8266:esp8266:huzzah 🔺 +196 - +200 +0.02 - +0.02 🔺 0 - +12 0.0 - +0.01
Click for full report table
Board examples/ArduinoIoTCloud-Advanced
flash
% examples/ArduinoIoTCloud-Advanced
RAM for global variables
% examples/ArduinoIoTCloud-Basic
flash
% examples/ArduinoIoTCloud-Basic
RAM for global variables
% examples/utility/ArduinoIoTCloud_Travis_CI
flash
% examples/utility/ArduinoIoTCloud_Travis_CI
RAM for global variables
% examples/utility/Provisioning
flash
% examples/utility/Provisioning
RAM for global variables
% examples/utility/SelfProvisioning
flash
% examples/utility/SelfProvisioning
RAM for global variables
%
arduino:mbed_nano:nanorp2040connect 132 0.0 8 0.0 132 0.0 8 0.0 132 0.0 8 0.0 149 0.0 8 0.0 149 0.0 8 0.0
arduino:mbed_nicla:nicla_vision 136 0.01 8 0.0 80 0.0 8 0.0 80 0.0 8 0.0 176 0.01 8 0.0
arduino:mbed_opta:opta 144 0.02 8 0.0 144 0.02 8 0.0 144 0.02 8 0.0 176 0.02 8 0.0
arduino:mbed_portenta:envie_m7 144 0.02 8 0.0 80 0.01 8 0.0 144 0.02 8 0.0 112 0.01 8 0.0
arduino:samd:mkr1000 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrgsm1400 160 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrnb1500 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrwan1300 192 0.07 8 0.02 192 0.07 8 0.02 192 0.07 8 0.02
arduino:samd:mkrwifi1010 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02 176 0.07 8 0.02
arduino:samd:nano_33_iot 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02 168 0.06 8 0.02
esp32:esp32:esp32 7080 0.54 276 0.08 1040 0.08 108 0.03 7032 0.54 276 0.08
esp8266:esp8266:huzzah 196 0.02 0 0.0 200 0.02 12 0.01 200 0.02 4 0.0
Click for full report CSV
Board,examples/ArduinoIoTCloud-Advanced<br>flash,%,examples/ArduinoIoTCloud-Advanced<br>RAM for global variables,%,examples/ArduinoIoTCloud-Basic<br>flash,%,examples/ArduinoIoTCloud-Basic<br>RAM for global variables,%,examples/utility/ArduinoIoTCloud_Travis_CI<br>flash,%,examples/utility/ArduinoIoTCloud_Travis_CI<br>RAM for global variables,%,examples/utility/Provisioning<br>flash,%,examples/utility/Provisioning<br>RAM for global variables,%,examples/utility/SelfProvisioning<br>flash,%,examples/utility/SelfProvisioning<br>RAM for global variables,%
arduino:mbed_nano:nanorp2040connect,132,0.0,8,0.0,132,0.0,8,0.0,132,0.0,8,0.0,149,0.0,8,0.0,149,0.0,8,0.0
arduino:mbed_nicla:nicla_vision,136,0.01,8,0.0,80,0.0,8,0.0,80,0.0,8,0.0,176,0.01,8,0.0,,,,
arduino:mbed_opta:opta,144,0.02,8,0.0,144,0.02,8,0.0,144,0.02,8,0.0,176,0.02,8,0.0,,,,
arduino:mbed_portenta:envie_m7,144,0.02,8,0.0,80,0.01,8,0.0,144,0.02,8,0.0,112,0.01,8,0.0,,,,
arduino:samd:mkr1000,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrgsm1400,160,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrnb1500,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrwan1300,192,0.07,8,0.02,192,0.07,8,0.02,192,0.07,8,0.02,,,,,,,,
arduino:samd:mkrwifi1010,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,176,0.07,8,0.02
arduino:samd:nano_33_iot,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,168,0.06,8,0.02
esp32:esp32:esp32,7080,0.54,276,0.08,1040,0.08,108,0.03,7032,0.54,276,0.08,,,,,,,,
esp8266:esp8266:huzzah,196,0.02,0,0.0,200,0.02,12,0.01,200,0.02,4,0.0,,,,,,,,

@per1234 per1234 added type: enhancement Proposed improvement topic: code Related to content of the project itself labels Feb 16, 2023
@github-actions
Copy link

Memory usage change @ 77ea16d

Board flash % RAM for global variables %
arduino:mbed_nano:nanorp2040connect 🔺 +132 - +149 0.0 - 0.0 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_nicla:nicla_vision 🔺 +80 - +176 0.0 - +0.01 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_opta:opta 🔺 +144 - +176 +0.02 - +0.02 🔺 +8 - +8 0.0 - 0.0
arduino:mbed_portenta:envie_m7 🔺 +80 - +144 +0.01 - +0.02 🔺 +8 - +8 0.0 - 0.0
arduino:samd:mkr1000 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrgsm1400 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrnb1500 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrwan1300 🔺 +192 - +192 +0.07 - +0.07 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:mkrwifi1010 🔺 +152 - +176 +0.06 - +0.07 🔺 +8 - +8 +0.02 - +0.02
arduino:samd:nano_33_iot 🔺 +152 - +168 +0.06 - +0.06 🔺 +8 - +8 +0.02 - +0.02
esp32:esp32:esp32 🔺 +680 - +724 +0.05 - +0.06 0 - 0 0.0 - 0.0
esp8266:esp8266:huzzah 🔺 +8 - +136 0.0 - +0.01 🔺 0 - +12 0.0 - +0.01
Click for full report table
Board examples/ArduinoIoTCloud-Advanced
flash
% examples/ArduinoIoTCloud-Advanced
RAM for global variables
% examples/ArduinoIoTCloud-Basic
flash
% examples/ArduinoIoTCloud-Basic
RAM for global variables
% examples/utility/ArduinoIoTCloud_Travis_CI
flash
% examples/utility/ArduinoIoTCloud_Travis_CI
RAM for global variables
% examples/utility/Provisioning
flash
% examples/utility/Provisioning
RAM for global variables
% examples/utility/SelfProvisioning
flash
% examples/utility/SelfProvisioning
RAM for global variables
%
arduino:mbed_nano:nanorp2040connect 132 0.0 8 0.0 132 0.0 8 0.0 132 0.0 8 0.0 149 0.0 8 0.0 149 0.0 8 0.0
arduino:mbed_nicla:nicla_vision 136 0.01 8 0.0 80 0.0 8 0.0 80 0.0 8 0.0 176 0.01 8 0.0
arduino:mbed_opta:opta 144 0.02 8 0.0 144 0.02 8 0.0 144 0.02 8 0.0 176 0.02 8 0.0
arduino:mbed_portenta:envie_m7 144 0.02 8 0.0 80 0.01 8 0.0 144 0.02 8 0.0 112 0.01 8 0.0
arduino:samd:mkr1000 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrgsm1400 160 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrnb1500 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02
arduino:samd:mkrwan1300 192 0.07 8 0.02 192 0.07 8 0.02 192 0.07 8 0.02
arduino:samd:mkrwifi1010 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02 176 0.07 8 0.02
arduino:samd:nano_33_iot 152 0.06 8 0.02 152 0.06 8 0.02 152 0.06 8 0.02 168 0.06 8 0.02 168 0.06 8 0.02
esp32:esp32:esp32 724 0.06 0 0.0 680 0.05 0 0.0 688 0.05 0 0.0
esp8266:esp8266:huzzah 8 0.0 0 0.0 136 0.01 12 0.01 12 0.0 4 0.0
Click for full report CSV
Board,examples/ArduinoIoTCloud-Advanced<br>flash,%,examples/ArduinoIoTCloud-Advanced<br>RAM for global variables,%,examples/ArduinoIoTCloud-Basic<br>flash,%,examples/ArduinoIoTCloud-Basic<br>RAM for global variables,%,examples/utility/ArduinoIoTCloud_Travis_CI<br>flash,%,examples/utility/ArduinoIoTCloud_Travis_CI<br>RAM for global variables,%,examples/utility/Provisioning<br>flash,%,examples/utility/Provisioning<br>RAM for global variables,%,examples/utility/SelfProvisioning<br>flash,%,examples/utility/SelfProvisioning<br>RAM for global variables,%
arduino:mbed_nano:nanorp2040connect,132,0.0,8,0.0,132,0.0,8,0.0,132,0.0,8,0.0,149,0.0,8,0.0,149,0.0,8,0.0
arduino:mbed_nicla:nicla_vision,136,0.01,8,0.0,80,0.0,8,0.0,80,0.0,8,0.0,176,0.01,8,0.0,,,,
arduino:mbed_opta:opta,144,0.02,8,0.0,144,0.02,8,0.0,144,0.02,8,0.0,176,0.02,8,0.0,,,,
arduino:mbed_portenta:envie_m7,144,0.02,8,0.0,80,0.01,8,0.0,144,0.02,8,0.0,112,0.01,8,0.0,,,,
arduino:samd:mkr1000,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrgsm1400,160,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrnb1500,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,,,,
arduino:samd:mkrwan1300,192,0.07,8,0.02,192,0.07,8,0.02,192,0.07,8,0.02,,,,,,,,
arduino:samd:mkrwifi1010,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,176,0.07,8,0.02
arduino:samd:nano_33_iot,152,0.06,8,0.02,152,0.06,8,0.02,152,0.06,8,0.02,168,0.06,8,0.02,168,0.06,8,0.02
esp32:esp32:esp32,724,0.06,0,0.0,680,0.05,0,0.0,688,0.05,0,0.0,,,,,,,,
esp8266:esp8266:huzzah,8,0.0,0,0.0,136,0.01,12,0.01,12,0.0,4,0.0,,,,,,,,

@pennam pennam requested a review from mirkokurt February 20, 2023 12:30
@pennam pennam marked this pull request as ready for review February 20, 2023 12:30
@pennam pennam merged commit 8784783 into arduino-libraries:master Feb 28, 2023
@pennam pennam deleted the time_service_refactor branch May 17, 2024 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: code Related to content of the project itself type: enhancement Proposed improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants