Skip to content

Commit 121f860

Browse files
authored
Some improvements
1 parent 31ea5c7 commit 121f860

File tree

1 file changed

+58
-80
lines changed

1 file changed

+58
-80
lines changed

content/cloud/iot-cloud/tutorials/03.cloud-scheduler/cloud-scheduler.md

Lines changed: 58 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ software:
77
- iot-cloud
88
---
99

10-
It is now possible to schedule jobs with the [Arduino IoT Cloud](https://create.arduino.cc/iot/), 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 is done through the IoT Cloud dashboards.
10+
It is now possible to schedule jobs with the [Arduino IoT Cloud](https://create.arduino.cc/iot/), 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.
1111

1212
We can for example have:
1313

14-
- One trigger scheduled to go off every minute, and last for 10 seconds
15-
- One trigger scheduled to go off every hour, and last for 10 minutes.
14+
- One trigger scheduled to run every minute for 10 seconds
15+
- One trigger scheduled to run every hour for 10 minutes
1616

1717
## Goals
1818

@@ -21,48 +21,18 @@ The goals of this project are:
2121
- Learn how the `CloudSchedule` variable works.
2222
- Learn how to access local time in your sketch.
2323

24-
## Hardware & Software Needed
25-
26-
For this tutorial, you will need a cloud compatible board. You can see the full list below:
27-
28-
- [MKR 1000 WiFi](https://store.arduino.cc/arduino-mkr1000-wifi)
29-
- [MKR WiFi 1010](https://store.arduino.cc/arduino-mkr-wifi-1010)
30-
- [MKR GSM 1400](https://store.arduino.cc/arduino-mkr-gsm-1400)\*
31-
- [MKR NB 1500](https://store.arduino.cc/arduino-mkr-nb-1500-1413)\*
32-
- [Nano RP2040 Connect](https://store.arduino.cc/nano-rp2040-connect)
33-
- [Nano 33 IoT](https://store.arduino.cc/arduino-nano-33-iot)
34-
- [Portenta H7](https://store.arduino.cc/portenta-h7)
35-
36-
***The MKR GSM 1400 & MKR NB 1500 requires a SIM card with a data plan to work. You can read more about it in [this page](https://store.arduino.cc/digital/sim).***
24+
Make sure you have a [cloud-compatible board](/cloud/iot-cloud/tutorials/technical-reference#compatible-hardware).
3725

3826
## How Does it Work?
3927

40-
The working principle of the scheduler is pretty straight forward. The `CloudSchedule` variable can be configured to go off at a specific time, with a specific duration. In the code, you do not need to worry about any timers, as this is done in the Arduino IoT Cloud. The "jobs" are created in the dashboard, through a widget associated to the variable.
28+
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.
4129

42-
For example, we can set `schedule_variable` to be:
30+
For example, we can set such variable to be:
4331
- ON for 10 seconds, every minute.
4432
- ON for 8 hours, every day.
4533
- ON for a week, and then finish.
4634

47-
![Example of how a scheduler works.](assets/cloud-scheduler-img-01.png)
48-
49-
## API
50-
51-
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.
52-
53-
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`).
54-
55-
```arduino
56-
if(schedule_variable.isActive()) {
57-
//do something
58-
}
59-
```
60-
61-
The `CloudTime` variable is an unsigned integer which is used to store current time. We can use the `getLocalTime()` function to retrieve local time.
62-
63-
```arduino
64-
time_variable = ArduinoCloud.getLocalTime();
65-
```
35+
![Example of a schedule configured to run for 10 seconds every minute.](assets/cloud-scheduler-img-01.png)
6636

6737
## Application Examples
6838

@@ -75,15 +45,12 @@ There are countless examples where schedulers can be used, but predominantly it
7545
To test out functionality of your scheduler job, we can turn on an LED while the job is active.
7646

7747
```arduino
78-
// whenever the job is "active", turn on the LED
79-
if(schedule_variable.isActive()){
80-
digitalWrite(LED, HIGH);
81-
}
82-
83-
84-
// whenever the job is "not active", turn off the LED
85-
else{
86-
digitalWrite(LED, LOW);
48+
if (schedule_variable.isActive()) {
49+
// whenever the job is "active", turn on the LED
50+
digitalWrite(LED, HIGH);
51+
} else {
52+
// whenever the job is "not active", turn off the LED
53+
digitalWrite(LED, LOW);
8754
}
8855
```
8956

@@ -96,23 +63,22 @@ You can set up **multiple scheduler variables.** This way, you can have a differ
9663
- Change a string.
9764

9865
```arduino
99-
//scheduler 1 (write a high state to a pin)
100-
if(schedule_variable_1.isActive()){
101-
digitalWrite(3, HIGH);
102-
}
103-
else{
104-
digitalWrite(3, HIGH);
66+
// scheduler 1 (write a high state to a pin)
67+
if (schedule_variable_1.isActive()) {
68+
digitalWrite(3, HIGH);
69+
} else {
70+
digitalWrite(3, LOW);
10571
}
10672
107-
//scheduler 2 (read a sensor)
108-
if(schedule_variable_2.isActive()){
109-
sensor_variable = analogRead(A1);
73+
// scheduler 2 (read a sensor)
74+
if(schedule_variable_2.isActive()) {
75+
sensor_variable = analogRead(A1);
11076
}
11177
112-
//scheduler 3 (change a string)
113-
if(schedule_variable_3.isActive()){
114-
string_variable = "";
115-
string_variable = "Update something here!";
78+
// scheduler 3 (change a string)
79+
if (schedule_variable_3.isActive()) {
80+
string_variable = "";
81+
string_variable = "Update something here!";
11682
}
11783
```
11884

@@ -122,14 +88,12 @@ string_variable = "Update something here!";
12288
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](https://store.arduino.cc/products/arduino-mkr-relay-proto-shield).
12389

12490
```arduino
125-
//can be configured to be ON between 8am - 5pm
126-
if(schedule_variable.isActive()){
127-
digitalWrite(relay, HIGH);
128-
}
129-
130-
//can be set to OFF between 5pm - 8am the next morning
131-
else{
132-
digitalWrite(relay, LOW);
91+
if (schedule_variable.isActive()) {
92+
// can be configured to be ON between 8am - 5pm
93+
digitalWrite(relay, HIGH);
94+
} else {
95+
//can be set to OFF between 5pm - 8am the next morning
96+
digitalWrite(relay, LOW);
13397
}
13498
```
13599

@@ -140,25 +104,21 @@ digitalWrite(relay, LOW);
140104
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.
141105

142106
```arduino
143-
//configure to be "on" for 10 seconds every day
144-
if(schedule_variable.isActive()){
145-
digitalWrite(pump, HIGH);
146-
}
147-
148-
else{
149-
digitalWrite(pump, LOW);
107+
// configure to be "on" for 10 seconds every day
108+
if (schedule_variable.isActive()) {
109+
digitalWrite(pump, HIGH);
110+
} else {
111+
digitalWrite(pump, LOW);
150112
}
151113
```
152114

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

155117
```arduino
156-
if(schedule_variable.isActive()){
157-
analogWrite(pump, 127); //range between 0-255
158-
}
159-
160-
else{
161-
analogWrite(pump, 0);
118+
if (schedule_variable.isActive()) {
119+
analogWrite(pump, 127); // range between 0-255
120+
} else {
121+
analogWrite(pump, 0);
162122
}
163123
```
164124

@@ -171,6 +131,24 @@ In this section you will find a step by step tutorial that will guide you to cre
171131
- Create a basic sketch & upload it to the board.
172132
- Create a dashboard.
173133

134+
### API
135+
136+
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.
137+
138+
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`).
139+
140+
```arduino
141+
if(schedule_variable.isActive()) {
142+
//do something
143+
}
144+
```
145+
146+
The `CloudTime` variable is an unsigned integer which is used to store current time. We can use the `getLocalTime()` function to retrieve local time.
147+
148+
```arduino
149+
time_variable = ArduinoCloud.getLocalTime();
150+
```
151+
174152
### Create a Thing
175153

176154
***If you are new to the Arduino IoT Cloud, you can either visit the [Getting Started with Arduino IoT Cloud](https://docs.arduino.cc/cloud/iot-cloud/tutorials/iot-cloud-getting-started) guide, or any of the tutorials in the [Arduino IoT Cloud documentation](https://docs.arduino.cc/cloud/iot-cloud). There you will find detailed step by step guides.***

0 commit comments

Comments
 (0)