techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

I2C and python 2 or 3 using python-smbus

Wed Nov 07, 2012 8:31 pm

For those who are aware python-smbus only works with python 2.x and many people find it difficult to work out what is actually happening.

First of all this document relating to trhe actual c functions (at lower level) helps to explain what happens on the i2c bus for all the parameters.

http://git.kernel.org/?p=linux/kernel/g ... s-protocol

I am in converstaion with the i2c-tools maintainer and getting the details together to look at adding Python 3.x support for python-smbus when I get time.

Other developments may occur later, time and energy permitting.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

hunternet93
Posts: 336
Joined: Mon Dec 12, 2011 4:34 pm

Re: I2C and python 2 or 3 using python-smbus

Fri Nov 09, 2012 8:24 pm

For communicating with I2C, the Quick2Wire (http://quick2wire.com) library works very well. It's written for Python 3 but also works with Python 2. I'm using it for my first ever project involving I2C, I had it up and running in a couple hours.

catmaker
Posts: 50
Joined: Thu May 24, 2012 8:32 am

Re: I2C and python 2 or 3 using python-smbus

Sat Dec 08, 2012 3:15 pm

Hi, I found this linux-i2c thread where the python-smbus source was upgraded to build & install on python3:
http://www.spinics.net/lists/linux-i2c/ ... .html___RE: Howto compile py-smbus with python 3.2

I've tried it on my Debian Raspberry Pi and it works.

Code: Select all

wget http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.0.orig.tar.bz2
tar xf i2c-tools_3.1.0.orig.tar.bz2
cd i2c-tools-3.1.0/py-smbus
cp smbusmodule.c smbusmodule.c.orig  # make backup
# copy the email code into smbusmodule.c
python3 setup.py build
sudo python3 setup.py install
Until the poster's patch goes upstream, I hope the above helps.

maria22
Posts: 2
Joined: Fri Dec 28, 2012 12:55 pm

Re: I2C and python 2 or 3 using python-smbus

Fri Dec 28, 2012 12:58 pm

@hunternet93 I'm trying to get quick2wire I2C running with Python2.7 but it doesn't seem to work. I'm using the mcp23008 example which work fine for Python3 but returns nothing for Python 2. What did you do to make it work?

hunternet93
Posts: 336
Joined: Mon Dec 12, 2011 4:34 pm

Re: I2C and python 2 or 3 using python-smbus

Fri Dec 28, 2012 2:50 pm

maria22 wrote:@hunternet93 I'm trying to get quick2wire I2C running with Python2.7 but it doesn't seem to work. I'm using the mcp23008 example which work fine for Python3 but returns nothing for Python 2. What did you do to make it work?
I just installed and used it like normal. What problems are you having?

maria22
Posts: 2
Joined: Fri Dec 28, 2012 12:55 pm

Re: I2C and python 2 or 3 using python-smbus

Sun Dec 30, 2012 5:17 pm

This program:

#!/usr/bin/env python3
import sys
# appends to PYTHONPATH the location of the example codes
sys.path.append(r'/home/pi/quick2wire-python-api/src/')

from quick2wire.i2c import I2CMaster, writing_bytes, reading
import time

address = 0x20
iodir_register = 0x00
gpio_register = 0x09

with I2CMaster() as master:
master.transaction(
writing_bytes(address, iodir_register, 0xFF))

while True:
read_results = master.transaction(
writing_bytes(address, gpio_register),
reading(address, 1))

print (read_results)

time.sleep(1)

Works fine in Python 3:

[b'\x80']

But doesn't return anything in Python 2.

['\x00']

hunternet93
Posts: 336
Joined: Mon Dec 12, 2011 4:34 pm

Re: I2C and python 2 or 3 using python-smbus

Mon Dec 31, 2012 3:05 am

I don't see anything wrong with the code, I don't have my Pi and an i2c device handy so I can't test. I did run in to some problems where I started getting empty values, but I haven't had the time to track it down yet. Maybe open an issue at https://github.com/quick2wire/quick2wir ... api/issues?

tomcox
Posts: 2
Joined: Tue Apr 30, 2013 9:38 pm

Re: I2C and python 2 or 3 using python-smbus

Tue Apr 30, 2013 9:44 pm

Techpaul wrote:

I am in converstaion with the i2c-tools maintainer and getting the details together to look at adding Python 3.x support for python-smbus when I get time.

Did this ever happen?

techpaul
Posts: 1512
Joined: Sat Jul 14, 2012 6:40 pm
Location: Reading, UK
Contact: Website

Re: I2C and python 2 or 3 using python-smbus

Tue Apr 30, 2013 11:36 pm

Not had time yet, had many work and other things get in the way like many server rebuilds and other panic jobs. Other disasters have not helped.

Trying to get to it.
Just another techie on the net - For GPIO boards see http:///www.facebook.com/pcservicesreading
or http://www.pcserviceselectronics.co.uk/pi/

lightxx
Posts: 2
Joined: Mon Sep 02, 2013 8:23 am

Re: I2C and python 2 or 3 using python-smbus

Mon Sep 02, 2013 8:28 am

I hope you're still active here. I tried to compile smbusmodule.c as per your instructions, but upon running

Code: Select all

sudo python3 setup.py build 
i get

Code: Select all

[email protected] ~/Downloads/i2c-tools-3.1.0/py-smbus $ sudo python3 setup.py build
running build
running build_ext
building 'smbus' extension
gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -fPIC -I/usr/include/python3.2mu -c smbusmodule.c -o build/temp.linux-armv6l-3.2/smbusmodule.o
smbusmodule.c: In function ‘SMBus_set_addr’:
smbusmodule.c:161:3: warning: implicit declaration of function ‘ioctl’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_write_quick’:
smbusmodule.c:190:2: warning: implicit declaration of function ‘i2c_smbus_write_quick’ [-Wimplicit-function-declaration]
smbusmodule.c:190:48: error: ‘I2C_SMBUS_WRITE’ undeclared (first use in this function)
smbusmodule.c:190:48: note: each undeclared identifier is reported only once for each function it appears in
smbusmodule.c:183:8: warning: variable ‘result’ set but not used [-Wunused-but-set-variable]
smbusmodule.c: In function ‘SMBus_read_byte’:
smbusmodule.c:214:2: warning: implicit declaration of function ‘i2c_smbus_read_byte’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_write_byte’:
smbusmodule.c:237:2: warning: implicit declaration of function ‘i2c_smbus_write_byte’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_read_byte_data’:
smbusmodule.c:261:2: warning: implicit declaration of function ‘i2c_smbus_read_byte_data’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_write_byte_data’:
smbusmodule.c:284:2: warning: implicit declaration of function ‘i2c_smbus_write_byte_data’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_read_word_data’:
smbusmodule.c:309:2: warning: implicit declaration of function ‘i2c_smbus_read_word_data’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_write_word_data’:
smbusmodule.c:332:2: warning: implicit declaration of function ‘i2c_smbus_write_word_data’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_process_call’:
smbusmodule.c:357:2: warning: implicit declaration of function ‘i2c_smbus_process_call’ [-Wimplicit-function-declaration]
smbusmodule.c: In function ‘SMBus_read_block_data’:
smbusmodule.c:394:23: error: storage size of ‘data’ isn’t known
smbusmodule.c:402:2: warning: implicit declaration of function ‘i2c_smbus_access’ [-Wimplicit-function-declaration]
smbusmodule.c:402:33: error: ‘I2C_SMBUS_READ’ undeclared (first use in this function)
smbusmodule.c:403:5: error: ‘I2C_SMBUS_BLOCK_DATA’ undeclared (first use in this function)
smbusmodule.c:394:23: warning: unused variable ‘data’ [-Wunused-variable]
smbusmodule.c: In function ‘SMBus_list_to_data’:
smbusmodule.c:433:6: error: dereferencing pointer to incomplete type
smbusmodule.c:441:7: error: dereferencing pointer to incomplete type
smbusmodule.c: In function ‘SMBus_write_block_data’:
smbusmodule.c:455:23: error: storage size of ‘data’ isn’t known
smbusmodule.c:464:33: error: ‘I2C_SMBUS_WRITE’ undeclared (first use in this function)
smbusmodule.c:465:5: error: ‘I2C_SMBUS_BLOCK_DATA’ undeclared (first use in this function)
smbusmodule.c:455:23: warning: unused variable ‘data’ [-Wunused-variable]
smbusmodule.c: In function ‘SMBus_block_process_call’:
smbusmodule.c:482:23: error: storage size of ‘data’ isn’t known
smbusmodule.c:491:33: error: ‘I2C_SMBUS_WRITE’ undeclared (first use in this function)
smbusmodule.c:492:5: error: ‘I2C_SMBUS_BLOCK_PROC_CALL’ undeclared (first use in this function)
smbusmodule.c:482:23: warning: unused variable ‘data’ [-Wunused-variable]
smbusmodule.c: In function ‘SMBus_read_i2c_block_data’:
smbusmodule.c:509:23: error: storage size of ‘data’ isn’t known
smbusmodule.c:519:33: error: ‘I2C_SMBUS_READ’ undeclared (first use in this function)
smbusmodule.c:509:23: warning: unused variable ‘data’ [-Wunused-variable]
smbusmodule.c: In function ‘SMBus_write_i2c_block_data’:
smbusmodule.c:538:23: error: storage size of ‘data’ isn’t known
smbusmodule.c:547:33: error: ‘I2C_SMBUS_WRITE’ undeclared (first use in this function)
smbusmodule.c:538:23: warning: unused variable ‘data’ [-Wunused-variable]
smbusmodule.c: In function ‘SMBus_read_i2c_block_data’:
smbusmodule.c:528:1: warning: control reaches end of non-void function [-Wreturn-type]
smbusmodule.c: In function ‘SMBus_block_process_call’:
smbusmodule.c:499:1: warning: control reaches end of non-void function [-Wreturn-type]
smbusmodule.c: In function ‘SMBus_read_block_data’:
smbusmodule.c:410:1: warning: control reaches end of non-void function [-Wreturn-type]
error: command 'gcc' failed with exit status 1
any ideas what's happening here?

Thanks,
Tom
catmaker wrote:Hi, I found this linux-i2c thread where the python-smbus source was upgraded to build & install on python3:
http://www.spinics.net/lists/linux-i2c/ ... .html___RE: Howto compile py-smbus with python 3.2

I've tried it on my Debian Raspberry Pi and it works.

Code: Select all

wget http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.0.orig.tar.bz2
tar xf i2c-tools_3.1.0.orig.tar.bz2
cd i2c-tools-3.1.0/py-smbus
cp smbusmodule.c smbusmodule.c.orig  # make backup
# copy the email code into smbusmodule.c
python3 setup.py build
sudo python3 setup.py install
Until the poster's patch goes upstream, I hope the above helps.

lightxx
Posts: 2
Joined: Mon Sep 02, 2013 8:23 am

Re: I2C and python 2 or 3 using python-smbus

Mon Sep 02, 2013 9:47 am

ok. just in case somebody else encounters this problem, you have to download a 2.* version of lm-sensors (http://lm-sensors.org/wiki/Download, unpack it and copy the kernel/include/i2c-dev.h file to the directory where the patched smbusmodule.c file resides. then change

Code: Select all

#include <linux/i2c-dev.h>
to

Code: Select all

#include "i2c-dev.h"
that should do the trick

DaveBolt
Posts: 4
Joined: Wed May 07, 2014 11:18 pm

Re: I2C and python 2 or 3 using python-smbus

Fri Aug 01, 2014 7:02 pm

You might also need to install python3-dev. Here is my revised set of instructions.

Code: Select all

sudo apt-get update
sudo apt-get install python3-dev
wget http://ftp.de.debian.org/debian/pool/main/i/i2c-tools/i2c-tools_3.1.1.orig.tar.bz2
tar -xf i2c-tools_3.1.1.orig.tar.bz2
cd i2c-tools-3.1.1/py-smbus
cp smbusmodule.c smbusmodule.c.orig  # make backup
Get the code from http://www.spinics.net/lists/linux-i2c/msg08427.html and replace the content of smbusmodule.c with that code.
Replace

Code: Select all

#include <linux/i2c-dev.h>
with

Code: Select all

#include "i2c-dev.h"
Download a 2.x version of lm-sensors from http://lm-sensors.org/wiki/Download and unpack it. (I had some issues trying to use tar or Xarchiver on my Pi, but managed just fine using 7-Zip on a PC).
Copy kernel/i2c-dev.h to the directory containing your modified smbusmodule.c
Run the following commands.

Code: Select all

    python3 setup.py build
    sudo python3 setup.py install
Now if you start Idle 3 you should be able to include smbus in your code without problems.
Thanks to everyone who contributed above and got me this far.

Mechoption
Posts: 9
Joined: Thu Nov 12, 2015 1:46 am

Re: I2C and python 2 or 3 using python-smbus

Tue Mar 29, 2016 1:40 am

If you stumble across this old thread (like I did), there is now a much easier way to use i2c with python3 on your Pi:

Code: Select all

sudo apt-get update 
sudo apt-get install python3-smbus
Courtesy this: http://www.linuxcircle.com/2015/05/03/h ... -python-3/

Henzell
Posts: 1
Joined: Thu Feb 23, 2017 7:47 pm

Re: I2C and python 2 or 3 using python-smbus

Thu Feb 23, 2017 7:53 pm

Since the lm-sensors -site has been down the last months, and it is not likely that it returns,
here is the link to the i2c-dev.h file:

https://github.com/Distrotech/i2c-tools ... lude/linux

Return to “Python”