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/01.guides/04.micropython/content.md
+90-2
Original file line number
Diff line number
Diff line change
@@ -134,7 +134,95 @@ For more options on how to install libraries on your board, check out our [Insta
134
134
135
135
## Programming the Board
136
136
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
+
You can connect the GIGA R1 to the Arduino Cloud with MicroPython. 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. 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.
142
+
143
+
**Code example:**
144
+
```python
145
+
from secrets importDEVICE_ID
146
+
from secrets importSECRET_KEY
147
+
148
+
# Switch callback, toggles the LED.
149
+
defon_switch_changed(client, value):
150
+
# Note the client object passed to this function can be used to access
151
+
# and modify any registered cloud object. The following line updates
152
+
# the LED value.
153
+
client["led"] = value
154
+
155
+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
156
+
# objects to cloud objects. Note a username and password can be used for basic authentication
157
+
# 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.
167
+
client.register("led", value=None)
168
+
169
+
# 3. Start the Arduino cloud client.
170
+
client.start()
171
+
```
172
+
173
+
Remember that our `secrets.py` file should look like:
174
+
```python
175
+
WIFI_SSID=""# WiFi network SSID (for MicroPython)
176
+
WIFI_PASS=""# WiFi network key (for MicroPython)
177
+
DEVICE_ID=""# Provided by Arduino cloud when creating a device.
178
+
SECRET_KEY=""# Provided by Arduino cloud when creating a device.
179
+
```
180
+
181
+
#### Sync
182
+
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.
183
+
184
+
Alternatively, you can select the synchronous method by passing sync_mode=True when creating the client object and calling client.update() periodically after connecting.
185
+
186
+
Code example:
187
+
```python
188
+
from secrets importDEVICE_ID
189
+
from secrets importSECRET_KEY
190
+
191
+
# Switch callback, toggles the LED.
192
+
defon_switch_changed(client, value):
193
+
# Note the client object passed to this function can be used to access
194
+
# and modify any registered cloud object. The following line updates
195
+
# the LED value.
196
+
client["led"] = value
197
+
198
+
# 1. Create a client object, which is used to connect to the IoT cloud and link local
199
+
# objects to cloud objects. Note a username and password can be used for basic authentication
200
+
# 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.
210
+
client.register("led", value=None)
211
+
212
+
# In synchronous mode, this function returns immediately after connecting to the cloud.
213
+
client.start()
214
+
215
+
# Update the client periodically.
216
+
whileTrue:
217
+
client.update()
218
+
time.sleep(0.100)
219
+
```
220
+
221
+
`secrets.py` file should look the same on both implementations.
222
+
223
+
### Project example
224
+
225
+
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.
138
226
139
227
140
228
```python
@@ -432,4 +520,4 @@ If the code is not working, there are some common issues we can troubleshoot:
432
520
433
521
## Conclusion
434
522
435
-
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.
523
+
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