0

So I am trying to get from my arduino some resource values and some id's , and I can't figure out how to solve this. I haven't played with python, this is my first time, so it is have to be something easy ...

this is my python program

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

This is the error :

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

And this is the arduino data I try to post

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();
      } 
}

4 Answers 4

1
IndexError: list index out of range

implies you are reading past array bounds. There are less than 6 elements in tokens.

Sign up to request clarification or add additional context in comments.

2 Comments

now I get this : 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]) ) ValueError: invalid literal for int() with base 10: '' I try to save 6 values ... doesn't that mean that I have to start at -5?
The ValueError is caused by trying to coerce '' (Empty String) into Int. Either token, 3,4,5 is '' or alternatively, the last line in your file needs to be escaped (often caused by trailing /n) You should use try/ catch for anything you might fail at doing.
0

Your line[:-5] will strip off a lot of the end (and I'm not sure why you're doing that for that matter). As a result, there aren't 6 fields separated by |. The split() call is giving you less than six results, and then your attempt to access tokens[5] (and tokens[4] for that matter) fails because those don't exist.

In general you should also be checking to make sure that the list is the size you want anyway, because otherwise any malformed data can crash your program.

Comments

0

tokens = line[:-5].split('|')
TO:
tokens = line[:-2].split('|')

2 Comments

I am afraid this is not correct - this might just work in your particular use case, but not a correct way of solving the problem. What if your id_lum is < 10, this will not work.
it will, because I have the "|", to make the split... ... thank you. :) ... this it is ok for me, so I will leave it like this for now.
0

Others have tried to answer the question - but I think the basic confusion is not resolved

line[:-5] will remove last 5 characters from a line

For example if your line is 'abcdefghijklm' then line[:-5] would give 'abcdefgh'.

Now let's look at your adruino code specifically following line

Serial.print(id_lum)

Now whenever these values are greater than 99999, you are okay, but for values from 10000 to 99999 it is going to remove the last 5 digits but keep the | in place, for values less than 1000, in fact some bits from your id_hum are also going to get stripped. (certainly not what you want).

In general you are just good without line[:-5]. That [-5] has nothing to do with the fact that you are interested in 6 fields.

So simply changing your line tokens = line[:-5].split('|') to tokens = line.strip().split('|')

should be all you want. Extra strip() is to remove trailing white-space if any.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.