Python code to read from the serial port of a Raspberry Pi

To develop your projects with Arduino or ESP8266 on Raspberry Pi basis, one may need to access the serial port for the update. This tutorial should work on all distributions of Linux and Raspbian in particular. I wrote this tutorial because I needed to access messages from my Gateway MySensors ESP8266 plugged into the USB port of my Raspberry Pi 3.

Install pyserial on Raspbian

Depending on the version of python, there are several commands to install the package pyserial. To know the version of python run

 python -version

If the pip command is available (run pip to see if return or not command not found bash) run (replace with pip2 and pip3 based on the python version used).

 sudo install pyserial pip3

Otherwise through apt-get

 sudo apt-get install python-serial python3-serial

Read on the serial port

Free serial port

Before going any further, check that the console is not listening on the serial port. To do this run

 sudo raspi-config

Then follow the images

raspi-config port serie

2-raspi-config-port-serie

3-raspi-config-port-serie

4-raspi-config-port-serie

Find the ttyUSB port that corresponds to your device

There are several ways to determine the USB port to which the device is connected. For options go here. The fastest is to connect the unit to the USB port then to immediately run the command dmesg -s 1024.

You’ll get directly the tty port on which it is connected.

dmesg-s-1024-port-usb-serie

Python code to read the serial port

Open nano

sudo nano read_serial.py

And copy the following code

#!/usr/bin/env python
import time
import serial

ser = serial.Serial(
 port='/dev/ttyUSB0',
 baudrate = 9600,
 parity=serial.PARITY_NONE,
 stopbits=serial.STOPBITS_ONE,
 bytesize=serial.EIGHTBITS,
 timeout=1
)
counter=0


while 1:
 x=ser.readline()
 print x

Register with the combination Ctrl + X then Y by giving a name to the file (for example read_serial.py).

Now, launch the program to read messages sent on the serial port from your device (for example an Arduino).

sudo python read_serial.py
raspberry lire port serie code python esp8266 mysensors gateway wifi

Reading the messages of a Gateway MySensors on the serial port of a Raspberry Pi.

To exit the program, make the combinations of keys Ctrl + C.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

1 Comment
  1. Hi!
    This was really helpful (I think, if I get it to work in the end 🙂 ), but I don’t get the same output as you in order to identify the tty port!

    After disconnecting the USB device (I just tried with the mouse, since I don’t have access yet to the controller I am going to have serial communication with), and it only says “USB disconnect, device number 7″… (I attached a picture of it)

    So how to know what kind of port specification I should write in “port=’/dev/ttyUSB0′,”???

    https://uploads.disquscdn.com/images/151dd8c26212723629002f91eb5cf6527b35ead43d69fd3bd477a5ff0fe36834.jpg

Leave a Reply

DIY Projects
%d bloggers like this: