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/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-micropython/giga-micropython.md
+87
Original file line number
Diff line number
Diff line change
@@ -202,6 +202,93 @@ In the table below, you will find the matching between the **STM32H7** and **Ard
202
202
| PA4 | A12 |
203
203
| PA5 | A13 |
204
204
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 importDEVICE_ID
215
+
from secrets importSECRET_KEY
216
+
217
+
# Switch callback, toggles the LED.
218
+
defon_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.
# 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 importDEVICE_ID
258
+
from secrets importSECRET_KEY
259
+
260
+
# Switch callback, toggles the LED.
261
+
defon_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.
# 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
+
whileTrue:
286
+
client.update()
287
+
time.sleep(0.100)
288
+
```
289
+
290
+
`secrets.py` file should look the same on both implementations.
291
+
205
292
## Conclusion
206
293
207
294
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