树莓派4B+LCD1602+Python脚本, 检测网站情况

硬件设备

5V 3A电源 树莓派4B 4G内存 液晶显示屏LCD1602 LCD液晶屏转接板 杜邦线

软件设备

镜像系统:2022-01-28-raspios-bullseye-arm64-full(Debian11) 语言:Python3.7

效果图

image-20220404215420605

模块上有一颗可调电阻,用于调节显示的对比度。如果你新拿到一块屏幕无论怎么调试都不见显示,记得调节一下这里

image-20220404215638919

接线
1
2
3
4
GND --- GND
VCC --- 接树莓派 5V
SDA --- I2C 数据
SCL --- I2C 时钟

微信图片_20220405165148

启动树莓派I2C
1
2
3
sudo  apt-get install -y python-smbus
sudo apt-get install -y i2c-tools
sudo raspi-config

image-20220404220733270

重启 sudo reboot

测试
1
sudo i2cdetect -y 1

image-20220404220926386

如上图说明已成功连接LCD1602。

添加驱动程序 LCD1602.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/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要在同一个模块)

1
sudo python3 main.py

[video width=”476” height=”1280” mp4=”https://wangwangyz.site/wp-content/uploads/2022/04/树莓派测试视频.mp4"\]\[/video\]

References

树莓派通过 I2C 驱动 LCD1602 液晶屏 树莓派实验室 (nxez.com)


桂ICP备2024024328号