Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 帮助创建温湿度脚本_Linux_Python_Raspberry Pi_Temperature - Fatal编程技术网

Linux 帮助创建温湿度脚本

Linux 帮助创建温湿度脚本,linux,python,raspberry-pi,temperature,Linux,Python,Raspberry Pi,Temperature,我是一个完全没有经验的a级学生,试图掌握python来完成作业。我有一周的时间来完成它——我对该做什么知之甚少,也没有编码方面的经验——我真的被卡住了,在他的论坛上,我可能会觉得自己很愚蠢 我必须创建一个带有树莓pi和DHT22传感器的温度和湿度记录器。我将编写一个脚本,生成一个休眠10秒的循环——我将运行该脚本两天,以收集足够生成图形的数据。到目前为止,我的代码是这样的,它不起作用-可能是因为一些明显的原因-数据需要在一个leafpad文件的两列中显示出来 # Assign header de

我是一个完全没有经验的a级学生,试图掌握python来完成作业。我有一周的时间来完成它——我对该做什么知之甚少,也没有编码方面的经验——我真的被卡住了,在他的论坛上,我可能会觉得自己很愚蠢

我必须创建一个带有树莓pi和DHT22传感器的温度和湿度记录器。我将编写一个脚本,生成一个休眠10秒的循环——我将运行该脚本两天,以收集足够生成图形的数据。到目前为止,我的代码是这样的,它不起作用-可能是因为一些明显的原因-数据需要在一个leafpad文件的两列中显示出来

# Assign header details to STRING variables - change manually 
txt_studentid = '999'     
txt_pi_location = '999.99999'  
txt_pi_latitude = '999.99999'   
txt_pi_longitude = '999.99999'   

import Adafruit_DHT   
pin = 4   
sensor = Adafruit_DHT.DHT22 
# Import Time module import time 
# open file to write   
f = open('/home/pi/y_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)  

你快到了。您编写的代码有一点不必要的长,但如果您是新手,不知道诀窍和正确的函数,这是正常的;)
不过,它仍在进行一些小的更改
您需要在读取传感器的行之后缩进所有内容。否则,它不会包含在循环中,您会不断读取值,但不会写入值。
然后有两条注释搞乱了,第一条注释掉了您肯定需要的
导入时间,第二条注释在
之后,为True:
您将
示例
移到了下一行。
您还写了
f,写了(…
两次。请注意
而不是

最后,您对
open
的最后一次调用仍然包含Lambert已经谈到的
('
)。
最后三个问题是语法问题,python本身应该指出

我还建议将filepath放在它自己的变量顶部的某个位置,以便您可以更轻松地更改它。
此外,不必为时间戳的所有部分的数字和字符串版本保留变量。只需获取一次时间,然后在格式字符串中读取其属性。请参阅并查看我的代码

以下是我编写脚本的方式:

import time
import Adafruit_DHT

# Assign header details to STRING variables - change manually 
txt_studentid = '999'
txt_pi_location = '999.99999'
txt_pi_latitude = '999.99999'
txt_pi_longitude = '999.99999'

filepath = '/home/pi/my_data.txt'
pin = 4
sensor = Adafruit_DHT.DHT22
# Import Time module import time
# open file to write
with open(filepath, 'w') as f:
    f.write(txt_studentid + '\n')
    f.write(txt_pi_location + '\n')
    f.write(txt_pi_latitude + '\n')
    f.write(txt_pi_longitude + '\n')

line_template = ('{time.tm_mon},{time.tm_mday},{time.tm_hour},{time.tm_min},' +
                 '{time.tm_sec},{hum},{temp}\n')
while True:
    # get current time
    sampletime = time.localtime()
    # sample sensor values
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    # append time and values to file
    with open(filepath, 'a') as f:
        f.write(line_template.format(time=sampletime,
                                     hum=humidity,
                                     temp=temperature))
    # wait 10 seconds
    time.sleep(10)

就说我在sudo python中键入my_script.py,没有错误消息,
f=open
语句可以使用一些更正。在第一个语句中,您打开一个名为
y_data.txt的文件,在第二个语句中,您将附加到一个名为
my_data.txt
的文件。在第二个
f=open
语句中,您还有一个
(“
太多了。您的while循环不正确,它陷入了无限循环。