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 Python Using Linux Drivers

By Harry Fairhead & Mike James

driversPython360

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 ***NEW!!
  5.  GPIO Using I/O Control
  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

 <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.

Banner


Lost at SQL - The Game
21/05/2023

"Lost at SQL" offers a gamified approach to learning the basics of SQL addressed to total beginners.



Julia 1.9 Adds Native Code Caching
23/05/2023

Julia 1.9 has been released with improvements including caching for native code, heap snapshot support, and improved sorting.


More News

picobook

 



 

Comments




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

<ASIN:1871962641>

<ASIN:1871962668>

<ASIN:1871962463>

<ASIN:1871962587>



Last Updated ( Wednesday, 23 November 2022 )