The Raspberry Pi Pico is a great little device. It has excellent capabilities, is low cost, has a well-thought-out SDK, and exceptional documentation but is missing just one thing – network connectivity.
Several RP2040 based MCUs, are either currently on the market or are soon to be released. However, compared to the price of a Pico, these devices are rather costly. In addition, none of these solutions include hardwired Ethernet connectivity.
This article will show you how to add network capabilities to your Pico cost-effectively using an existing network-capable Raspberry Pi. We will leverage the Telemetrix for the Raspberry Pi Pico package combined with two open-source networking utilities. No additional hardware is required.
Just follow the steps outlined below. You, too, will be able to control and monitor the Pico's GPIO pins over the network using simple Python scripts from your PC.
How Does This Work?The Telemetrix project allows you to monitor and control the GPIO of a Raspberry Pi Pico by sending messages from your PC across a USB/Serial link to firmware resident on the Pico. If you enable a pin as an input, any changes are automatically reported as well.
What if we could take the serial data stream and convert it to a TCP/IP stream, effectively relaying the serial data over TCP?
That is precisely what we are about to do, so let's get started.
Step 1: Load The Telemetrix Software On Your PicoJust follow the steps described on this page of the Telemetrix User's Guide.
Step 2: Make Sure That Raspberry Pi OS Is Up To DateOn your Raspberry Pi computer, open a terminal window and enter the following command:
sudo apt-get update
Followed by this command:
sudo apt-get upgrade
When that completes, go to Step 3.
Step 3: Find The IP Address Of Your Raspberry PiOpen a terminal window and type the following command:
ifconfig
You should see something similar to the screenshot below.
The eth0 entry shows the IP address my router has assigned to the Ethernet interface of the Raspberry Pi, and the wlan0 entry shows the WiFi address assigned. Since I have both an Ethernet cable connected to my router and the Raspberry Pi configured to use WiFi, both of these entries have IP addresses assigned. Of course, I could choose to use either of these addresses, but since I want to use WiFi for this demo, I am using the wlan0 address of 192.168.2.126.
Please make a note of the addresses listed for your Raspberry Pi. We will be using the IP address in a later step.
Step 4: Install And Configure ser2net On Your Raspberry Pi ComputerThe ser2net utility is a serial to network proxy. It translates an IP data stream to serial data and redirects the data to a specific serial port. It also translates data coming from the serial port into a TCP/IP stream. ser2net runs as a daemon process, and once installed, it will start automatically each time you boot your Raspberry Pi.
To install ser2net, type the following command in the terminal:
sudo apt-get install ser2net
Next, we need to configure ser2net by modifying its config file. Before doing so, first, stop the daemon. Type the following command into the terminal:
sudo systemctl stop ser2net
Now modify the /etc/ser2net.config file by invoking your favorite editor using sudo.
For illustration purposes, let's use the nano editor. In the terminal, type the following command:
sudo nano /etc/ser2net.config
We need to add the following line to the bottom of the file:
3333:raw:0:/dev/ttyACM0:115200,remctl
Save the file and exit the editor.
The added line tells ser2net to redirect IP data received on IP Port 3333 to the serial device on com port /dev/ttyACM0. For further information about ser2net, refer to its man page by typing:
man ser2net
Now, let's re-enable the ser2net daemon. In the terminal window, type:
sudo systemctl start ser2net
Step 5: Connect A USB Cable Between The Pico and Raspberry PiAfter connecting the cable between the two devices, type:
ls -l /dev/ttyA*
You should see ttyACM0 listed.
ls -l /dev/ttyA*
crw-rw---- 1 root dialout 166, 0 Jun 12 19:17 /dev/ttyACM0
crw-rw---- 1 root dialout 204, 64 Jun 12 19:40 /dev/ttyAMA0
Step 6: Is Your Client Computer Windows Based?If you use either Linux or macOS, please skip to Step 7.
For Windows, you will need to have Windows Subsystem for Linux (WSL) installed on your computer.
WSL lets developers run a GNU/Linux environment -- including most command-line tools, utilities, and applications -- directly on Windows, unmodified, without the overhead of a traditional virtual machine or dual-boot setup.
To install WSL, please follow this tutorial. When selecting a version of Linux, please select Ubuntu 20.04.
After installing, please go to step 7. Use a WSL terminal window for all further steps.
Step 7: Installing SOCATSocat is a command line-based utility that establishes two bidirectional byte streams and transfers data between them. Socat is the peer of ser2net used on the Raspberry Pi server computer. We will install it on our client PC.
In a terminal window on the client computer, type:
sudo apt-get install socat
Step 8: Telemetrix Client InstallationThere are a few preliminaries to take care of.
First, make sure that you have Python3 installed on your computer. Here is a procedure to do so.
Next, make sure that you have the pip3 utility installed to download packages from the PyPi repository. Check if you have pip3 installed by typing:
pip3 -V
The version of pip3 should appear in the terminal. If there is a blank line or if you get an error message about installing pip3, then enter the following:
sudo apt-get install python3-pip
We are now ready to install Telemetrix from PyPi. In a terminal window, type:
sudo pip3 install telemetrix-rpi-pico
Step 9: Getting And Modifying A Telemetrix Example ScriptClick on this link to go to the telemtrix-rpi-pico distribution page. Click on the green Code button.
Save and then extract the.zip file. After unzipping, go to the examples directory and edit blink.py. We will use this example since it does not require any additional hardware.
Change the following line in the file from:
# Create a Telemetrix instance.
board = telemetrix_rpi_pico.TelemetrixRpiPico()
To:
# Create a Telemetrix instance.
board = telemetrix_rpi_pico.TelemetrixRpiPico(com_port='/home/afy/MyProxySerialPort')
The modified line is contained within a single line of code.
Save the file.
As we shall see in Step 10, the socat utility will create a pseudo com port called.
/home/afy/MyProxySerialPort
Here we are telling the Python script to use that com port.
Step 10: Start SOCAT and Run The Blink ExampleIn a terminal window, copy and paste the following command. Before running it, make sure that the IP address matches the one of your Raspberry Pi.
socat pty,link=$HOME/MyProxySerialPort,waitslave tcp:192.168.2.126:3333,forever,reuseaddr,keepalive &
This command resides on a single line. At the end of the line, the ampersand places the command in the background so that you can run the blink.py script within the same terminal window.
Now run the modified blink.py, and you should see the LED on the Pico blink.
python3 blink.py
ConclusionYou are now ready to create your own programs or, if you prefer, to run some of the other demos. Just remember to specify the com_port parameter as we did in Step 9.
If you wish to write your own scripts, please consult the Telemetrix API and User's Guide.
Happy Pico Networking!
Comments