zefanja / aqi

Measure AQI based on PM2.5 or PM10 with a Raspberry Pi and a SDS011 particle sensor
GNU General Public License v3.0
94 stars 46 forks source link

Intermittent Error "IndexError: list index out of range" #3

Closed jimbob666 closed 5 years ago

jimbob666 commented 5 years ago

Issue I am getting an intermittent error "IndexError: list index out of range". Seems to happens after a bit maybe 10 to 15 runs or so.

Workaround Idea Was thinking of not having this sleep for 5min but rather run on demand via a cron every 5min based on it never failing the first set of runs. Also if it does fail, another cron will run it in 5min.

My Setup Info I'm using your code based on the instructions in this article, "https://openschoolsolutions.org/measure-particulate-matter-with-a-raspberry-pi/". I'm using a Raspberry PI 3 running Raspbian and Python 2.7.x. with this sensor, Particulates sensor SDS011. When I run aqi.py from the command prompt, after a bit (again maybe 10 to 15 runs) I get this error,

PM2.5:  0.0 , PM10:  0.0
PM2.5:  2.5 , PM10:  15.4
PM2.5:  3.4 , PM10:  16.3
PM2.5:  3.1 , PM10:  11.8
PM2.5:  2.7 , PM10:  9.2
PM2.5:  2.8 , PM10:  7.8
PM2.5:  2.6 , PM10:  6.9
PM2.5:  2.6 , PM10:  7.1
PM2.5:  2.7 , PM10:  6.7
PM2.5:  2.8 , PM10:  6.9
PM2.5:  2.9 , PM10:  6.9
PM2.5:  2.9 , PM10:  6.6
PM2.5:  2.9 , PM10:  6.4
PM2.5:  2.9 , PM10:  6.4
PM2.5:  2.9 , PM10:  6.2
Going to sleep for 5min...
Traceback (most recent call last):
  File "./aqi.py", line 105, in <module>
    print("PM2.5: ", values[0], ", PM10: ", values[1])
IndexError: list index out of range <-- HERE IS THE ERROR
jimbob666 commented 5 years ago

I added this exception for now with the hope when this happens it just will try again in 5min.

        if values is not None:
            **try**:
                    print("PM2.5: ", values[0], ", PM10: ", values[1])
                    time.sleep(2)
            **except IndexError:
                    print("There was an IndexRange error")**
jimbob666 commented 5 years ago

Bad news, no dice on adding the exception. I still get the IndexRange error, I do see it is happening when it samples so you can see in this error it is doing it a few times. The workaround idea I initially posted is sounding better and better.

Going to sleep for 5min...

PM2.5:  0.2 , PM10:  0.7
PM2.5:  0.2 , PM10:  0.7
PM2.5:  0.2 , PM10:  0.7
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
PM2.5:  0.2 , PM10:  0.4
There was an IndexRange error
There was an IndexRange error
There was an IndexRange error
There was an IndexRange error
Traceback (most recent call last):
  File "./aqi.py", line 105, in <module>
    data.append({'pm25': values[0], 'pm10': values[1], 'time': time.strftime("%m.%d.%Y %H:%M:%S")})
IndexError: list index out of range
zefanja commented 5 years ago

Could you please uncomment line 48 in aqi.py and post the output here? Maybe you have to move the line with the print statement before the return statement. So it looks like this:

def process_data(d):
    r = struct.unpack('<HHxxBB', d[2:])
    pm25 = r[0]/10.0
    pm10 = r[1]/10.0
    checksum = sum(ord(v) for v in d[2:8])%256
    print("PM 2.5: {} μg/m^3  PM 10: {} μg/m^3 CRC={}".format(pm25, pm10, "OK" if (checksum==r[2] and r[3]==0xab) else "NOK"))
    return [pm25, pm10]
wheelq commented 5 years ago

but that's not the source of the issue

wheelq commented 5 years ago

@jimbob666 check this: https://github.com/wheelq/aqi/tree/patch-1