3

I have a gas sensor connected to my Raspberry Pi 2 (5V, GND, SDA, SCL) I use the Raspbian OS. I try to communicate with the sensor according to this manual:

http://co2meters.com/Documentation/AppNotes/AN142-RaspberryPi-K_series.pdf

i2cdetect delivers the following the right address : 0x68

I used the following test program: ( http://www.byvac.com/downloads/sws/notsmb_1_0.zip )

GNU nano 2.2.6 File: test.py

from notsmb import notSMB
bus = notSMB(I2CBUS)
resp = bus.i2c(0x68,[0x22,0x00,0x08,0x2A],4)
print(resp)

But I get write errors all the time:

pi@raspberrypi ~ $ sudo python test.py
Traceback (most recent call last):
File "test.py", line 5, in <module>
resp = bus.i2c(0x68,[0x22,0x00,0x08,0x2A],4)

EDIT to add:

The complete code I have used:

#!/usr/bin/python

import time
from notsmb import notSMB
I2CBUS = 1
CO2ADDR = 0x68
READ = 0x22
readBytes = [0x00,0x08,0x2A]
bus = notSMB(1)
while True:
   try:
      pi=bus.i2c(CO2ADDR,[0x22,0x00,0x08,0x2A],4)
      time.sleep(0.1)
      co2Val=(pi[1]*256+pi[2])
      print(co2Val);
      f=open("/var/www/bhm/K-30/co2wert.txt","w")
      f.write(str(co2Val))
      f.close
      break
   except:
      blank=0;

And the error message:

pi@raspberrypi:~ $ sudo python /var/www/bhm/K-30/co2messung.py
Traceback (most recent call last):
File "/var/www/bhm/K-30/co2messung.py", line 12, in <module>
pi=bus.i2c(CO2ADDR,[0x22,0x00,0x08,0x2A],4)
KeyboardInterrupt
  • Could you post the script you are actually using? That snippet is incomplete. By the way the problem is fairly obvious, the code you are using would confirm the problem. – joan Jun 3 '16 at 19:27
  • sorry, i used the code from: co2meters.com/Documentation/AppNotes/… – Tobs96 Jun 3 '16 at 20:03
  • If you want help please post the code you are using. – joan Jun 3 '16 at 20:30
  • Looking at some notSMB information, I see another tool recommended to use for i2c communication, Linux_i2c, could give that a try? Would need to change your code, but not too much. – Gergely Sep 6 '16 at 8:13
1

Unfortunately I don't have the CO sensor to test, but...

From the notsmbmodule.c file it looks like you have to call bus.open(1) before using the .i2c(...) function.

When you create the instance of bus the file descriptor (fd) and address (addr) are both set to -1

static PyObject *notSMB_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    notSMB *self;
    if ((self = (notSMB *)type->tp_alloc(type, 0)) == NULL)
        return NULL;

    self->fd = -1;
    self->addr = -1;

    return (PyObject *)self;
}

When you call bus.i2c(...) it is trying to do an ioctl(...) on the file descriptor without having done an open(...) to get a valid file descriptor first.

So, I think it may work if you change your code like so (add the bus.open(1) line):

#!/usr/bin/python

import time
from notsmb import notSMB
I2CBUS = 1
CO2ADDR = 0x68
READ = 0x22
readBytes = [0x00,0x08,0x2A]
bus = notSMB(1)

bus.open(1)

while True:
   try:
      pi=bus.i2c(CO2ADDR,[0x22,0x00,0x08,0x2A],4)
      time.sleep(0.1)
      co2Val=(pi[1]*256+pi[2])
      print(co2Val);
      f=open("/var/www/bhm/K-30/co2wert.txt","w")
      f.write(str(co2Val))
      f.close
      break
   except:
      blank=0;

protected by Community Sep 6 '16 at 8:17

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.