suvajitb1
Posts: 4
Joined: Wed Sep 13, 2017 7:18 pm

Sensair CO2 Sensor [obsolete]

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:

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)
But unfortunately I'm getting an error regarding the ord() function.

The error states ord(resp[3]) string index out of range: Image

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)
I'm only getting: b' '

Please help how to proceed with this!
Attachments
error_ord.PNG
error_ord.PNG (13.13 KiB) Viewed 2216 times

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Sensair CO2 Sensor

Thu Sep 14, 2017 1:34 am

so I would say the "resp = ser.read(7)" is timing out before it receives 7 bytes. I would add the line "print len(resp)" just after the read so that you can see how many bytes have been read. U may need to increase your time out

suvajitb1
Posts: 4
Joined: Wed Sep 13, 2017 7:18 pm

Re: Sensair CO2 Sensor

Thu Sep 14, 2017 4:34 pm

OutoftheBOTS wrote:
Thu Sep 14, 2017 1:34 am
so I would say the "resp = ser.read(7)" is timing out before it receives 7 bytes. I would add the line "print len(resp)" just after the read so that you can see how many bytes have been read. U may need to increase your time out
You are right. The port in unable to read any data. The len of the string is zero. But is it for the timeout? Because when I'm writing the command one by one on the IDLE python 3 shell the response is same. And I'm waiting for quite enough time to read the data. I'm sure that baudrate is 9600 and parity = none as when I'm connecting the sensor directly to the PC COM port with a software provided by Senseair and there I'm using these settings and there I'm able to see the data. That means the sensor is working but may be my serial port of PI not. Also when I'm writing the data to the port to fetch there is a value return = 7.

I'm attaching the screenshot.
Attachments
error_k-30.PNG
error_k-30.PNG (29.63 KiB) Viewed 2176 times

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Sensair CO2 Sensor

Thu Sep 14, 2017 8:53 pm

well obviously the sensor isn't sending the data with the timeout. This isn't because the timeout is too short but because the sensor isn't sending the data so the RPi times out.

You may have to look ta just how the sensor is suppose to work

Return to “Automation, sensing and robotics”