Arduino IoT Cloud Scheduler Feature

Learn how to use the scheduler feature to trigger repeating jobs during specific times.

It is now possible to schedule jobs with the Arduino IoT Cloud, using the new

CloudSchedule
variable type. You can pick a start & end date for when the variable should be triggered, and for how long it should be active. This variable can be controlled in real time using a graphical widget that you can place on an IoT Cloud dashboard.

We can for example have:

  • One trigger scheduled to run every minute for 10 seconds
  • One trigger scheduled to run every hour for 10 minutes

Goals

The goals of this project are:

  • Learn how the
    CloudSchedule
    variable works.
  • Learn how to access local time in your sketch.

Make sure you have a cloud-compatible board.

How Does it Work?

A variable of

CloudSchedule
type can be configured to trigger at a specific time, with a specific duration. In the code, you do not need to worry about the logic for this. The configuration of the variable is done in the dashboard, through a widget associated to the variable.

For example, we can set such variable to be:

  • ON for 10 seconds, every minute.
  • ON for 8 hours, every day.
  • ON for a week, and then finish.

Example of a schedule configured to run for 10 seconds every minute.
Example of a schedule configured to run for 10 seconds every minute.

Application Examples

There are countless examples where schedulers can be used, but predominantly it can be utilized for timer projects. Below are a few examples that can be used as inspiration.

Note that "schedule_variable" used in these examples is the name we give when creating the variable in the Thing settings.

Basic Example

To test out functionality of your scheduler job, we can turn on an LED while the job is active.

1if (schedule_variable.isActive()) {
2 // whenever the job is "active", turn on the LED
3 digitalWrite(LED, HIGH);
4} else {
5 // whenever the job is "not active", turn off the LED
6 digitalWrite(LED, LOW);
7}

Multiple Schedulers

You can set up multiple scheduler variables. This way, you can have a different code executing at specific times. In the example below, there are three schedulers, each performing a unique task whenever triggered:

  • Write a high state to a pin.
  • Read a sensor.
  • Change a string.
1// scheduler 1 (write a high state to a pin)
2if (schedule_variable_1.isActive()) {
3 digitalWrite(3, HIGH);
4} else {
5 digitalWrite(3, LOW);
6}
7
8// scheduler 2 (read a sensor)
9if(schedule_variable_2.isActive()) {
10 sensor_variable = analogRead(A1);
11}
12
13// scheduler 3 (change a string)
14if (schedule_variable_3.isActive()) {
15 string_variable = "";
16 string_variable = "Update something here!";
17}

Smart Light Automation

Turning on and off light sources can be a great power saver for the office, home or vacation home. This can be achieved with a few lines of code and a relay shield.

1if (schedule_variable.isActive()) {
2 // can be configured to be ON between 8am - 5pm
3 digitalWrite(relay, HIGH);
4} else {
5 //can be set to OFF between 5pm - 8am the next morning
6 digitalWrite(relay, LOW);
7}

Plant Watering

For a "smart watering" setup, if you connect a pump through a relay, you can schedule a job to be on for a period of time (pumping water) and then turn off. This could for example occur for a few seconds every day to keep your plants alive.

1// configure to be "on" for 10 seconds every day
2if (schedule_variable.isActive()) {
3 digitalWrite(pump, HIGH);
4} else {
5 digitalWrite(pump, LOW);
6}

Or, if you use a driver, such as L298N, you can control the pump through:

1if (schedule_variable.isActive()) {
2 analogWrite(pump, 127); // range between 0-255
3} else {
4 analogWrite(pump, 0);
5}

Creating a Scheduler (Tutorial)

In this section you will find a step by step tutorial that will guide you to creating and testing out the scheduler feature.

  • Create a Thing.
  • Configure an Arduino board.
  • Create a basic sketch & upload it to the board.
  • Create a dashboard.

API

The two variables types introduced in this tutorial are

CloudTime
and
CloudSchedule
. These are used to retrieve local time and to schedule a job respectively.

The

CloudSchedule
variable is of a complex type. It has a internal boolean state, which can be checked through the
isActive()
function (returns
true
or
false
).

1if(schedule_variable.isActive()) {
2 //do something
3}

The

CloudTime
variable is an unsigned integer which is used to store current time. We can use the
getLocalTime()
function to retrieve local time.

1time_variable = ArduinoCloud.getLocalTime();

Create a Thing

If you are new to the Arduino IoT Cloud, you can either visit the Getting Started with Arduino IoT Cloud guide, or any of the tutorials in the Arduino IoT Cloud documentation. There you will find detailed step by step guides.

1. Navigate to Arduino IoT Cloud. You will need to log in with your Arduino account.

2. Create a new Thing by clicking on the "Create Thing" button. You can name it something like "Scheduler Test".

3. Now, we need to attach a device to our Thing, by clicking on "Select Device". If you already have a device configured, you can select it, otherwise, follow the configuration.

Note that your board will need to be connected to your computer at this point.

