The Raspberry Pi GPIO serial port configuration has changed under Jessie and also with the Raspberry PI 3. By Default serial port is disabled and on RPI 3 Bluetooth made it even made it more confusing to enable Serial Port on GPIO. In this post i’ll describes how to enable serial port on GPIO.
As i m not interested in Bluetooth so i m going to disable it and keep GPIO serial. In order to enable serial port edit /boot/config.txt and add enable_uart=1 at the end of file.
Login to your Raspberry pi via putty or any other shell software and run following command to edit /boot/config.txt
1 2 |
sudo nano /boot/config.txt enable_uart=1 |
Because Bluetooth and serial port on gpio there are two serial port /dev/ttyS0 and /dev/ttyAMA0, by default ttyS0 is serial port and mapped to GPIO pins 14 and 15. Before enable uart and modifying config.txt file:
1 2 3 4 |
root@pihome:~# ls -lh /dev/serial* lrwxrwxrwx 1 root root 5 Jul 25 14:19 serial0 -> ttyS0 lrwxrwxrwx 1 root root 7 Jul 25 14:19 serial1 -> ttyAMA0 |
After modification you should see following:
1 2 3 4 |
root@pihome:~# ls -lh /dev/serial* lrwxrwxrwx 1 root root 7 Jul 25 14:27 serial0 -> ttyAMA0 lrwxrwxrwx 1 root root 5 Jul 25 14:27 serial1 -> ttyS0 |
We are not using our serial port other then communicating to ardunot gateway and receving message and send out messages we need to disable it.
Pre-Raspberry Pi 3
1 2 |
sudo systemctl stop serial-getty@ttyAMA0.service sudo systemctl disable serial-getty@ttyAMA0.service |
For Raspberry Pi3
1 2 |
sudo systemctl stop serial-getty@ttyS0.service sudo systemctl disable serial-getty@ttyS0.service |
Now we need to remove console from cmdline.txt file.
1 |
sudo nano /boot/cmdline.txt |
if file contain line or text console=serial0, just remove it, here is example from my RPI 3
1 |
dwc_otg.lpm_enable=0 console=tty1 root=PARTUUID=e8a97676-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait |
Here is example from Raspberry pi B+ model
1 |
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait |
Last settings to swap serial on Raspberry Pi 3, all pre-Raspberry pi 3 model can ignore this. Edit config.txt file and look for dtoverlay=pi3-miniuart-bt and remove comments, if this line isnt there just add it to config.txt file.
1 |
sudo nano /boot/config.txt |
Here are the last few lines from my config.txt file
1 2 3 4 5 6 |
# Enable audio (loads snd_bcm2835) dtparam=audio=on enable_uart=1 dtoverlay=pi3-miniuart-bt # remove comment from following line to disable bluetooth #dtoverlay=pi3-disable-bt |
Pre-RPI 3 Model – Settings
By default, raspberry pi serial port display login screen hence we need to disable this so we can use this serial port. Open inittab file and locate T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 line and comment out it.
1 2 |
vi /etc/inittab # T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100 |
Now we need to remove one more line from /boot/cmdline.txt to prevent raspberry pi from sending data during boot to serial port.
1 2 |
vi /boot/cmdline.txt console=ttyAMA0,115200 kgdboc=ttyAMA0,115200 |
Now it’s time to reboot your raspberry pi, once rpi rebooted successfully login via ssh and issue command sudo dmesg, this will bring lots of text but we only need to look for UART line to identify our serial port which in my case is ttyAMA0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
~ sudo dmesg [ 0.000000] Booting Linux on physical CPU 0x0 [ 0.000000] Initializing cgroup subsys cpuset [ 0.000000] Initializing cgroup subsys cpu [ 0.000000] Initializing cgroup subsys cpuacct [ 0.000000] Linux version 4.1.19+ (dc4@dc4-XPS13-9333) (gcc version 4.9.3 (crosstool-NG crosstool-ng-1.22.0-88-g8460611) ) #858 Tue Mar 15 15:52:03 GMT 2016 [ 0.000000] CPU: ARMv6-compatible processor [410fb767] revision 7 (ARMv7), cr=00c5387d [ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT nonaliasing instruction cache [ 0.000000] Machine model: Raspberry Pi Model B Plus Rev 1.2 …. …. [ 0.136028] Serial: AMBA PL011 UART driver [ 0.136405] 20201000.uart: ttyAMA0 at MMIO 0x20201000 (irq = 83, base_baud = 0) is a PL011 rev2 |
Installing dependencies
Login to your raspberry pi via ssh and issue following commands one by one to install Python, PySerial, MSQL-Python for interfacing with serial port from python and save data to MySQL database.
1 2 3 4 5 6 7 |
sudo apt-get install python2.7 wget https://bootstrap.pypa.io/get-pip.py sudo python get-pip.py sudo pip install pyserial sudo apt-get install libmysqlclient-dev sudo apt-get install python2.7-dev sudo pip install MySQL-python |
If you want to continue building PiHome with MySensors Serial Gateway please following this Post.
One comment