How to Monitor Arduinos Connected to Multiple Ports of Raspberry Pi / Laptop Simultaneously Using Python
by svraut29 in Circuits > Raspberry Pi
1535 Views, 1 Favorites, 0 Comments
How to Monitor Arduinos Connected to Multiple Ports of Raspberry Pi / Laptop Simultaneously Using Python
Do you need to connect multiple Arduino devices to your Raspberry pi and monitor all of them simultaneously? This tutorial will show you how to easily monitor multiple Arduino connected to your Raspberry Pi. We will send a simple message "Hello from Arduino No. {1,2,3, and so on}" on the serial output of Arduino to Raspberry Pi via USB.
Instead of Raspberry Pi, you can also use your Laptop/PC which has Python 3 installed in it.
Supplies
- Arduino Uno or Nano or any Arduino board x N
- Power USB cables x N
- Raspberry Pi and related kit (monitor, power cable, etc.) or Laptop/PC
Connect, Power Up and Start Your Raspberry Pi
Follow this tutorial to start up your Raspberry Pi: https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up/4
Upload Codes in the Arduino Devices
Upload the below code to your Arduino devices. Simply modify the device id, so that we know which Arduino is sending message to Raspberry Pi. Modify #define Arduino_no as 1,2,3, and so on.
#define Arduino_no 1
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
delay(5000);
Serial.println("Hello from Arduino No."+ Arduino_no);
}
Verify Working of Arduino on COM Port of Arduino IDE
Test your Arduino code first on Arduino IDE to ensure that each device is printing the message "Hello from Arduino No. {1,2,3,so on}" on your COM port.
Close Arduino IDE and COM Port
Close all Arduino windows, especially the COM port. In any device, only one program can access a port at a time. If COM port is open, our python program will not be able to access the port.
Install PyMultiSerial Python Module on Raspberry Pi
Execute the below command in python cmd/console:
pip3 install pyMultiSerial
pyMultiSerial is a powerful python module/library for working with serial ports of RaspberryPi/Laptop/PC.
Run the Below Python Code in Raspberry Pi / Laptop
import pyMultiSerial as p
# Create object of class pyMultiSerial
ms = p.MultiSerial()
ms.baudrate = 9600
ms.timeout = 2
#Add Callbacks
#Callback functions provide you an interface to perform an action at certain event.
# Callback function on detecting a port connection.
# Parameters: Port Number, Serial Port Object
# Return: True if the port is to be accepted, false if the port is to be rejected based on some condition
def port_connection_found_callback(portno, serial):
print ("Port Found: "+portno)
#register callback function
ms.port_connection_found_callback = port_connection_found_callback
# Callback on receiving port data
# Parameters: Port Number, Serial Port Object, Text read from port
def port_read_callback(portno, serial, text):
print ("Received '"+text+"' from port "+portno)
pass
#register callback function
ms.port_read_callback = port_read_callback
# Callback on port disconnection. Triggered when a device is disconnected from port.
# Parameters: Port No
def port_disconnection_callback(portno):
print("Port "+portno+" disconnected")
#register callback function
ms.port_disconnection_callback = port_disconnection_callback
# Start Monitoring ports
ms.Start()
## To stop monitoring, press Ctrl+C in the console or command line.
# Caution: Any code written below ms.Start() will be executed only after monitoring is stopped.
# Make use of callback functions to execute your code.
Check the Results
Now connect your Arduino devices to the USB port of Raspberry Pi. As soon as you connect, a message will be printed in the python console of Raspberry Pi - "Port COM{port_no} connected". When the Arduino prints data on serial output, the message "Hello from Arduino No. {Arduino_no}" will be printed on python console of Raspberry Pi.