質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

2680閲覧

GPIO エラーが連続で出る

jw-tsuji

総合スコア3

Tkinter

Tkinterは、GUIツールキットである“Tk”をPythonから利用できるようにした標準ライブラリである。

Raspberry Pi

Raspberry Piは、ラズベリーパイ財団が開発した、名刺サイズのLinuxコンピュータです。 学校で基本的なコンピュータ科学の教育を促進することを意図しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2020/05/27 08:23

ラズベリーパイでpython tkinterで下記のプログラムを作成していました。問題なくrootウインドウが出て、プログラムは実行されていました。

ところが急にGPIOのエラーが出るようになって困っています。
GPIO.clean()を最初においたり、最後においたりしましたが、改善されません。
また、ラズベリーパイを再起動しても、設定でGPIOを無効にして、再度有効ににしても改善されません。
どなたか改善策を教えてください。

発生している問題・エラーメッセージ

GPIOに関するものです。

エラーメッセージ project4-3.py:56: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(GPIO20, GPIO.OUT) project4-3.py:57: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(GPIO21, GPIO.OUT) project4-3.py:58: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings. GPIO.setup(GPIO26, GPIO.OUT) このエラーが連続して出て、プログラムが実行されません。``` ### 該当のソースコード import tkinter as tk import tkinter.font as font import tkinter.ttk as ttk import os import sys import glob import time from time import sleep import RPi.GPIO as GPIO from tkinter import Tk, Label, Button import threading def restart_program(): """Restarts the current program. Note: this function does not return. Any cleanup action (like saving data) must be done before calling this function.""" python = sys.executable os.execl(python, python, * sys.argv) #GPIO.cleanup() GPIO20 = 20 #ヒーター GPIO21 = 21 #ファン GPIO26 = 26 #ブザー GPIO.setmode(GPIO.BCM) GPIO.setup(GPIO20, GPIO.OUT) GPIO.setup(GPIO21, GPIO.OUT) GPIO.setup(GPIO26, GPIO.OUT) time_mov=False tloop=False sloop=False hloop=False GPIO20=True GPIO21=True GPIO26=True #読み込み 温度 file = open('OUTPUT-T.txt', 'r') b=file.read() ther_s=b #print(ther_s) file.close() #読み込み 時間 file = open('OUTPUT.txt', 'r') a=file.read() time_s=a #print(time_s) file.close() root=tk.Tk() root.title('Project') root.geometry("800x600+600+200") my_font=font.Font(size=40) ther_set = tk.StringVar() ther_set.set(ther_s) ther_ss=int(ther_s) time_set = tk.StringVar() time_set.set(time_s) time_ss=int(time_s) time_k=tk.StringVar() #time_ke="" strt=tk.StringVar() var = tk.StringVar() ther_spn=tk.IntVar() time_spn=tk.IntVar() #iw tsensor='/sys/bus/w1/devices/28-030e97944a4f/w1_slave' def traw(): f=open(tsensor,'r') lines=f.readlines() f.close() return lines def readtemp(): lines=traw() while lines[0].strip()[-3:]!='YES': time.sleep(0.3) lines=traw() tout=lines[1].find('t=') if tout!=-1: tstr=lines[1].strip()[tout+2:] tc = round(float(tstr)/1000)#.1=なしで整数表示 #space=(' ℃')#'C F' #tf=round(tc*9.0/5.0+32.0,1) return tc #,tf,space def on_alarm(): global var,temp var.set(readtemp()) temp=readtemp() root.after(1000,on_alarm) root.after(1000, on_alarm) #1w label=tk.Label(root,font=(my_font),fg='blue', textvariable = var,width=5,justify="center") label.grid(row=1,column=1,columnspan=1) #iw labbel=tk.Label(root,text='温 度 : ',fg='blue',font=(my_font),width=8) labbel.grid(row=1,column=0,columnspan=1) labbel=tk.Label(root,text=' ℃',fg='blue',font=(my_font),width=5) labbel.grid(row=1,column=2,columnspan=1) labbel=tk.Label(root,text='保管庫操作パネル',font=(my_font),width=15) labbel.grid(row=0,column=0,columnspan=4) label= tk.Label(root,textvariable=time_k,fg='red',font=(my_font),width=5) label.grid(row=2,column=1) labbel=tk.Label(root,text="残時間 :",fg='red',font=(my_font),width=8) labbel.grid(row=2,column=0,columnspan=1) labbel=tk.Label(root,width=3,text="分",fg='red',font=(my_font)) labbel.grid(row=2,column=2,columnspan=1) #labbel=tk.Label(root,text='完了',fg='blue',font=(my_font),width=14) #labbel.grid(row=4,column=0,columnspan=2) def timeup(): global tloop,sloop,hloop tloop=False sloop=False hloop=False restart() def timer(): global tloop,sloop,strt,time_k,kei,time_ke,time_ss #timend=root.after(500,timer) if sloop==True: if tloop==False: strt=time.perf_counter() tloop=True else: kei=(time.perf_counter()-strt) #time_k=int(((time_ss)*60-kei)/60) #time_k=int(((time_ss)*60-kei)/60) #time_ke=time_k #label= tk.Label(root,text=time_ke,fg='red',font=(my_font),width=5) #label.grid(row=2,column=1) if time_ke<=0: timeup() root.after(500,timer) thread = threading.Thread(target=timer) thread.start() def restart(): GPIO.output(20,0) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(0.05) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(60) GPIO.output(21,0) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(0.05) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(0.2) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(0.05) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) GPIO.cleanup() restart_program() def startup(): global tloop,sloop,hloop b_start=tk.Button(root,text="START",command=start,fg='green',font=(my_font),width=5,state=tk.NORMAL) b_start.grid(row=5,column=0) b_stop=tk.Button(root,text="STOP",command=restart_program(),fg='red',font=(my_font),width=5,state=tk.DISABLED) b_stop.grid(row=5,column=1) b_reset=tk.Button(root,text="SET",command=rset,fg='blue',font=(my_font),width=5,state=tk.NORMAL) b_reset.grid(row=5,column=2) b_reset=tk.Button(root,text="REC",command=rem,fg='red',font=(my_font),width=5,state=tk.DISABLED) b_reset.grid(row=5,column=3) labbel=tk.Label(root,text='待機中',fg='blue',font=(my_font),width=10) labbel.grid(row=4,column=0,columnspan=2) def gpioc(): global ther_ss,hloop,temp #gpioend=root.after(100,gpioc) if temp<ther_ss and hloop==True: GPIO.output(20,1) ther_spn= tk.Spinbox(root,textvariable =ther_set, from_=25, to=110,increment = 1, font=(my_font),fg='invisible',width=5,justify="center") ther_spn.grid(row=1,column=3) time_spn= tk.Spinbox(root,textvariable =time_set, from_=1, to=120,increment = 1, font=(my_font),fg='red',width=5,justify="center") time_spn.grid(row=2,column=3) else: GPIO.output(20,0) root.after(1000,gpioc) def start(): global sloop,tloop,hloop,temp,time_ke b_start=tk.Button(root,text="START",command=start,fg='green',font=(my_font),width=5,state=tk.DISABLED) b_start.grid(row=5,column=0) b_stop=tk.Button(root,text="STOP",command=restart,fg='red',font=(my_font),width=5,state=tk.NORMAL) b_stop.grid(row=5,column=1) b_reset=tk.Button(root,text="SET",command=rset,fg='blue',font=(my_font),width=5,state=tk.DISABLED) b_reset.grid(row=5,column=2) b_reset=tk.Button(root,text="REC",command=rem,fg='red',font=(my_font),width=5,state=tk.DISABLED) b_reset.grid(row=5,column=3) labbel=tk.Label(root,text='作動中',fg='blue',font=(my_font),width=10) labbel.grid(row=4,column=0,columnspan=2) GPIO.output(21,1) hloop=True gpioc() #global stopend #stopend=root.after(500,start) if temp>=ther_ss and sloop==False: sloop=True timer() root.after(500,start) def rset(): b_start=tk.Button(root,text="START",command=start,fg='green',font=(my_font),width=5,state=tk.DISABLED) b_start.grid(row=5,column=0) b_stop=tk.Button(root,text="STOP",command=restart,fg='red',font=(my_font),width=5,state=tk.DISABLED) b_stop.grid(row=5,column=1) b_reset=tk.Button(root,text="SET",command=rset,fg='blue',font=(my_font),width=5,state=tk.DISABLED) b_reset.grid(row=5,column=2) b_reset=tk.Button(root,text="REC",command=rem,fg='red',font=(my_font),width=5,state=tk.NORMAL) b_reset.grid(row=5,column=3) def rem(): ther_set=ther_spn.get() ther_s=ther_set print(ther_set) time_set=time_spn.get() time_s=time_set print(time_set) b_start=tk.Button(root,text="START",command=start,fg='green',font=(my_font),width=5,state=tk.NORMAL) b_start.grid(row=5,column=0) b_stop=tk.Button(root,text="STOP",command=restart,fg='red',font=(my_font),width=5,state=tk.DISABLED) b_stop.grid(row=5,column=1) b_reset=tk.Button(root,text="SET",command=rset,fg='blue',font=(my_font),width=5,state=tk.NORMAL) b_reset.grid(row=5,column=2) b_reset=tk.Button(root,text="REC",command=rem,fg='red',font=(my_font),width=5,state=tk.DISABLED) b_reset.grid(row=5,column=3) file = open('OUTPUT-T.txt', 'w') d=ther_s file.write(d) print(d) file.close() #読み込み 時間 file = open('OUTPUT.txt', 'w') c=time_s file.write(c) print(c) file.close() GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) time.sleep(0.05) GPIO.output(26,1) time.sleep(0.1) GPIO.output(26,0) GPIO.cleanup() restart_program() ther_spn= tk.Spinbox(root,textvariable =ther_set, from_=25, to=110,increment = 1, font=(my_font),fg='blue',width=5,justify="center") ther_spn.grid(row=1,column=3) time_spn= tk.Spinbox(root,textvariable =time_set, from_=1, to=120,increment = 1, font=(my_font),fg='red',width=5,justify="center") time_spn.grid(row=2,column=3) startup() #kyoutu root.mainloop() if __name__ == '__main__': main() ```Python3.7.3 ソースコード

