几个月以来,我在Raspberry Pi上使用以下脚本读取电表(PVE系统)。上周A购买了新的Pi 3+。该脚本不适用于该PI。它创建了“工作文件”,但保持为空。
我没有收到任何错误,所以我不知道为什么它不起作用。将仪表连接到我的旧Pi上后,一切正常。
有人可以帮助我在新PI上运行此脚本吗?谢谢。
#!/usr/bin/python
import RPi.GPIO as GPIO
import datetime
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
workfile = '/var/log/gpio23-counter'
counter = 0
while True:
GPIO.wait_for_edge(23, GPIO.RISING)
# reading
try:
f = open(workfile, 'ab+') # open for reading. If it does not exist, create it
value = int(f.readline().rstrip()) # read the first line; it should be an integer value
except:
value = 0 # if something went wrong, reset to 0
#print "old value is", value
f.close() # close for reading
# writing
f = open(workfile, 'w')
f.write((str(value+1)+ '\n')) # the value
f.write((str(datetime.datetime.now())+ '\n')) # timestamp
f.close()
GPIO.wait_for_edge(23, GPIO.FALLING)
GPIO.cleanup()