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/00.guides/03.micropython/content.md
+39-29Lines changed: 39 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,14 +12,16 @@ libraries:
12
12
13
13
## Introduction
14
14
15
-
This tutorial guides you on how to use the MicroPython library to connect your Arduino device to the Arduino IoT Cloud.
15
+
This tutorial guides you on how to use the MicroPython library to connect your Arduino device to the Arduino IoT Cloud.
16
+
17
+
It requires your board to have a version of MicroPython installed, which is covered in [this article](/micropython/basics/board-installation).
16
18
17
19
## Goals
18
20
19
21
The goals of this tutorial are:
20
22
21
23
- Connect your Arduino device to your Wi-Fi® network.
22
-
- Connect your Arduino device to the Arduino IoT Cloud.
24
+
- Connect your Arduino device to the Arduino Cloud via MicroPython.
23
25
- Control an LED using the Arduino IoT Cloud.
24
26
25
27
## Hardware & Software Needed
@@ -31,34 +33,50 @@ The goals of this tutorial are:
31
33
32
34
***To install MicroPython, read the [MicroPython Installation Guide](https://docs.arduino.cc/micropython/basics/board-installation) written for all Arduino boards.***
33
35
36
+
## Cloud Setup
37
+
38
+
Before we start, make sure you have MicroPython installed on your board. If you haven't you can follow [this tutorial](https://docs.arduino.cc/micropython/basics/board-installation).
39
+
40
+
Then, we need to configure a Thing in the [Arduino Cloud](app.arduino.cc/things) consisting of two boolean variables called `led` and `ledSwitch`. Follow the instructions below to do so.
34
41
42
+
### Thing & Device Configuration
35
43
36
-
## Setting Up Your Device and IoT Cloud
44
+
1. Create a new Thing, by clicking on the **"Create Thing"** button.
45
+
2. Click on the **"Select Device"** in the **"Associated Devices"** section of your Thing.
46
+
3. Click on **"Set Up New Device"**, and select the bottom category (**"Manual Device"**). Click continue in the next window, and choose a name for your device.
47
+
4. Finally, you will see a new **Device ID** and a **Secret Key** generate. You can download them as a PDF. Make sure to save it as you cannot access your Secret Key again.
37
48
38
-
Before we start, make sure you have MicroPython installed on your board. If you haven't you can follow [this tutorial](https://docs.arduino.cc/micropython/basics/board-installation). Then configure a Thing in the [Arduino IoT Cloud](https://create.arduino.cc/iot/) consisting of two boolean variables called `led` and `ledSwitch`. To set up a Thing and a corresponding dashboard, please follow these two tutorials:
-Learn more about Things in the [Things documentation]()
52
+
-Learn more about Devices in the [Devices documentation]()
42
53
43
-
The resulting Thing and dashboard should look similar to the following:
54
+
### Create Variables
44
55
45
-

56
+
Next step is to create some cloud variables, which we will later interact with via a MicroPython script.
46
57
47
-

58
+
1. While in Thing configuration, click on **"Add Variable"** which will open a new window.
59
+
2. Name your variable `led` and select it to be of an `boolean` type.
60
+
3. Click on **"Add Variable"** at the bottom of the window.
61
+
4. Create another variable, name it `ledSwitch` and select it to be `int` type.
48
62
49
-
Also, your device needs to be registered. Follow the flow "Any Device" ("Manual") when clicking **Add** in the "Devices" tab.
63
+
You should now have **two variables**:
64
+
-`test_switch`
65
+
-`test_value`
50
66
51
-

67
+
It is important that they are named exactly like this, as we will be using them in the example script of this guide.
52
68
53
-
Give your board the desired name.
69
+
Your Thing should look something like this when you are finished:
54
70
55
-

71
+

56
72
57
-
Eventually write down the Device ID / Secret Key pair that you will need to connect your device to Arduino IoT Cloud.
73
+
***Learn more about how variables work in the [Variables documentation]()***
58
74
59
-

75
+
## MicroPython Setup
60
76
61
-
You will obtain a pair of device id and device key after registration. Store these details, along with your Wi-Fi® credentials, in a `secrets.py` file. Here is an example of how `secrets.py` should look like:
77
+
### Create Secret.py File
78
+
79
+
During the [device configuration](#thing--device-configuration), you obtained a **device ID** and **secret key**. These details can be stored, along with your Wi-Fi® credentials, in a `secrets.py` file. Here is an example of how `secrets.py` should look like:
This file should be copied over to the flash drive that mounts when MicroPython boots. To do so you can use the file manager tool in Arduino Lab for MicroPython or drag & drop the file manually. Please note that the latter option is not recommended as the file system can potentially get corrupted when copying files manually.
71
-
72
-
After configuring your device, **assign** it to the thing that you created previously. This gives access permission to the registered board.
73
-
88
+
In a MicroPython editor, you can create this file, and save it on your board running MicroPython.
74
89
90
+
This file should be copied over to the flash drive that mounts when MicroPython boots. To do so you can use the file manager tool in Arduino Lab for MicroPython. Please note that the latter option is not recommended as the file system can potentially get corrupted when copying files manually.
75
91
76
-
##Installing The Library
92
+
### Install Cloud Library
77
93
78
-
To install the Arduino IoT Cloud (Micro)Python library on your board, you can use thy Python based tool `mpremote`. This requires Python to be installed. On macOS and Linux Python usually comes pre-installed. If it's not installed on your system you may download it from [here](https://www.python.org/downloads/). Then, to install `mpremote` you can use pip:
94
+
To install the Arduino IoT Cloud (Micro)Python library on your board, you can use the Python based tool `mpremote`. This requires Python to be installed. On macOS and Linux Python usually comes pre-installed. If it's not installed on your system you may download it from [here](https://www.python.org/downloads/). Then, to install `mpremote` you can use pip:
79
95
80
96
```bash
81
97
$ pip install mpremote
@@ -172,14 +188,10 @@ if __name__ == "__main__":
172
188
-`on_switch_changed` - Is the callback that gets executed when the `ledSwitch` variable is changed by toggling the switch on the cloud dashboard. This function in turn toggles the on-board LED and updates the cloud variable `led` that reflects the state of the on-board LED to be displayed in the cloud dashboard.
173
189
-`client.start()` - Enters a loop that runs as long as the board is connected to the cloud and synchronises data as it runs.
174
190
175
-
176
-
177
191
## Testing It Out
178
192
179
193
Open Arduino Lab for MicroPython and connect to your board. Pasting the above code and run the script. Then open your Arduino IoT Cloud dashboard. You should see the registered "ledSwitch" and "led" widgets. Toggle the "ledSwitch", and the LED on your Arduino board should light up accordingly. The state of the "led" variable should also change, mirroring the state of the physical LED.
180
194
181
-
182
-
183
195
## Troubleshoot
184
196
185
197
If the code is not working, there are some common issues we can troubleshoot:
@@ -189,8 +201,6 @@ If the code is not working, there are some common issues we can troubleshoot:
189
201
- Ensure the device ID and Cloud password in the `secrets.py` file match with what is registered on the IoT Cloud.
190
202
- Make sure your IoT Cloud Thing is correctly set up and your device is assigned to it.
191
203
192
-
193
-
194
204
## Conclusion
195
205
196
-
This tutorial has guided you through the process of connecting your Arduino device to the Arduino IoT Cloud using MicroPython. You learned how to install the necessary library, set up your device, and control an LED via the IoT Cloud. This opens up possibilities for more complex applications, as you can control and monitor your Arduino device remotely.
206
+
This tutorial has guided you through the process of connecting your Arduino device to the Arduino Cloud using MicroPython. You learned how to install the necessary library, set up your device, and control an LED via the Arduino Cloud. This opens up possibilities for more complex applications, as you can control and monitor your Arduino device remotely.
0 commit comments