2016-01-07 142 views
-1

腳本正在讀取打印到終端的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() 
+0

響應代碼是什麼? – Netwave

+0

沒有響應代碼,它將溫度打印到終端。但它不會將溫度傳遞給事物。 – ThomasD

回答

4

既然你有一個return語句後面的代碼將不會執行。也許你應該dedented了這樣的代碼後迴歸:

# 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) 

不過,如果equal_pos == -1(它沒有做到這一點無論如何)read_temp_raw不會返回任何東西。

+0

我已經嘗試將溫度= read_temp()的代碼部分全部移到左邊,並將其移動到while true:部分下。這不會改變任何東西。 – ThomasD

+0

您當然會決定何時應該運行該代碼。在那個位置,它可能不是你想要的。將其移至「while True」循環之後將不起作用,因爲該循環不應該終止,並且之後的代碼將不會運行。 – skyking