Skip to content

Commit 8b68351

Browse files
authored
Merge pull request #2203 from arduino/Pedromsousalima/Cloud/SYNCMicroPython
[MKC-1693] Added reference to SYNC method on micropython
2 parents 6d79ff1 + b64dd91 commit 8b68351

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

content/arduino-cloud/01.guides/04.micropython/content.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,98 @@ For more options on how to install libraries on your board, check out our [Insta
134134

135135
## Programming the Board
136136

137-
Here is the example code to copy and paste into your program. It connects your device to Arduino Cloud over Wi-Fi®.
137+
### Cloud Connection
138+
There are two main methods to create this connection `async` and `sync`.
139+
140+
#### Async (Default)
141+
This is the method currently implemented by default with the Cloud. Asynchronous operations allow tasks to run independently of the main program flow. Functions can start and continue without waiting for other tasks to finish. This non-blocking behavior is achieved using techniques like callbacks, coroutines, or the async and await keywords in MicroPython.
142+
143+
Asynchronous functions are particularly useful for handling network communication, as they enable the boards to perform other operations (like reading sensors or updating outputs) while waiting for data from the Arduino Cloud.
144+
145+
146+
**Code example:**
147+
```python
148+
from secrets import DEVICE_ID
149+
from secrets import SECRET_KEY
150+
151+
# Switch callback, toggles the LED.
152+
def on_switch_changed(client, value):
153+
# Note the client object passed to this function can be used to access
154+
# and modify any registered cloud object. The following line updates
155+
# the LED value.
156+
client["led"] = value
157+
158+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
159+
# objects to cloud objects. Note a username and password can be used for basic authentication
160+
# on both CPython and MicroPython. For more advanced authentication methods, please see the examples.
161+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
162+
163+
# 2. Register cloud objects.
164+
# Note: The following objects must be created first in the dashboard and linked to the device.
165+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
166+
# the client object and new value args.
167+
client.register("sw1", value=None, on_write=on_switch_changed)
168+
169+
# The LED object is updated in the switch's on_write callback.
170+
client.register("led", value=None)
171+
172+
# 3. Start the Arduino cloud client.
173+
client.start()
174+
```
175+
176+
Remember that our `secrets.py` file should look like:
177+
```python
178+
WIFI_SSID = "" # WiFi network SSID (for MicroPython)
179+
WIFI_PASS = "" # WiFi network key (for MicroPython)
180+
DEVICE_ID = "" # Provided by Arduino cloud when creating a device.
181+
SECRET_KEY = "" # Provided by Arduino cloud when creating a device.
182+
```
183+
184+
#### Sync
185+
In synchronous operations, tasks are executed one after another in a sequential manner. Each function call waits for the previous one to complete before starting. This approach is straightforward and easier to implement but can cause delays if a task takes a long time to finish, as it blocks the execution of subsequent code. In the context of network communication with the Arduino Cloud, synchronous functions may lead to unresponsiveness during data transmission or reception.
186+
187+
Alternatively, you can select the synchronous method by passing sync_mode=True when creating the client object and calling client.update() periodically after connecting.
188+
189+
Code example:
190+
```python
191+
from secrets import DEVICE_ID
192+
from secrets import SECRET_KEY
193+
194+
# Switch callback, toggles the LED.
195+
def on_switch_changed(client, value):
196+
# Note the client object passed to this function can be used to access
197+
# and modify any registered cloud object. The following line updates
198+
# the LED value.
199+
client["led"] = value
200+
201+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
202+
# objects to cloud objects. Note a username and password can be used for basic authentication
203+
# on both CPython and MicroPython. For more advanced authentication methods, please see the examples.
204+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY, sync_mode=True)
205+
206+
# 2. Register cloud objects.
207+
# Note: The following objects must be created first in the dashboard and linked to the device.
208+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
209+
# the client object and new value args.
210+
client.register("sw1", value=None, on_write=on_switch_changed)
211+
212+
# The LED object is updated in the switch's on_write callback.
213+
client.register("led", value=None)
214+
215+
# In synchronous mode, this function returns immediately after connecting to the cloud.
216+
client.start()
217+
218+
# Update the client periodically.
219+
while True:
220+
client.update()
221+
time.sleep(0.100)
222+
```
223+
224+
`secrets.py` file should look the same on both implementations.
225+
226+
### Project example
227+
228+
Here is the example code to copy and paste into your program. It connects your device to Arduino Cloud over Wi-Fi® and toggles the LED of the board using the Arduino Cloud dashboard.
138229

139230

140231
```python

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,4 @@ In the table below, you will find the matching between the **STM32H7** and **Ard
206206

207207
In this article, we have learned how to install MicroPython on the GIGA R1, using the `dfu-util` tool. We have also gone through some useful tips and tricks that can help you develop and run MicroPython code on your board.
208208

209-
For more information, visit the [MicroPython with Arduino documentation](/micropython).
209+
For more information, visit the [MicroPython with Arduino documentation](/micropython).

0 commit comments

Comments
 (0)