脚本正在读取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)
修改 单独运行此代码会将温度发送到一次,然后停止。
# 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()
答案 0 :(得分:4)
由于您有return
语句,因此不会执行其后面的代码。也许你应该在返回之后缩小代码,如下所示:
# 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)
但是如果read_temp_raw
equal_pos == -1
不会返回任何内容(它无论如何都不会这样做)。