使用 Raspberry PI 在 tkinter GUI 上已导入不同的 Board 方法错误

Different Board method already imported error on tkinter GUI using Raspberry PI

我编写了一个代码,借助与 raspberry pi 接口的传感器测量电导率和流量,并每 5 秒在 tkinter gui 上显示和更新一次值。

由于导入板和 GPIO.setmode(GPIO.BOARD) 在同一 program.The 中使用,我收到错误 我收到的确切错误是“板的另一种模式已设置”帮助我来解决这个错误。

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, width=40, height=5, font=('Consolas', 18, 'bold')) #Label for Conductivity sensor
lblco.pack()

由于导入板和 GPIO.setmode(GPIO.BOARD) 在同一程序中使用,它生成 error.Use 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, width=40, height=5, font=('Consolas', 18, 'bold')) #Label for Conductivity sensor
lblco.pack()