Skip to content

Commit 0593080

Browse files
Updatecloud
1 parent ecb904c commit 0593080

File tree

1 file changed

+90
-2
lines changed
  • content/arduino-cloud/01.guides/04.micropython

1 file changed

+90
-2
lines changed

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

+90-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,95 @@ 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+
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 import DEVICE_ID
146+
from secrets import SECRET_KEY
147+
148+
# Switch callback, toggles the LED.
149+
def on_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.
158+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY)
159+
160+
# 2. Register cloud objects.
161+
# Note: The following objects must be created first in the dashboard and linked to the device.
162+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
163+
# the client object and new value args.
164+
client.register("sw1", value=None, on_write=on_switch_changed)
165+
166+
# 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 import DEVICE_ID
189+
from secrets import SECRET_KEY
190+
191+
# Switch callback, toggles the LED.
192+
def on_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.
201+
client = ArduinoCloudClient(device_id=DEVICE_ID, username=DEVICE_ID, password=SECRET_KEY, sync_mode=True)
202+
203+
# 2. Register cloud objects.
204+
# Note: The following objects must be created first in the dashboard and linked to the device.
205+
# When the switch is toggled from the dashboard, the on_switch_changed function is called with
206+
# the client object and new value args.
207+
client.register("sw1", value=None, on_write=on_switch_changed)
208+
209+
# 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+
while True:
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.
138226

139227

140228
```python
@@ -432,4 +520,4 @@ If the code is not working, there are some common issues we can troubleshoot:
432520

433521
## Conclusion
434522

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

Comments
 (0)