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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

2回答

718閲覧

importでの数値の渡し方

sphy

総合スコア21

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/05/08 16:10

ラズパイに接続した温度計から数値をprintすることはできたのですが、
この数値をprintではなくべつの.pyでインポートして読み込みたいのですが
関数と引数の渡し方がわかりません。
どのようにすれば、渡すことができますか?

tem.py

python

1import os 2import time 3 4 5def readSensor(id): 6 7 tfile = open("/sys/bus/w1/devices/"+id+"/w1_slave") 8 text = tfile.read() 9 tfile.close() 10 secondline = text.split("\n")[1] 11 temperaturedata = secondline.split(" ")[9] 12 temperature = float(temperaturedata[2:]) 13 temperature = temperature / 1000 14 print(temperature)#←temperatureをtem2.pyで読み込みたい 15 16print(readSensor()) 17 18def readSensors(): 19 count = 0 20 for file in os.listdir("/sys/bus/w1/devices/"): 21 #print(file) 22 if (file.startswith("28-")): 23 readSensor(file) 24 count+=1 25 26 if (count == 0): 27 print("No sensor found! Check connection") 28 29def loop(): 30 while True: 31 readSensors() 32 time.sleep(1) 33 34def destroy(): 35 pass 36 37if __name__ == "__main__": 38 try: 39 loop() 40 except KeyboardInterrupt: 41 destroy()

tem2.py

python

1import tem 2 3if temperature>10: 4 print("ON") 5else: 6 print("OFF") 7#のようなことを実現したいです 8

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

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

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

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

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

guest

回答2

0

ご教授を参考に修正してみたところうまくいきました。

tem.py

python

1import os 2import time 3 4 5def readSensor(id): 6 7 tfile = open("/sys/bus/w1/devices/"+id+"/w1_slave") 8 text = tfile.read() 9 tfile.close() 10 secondline = text.split("\n")[1] 11 temperaturedata = secondline.split(" ")[9] 12 temperature = float(temperaturedata[2:]) 13 temperature = temperature / 1000 14 return temperature 15 16print(readSensor()) 17 18def readSensors(): 19 count = 0 20 for file in os.listdir("/sys/bus/w1/devices/"): 21 #print(file) 22 if (file.startswith("28-")): 23 readSensor(file) 24 count+=1 25 26 if (count == 0): 27 print("No sensor found! Check connection") 28 29def loop(): 30 while True: 31 readSensors() 32 time.sleep(1) 33 34def destroy(): 35 pass 36 37if __name__ == "__main__": 38 try: 39 loop() 40 except KeyboardInterrupt: 41 destroy()

tem2.py

python

1import tem 2import os 3 4id = os.listdir("/sys/bus/w1/devices/") 5temperature = tem.readSensor(id[1]) 6 7if temperature>10: 8 print("ON") 9else: 10 print("OFF")

無事、別の.pyに温度の数値を読み込むことができました。

投稿2019/05/09 03:06

sphy

総合スコア21

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

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

0

ベストアンサー

素直な解決法としてはreturnで返してあげることです。
ただし呼出元でidを指定してあげる必要があります。

Python

1#tem.py 2def readSensor(id): 3 # 略 4 print(temperature)#←temperatureをtem2.pyで読み込みたい 5 return temperature# *** 6 7#tem2.py 8from tem import readSensor 9 10temperature = readSensor(指定のid) 11if temperature>10: 12 print("ON") 13else: 14 print("OFF")

投稿2019/05/08 20:21

can110

総合スコア38262

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

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

sphy

2019/05/09 02:59

returnを使うことはわかっていたのですが、 なるほどIDを指定する必要があったのですね。 わかりやすいご教授ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問