Skip to content

Commit fb5112e

Browse files
authored
Merge pull request #210 from arduino/sebromero/x8-rpc
Add Python-Arduino Data exchange tutorial [PC-853]
2 parents f701d13 + 76e2fed commit fb5112e

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
---
2+
title: 'Data Exchange Between Python on Linux and an Arduino Sketch'
3+
description: 'This tutorial will show you how to run a python application that exchanges data with an Arduino Sketch.'
4+
tags:
5+
- RPC
6+
- Python
7+
author: 'Sebastian Romero'
8+
hardware:
9+
- hardware/04.pro/boards/portenta-x8
10+
---
11+
12+
## Overview
13+
14+
The container infrastructure provided by Arduino contains a pre-built Python image that you can use to run python applications on the Portenta X8. In this tutorial we're going to build a container based on a provided one. While all the peripherals can be accessed from the iMX8 processor running the Linux environment, it can be useful to let the onboard microcontroller take care of certain peripheral handling and just exchange the required data between the microcontroller and the Python application. In this tutorial you will learn how to do that. If you haven't done so, read through the [foundations article](/tutorials/portenta-x8/x8-fundamentals) to understand the fundamental concepts of the X8 and the provided infrastructure.
15+
16+
## Goals
17+
18+
- Learn how the RPC mechanism on the X8 works
19+
- Learn how to exchange sensor data between Linux and an Arduino sketch
20+
- Learn how to modify a container and run it
21+
- Learn how to use commands to debug the container and service infrastructure
22+
23+
## Required Hardware and Software
24+
25+
- [Portenta X8](https://store.arduino.cc/products/portenta-x8) board
26+
- [Portenta breakout](https://docs.arduino.cc/hardware/portenta-breakout) board
27+
- Any sensor (in this example we'll use an [BME680](https://www.bosch-sensortec.com/products/environmental-sensors/gas-sensors/bme680/) I<sup>2</sup>C module)
28+
29+
## Python on the X8
30+
31+
Python is a modern and powerful scripting language that can be used for all sorts of use cases. In this tutorial we only read sensor data from an Arduino sketch without doing anything interesting with it, but you could extend the example and process the data further.
32+
33+
### Communication Between Linux and Arduino Sketches
34+
35+
The python script will run on the Linux side and therefore on the iMX8 processor. The Arduino sketch on the other hand will run on the STM32H747 microcontroller. That allows for real-time processing on the Arduino side while running a fully fledged operating system on iMX8. However the two processors need a communication mechanism to exchange data with one another. The communication mechanism that is being used is referred to as RPC (Remote Procedure Call). To facilitate the communication the M7 core on the STM32H747 microcontroller is used which hands over any data / request to the M4 core. That means your Arduino sketch will solely run on the M4 core. Dual core processing on the Arduino side is currently not supported.
36+
37+
On the Linux side there is a service that takes care of sending data between the two worlds. It's called `m4-proxy`. You can check if the service is running by logging into the X8 via `adb shell` and then executing `sudo journalctl -fu m4-proxy`. If, for whatever reason, the service has stopped, you can restart it with `sudo systemctl restart m4-proxy`
38+
39+
## The Arduino Sketch
40+
41+
The Arduino sketch to read sensor data doesn't look much different from an ordinary sketch. The only thing that differs is that we expose the sensor data via RPC.
42+
43+
```arduino
44+
RPC.bind("temperature", []{ return bme.temperature; });
45+
RPC.bind("humidity", []{ return bme.humidity; });
46+
RPC.bind("pressure", []{ return bme.pressure / 100.0F; });
47+
RPC.bind("gas", []{ return bme.gas_resistance / 1000.0; });
48+
RPC.bind("altitude", []{ return bme.readAltitude(SEALEVELPRESSURE_HPA); });
49+
```
50+
51+
Two additional header files need to be included:
52+
53+
```arduino
54+
#include <RPC.h>
55+
#include <SerialRPC.h>
56+
```
57+
58+
The bind function makes the data available via the specified name e.g. "temperature". In our example an anonymous function is created that returns the corresponding sensor property whenever requested. Alternatively you could bind the name to an existing, named function instead. The data can then easily be requested using that name (e.g. "humidity") by querying the `m4-proxy` service. Once data is being requested it is packaged as a message and sent over SPI to the iMX8.
59+
60+
You can find the sketch in the software package [here](assets/python-sensor-rpc.zip). You may need to change the sketch depending on what sensor you would like to read from. If you're using an I<sup>2</sup>C sensor, you can connect SCL to **PWM6** and SDA to **PWM8** on the Portenta breakout. That's because the labeled I<sup>2</sup>C pins on the Portenta Breakout are only available on the Linux side. If you're using an analog sensor you can connect it to any analog pin. Please refer to the pinout diagram on the Portenta Breakout [documentation page](/hardware/portenta-breakout).
61+
62+
Make sure you've installed the "Arduino Mbed OS Portenta Boards" core and upload the sketch to the X8 in the Arduino IDE or via Arduino CLI.
63+
64+
### Debugging the Arduino Sketch
65+
66+
To check if the Arduino sketch is working correctly you may want to read the messages from the `Serial.println` statements. You can't currently read them directly in the serial monitor of the Arduino IDE. Instead, you can use a simple service called `py-serialrpc` which listens for those messages and prints them to the console. This service needs to run on the Linux side of the X8. You can get the files [here](assets/py-serialrpc.zip). Upload them to the X8 with `adb push py-serialrpc /home/fio`.
67+
68+
Log into the X8 shell with `adb shell` and navigate into the `serialrpc` folder. Build the container using `sudo docker build . -t py-serialrpc`. The `-t` flag assigns a tag to the container. Then run the container by executing `cd..` and then `sudo docker-compose up -d`. The `-d` flag detaches the container so it runs in the background. Note that this will run the docker container persistently across reboots by registering it as a systemd service. To stop the container, run `sudo docker-compose stop`.
69+
70+
Check if the container is running by executing `sudo docker ps`. You can then access the log of this service at any time by executing `sudo docker-compose logs -f --tail 20` from the **same directory**. If you don't 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 that are being sent from the M4.
71+
72+
## The Python Application
73+
74+
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.:
75+
76+
```python
77+
m4_proxy_address = 'm4-proxy'
78+
m4_proxy_port = 5001
79+
rpc_address = RpcAddress(m4_proxy_address, m4_proxy_port)
80+
rpc_client = RpcClient(rpc_address)
81+
temperature = rpc_client.call('temperature')
82+
```
83+
84+
The files for the complete Python application can be found in the same package as the Arduino sketch (see above). Upload the `python-sensor-rpc` folder to the X8 via `adb push python-sensor-rpc /home/fio`. Log into the X8 via `adb shell`. Then navigate into the `python-sensor-rpc` folder and execute `sudo docker build . -t python-sensor-rpc`. When it's done you can run the container with `sudo docker-compose up`. After a few seconds you should see the output from the Python application featuring the sensor readings on the M4 that were piped through the RPC mechanism. The output should look similar to the following:
85+
86+
```
87+
python-sensor-rpc_1 | ============================================
88+
python-sensor-rpc_1 | == Portenta X8 Sensor reading ==
89+
python-sensor-rpc_1 | ============================================
90+
python-sensor-rpc_1 |
91+
python-sensor-rpc_1 | Temperature: 25.904266357421875
92+
python-sensor-rpc_1 | Humidity: 25.564695358276367
93+
python-sensor-rpc_1 | Pressure: 976.4400024414062
94+
python-sensor-rpc_1 | Gas: 136.496
95+
python-sensor-rpc_1 | Altitude: 311.0769348144531
96+
```
97+
98+
Keep in mind that, whenever you change anything in the Python script on your computer you will have to sync it back to the X8 and re-build the container:
99+
100+
```
101+
# On your computer
102+
adb push python-sensor-rpc /home/fio
103+
104+
# On X8
105+
sudo docker-compose down
106+
sudo docker build . -t python-sensor-rpc
107+
sudo docker-compose up
108+
```
109+
110+
Alternatively you could modify the files directly on the X8 using an editor such as VIM so you don't need to upload the files all the time. Re-building the container will be necessary in any case though. In case you wonder how to specify the python script that is executed when running a container, have a look at the `Dockerfile` file. There you'll find the `ENTRYPOINT` command that takes multiple arguments. In our example: `ENTRYPOINT [ "python3", "m4_to_python.py"]`
111+
112+
## Conclusion
113+
114+
In this tutorial you learned how to use the docker infrastructure to build a container that runs a python application. You have also learned how to use the RPC mechanism to exchange data between the microcontroller and the iMX8 which runs the Linux operating system.
115+
116+
### Next Steps
117+
118+
- You may now further process the data that you receive from the Arduino sketch and e.g. upload it to a cloud service or similar.
119+
- Familiarize yourself with Docker commands to adjust the docker configuration to your needs.

0 commit comments

Comments
 (0)