You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/arduino-cloud/01.getting-started/06.cloud-variables/cloud-variables.md
+28-16Lines changed: 28 additions & 16 deletions
Original file line number
Diff line number
Diff line change
@@ -8,21 +8,21 @@ difficulty: beginner
8
8
9
9
## Overview
10
10
11
-
An essential component of the [Arduino Cloud](https://create.arduino.cc/iot/) is a **cloud variable**. A cloud variable is the same as a regular variable that you use in an Arduino sketch, but with some additional functionality.
11
+
An essential component of the [Arduino Cloud](https://create.arduino.cc/iot/) is a **cloud variable**.
12
12
13
-
A cloud variable is synced between your Arduino board and the Arduino Cloud. So if a variable is updated on your board (like a sensor reading), the Arduino Cloud will also receive this value. Similarly, if you update a variable from the cloud, it also updates on your board.
13
+
A cloud variable is synced between your Arduino board and the Arduino IoT Cloud. If a variable is updated on your board (like reading a sensor), the Arduino Cloud will also receive this value. Similarly, if you update a variable from the cloud, it also updates on your board.
14
14
15
-
This means that at any given time, you are able to read and send data to and from your board, as long as your board is connected to the Arduino IoT Cloud.
15
+
As long as your board maintains connection to the Arduino IoT Cloud, variables can be updated.
16
16
17
17
**In this article, we will cover:**
18
18
- How to sync variables between your board and the Arduino IoT Cloud.
19
-
-What variables are available to use.
19
+
-Types of variables and list of available ones.
20
20
- How to structure a sketch for optimal variable synchronization.
21
21
- How to synchronize variables between devices.
22
22
23
23
## Create and Configure Variables
24
24
25
-
Creating and configuring variables are done inside **Thing**, starting with the **"Add Variable"** button.
25
+
Creating and configuring variables are done inside a **Thing**, starting with the **"Add Variable"** button.
26
26
27
27

28
28
@@ -50,19 +50,19 @@ Whenever you add, change or remove a variable, a file called `thingProperties.h`
50
50
51
51
Since it is defined in `thingProperties.h`, you do not need to declare it in your `.ino` file.
52
52
53
-
Let's say we create a integer variable called `sensor_value`. To use this in a sketch, to for example read a sensor, we can use:
53
+
Let's say we create an integer variable called `sensor_value`. To use this in a sketch, we simply use:
54
54
55
55
```arduino
56
56
sensor_value = analogRead(A0);
57
57
```
58
58
59
-
Note that we do not need to define the variable anywhere, as it has already been configured in `thingProperties.h`.
59
+
We do not need to define the variable anywhere, as it has already been configured in `thingProperties.h`.
60
60
61
61
***Note that if you change a variable, you will need to upload the code to your board for the effects to come in change.***
62
62
63
63
### Generated Functions
64
64
65
-
When creating a variable with a **Read & Write** permission, a function is also generated in your sketch.
65
+
When creating a variable with a **Read & Write** permission, a function is generated at the bottom of your sketch.
66
66
67
67
For example, a boolean variable named `button_switch` will generate a function called `void onButtonSwitch(){}`. This function executes every time the variable is changed from the cloud (through a dashboard).
68
68
@@ -85,9 +85,7 @@ Data between a board and the cloud synchronizes whenever the `ArduinoCloud.updat
85
85
86
86
It is a good practice to **not** use the `delay()` function in a cloud sketch. Please refer to the [millis()](https://www.arduino.cc/reference/en/language/functions/time/millis/) function that can be used to create non-blocking delays.
87
87
88
-
A variable's sync between a board and the cloud is limited to **two message per second (500ms)**.
89
-
90
-
Below is an example on how to use the `millis()` function.
88
+
Below is an example on how to use the `millis()` function:
91
89
92
90
```arduino
93
91
unsigned long previousMillis = 0;
@@ -104,23 +102,29 @@ void loop(){
104
102
previousMillis = currentMillis;
105
103
106
104
//code here will update every 1 second
107
-
//without blocking the program (and cloud update)
105
+
//without blocking the program and the cloud update
108
106
}
109
107
110
108
```
111
109
112
-
## Sync Variables Between Things
110
+
***Note that a variable's sync between a board and the cloud is limited to two message per second (500ms)***
111
+
112
+
### Sync Variables Between Things
113
113
114
114
It is possible to sync one or many variables with each other, between Things. This is the easiest method available to connect two Arduino board devices, wirelessly.
115
115
116
116
This is done in the configuration of a variable, in the **Sync With Other Things** option.
117
117
118
-
***To learn how to use this feature, read the [Device to Device]() tutorial.***
118
+
***To learn how to use this feature, read the [Device to Device](https://docscontent-karlsoderbyvariablesarticle.gtsb.io/arduino-cloud/features/device-to-device) tutorial.***
119
119
120
120
## List of Variables
121
121
122
+
Cloud variables are divided into three categories: **basic, specialized** and **complex** types.
@@ -131,7 +135,7 @@ This is done in the configuration of a variable, in the **Sync With Other Things
131
135
132
136
### Specialized Types
133
137
134
-
For your convenience, IoT Cloud provides specialized types which are just wrappers around basic types but declare the variable semantics more explicitly. This enables smarter integrations with third-party services (such as Alexa) and better visualization of widgets in dashboards.
138
+
Specialized types are wrappers around basic types but declare the variable semantics more explicitly. This enables smarter integrations with third-party services (such as Alexa) and better visualization of widgets in dashboards.
135
139
136
140
You can use them just like a normal variable of the wrapped type, since they support assignment and comparison operators.
137
141
@@ -485,4 +489,12 @@ void onTvChange() {
485
489
}
486
490
}
487
491
488
-
```
492
+
```
493
+
494
+
## Summary
495
+
496
+
In this article, we have covered how to use variables in the Arduino IoT Cloud, and what variables are available.
497
+
498
+
We have also shown some code examples and good practices to keep variable synchronization optimal, such as using the `millis()` function.
499
+
500
+
The use of cloud variables is almost identical to how you use variables in a regular sketch, with the exception that they are synchronized with the Arduino IoT Cloud.
0 commit comments