krisss
Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 7:08 pm

I'm using a raspberry pi zero to communicate with a Chipcap 2 temp/humidity sensor via i2c. I've followed users' advice to use quick2wire for i2c communication: https://github.com/quick2wire/quick2wir ... ith-i2c.md
I believe it installed properly. Just in case, my method was to copy the Github folder into /home/pi and used the command 'sudo python3 setup.py install' and it seems to have installed.

My code however is not functioning properly. When I use the command 'i2cdetect 0' I get the error 'Could not open file '/dev/i2c-0' or '/dev/i2c/0.'' Using 'i2cdetect 1' works and I am able to see my device on the bus. When I try to run my code I get a similar error telling me that there is no such file or directory '/dev/i2c-0.'

Here are snippets of my code:

Code: Select all

import smbus
import csv
import time
from datetime import datetime
import subprocess, sys
import quick2wire.i2c as i2c
import logging

sens_addr = 0x28
bus = smbus.SMBus(1)

with i2c.I2CMaster() as bus: #this is where the error happens
                read_results = bus.transaction(
                    i2c.reading(sens_addr, 4)) 
                    
                block = read_results[0,1,2,3][0] #haven't gotten far enough to see if this works
Perhaps I should mention that since smbus was working for me for the most part, I am importing and using smbus within the same code. I don't know if that would cause any issues. TIA

User avatar
joan
Posts: 15360
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 7:25 pm

I can't help with quickwire (but I'd guess it defaults to the wrong bus).

As a test of your device try the following.

$ sudo pigpiod # start the pigpio daemon

$ pigs i2co 1 0x28 0 # get handle for device at address 0x28 on bus 1
0
$ pigs i2crd 0 4 # read 4 bytes from device associated with handle 0

http://abyz.me.uk/rpi/pigpio/pigs.html#I2CO
http://abyz.me.uk/rpi/pigpio/pigs.html#I2CRD

krisss
Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 7:36 pm

That returned the following:
4 26 128 101 200

I'm curious to see if the last 4 bytes are good data. I assume the '4' is meaningless? How would I get quick2wire to default to the correct bus?

EDIT: Running the last 4 bytes through a conversion program, it seems to be actual data, but slightly inaccurate. Probably nothing I can do about that. Still unable to get my code working.

User avatar
joan
Posts: 15360
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 7:49 pm

The initial 4 is the number of returned bytes (if there was an error it would be a negative number).

Can't help with the quickwire bus problem. Someone else will know.

User avatar
DougieLawson
Posts: 40775
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 8:26 pm

The biggest problem with quick2wire is that it won't recognise any RPi 1B+, 2B, 3B, 3B+, Z, ZC, ZW, ZWH as it's five year old code that hasn't been updated since the 26-pin RPi 1B REV2 (with the J8 header) came out.

Code: Select all

def revision():
    try:
        with open('/proc/cpuinfo','r') as f:
            for line in f:
                if line.startswith('Revision'):
                    return 1 if line.rstrip()[-1] in ['2','3'] else 2
            else:
                return 0
    except:
        return 0
doesn't work on modern 40-pin Raspberries.
Any language using left-hand whitespace for syntax is ridiculous

Any DMs sent on Twitter will be answered next month.
Fake doctors - are all on my foes list.

Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

User avatar
joan
Posts: 15360
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 8:44 pm

The following pigpio Python script should read the device.

Code: Select all

#!/usr/bin/env python

import time
import pigpio

pi = pigpio.pi()

if not pi.connected:
   exit()

h = pi.i2c_open(1, 0x28) # get handle for device 0x28 on bus 1

try:
   while True:
      (count, data) = pi.i2c_read_device(h, 4)
      if count == 4:
         print(data)
      else:
         print(pigpio.error_text(count))
      time.sleep(1)

except KeyboardInterrupt:
   print("\nTidying up")

pi.i2c_close(h)

pi.stop()

krisss
Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 8:53 pm

Wow! Thanks for your dedication. That code only works after I start the pigpio daemon in the console but after that it prints 'bytearray(b'\x1d\xf4eL')' once followed by 'bytearray(b']\xf4eM')' once per second ad infinitum.

User avatar
joan
Posts: 15360
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Troubleshooting quick2wire for i2c communication

Fri Aug 24, 2018 9:03 pm

krisss wrote:
Fri Aug 24, 2018 8:53 pm
Wow! Thanks for your dedication. That code only works after I start the pigpio daemon in the console but after that it prints 'bytearray(b'\x1d\xf4eL')' once followed by 'bytearray(b']\xf4eM')' once per second ad infinitum.
You can see the individual bytes using data[0], ..., data[3].

krisss
Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Re: Troubleshooting quick2wire for i2c communication

Mon Aug 27, 2018 4:37 pm

That works! I am now able to read from the chip. Thank you a lot. One small problem, though: the program is supposed to run at startup but without turning on the gpio demon first in the console, it won't run. Is there a way to do this from my program?

User avatar
joan
Posts: 15360
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Troubleshooting quick2wire for i2c communication

Mon Aug 27, 2018 5:15 pm

krisss wrote:
Mon Aug 27, 2018 4:37 pm
That works! I am now able to read from the chip. Thank you a lot. One small problem, though: the program is supposed to run at startup but without turning on the gpio demon first in the console, it won't run. Is there a way to do this from my program?
The following command (executed once) will start the daemon at boot.


sudo systemctl enable pigpiod


If you just want to start it in your script the following should work.

import os
...
os.system("sudo pigpiod")


I think os.system is deprecated from Python, so you might want to look into an alternative such as subprocess.check_output.

krisss
Posts: 15
Joined: Mon Aug 06, 2018 5:24 pm

Re: Troubleshooting quick2wire for i2c communication

Mon Aug 27, 2018 8:30 pm

joan wrote:
Mon Aug 27, 2018 5:15 pm
The following command (executed once) will start the daemon at boot.


sudo systemctl enable pigpiod
This seems to be working. Thanks a lot!

RedNoVeno
Posts: 1
Joined: Wed Sep 19, 2018 8:00 pm

Re: Troubleshooting quick2wire for i2c communication

Wed Sep 19, 2018 8:39 pm

Regarding quick2wire, I have managed to get the library section for I2C to work on my Pi3 by commenting out a segment of code (which defaults to the wrong bus)

Also remember to comment out "i2c-bcm2708" from the /etc/modules-load.d/modules.conf

open /home/pi/quick2wire-python-api/quick2wire (which should be the installation package from the git library if you have not deleted it)

otherwise download a new copy of the installation package>

Code: Select all

$ git clone https://github.com/quick2wire/quick2wire-python-api.git
Once inside the quick2wire directory, open i2c.py and comment out the condition that checks for the Raspberry revision.

Code: Select all

default_bus = 1 if revision() > 1 else 0

Code: Select all

default_bus = 1 #if revision() > 1 else 0
Now run the setup.py again to rewrite the python 3.5 dependencies.

Code: Select all

$cd quick2wire-python-api
$sudo python3 setup.py install
Reboot and you should have compatibility for the quick2wire i2c lib.

I prefer the quick2wire method because it is a dependency for the libraries of some sensors like the AM2315.

Return to “Interfacing (DSI, CSI, I2C, etc.)”