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
@@ -92,16 +92,16 @@ If the service has stopped unexpectedly, you can restart it with the following c
92
92
sudo systemctl restart m4-proxy
93
93
```
94
94
95
-
## The Arduino Sketch
95
+
## Arduino Sketch: Sensor Data Over RPC
96
96
97
97
The Arduino sketch to read sensor data does not look much different from an ordinary sketch. The only difference is that we expose the sensor data via RPC.
Two additional header files need to be included to enable the RPC mechanism on Portenta X8:
@@ -125,13 +125,13 @@ That is because the labeled I<sup>2</sup>C pins on the Portenta Breakout are onl
125
125
126
126
Make sure you have installed the **Arduino Mbed OS Portenta Boards** core and upload the sketch to the X8 in the Arduino IDE or via Arduino CLI.
127
127
128
-
***The example code provided in the repository uses preset sensor values (returning fixed values like 100, 200, 300, etc.) for demonstration purposes. It allows you to test the RPC mechanism without connecting actual hardware sensors. To use real sensor data, you will need to modify the Arduino sketch to include your sensor library and update the `RPC.bind()` calls to return actual sensor readings instead of the preset values.***
128
+
***The example code provided in the repository uses preset sensor values (returning fixed values like 100, 200, 300, etc.) for demonstration purposes. It allows you to test the RPC mechanism without connecting actual hardware sensors. To use real sensor data, you will need to modify the Arduino sketch to include your sensor library and update the `RPC.bind()` calls to return actual sensor readings instead of the preset values. For sensor implementation example please refer to [this section](#sensor-implementation).***
129
129
130
130
### Debugging the Arduino Sketch
131
131
132
-
To check if the Arduino sketch is working correctly, you may want to read the messages from the `Serial.println` statements. You cannot currently read them directly from the Arduino IDE's serial monitor. Instead, you can use a simple service called **`py-serialrpc`**, which listens for those messages and prints them to the console.
132
+
To check if the Arduino sketch is working correctly, you may want to read the messages from the `Serial.println` statements. You cannot currently read them directly from the Arduino IDE's serial monitor. Instead, you can use the **`python-rpc-serial`** container, which listens for those messages and prints them to the console.
133
133
134
-
This service needs to run on the Linux side of the X8. You can get the files [here](https://github.com/arduino/portenta-containers/tree/main/python-rpc-serial). Clone or download the repository to your local machine, then from the command prompt, navigate to the adb tool folder and upload the files to the X8 with the command:
134
+
This container needs to run on the Linux side of the X8. You can get the files [here](https://github.com/arduino/portenta-containers/tree/main/python-rpc-serial). Clone or download the repository to your local machine, then from the command prompt, navigate to the adb tool folder and upload the files to the X8 with the command:
If you do not wish to run the container in the background, skip the `-d` flag, you will get the console output directly in the executing shell. Once the container is running, you will see the messages being sent from the M4.
202
205
203
-
## The Python® Application
206
+
## Python® Application: Reading Sensor Data
204
207
205
208
The Python® application requests the sensor data from the M4 over RPC and unpacks the message. Data can be requested by calling the function exposed over RPC on the M4, e.g.:
You have two options to run the Python® application.
@@ -262,17 +265,7 @@ docker run -d \
262
265
263
266
After a few seconds, you should see the output from the Python application featuring the sensor readings on the M4 that exchanges through the RPC mechanism. The output should look similar to the following:
@@ -330,6 +323,174 @@ If you're wondering how to specify the Python® script to run when a container i
330
323
ENTRYPOINT ["python3", "main.py"]
331
324
```
332
325
326
+
## Sensor Implementation
327
+
328
+
The example provided in the repository uses preset sensor values for testing. To connect and read from actual sensors, you need to modify the Arduino sketch to include the appropriate sensor library and implement proper sensor initialization and reading.
329
+
330
+
### BME680 Sensor Example
331
+
332
+
The BME680 is an environmental sensor that provides temperature, humidity, pressure, gas resistance, and altitude readings. Here is an example implementation:
333
+
334
+
```arduino
335
+
#include <RPC.h>
336
+
#include <SerialRPC.h>
337
+
#include <Wire.h>
338
+
#include <Adafruit_Sensor.h>
339
+
#include "Adafruit_BME680.h"
340
+
341
+
#define SEALEVELPRESSURE_HPA (1013.25)
342
+
343
+
Adafruit_BME680 bme;
344
+
345
+
void setup()
346
+
{
347
+
Serial.begin(115200);
348
+
Serial.println("BME680 test on M4");
349
+
350
+
Wire.begin();
351
+
RPC.begin();
352
+
353
+
Serial.println("Trying to find sensor...");
354
+
355
+
for (auto status = bme.begin(); !status; delay(250)) {
356
+
Serial.println("Could not find a valid BME680 sensor, check wiring!");
This sketch includes sensor initialization with error checking, configuration of oversampling rates, and continuous sensor readings in the loop that can be monitored through the serial output or accessed via RPC from the Python application.
409
+
410
+
### BME280 Sensor Example
411
+
412
+
The BME280 provides temperature, humidity, pressure, and altitude readings but lacks a gas sensor. Here is an example implementation:
413
+
414
+
```arduino
415
+
#include <RPC.h>
416
+
#include <SerialRPC.h>
417
+
#include <Wire.h>
418
+
#include <Adafruit_BME280.h>
419
+
#include <Adafruit_Sensor.h>
420
+
421
+
#define SEALEVELPRESSURE_HPA (1013.25)
422
+
423
+
Adafruit_BME280 bme;
424
+
425
+
void setup()
426
+
{
427
+
Serial.begin(115200);
428
+
Serial.println("BME280 test on M4");
429
+
430
+
Wire.begin();
431
+
RPC.begin();
432
+
433
+
for (auto status = bme.begin(); !status; delay(250)) {
434
+
Serial.println("Could not find a valid BME280 sensor, check wiring!");
For the BME280, a dummy gas RPC binding that returns 0 is included since this sensor does not have gas sensing capabilities. This provides compatibility with the Python script that expects all five RPC calls.
473
+
474
+

475
+
476
+
### Python Script Considerations
477
+
478
+
The Python script in the repository uses an optimized approach for making multiple RPC calls:
data =tuple(get_value(measure) for measure in sensors)
487
+
except RpcError.TimeoutError:
488
+
print("Unable to retrieve data from the M4.")
489
+
return data
490
+
```
491
+
492
+
This approach creates a new `RpcClient` instance for each call due to a known limitation with the `msgpackrpc` library. The lambda function and tuple comprehension provide a clean way to collect all sensor readings.
493
+
333
494
## Troubleshooting
334
495
335
496
### RPC Communication Issues
@@ -374,7 +535,9 @@ Flash the firmware using the programming script:
374
535
sudo /usr/arduino/extra/program.sh
375
536
```
376
537
377
-
The programming script will verify and flash the new firmware. You should see output indicating the programming progress, verification, and successful reset. After flashing completes, restart the X8 and try rerunning your Python application.
538
+
The programming script will verify and flash the new firmware. You should see output indicating the programming progress, verification, and successful reset. After flashing completes, restart the X8 and try rerunning your Python application or [example](#building-the-image-from-source).
@@ -406,41 +569,12 @@ Replace `YOUR_USERNAME` with your actual Windows username. Build the firmware:
406
569
make
407
570
```
408
571
409
-
### Understanding the Example Code
410
-
411
-
The `python-rpc-sensors` example uses preset sensor values for demonstration purposes. The Arduino sketch returns fixed preset values (100, 200, 300, 400, 500) rather than reading from actual hardware sensors. It allows you to test the RPC mechanism without connecting physical sensors.
412
-
413
-
To use real sensor data, you need to modify the Arduino sketch to include your sensor library (such as `Adafruit_BME680` or `Adafruit_BMP280`) and update the `RPC.bind()` calls to return actual sensor readings. For example, with a real BME680 sensor:
If you are using a BMP280 sensor instead of a BME680, be aware that the BMP280 provides temperature, humidity, pressure, and altitude readings but lacks a gas resistance sensor. The example Python script calls all five RPC functions, including `gas`, which will cause a timeout error if your Arduino sketch does not implement it.
430
-
431
-
To use a BMP280 sensor, you can either modify your Arduino sketch to provide a dummy gas value by adding:
432
-
433
-
```arduino
434
-
RPC.bind("gas", []{ return 0; });
435
-
```
436
-
437
-
Since the BMP280 does not have a gas sensor, you can modify the Python script to skip the gas reading by commenting out the gas RPC call. The BMP280 does support altitude calculation, so you can include that RPC call in your BMP280 sketch if needed.
438
-
439
572
## Conclusion
440
573
441
-
In this tutorial, you learned how to use the Docker infrastructure to run a Python® application on the Portenta X8. You explored two approaches to running the application: using a prebuilt Docker image for quick deployment and building the image from source for customization. You have also learned how to use the RPC mechanism to exchange data between the microcontroller and the iMX8, which runs Linux.
574
+
In this tutorial, you learned how to use the Docker infrastructure to run a Python® application on the Portenta X8. You explored two approaches to running the application: using a prebuilt Docker image for quick deployment and building the image from source for customization. You have also learned how to use the RPC mechanism to exchange data between the microcontroller and the iMX8, which runs Linux, and how to implement real sensor readings using BME680 and BME280 sensors.
442
575
443
576
### Next Steps
444
577
445
578
- You may further process the data you receive from the Arduino sketch and, e.g., upload it to a Cloud service or similar.
446
579
- Familiarize yourself with Docker commands to adjust the docker configuration to your needs.
580
+
- Experiment with other sensors and create custom RPC bindings for your specific use case.
0 commit comments