腳本正在讀取打印到終端的DS18B20傳感器的溫度。 Python忽略了它應該將這些數據發送到thingspeak.com的代碼部分
它沒有給出錯誤代碼。部分代碼被忽略 - 爲什麼?
任何人都知道什麼是錯的?
我的代碼:
# Temperature to Thingspeak.com
# python
import httplib, urllib, os, glob, time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def getpid():
dataAsString = str(os.getpid())
fb = open("/home/pi/pidfile.pid","w")
fb.write(dataAsString)
fb.close()
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string)/1000.0
return temp_c
temperature = read_temp()
params = urllib.urlencode({'field1': temperature, 'key':'Pon_aquí_tu_key'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()
tme.sleep(16)
while true:
getpid()
dataAsInt = str(read_temp())
dataAsString = str(dataAsInt)
print dataAsString
time.sleep(16)
編輯 單獨運行該代碼發送溫度thingspeak一次,然後停止。
# Registrador de temperatura Nergiza.com
# python
import httplib, urllib, os, glob, time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string)/1000.0
return temp_c
temperatura = read_temp()
params = urllib.urlencode({'field1': temperatura, 'key':'Pon_aquí_tu_key'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept":
"text/plain"}
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.request("POST", "/update", params, headers)
response = conn.getresponse()
print response.status, response.reason
data = response.read()
conn.close()
響應代碼是什麼? – Netwave
沒有響應代碼,它將溫度打印到終端。但它不會將溫度傳遞給事物。 – ThomasD