It is now possible to schedule jobs with the Arduino 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 Arduino Cloud dashboard.We can for example have:
The goals of this project are:
CloudSchedule
variable works.Make sure you have a cloud-compatible board.
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:
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.
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 LED3 digitalWrite(LED, HIGH);4} else {5 // whenever the job is "not active", turn off the LED6 digitalWrite(LED, LOW); 7}
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:
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}
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 morning6 digitalWrite(relay, LOW); 7}
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 day2if (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-2553} else {4 analogWrite(pump, 0);5}
In this section you will find a step by step tutorial that will guide you to creating and testing out the scheduler feature.
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 something3}
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();
If you are new to the Arduino Cloud, you can either visit the Getting Started with Arduino Cloud guide, or any of the tutorials in the Arduino Cloud documentation. There you will find detailed step by step guides.
1. Navigate to Arduino 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.
For this project, we will need to create the following variables, by clicking on the "Add Variable" button.
Variable Name | Variable Type | Permission |
---|---|---|
schedule_test | CloudSchedule | Read & Write |
time_read | CloudTime | Read Only |
light | bool | Read Only |
counter | int | Read Only |
Your Thing overview should now look like the image below:
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.
To connect your device to the Arduino Cloud, you need to enter your network credentials in the "network" section.
Note that this only applies to Wi-Fi enabled boards.
Now, let's take a look at the sketch used for this tutorial. First, we need to navigate to the "Sketch Tab".
schedule_test
variable is true, and if it is, increase the counter
variable and turn on the light
.light
variable is used for feedback later on, when we create a dashboard. Whenever schedule_test
is true, so is light
.LED
variable is also attached to pin 6, which is ON when the job is active, and OFF when the job is inactive.1#include "thingProperties.h"2
3bool toggle;4int LED = 6;5
6void setup() {7 Serial.begin(9600);8 delay(1500); 9
10 initProperties();11
12 ArduinoCloud.begin(ArduinoIoTPreferredConnection);13 setDebugMessageLevel(2);14 ArduinoCloud.printDebugInfo();15 counter = 0;16 light = false;17}18
19void loop() {20 ArduinoCloud.update();21 // Your code here 22 if (schedule_test.isActive()) {23 light = true;24 digitalWrite(LED, HIGH);25
26 if (toggle) {27 counter = ++counter;28 toggle = false;29 }30 } else {31 light = false;32 digitalWrite(LED, LOW);33 toggle = true;34 }35 if(ArduinoCloud.connected()){36 time_read = ArduinoCloud.getLocalTime();37 }38}39
40void onScheduleTestChange() {41 42}
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.
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.
Widget | Variable |
---|---|
Scheduler | schedule_test |
Time Picker | time_read |
Value | value |
LED | light |
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).
When the dashboard is finished, it should look something like the image below, where we have four widgets in total:
We are now finished with the setup, and can move on to testing out the scheduler feature.
To schedule a job, there are several parameters that can be set:
schedule_test
variable will be true
for 1 minute.To create a job, simply click anywhere on the scheduler widget, and configure the job after your preferences.
In this tutorial, we created a job that has the following parameters:
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.If you have any issues with finishing this tutorial, you can check out some common troubleshooting issues:
Schedule_test
, the code provided in this tutorial will not work since it is case sensitive.The scheduler is yet another amazing Arduino 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.