Advertisement
silver2row

trying python3-smbus for coding

Jan 10th, 2024
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. from time import sleep
  4. import smbus
  5.  
  6. bus = smbus.SMBus(1)
  7. addr = 0x28
  8. SENS_STS = 0x01
  9. PRES_REF_H = 0x0B
  10. PRES_REF_L = 0x0C
  11. MEAS_RATE_H = 0x02
  12. MEAS_RATE_L = 0x03
  13. MEAS_CFG = 0x04
  14.  
  15. MEAS_STS = 0x07
  16.  
  17. CO2PPM_H = 0x05
  18. CO2PPM_L = 0x06
  19. sleep(1)
  20.  
  21. try:
  22.     rack = int(input("Please type a 1 or 0, thank you... "))
  23.     while True:
  24.         if rack >= 1:
  25.             bus.read_byte_data(addr, SENS_STS)
  26.             bus.read_byte_data(addr, PRES_REF_H)
  27.             bus.read_byte_data(addr, PRES_REF_L)
  28.             bus.read_byte_data(addr, MEAS_RATE_H)
  29.             bus.read_byte_data(addr, MEAS_RATE_L)
  30.             bus.read_byte_data(addr, MEAS_CFG)
  31.             bus.read_byte_data(addr, MEAS_STS)
  32.             bus.read_byte_data(addr, CO2PPM_H)
  33.             bus.read_byte_data(addr, CO2PPM_L)
  34.             sleep(0.5)
  35.             bus.write_byte_data(addr, PRES_REF_H, 0x03)
  36.             bus.write_byte_data(addr, PRES_REF_L, 0xF5)
  37.             bus.write_byte_data(addr, MEAS_RATE_H, 0x00)
  38.             bus.write_byte_data(addr, MEAS_RATE_L, 0x0A)
  39.             bus.write_byte_data(addr, MEAS_CFG, 0x01)
  40.             OnePlus = str(CO2PPM_H) + str(CO2PPM_L)
  41.             print("PPM of CO2 in your home is: ".format(OnePlus))
  42.         elif rack == 0:
  43.             bus.read_byte_data(addr, SENS_STS)
  44.             bus.read_byte_data(addr, PRES_REF_H)
  45.             bus.read_byte_data(addr, PRES_REF_L)
  46.             bus.read_byte_data(addr, MEAS_RATE_H)
  47.             bus.read_byte_data(addr, MEAS_RATE_L)
  48.             bus.read_byte_data(addr, MEAS_CFG)
  49.             bus.read_byte_data(addr, MEAS_STS)
  50.             bus.read_byte_data(addr, CO2PPM_H)
  51.             bus.read_byte_data(addr, CO2PPM_L)
  52.             sleep(0.5)
  53.             bus.write_byte_data(addr, PRES_REF_H, 0x03)
  54.             bus.write_byte_data(addr, PRES_REF_L, 0xF5)
  55.             bus.write_byte_data(addr, MEAS_RATE_H, 0x00)
  56.             bus.write_byte_data(addr, MEAS_RATE_L, 0x0A)
  57.             bus.write_byte_data(addr, MEAS_CFG, 0x01)
  58.             TwoPlus = str(CO2PPM_H) + (str(CO2PPM_L)
  59.             print("The PPM of CO2 in your home is: ".format(TwoPlus))
  60.         else:
  61.             pass
  62. except KeyboardInterrupt:
  63.     print("Done for now!")
  64.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement