本文最后更新于 296 天前,其中的信息可能已经有所发展或是发生改变。
树莓派4B+LCD1602+Python脚本, 检测网站情况
硬件设备
5V 3A电源 树莓派4B 4G内存 液晶显示屏LCD1602 LCD液晶屏转接板 杜邦线
软件设备
镜像系统:2022-01-28-raspios-bullseye-arm64-full(Debian11) 语言:Python3.7
效果图
模块上有一颗可调电阻,用于调节显示的对比度。如果你新拿到一块屏幕无论怎么调试都不见显示,记得调节一下这里
接线
GND --- GND
VCC --- 接树莓派 5V
SDA --- I2C 数据
SCL --- I2C 时钟
启动树莓派I2C
sudo apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config
重启 sudo reboot
测试
sudo i2cdetect -y 1
如上图说明已成功连接LCD1602。
添加驱动程序 LCD1602.py
import time
import smbus
BUS = smbus.SMBus(1)
LCD_ADDR = 0x27
BLEN = 1
def turn_light(key):
global BLEN
BLEN = key
if key ==1:
BUS.write_byte(LCD_ADDR, 0x08)
else:
BUS.write_byte(LCD_ADDR, 0x00)
def write_word(addr, data):
global BLEN
temp = data
if BLEN == 1:
temp |= 0x08
else:
temp &=0xF7
BUS.write_byte(addr, temp)
def send_command(comm):
buf = comm & 0xF0
buf |= 0x04
write_word(LCD_ADDR, buf)
time.sleep(0.002)
buf &=0xFB
write_word(LCD_ADDR, buf)
# Send bit3-0 secondly
buf = (comm & 0x0F) << 4
buf |= 0x04 # RS = 0, RW = 0, EN = 1
write_word(LCD_ADDR ,buf)
time.sleep(0.002)
buf &= 0xFB # Make EN = 0
write_word(LCD_ADDR ,buf)
def send_data(data):
# Send bit7-4 firstly
buf = data & 0xF0
buf |= 0x05 # RS = 1, RW = 0, EN = 1
write_word(LCD_ADDR ,buf)
time.sleep(0.002)
buf &= 0xFB # Make EN = 0
write_word(LCD_ADDR ,buf)
# Send bit3-0 secondly
buf = (data & 0x0F) << 4
buf |= 0x05 # RS = 1, RW = 0, EN = 1
write_word(LCD_ADDR ,buf)
time.sleep(0.002)
buf &= 0xFB # Make EN = 0
write_word(LCD_ADDR ,buf)
def init_lcd():
try:
send_command(0x33) # Must initialize to 8-line mode at first
time.sleep(0.005)
send_command(0x32) # Then initialize to 4-line mode
time.sleep(0.005)
send_command(0x28) # 2 Lines & 5*7 dots
time.sleep(0.005)
send_command(0x0C) # Enable display without cursor
time.sleep(0.005)
send_command(0x01) # Clear Screen
BUS.write_byte(LCD_ADDR ,0x08)
except:
return False
else:
return True
def clear_lcd():
send_command(0x01) # Clear Screen
def print_lcd(x, y, str):
if x < 0:
x = 0
if x > 15:
x = 15
if y <0:
y = 0
if y > 1:
y = 1
# Move cursor
addr = 0x80 + 0x40 * y + x
send_command(addr)
for chr in str:
send_data(ord(chr))
if __name__ == '__main__':
init_lcd()
print_lcd(0, 0, 'Hello, world!')
添加自己的运行文件main.py
#!/user/bin/env python
import smbus
import time
import subprocess
import pymysql
from sshtunnel import SSHTunnelForwarder
import LCD1602 as LCD
#获取网站访问数据
def getVisit():
ssh_host = "ip" # 你的ip地址或主机名
ssh_port = 22 # 连接mysql服务器的端口号,一般都是22,必须是数字
ssh_user = "user" # 这是你的用户名
ssh_password = "password" # 这是你的用户密码
mysql_host = "127.0.0.1" # 这是你mysql服务器的主机名或ip地址
mysql_port = 3306 # 这是你mysql服务器上的端口,3306,mysql就是3306,必须是数字
mysql_user = "root" # 这是你mysql数据库上的用户名
mysql_password = "password" # 这是你mysql数据库的密码
mysql_db = "wordpress" # mysql服务器上的数据库名
# 严格缩进要求,否则连接失败
with SSHTunnelForwarder(
(ssh_host, ssh_port),
ssh_username=ssh_user,
ssh_password=ssh_password,
remote_bind_address=(mysql_host, mysql_port)) as server:
conn = pymysql.connect(host=mysql_host,
port=server.local_bind_port,
user=mysql_user,
passwd=mysql_password,
db=mysql_db)
cursor = conn.cursor()
cursor.execute("select * from wp_statistics_visit where last_counter=\"" + time.strftime("%Y-%m-%d", time.localtime(time.time()))+"\"")
data = cursor.fetchall()
for row in data:
visit = row[3]
cursor.execute("select * from wp_statistics_visitor where last_counter=\"" + time.strftime("%Y-%m-%d", time.localtime(time.time()))+"\"")
data2 = cursor.fetchall()
temp = 0
last = ""
for row in data2:
if row[0]>temp:
temp = row[0]
last = row[7]
cursor.execute("select * from wp_statistics_useronline")
data3 = cursor.fetchall()
onlineNum = 0
platform = ""
if data3 !=pymysql.NULL:
for row in data3:
onlineNum+=1
platform=row[7]
conn.commit()
server.close()
cursor.close()
if conn != pymysql.NULL:
print("conn is connect")
return visit,last, onlineNum, platform
if __name__ == "__main__":
LCD.init_lcd()
LCD.turn_light(1)
LCD.print_lcd(0, 0, " Hello")
LCD.print_lcd(0, 1, "Raspberry Pi")
time.sleep(5)
while True:
now = time.strftime("%m-%d %H:%M", time.localtime(time.time()))
siteData = getVisit()
LCD.clear_lcd()
LCD.print_lcd(0, 0, 'TIME:'+now)#显示当前时间
LCD.print_lcd(0, 1, 'ONLINE:'+str(siteData[2])+" "+siteData[3])
time.sleep(5)
LCD.clear_lcd()
LCD.print_lcd(0, 0, 'VISIT:'+str(siteData[0])+" LAST:")
LCD.print_lcd(0, 1, siteData[1])
time.sleep(5)
其中 LCD.print_lcd() 用来显示字符,前两个参数分别表示 X、Y 坐标(从 0 开始),后面的内容将从这个坐标的位置开始显示。第三个参数就是要显示的内容了。
LCD.turn_light(0) 表示关闭背光,LCD.turn_light(1) 表示打开背光。
最后运行程序(LCD1602.py和main.py要在同一个模块)
sudo python3 main.py