Python 为什么在我的.txt文件中只出现前三行-?!树莓皮-温度湿度读数
我是一个完全没有经验的a级学生,试图掌握python来完成作业。我有一周的时间来完成它——我对该做什么知之甚少,也没有编码方面的经验——我真的被卡住了,在他的论坛上,我可能会觉得自己很愚蠢 我必须创建一个带有树莓pi和DHT22传感器的温度和湿度记录器。我将编写一个脚本,生成一个休眠10秒的循环——我将运行该脚本两天,以收集足够的数据来生成图形。到目前为止,我的代码是这样的,它不起作用-可能是因为一些明显的原因。数据需要在一个leafpad文件中以两列的形式显示出来。当我使用sudo python执行脚本时,似乎没有发生任何事情-在我的Python 为什么在我的.txt文件中只出现前三行-?!树莓皮-温度湿度读数,python,raspberry-pi,Python,Raspberry Pi,我是一个完全没有经验的a级学生,试图掌握python来完成作业。我有一周的时间来完成它——我对该做什么知之甚少,也没有编码方面的经验——我真的被卡住了,在他的论坛上,我可能会觉得自己很愚蠢 我必须创建一个带有树莓pi和DHT22传感器的温度和湿度记录器。我将编写一个脚本,生成一个休眠10秒的循环——我将运行该脚本两天,以收集足够的数据来生成图形。到目前为止,我的代码是这样的,它不起作用-可能是因为一些明显的原因。数据需要在一个leafpad文件中以两列的形式显示出来。当我使用sudo pytho
ls
中没有创建任何.txt
文件(其中有一个文件中只有以下内容:
indoors
51.58778
-0.15944
但是LX终端中没有错误消息。我是否做了一些非常明显的错误
#将标题详细信息分配给字符串变量-手动更改
txt_studentid='999'txt_pi_位置=‘室内’
txt_pi_纬度='51.58778'
经度='-0.15944'
import Adafruit_DHT
pin = 4
sensor = Adafruit_DHT.DHT22
# Import Time module import time
# open file to write
f = open('/home/pi/my_data.txt','w')
f.write(txt_studentid)
f.write('\n')
f.write(txt_pi_location)
f.write('\n')
f.write(txt_pi_latitude)
f.write('\n')
f.write(txt_pi_longitude)
f.write('\n')
f.close()
while True:
# store off the date and time details for this
sample num_month = time.localtime().tm_mon
num_day = time.localtime().tm_mday
num_hour = time.localtime().tm_hour
num_min = time.localtime().tm_min
num_sec = time.localtime().tm_sec
num_humidity, num_temperature = Adafruit_DHT.read_retry(sensor, pin)
txt_month = str(num_month)
txt_day = str(num_day)
txt_hour = str(num_hour)
txt_min = str(num_min)
txt_sec = str(num_sec)
txt_humidity = str(num_humidity)
txt_temperature = str(num_temperature)
f = open('/home/pi/my_data.txt','a')
f.write(txt_month)
f.write(',')
f.write(txt_day)
f.write(',')
f.write(txt_hour)
f.write(',')
f.write(txt_min)
f.write(',')
f.write(txt_sec)
f.write(',')
# write the temperature and humidity to file
f,write(txt_humidity)
f.write(',')
f,write(txt_temperature)
f.write(',')
# write new line
f.write('\n')
# close the file
f.close()
# wait for ten seconds
time.sleep(10)
我猜您的缩进是错误的。您将陷入while循环,并且永远不会向文件写入任何内容。请尝试缩进所有内容,从
num\u month=time.localtime().tm\u mon
到time.sleep(10)
您肯定希望至少在while
循环中包含文件写入;或者以某种方式跟踪读数以便以后保存
我已修改了您的代码以帮助您开始:
import Adafruit_DHT
import time
from datetime import datetime
pin = 4
sensor = Adafruit_DHT.DHT22
# Import Time module import time
# open file to write
f = open('/home/pi/my_data.txt','w')
f.write(txt_studentid)
f.write('\n')
f.write(txt_pi_location)
f.write('\n')
f.write(txt_pi_latitude)
f.write('\n')
f.write(txt_pi_longitude)
f.write('\n')
f.close()
f = open('/home/pi/my_data.txt','a')
begintime = datetime.now()
while True:
# store off the date and time details for this
sample_time = datetime.now()
sample num_month = time.localtime().tm_mon
num_day = time.localtime().tm_mday
num_hour = time.localtime().tm_hour
num_min = time.localtime().tm_min
num_sec = time.localtime().tm_sec
num_humidity, num_temperature = Adafruit_DHT.read_retry(sensor, pin)
txt_month = str(num_month)
txt_day = str(num_day)
txt_hour = str(num_hour)
txt_min = str(num_min)
txt_sec = str(num_sec)
txt_humidity = str(num_humidity)
txt_temperature = str(num_temperature)
f.write(txt_month)
f.write(',')
f.write(txt_day)
f.write(',')
f.write(txt_hour)
f.write(',')
f.write(txt_humidity)
f.write(',')
f.write(num_temperature)
f.write('\n')
time.sleep(10) #sleep for 10 seconds
timedelta = sample_time - begintime
if timedelta.days >= 2:
break
f.close()
我会尝试将
timedelta
要求设置为30秒左右,以确保在达到2天之前它能按预期工作。如果timedelta.days>=2:可以将更改为如果timedelta.seconds>=30:
它工作了,再加上一些愚蠢的语法编辑,谢谢!!