所以我想從我的arduino獲得一些資源值和一些id,我不知道如何解決這個問題。 我還沒有玩過蟒蛇,這是我第一次,所以它必須是東西容易...蟒蛇和arduino:列表索引超出範圍err
這是我的Python程序
import serial
import requests
import time
import json
ser = serial.Serial('/dev/ttyACM0', 9600, timeout = 1)
i = 0
collected = []
while (i < 15):
line = ser.readline()
if (line[:3] == '***'):
#print line
line = line.strip('*')
tokens = line[:-5].split('|')
#print tokens
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]))
#print list_tuple
collected.append(list_tuple)
i += 1
avg_temp = 0
avg_hum = 0
avg_lum = 0
id_t=0
id_h=0
id_l=0
for c in collected:
avg_temp += c[0]
avg_hum += c[1]
avg_lum += c[2]
id_t = c[3]
id_h = c[4]
id_l = c[5]
avg_temp = avg_temp/len(collected)
avg_hum = avg_hum/len(collected)
avg_lum = avg_lum/len(collected)
print "AVT: %.2f AVH: %.2f AVL: %.2f" % (avg_temp, avg_hum, avg_lum)
id_thing = id_t
id_thing_h = id_h
id_thing_l = id_l
v_type = 'temperature'
v_type_h = 'humidity'
v_type_l = 'luminosity'
# url = .....
#some http post requestes
#....
time.sleep(10)
print "Done"
ser.close()
這是錯誤:
Traceback (most recent call last):
File "ser.py", line 17, in <module>
list_tuple = (float(tokens[0]) , float(tokens[1]), float(tokens[2]), int(tokens[3]), int(tokens[4]), int(tokens[5]))
IndexError: list index out of range
這是Arduino的數據我嘗試發佈
while(1){
float temp, humi;
int err;
if((err=dht11.read(humi, temp))==0)
{
Serial.println();
Serial.print("***");
Serial.print(temp);
Serial.print("|");
Serial.print(humi);
Serial.print("|");
Serial.print(ledPin);
Serial.print("|");
Serial.print(id_temp);
Serial.print("|");
Serial.print(id_hum);
Serial.print("|");
Serial.print(id_lum);
Serial.println();
}
}
恐怕這是不正確的 - 這可能只適用於你的特定用例,但不是解決問題的正確方法。如果你的'id_lum '<10,這是行不通的。 – gabhijit
它會,b因爲我有「|」,做出分裂......謝謝。 :) ...這對我來說沒問題,所以我現在就會這樣。 – flori