Sensair CO2 Sensor [obsolete]
Posted: Wed Sep 13, 2017 7:33 pm
Hello
I'm using the following code to read a CO2 Senseair S8 sensor from the Serial port of my Pi3:
But unfortunately I'm getting an error regarding the ord() function.
The error states ord(resp[3]) string index out of range:
But my quetion is if I'm reading the data with 7 character of string using ser.read(7) then how can the index 3 be out of range.
When I'm not indexing the sensor and trying to print the data as: I'm only getting: b' '
Please help how to proceed with this!
I'm using the following code to read a CO2 Senseair S8 sensor from the Serial port of my Pi3:
Code: Select all
import serial
import time
#RPi pin connections:
#pin 6 GND
#pin 4 5v
#pin 8 TXD: UART data to S8
#pin 10 RXD: UART data from S8
ser = serial.Serial("/dev/ttyAMA0",baudrate =9600,timeout = .5)
print " AN-137: Raspberry Pi3 to K-30 Via UART\n"
ser.flushInput()
time.sleep(1)
for i in range(1,21): # Print 20 readings from sensor
ser.flushInput()
ser.write("0xFE,0x44,0x00,0x08,0x02,0x9F,0x25")
time.sleep(.5)
resp = ser.read(7)
high = ord(resp[3])
low = ord(resp[4])
co2 = (high*256) + low
print "i = ",i, " CO2 = " +str(co2)
time.sleep(.1)The error states ord(resp[3]) string index out of range:
But my quetion is if I'm reading the data with 7 character of string using ser.read(7) then how can the index 3 be out of range.
When I'm not indexing the sensor and trying to print the data as:
Code: Select all
print(resp)Please help how to proceed with this!