Skip to content

Commit 044c2b9

Browse files
committed
update micropython tutorial
1 parent 1f9392e commit 044c2b9

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed
479 KB
Loading
1.82 MB
Loading
1.68 MB
Loading

content/arduino-cloud/00.guides/03.micropython/content.md

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ libraries:
1212

1313
## Introduction
1414

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).
1618

1719
## Goals
1820

1921
The goals of this tutorial are:
2022

2123
- 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.
2325
- Control an LED using the Arduino IoT Cloud.
2426

2527
## Hardware & Software Needed
@@ -31,34 +33,50 @@ The goals of this tutorial are:
3133

3234
***To install MicroPython, read the [MicroPython Installation Guide](https://docs.arduino.cc/micropython/basics/board-installation) written for all Arduino boards.***
3335

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.
3441

42+
### Thing & Device Configuration
3543

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.
3748

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:
49+
![Device Key.](assets/device-key.png)
3950

40-
- [IoT Cloud Variables](https://docs.arduino.cc/arduino-cloud/getting-started/cloud-variables)
41-
- [IoT Cloud Dashboards & Widgets](https://docs.arduino.cc/arduino-cloud/getting-started/dashboard-widgets)
51+
- Learn more about Things in the [Things documentation]()
52+
- Learn more about Devices in the [Devices documentation]()
4253

43-
The resulting Thing and dashboard should look similar to the following:
54+
### Create Variables
4455

45-
![Thing with two boolean variables.](./assets/thing.png)
56+
Next step is to create some cloud variables, which we will later interact with via a MicroPython script.
4657

47-
![Dashboard with an LED and a Switch widget.](./assets/dashboard.png)
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.
4862

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`
5066

51-
![Dialog prompting to select setup flow](./assets/setup-device-prompt.png)
67+
It is important that they are named exactly like this, as we will be using them in the example script of this guide.
5268

53-
Give your board the desired name.
69+
Your Thing should look something like this when you are finished:
5470

55-
![Give your device a name of your choice.](./assets/set-device-name.png)
71+
![Finished Thing interface.](assets/thing.png)
5672

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]()***
5874

59-
![The connection credentials to be written down.](./assets/get-key.png)
75+
## MicroPython Setup
6076

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:
6280

6381
```python
6482
WIFI_SSID = "myNetwork" # Network SSID
@@ -67,15 +85,13 @@ DEVICE_ID = b"ef77wer88-0432-4574-85e1-54e3d5cac861"
6785
CLOUD_PASSWORD = b"TQHFHEKKKLSYMPB1OZLF"
6886
```
6987

70-
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.
7489

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.
7591

76-
## Installing The Library
92+
### Install Cloud Library
7793

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:
7995

8096
```bash
8197
$ pip install mpremote
@@ -172,14 +188,10 @@ if __name__ == "__main__":
172188
- `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.
173189
- `client.start()` - Enters a loop that runs as long as the board is connected to the cloud and synchronises data as it runs.
174190

175-
176-
177191
## Testing It Out
178192

179193
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.
180194

181-
182-
183195
## Troubleshoot
184196

185197
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:
189201
- Ensure the device ID and Cloud password in the `secrets.py` file match with what is registered on the IoT Cloud.
190202
- Make sure your IoT Cloud Thing is correctly set up and your device is assigned to it.
191203

192-
193-
194204
## Conclusion
195205

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

Comments
 (0)