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
+168-1Lines changed: 168 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,174 @@ A cloud variable is synced between your Arduino board and the Arduino Cloud. So
14
14
15
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.
16
16
17
-
In this article, we will explore how to use variables in the Arduino IoT Cloud,
17
+
**In this article, we will cover:**
18
+
- How to sync variables between your board and the Arduino IoT Cloud.
19
+
- What variables are available to use.
20
+
- How to structure a sketch for optimal variable synchronization.
21
+
- How to synchronize variables between devices.
18
22
19
23
`ArduinoCloud.update()`
20
24
25
+
## Create and Configure Variables
26
+
27
+
Creating and configuring variables are done inside **Thing**, starting with the **"Add Variable"** button.
28
+
29
+
![Click on "Add Variable"]()
30
+
31
+
### Variable Configuration
32
+
33
+
Inside a variable configuration, we have several options:
34
+
35
+
-**Name:** a friendly name for your variable. No spaces or special characters allowed.
36
+
-**(optional) Sync With Other Things:** sync a variable with a variable from another Thing. Whenever one variable updates, the other will follow.
37
+
-**Type:** type of variable. Choose between three categories.
38
+
-[**Basic:**]() e.g. `float`, `int`, `String`.
39
+
-[**Specialized:**]() e.g. `CloudAcceleration`, `CloudTemperature`, `CloudFrequency`.
40
+
-[**Complex:**]() e.g. `CloudColor`, `CloudTelevision`.
41
+
-**Declaration:** the declaration of your variable. This is what you will use in a sketch.
42
+
-**Variable Permission:**
43
+
-**Read & Write:** variable can be updated from board and cloud.
44
+
-**Read Only:** variable can only be updated from the board.
45
+
-**Variable Update Policy:**
46
+
-**On Change:** variable synchronizes whenever value changes (threshold is `0` by default).
47
+
-**Periodically:** variable synchronizes every `x` seconds.
48
+
49
+
### Automatic Sketch Generation
50
+
51
+
Whenever you add, change or remove a variable, a file called `thingProperties.h` is updated automatically. This is a configuration file that should always be included in your main sketch (it is generated automatically).
52
+
53
+
Since it is defined in `thingProperties.h`, you do not need to declare it in your `.ino` file.
54
+
55
+
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:
56
+
57
+
```arduino
58
+
sensor_value = analogRead(A0);
59
+
```
60
+
61
+
Note that we do not need to define the variable anywhere, as it has already been configured in `thingProperties.h`.
62
+
63
+
***Note that if you change a variable, you will need to upload the code to your board for the effects to come in change.***
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.
80
+
81
+
You can use them just like a normal variable of the wrapped type, since they support assignment and comparison operators.
The following variable types hold multiple values internally and are used to represent more complex data. In order to access such values, methods are provided.
To read the Color values, we can use the following method `Color colorValues = x.getValue();`. This will assign the hue, saturation, and brightness values to the `colorValues` variable.
To set the color, we can assign the CloudColor variable directly to float variables `x = {hue,saturation,brightness}`, or using the method ` x = Color(hue,saturation,brightness)`.
161
+
162
+
#### CloudLocation
163
+
164
+
Declared as `CloudLocation x;`.
165
+
166
+
To read the location values, we can use the following method `Location coordinates = x.getValue();`. This will assign the longitude and latitude values to the coordinates variable. If we want too access the values individually we can use `Serial.println(coordinates.lat)` and `Serial.println(coordinates.lon)`.
| Input |`InputValue` ([Up to 60 values](https://github.com/arduino-libraries/ArduinoIoTCloud/blob/master/src/property/types/automation/CloudTelevision.h) such as HDMI1, HDMI2, DVD, GAME...etc.) |`x.getInput()`|`x.setInput()`|
0 commit comments