Page 4 of 4
The Complete Program
import subprocess
import io
import fcntl
from time import sleep
def crcCheck(msb, lsb, check):
data32 = (msb << 16)|(lsb <<8)| check
divisor = 0x988000
for i in range(16):
if( data32 & 1<<(23 - i) ):
data32 ^= divisor
divisor>>= 1
return data32
def checkI2CBus():
temp = subprocess.Popen(["sudo", "dtparam", "-l"],
stdout = subprocess.PIPE)
output = str(temp.communicate())
print(output)
lasti2c=output.rfind("i2c_arm")
if lasti2c!=-1:
lasti2c=output.find("i2c_arm=on",lasti2c)
if lasti2c==-1:
temp = subprocess.Popen(["sudo", "dtparam", "i2c_arm=on"],
stdout = subprocess.PIPE)
output = str(temp.communicate())
return
checkI2CBus()
I2C_SLAVE=0x0703
fdr = io.open("/dev/i2c-1", "rb", buffering=0)
fdw = io.open("/dev/i2c-1", "wb", buffering=0)
fcntl.ioctl(fdr, I2C_SLAVE, 0x40)
fcntl.ioctl(fdw, I2C_SLAVE, 0x40)
fdw.write( bytearray([0xF3]))
while(True):
try:
data=fdr.read(3)
break
except:
sleep(0.01)
msb=data[0]
lsb=data[1]
crc=data[2]
data16= (msb << 8) |(lsb & 0xFC)
temp = -46.85 +(175.72 * data16 /65536)
print("Temperature=",temp,"C")
fdw.write( bytearray([0xF5]))
while(True):
try:
data=fdr.read(3)
break
except:
sleep(0.01)
msb=data[0]
lsb=data[1]
crc=data[2]
data16= (msb << 8) |(lsb & 0xFC)
hum = -6 + 125.0 * data16 / 65536
print("humidity=",hum,"%")
print(crcCheck(msb, lsb, crc))
fdw.close()
fdr.close()
Not included in this extract but in chapter
Summary
The I2C driver can be loaded dynamically and it provides the basic facilities to interface with any I2C device.
The I2C driver creates a number of new folders and it also accepts ioctl commands.
As an example of using the driver, the HTU21D is easy to set up and read. It also has a dedicated Linux driver which is discussed in Chapter 14.
Without clock stretching support, all we can do is to poll for data to be ready to read.
Computing a CRC is something every IoT programmer needs to know how to do in the general case.
There are a number of command line tools that let you work with I2C, but they need to be used with caution.
Raspberry Pi IoT In Python U sing Linux Drivers
By Harry Fairhead & Mike James
Buy from Amazon .
Contents
Choosing A Pi For IoT
Getting Started With Python
Drivers: A First Program
The GPIO Character Driver
GPIO Using I/O Control
GPIO Events
The Device Tree Extract: The DHT22
Some Electronics
Pulse Width Modulation Extract: PWM
SPI Devices
I2C Basics Extract: I2C ***NEW!!
The I2C Linux Driver
Advanced I2C
Sensor Drivers
1-Wire Bus Extract 1-Wire And The DS18B20
Going Further With Drivers
Appendix I
<ASIN:187196265X>
<ASIN:B08YXJ743K>
To be informed about new articles on I Programmer, sign up for our weekly newsletter , subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin .
Gato And Artificial General Intelligence 18/05/2022
DeepMind has set the cat among the pigeons - again. Gato is a transformer model trained on a range of different subject areas that claims to be a "multi-modal" solution, i.e. it's an AI that can do mo [ ... ]
C#11 Will Support Raw String Literals 26/04/2022
Details of what Microsoft is adding to C# 11 have been released, with changes concentrating on better performance along with new language features including raw string literals and checked user-define [ ... ]
More News
Comments
Make a Comment or View Existing Comments Using Disqus
or email your comment to: comments@i-programmer.info
<ASIN:1871962641>
<ASIN:1871962668>
<ASIN:1871962463>
<ASIN:1871962587>