如何解决不同的Board方法已经使用Raspberry PI在tkinter GUI上导入了错误
我已经编写了一个代码,借助与树莓派接口的传感器来测量电导率和流量,并每5秒显示和更新tkinter gui上的值。
由于在同一程序中使用了导入板和GPIO.setmode(GPIO.BOARD),我得到了错误。我得到的确切错误是“已经设置了板的另一种模式”帮助我解决此错误。
import os
import RPi.GPIO as GPIO
import tkinter as tk
import datetime,time
import threading
import board #used to define i2c communication in raspBerry pi
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
GPIO.setmode(GPIO.BOARD) #Getting error beacuse of import board
def read_sensor():
flowin =7
GPIO.setup(7,GPIO.IN) #defined pin for flow sensor input
rate=0
seconds=0
pulse=550
time_new = 0.0
while True:
y=2
time_new = time.time() + 5
rato= 0
while time.time() <= time_new:
x=GPIO.input(flowin)
if y!=x:
if x!= 0:
rate+= 1
litre=rate/pulse
y=x
seconds+=5
flowrate=(litre*60*12)
var.set(f'Flowrate:{flowrate:0.0f} litres/hrs')
def read_sensorco():
time_newco= 0.0
temprature=27 #Asumed that this temperature is constant.Later I have used temperature sensor to update real time temprature sensor
i2c = busio.I2C(board.SCL,board.SDA)
ads = ADS.ADS1115(i2c)
chan = AnalogIn(ads,ADS.P0)
while True:
time_newco = time.time() + 5
while time.time() <= time_newco:
compco=1.0+0.02*(temprature-25.0)
tdsvoltage=chan.voltage/compco
tdsvalue=(133.42*tdsvoltage*tdsvoltage*tdsvoltage-255.86*tdsvoltage*tdsvoltage+857.39*tdsvoltage)*0.5
#print(chan.value,chan.voltage)
print(tdsvalue)
varco.set(f'tdsvalue:{tdsvalue:0.0f} ppm')
# create the thread
task = threading.Thread(target=read_sensor,daemon=True)
taskco= threading.Thread(target=read_sensorco,daemon=True)
root = tk.Tk()
var = tk.StringVar()
varco= tk.StringVar()
lbl = tk.Label(root,textvariable=var,width=40,height=5,font=('Consolas',18,'bold'))
lbl.pack() #Label for Flow sensor
lblco = tk.Label(root,textvariable=varco,'bold')) #Label for Conductivity sensor
lblco.pack()
解决方法
由于在同一程序中使用导入板和GPIO.setmode(GPIO.BOARD)会产生错误。请使用GPIO.setmode(GPIO.BCM)方法设置该板来解决错误。
import os
import RPi.GPIO as GPIO
import tkinter as tk
import datetime,time
import threading
import board #used to define i2c communication in raspberry pi
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
GPIO.setmode(GPIO.BCM) #Use BCM method to setup the board
def read_sensor():
flowin =4 #The pin number is 4 in BCM configuration
GPIO.setup(flowin,GPIO.IN) #defined pin for flow sensor input
rate=0
seconds=0
pulse=550
time_new = 0.0
while True:
y=2
time_new = time.time() + 5
rato= 0
while time.time() <= time_new:
x=GPIO.input(flowin)
if y!=x:
if x!= 0:
rate+= 1
litre=rate/pulse
y=x
seconds+=5
flowrate=(litre*60*12)
var.set(f'Flowrate:{flowrate:0.0f} litres/hrs')
def read_sensorco():
time_newco= 0.0
temprature=27 #Asumed that this temperature is constant.Later I have used temperature sensor to update real time temprature sensor
i2c = busio.I2C(board.SCL,board.SDA)
ads = ADS.ADS1115(i2c)
chan = AnalogIn(ads,ADS.P0)
while True:
time_newco = time.time() + 5
while time.time() <= time_newco:
compco=1.0+0.02*(temprature-25.0)
tdsvoltage=chan.voltage/compco
tdsvalue=(133.42*tdsvoltage*tdsvoltage*tdsvoltage-255.86*tdsvoltage*tdsvoltage+857.39*tdsvoltage)*0.5
#print(chan.value,chan.voltage)
print(tdsvalue)
varco.set(f'tdsvalue:{tdsvalue:0.0f} ppm')
# create the thread
task = threading.Thread(target=read_sensor,daemon=True)
taskco= threading.Thread(target=read_sensorco,daemon=True)
root = tk.Tk()
var = tk.StringVar()
varco= tk.StringVar()
lbl = tk.Label(root,textvariable=var,width=40,height=5,font=('Consolas',18,'bold'))
lbl.pack() #Label for Flow sensor
lblco = tk.Label(root,textvariable=varco,'bold')) #Label for Conductivity sensor
lblco.pack()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。