Pi IoT In Python Using Linux Drivers - I2C
Written by Harry Fairhead & Mike James   
Monday, 01 November 2021
Article Index
Pi IoT In Python Using Linux Drivers - I2C
HTU21
Processing The Data
The Complete Program

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

  • I2C Tools

 

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.

 HTU21

Raspberry Pi IoT In PythonUsing Linux Drivers
Second Edition

By Harry Fairhead & Mike James

DriverPython2e360

Buy from Amazon.

Contents

  1.  Choosing A Pi For IoT
  2.  Getting Started With Python
  3.   Drivers: A First Program
  4.  The GPIO Character Driver 
  5.  GPIO Using Ioct ***NEW!!
  6.  GPIO Events
  7.  The Device Tree
       Extract: The DHT22
  8.  Some Electronics
  9.  Pulse Width Modulation
       Extract: PWM *
  10. SPI Devices
  11. I2C Basics
       Extract: I2C *
  12. The I2C Linux Driver
  13. Advanced I2C
  14. Sensor Drivers
  15. 1-Wire Bus
       Extract 1-Wire And The DS18B20 *
  16. Going Further With Drivers
  17. 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.

Banner


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

espbook

 

Comments




or email your comment to: comments@i-programmer.info

<ASIN:1871962641>

<ASIN:1871962668>

<ASIN:1871962463>

<ASIN:1871962587>



Last Updated ( Wednesday, 23 November 2022 )