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 PythonU sing Linux Drivers Second Edition
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 Ioct ***NEW!!
GPIO Events
Some Electronics
Pulse Width Modulation
Extract:
PWM *
SPI Devices
I2C Basics
Extract:
I2C *
The I2C Linux Driver
Advanced I2C
Sensor Drivers
Going Further With Drivers
Appendix I
*From the first edition waiting for update.
<ASIN:B0CT46R6LF>
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 .
Looking Forward To NAO 7 03/11/2024
Introduced to the world in 2004 by its creator Bruno Maisonnier the kid-sized, autonomous humanoid robot NAO, turns 20 this year. At less than 2 ft tall, it is small in stature, but plays a big r [ ... ]
Prompt Engineering Techniques To Make You An Expert 18/11/2024
Introducing a GitHub repository full of hot tips and instructions on how to build the perfect prompt presented in a collection of Jupiter Notebooks.
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>