在系统启动时使用respberry pi 3在2 * 16个字符的LCD上显示消息

时间:2018-07-07 21:30:42

标签: python raspberry-pi raspberry-pi3 iot lcd

我有raspberry pi 3 B

用例:在系统启动或重新启动时将ip addresstime打印到2 * 16 character lcd显示中。

为此,我正在使用从终端运行时工作正常的python代码。

问题: 在系统启动时,液晶屏上什么都没有显示。

详细信息

我尝试了三种方式:

方式1:

我已将以下几行添加到/etc/rc.local

echo "Display ip to 2*16 char display from /etc/rc.local" >> /home/pi/Workspace/python/pi-screen/logs.log

sudo /usr/bin/python /home/pi/Workspace/python/pi-screen/src/display.py >> /home/pi/Workspace/python/pi-screen/logs-1.log

在系统重新启动时,应该将其打印到lcd display,但不能。

当我使用display.py从控制台运行python display.py时,它正在运行文件并按预期打印到2 * 16 characters lcd display

方法2:

我什至尝试将脚本添加到/etc/init.d/lcd

使用/etc/init.d/lcd start运行时可以运行,但可以在系统启动时运行,但也不能运行

方法3:

使用.bashrc 工作(不推荐)

我将以下行添加到.bashrc文件中,并且有效

#Show IP Address (temp solution)
/home/pi/Workspace/python/pi-screen/src/display.py

不建议添加到.bashrc,因为它会在每次登录时打印到LCD屏幕上。

----------------------------------------------------------------------

使用的代码

lcd初始化文件 /etc/init.d/lcd

### BEGIN INIT INFO
# Provides: LCD - date / time / ip address
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Liquid Crystal Display
# Description: date / time / ip address
### END INIT INFO


#! /bin/sh
# /etc/init.d/lcd


export HOME
case "$1" in
    start)
        echo "Starting LCD"
        /home/pi/Workspace/python/pi-screen/src/display.py  2>&1 &
    ;;
    stop)
        echo "Stopping LCD"
        LCD_PID=`ps auxwww | grep pi-screen | head -1 | awk '{print $2}'`
        kill -9 $LCD_PID
    ;;
    *)
        echo "Usage: /etc/init.d/lcd {start|stop}"
        exit 1
    ;;
esac
exit 0

display.py

#!/usr/bin/python
import time
import display_conf
import utils

lcd = display_conf.get_lcd()

def show_msg(msg, how_long):
    lcd.message(msg)
    time.sleep(how_long)
    lcd.clear()


if __name__ == "__main__":
    now = utils.get_time()
    ip = utils.get_host_ip()
    msg = now + "\n" + ip

    show_msg(msg, 10)

display_config.py

import Adafruit_CharLCD as LCD

def get_lcd():
    # Raspberry Pi pin setup
    lcd_rs = 25
    lcd_en = 24
    lcd_d4 = 23
    lcd_d5 = 17
    lcd_d6 = 18
    lcd_d7 = 22
    lcd_backlight = 2

    # Define LCD column and row size for 16x2 LCD.
    lcd_columns = 16
    lcd_rows = 2

    lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight)

    return lcd

utils.py

import socket
import time

def get_host_ip():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect(("8.8.8.8", 80))
    ip = s.getsockname()[0]
    s.close()

    return ip

def get_time():
    now = time.strftime("%Y-%m-%d %H:%M:%S")
    return now

注意/home/pi/Workspace/python/pi-screen/src/display.py/etc/init.d/lcd都具有可执行权限。

2 个答案:

答案 0 :(得分:1)

要通过/etc/rc.local运行python脚本:

1)使用sudo /etc/rc.local编辑文件;

2)将以下内容添加到文件中之前 exit 0

(sleep 5;python /home/pi/Workspace/python/pi-screen/src/display.py)&

通过括号可以在后台运行多个命令。 sleep 5将使脚本的运行延迟5秒钟,因为启动rc.local时脚本所依赖的某些服务可能尚不可用。

或者,您可以使用crontab @reboot自动执行脚本。

使用crontab:

1)运行命令行sudo crontab -e;

2)将命令添加到文件末尾:

@reboot /usr/bin/python /home/pi/Workspace/python/pi-screen/src/display.py

答案 1 :(得分:-1)

简单的代码不会使它变得容易 1.使用此链接帮助http://www.circuitbasics.com/raspberry-pi-lcd-set-up-and-programming-in-python/正确连接 2.遵循此页面上写的所有说明 3.使用此代码

import time
import datetime
from RPLCD.gpio import CharLCD
from RPi import GPIO
GPIO.setmode (GPIO.BCM)
GPIO.setwarnings(False)
lcd = CharLCD(numbering_mode=GPIO.BCM, pin_rs=22, pin_rw=24, pin_e=23, pins_data=[26, 12, 16, 27],cols=16, rows=2,auto_linebreaks=True)
lcd.cursor_pos = (0,1) 
lcd.write_string(u'WELCOME EZIMAX')
while 1:    
    lcd.cursor_pos =(1,10)
    lcd.write_string("%s" %time.strftime("%H:%M")) 
    lcd.cursor_pos = (1,0)
    lcd.write_string("%s" %time.strftime("%d/%m/%y"))
  1. 如果您的连接线是BCM,请使用此代码
  2. 否则请根据BOARD接线更改代码。

如果您再次遇到任何问题或错误,请关注我并与我联系