Once your device is configured and attached to your Thing, we can move on to create variables.

Create Variables

For this project, we will need to create the following variables, by clicking on the "Add Variable" button.

Variable NameVariable TypePermission
schedule_testCloudScheduleRead & Write
time_readCloudTimeRead Only
lightboolRead Only
counterintRead Only

Your Thing overview should now look like the image below:

The Thing overview.
The Thing overview.

Set Time Zone

In order for our job to go off at the right time, we need to set the time zone for our Thing. This is done in our Thing configuration, by selecting your time zone through the drop down menu available at the bottom of the page.

Change time zone for a Thing.
Change time zone for a Thing.

Network Credentials

To connect your device to the Arduino IoT Cloud, you need to enter your network credentials in the "network" section.

Note that this only applies to Wi-Fi enabled boards.

Programming the Board

Now, let's take a look at the sketch used for this tutorial. First, we need to navigate to the "Sketch Tab".

  • This sketch first checks whether the
    schedule_test
    variable is true, and if it is, increase the
    counter
    variable and turn on the
    light
    .
  • The
    light
    variable is used for feedback later on, when we create a dashboard. Whenever
    schedule_test
    is true, so is
    light
    .
  • The
    LED
    variable is also attached to pin 6, which is ON when the job is active, and OFF when the job is inactive.

Complete Sketch

1#include "thingProperties.h"
2#include <RTCZero.h>
3
4bool toggle;
5int LED = 6;
6
7void setup() {
8 Serial.begin(9600);
9 delay(1500);
10
11 initProperties();
12
13 ArduinoCloud.begin(ArduinoIoTPreferredConnection);
14 setDebugMessageLevel(2);
15 ArduinoCloud.printDebugInfo();
16 counter = 0;
17 light = false;
18}
19
20void loop() {
21 ArduinoCloud.update();
22 // Your code here
23 if (schedule_test.isActive()) {
24 light = true;
25 digitalWrite(LED, HIGH);
26
27 if (toggle) {
28 counter = ++counter;
29 toggle = false;
30 }
31 } else {
32 light = false;
33 digitalWrite(LED, LOW);
34 toggle = true;
35 }
36 if(ArduinoCloud.connected()){
37 time_read = ArduinoCloud.getLocalTime();
38 }
39}
40
41void onScheduleTestChange() {
42
43}

If you want to see if your board successfully connects to the cloud, open the Serial Monitor right after the upload finishes. If there's any errors, you will see them there.

Creating a Dashboard

Finally, let's build a dashboard that will help us interface with our Arduino board.

1. Head over to the "Dashboards" section, and click on the "Build Dashboard" button.

2. Now we will need to create a set of widgets that we will link to our variables. Follow the table below to create them.

WidgetVariable
Schedulerschedule_test
Time Pickertime_read
Valuevalue
LEDlight

When you create a widget, you will need to click on "Link Variable" in the widget settings, and then choose the variable you want to link (see the image below).

Linking a widget to a variable.
Linking a widget to a variable.

When the dashboard is finished, it should look something like the image below, where we have four widgets in total:

The final dashboard.
The final dashboard.

We are now finished with the setup, and can move on to testing out the scheduler feature.

Scheduling a Job

To schedule a job, there are several parameters that can be set:

  • Start & end date for a "job". For example, start on 1st of December at 00:00, end at 31st of December at 12.00.
  • Duration of the job, set in hours, minutes, seconds. If we for example set 1 minute, the
    schedule_test
    variable will be
    true
    for 1 minute.
  • Repetition interval, how often should this job occur: every minute, hour, day, week, month, day or year.

To create a job, simply click anywhere on the scheduler widget, and configure the job after your preferences.

Scheduler Widget settings.
Scheduler Widget settings.

Testing the Scheduler

In this tutorial, we created a job that has the following parameters:

  • Start: today.
  • End: never.
  • Duration: 10 seconds.
  • Repeat Every: minute.

The sketch used for this is configured to switch the

light
variable to true, whenever the
schedule_test
variable is true. While it is true, the LED widget, which is linked to the
light
variable, will be on (for 10 seconds). Each time this happens, the
counter
variable will also increase by 1.

The scheduler in action.

Troubleshoot

If you have any issues with finishing this tutorial, you can check out some common troubleshooting issues:

  • Is your network credentials correct? If you change these, you will need to upload the sketch again for the changes to have effect.
  • If you can't link a widget to a variable, you may have selected the wrong variable type. The scheduler widget will only work with the scheduler variable.
  • Does the variables created in your Thing match the variables in your sketch? For example, if your variable is named
    Schedule_test
    , the code provided in this tutorial will not work since it is case sensitive.

Conclusion

The scheduler is yet another amazing Arduino IoT Cloud feature that allows you to control the future. In this tutorial, we used just one scheduler, but you might just as easily create 10 schedulers that can perform actions automatically. This makes it an ideal building block for IoT projects.