試したこと

GPIO.clean()を最初においたり、最後においたりしましたが、改善されません。
また、ラズベリーパイを再起動しても、設定でGPIOを無効にして、再度有効ににしても改善されません。

補足情報(FW/ツールのバージョンなど)

こにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

teamikl

2020/05/27 11:49

コードのインデントが崩れてしまってるので修正お願いします。 直接回答ではありませんが、コードの気になる点 - インデントが不明の部分ですが、timer()内でスレッドを多重に起動していませんか? - GPIO.clean() を最後において、正常にコードが呼ばれれるのは確認できましたか? - restart時に前のプロセスはどうなっているのでしょうか? 終了時の処理については、atexit モジュール等も調べてみて下さい。
thkana

2020/05/27 23:12

teratailのヘルプより https://teratail.com/help/question-tips#questionTips3-5-1 「最も良いのは、現象を再現するためのミニマムなプログラムを改めて作ることです。そうすれば、貴方自身が現象と問題をより良く理解することにもつながるからです。」 これを試すことをまずおすすめします。
guest

回答1

0

自己解決

初心者で、わからないことばかりです。
コードは、コピペしたのですが、少しインデントがずれているようです。
ご指摘ありがとうございます。
教えてくださったことを少し調べてみます。
ありがとうございます。

投稿2020/05/27 12:47

jw-tsuji

総合スコア3

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問