Skip to content

Commit f4c5238

Browse files
Added reference to ASYNC method on micropython
1 parent 62eaa90 commit f4c5238

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

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

+87
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,93 @@ In the table below, you will find the matching between the **STM32H7** and **Ard
202202
| PA4 | A12 |
203203
| PA5 | A13 |
204204

205+
## Arduino Cloud
206+
207+
You can connect the GIGA R1 to the Arduino Cloud with MicroPython. There are two main methods to create this connection `async` and `sync`.
208+
209+
### Async (Default)
210+
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. Asynchronous functions are particularly useful for handling network communication, as they enable the GIGA R1 to perform other operations (like reading sensors or updating outputs) while waiting for data from the Arduino Cloud.
211+
212+
**Code example:**
213+
```python
214+
from secrets import DEVICE_ID
215+
from secrets import SECRET_KEY
216+
217+
# Switch callback, toggles the LED.
218+
def on_switch_changed(client, value):
219+
# Note the client object passed to this function can be used to access
220+
# and modify any registered cloud object. The following line updates
221+
# the LED value.
222+
client["led"] = value
223+
224+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
225+
# objects to cloud objects. Note a username and password can be used for basic authentication
226+
# on both CPython and MicroPython. For more advanced authentication methods, please see the examples.
227+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
228+
229+
# 2. Register cloud objects.
230+
# Note: The following objects must be created first in the dashboard and linked to the device.
231+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
232+
# the client object and new value args.
233+
client.register("sw1", value=None, on_write=on_switch_changed)
234+
235+
# The LED object is updated in the switch's on_write callback.
236+
client.register("led", value=None)
237+
238+
# 3. Start the Arduino cloud client.
239+
client.start()
240+
```
241+
242+
Remember that our `secrets.py` file should look like:
243+
```python
244+
WIFI_SSID = "" # WiFi network SSID (for MicroPython)
245+
WIFI_PASS = "" # WiFi network key (for MicroPython)
246+
DEVICE_ID = "" # Provided by Arduino cloud when creating a device.
247+
SECRET_KEY = "" # Provided by Arduino cloud when creating a device.
248+
```
249+
250+
### Sync
251+
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.
252+
253+
Alternatively, you can select the synchronous method by passing sync_mode=True when creating the client object and calling client.update() periodically after connecting.
254+
255+
Code example:
256+
```python
257+
from secrets import DEVICE_ID
258+
from secrets import SECRET_KEY
259+
260+
# Switch callback, toggles the LED.
261+
def on_switch_changed(client, value):
262+
# Note the client object passed to this function can be used to access
263+
# and modify any registered cloud object. The following line updates
264+
# the LED value.
265+
client["led"] = value
266+
267+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
268+
# objects to cloud objects. Note a username and password can be used for basic authentication
269+
# on both CPython and MicroPython. For more advanced authentication methods, please see the examples.
270+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY, sync_mode=True)
271+
272+
# 2. Register cloud objects.
273+
# Note: The following objects must be created first in the dashboard and linked to the device.
274+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
275+
# the client object and new value args.
276+
client.register("sw1", value=None, on_write=on_switch_changed)
277+
278+
# The LED object is updated in the switch's on_write callback.
279+
client.register("led", value=None)
280+
281+
# In synchronous mode, this function returns immediately after connecting to the cloud.
282+
client.start()
283+
284+
# Update the client periodically.
285+
while True:
286+
client.update()
287+
time.sleep(0.100)
288+
```
289+
290+
`secrets.py` file should look the same on both implementations.
291+
205292
## Conclusion
206293

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

0 commit comments

Comments
 (0)