environment sensor library for raspberry dependencies to adafruit exist
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

bmp.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Distributed with a free-will license.
  2. # Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
  3. # BMP180
  4. # This code is designed to work with the BMP180_I2CS I2C Mini Module available from ControlEverything.com.
  5. # https://www.controleverything.com/content/Pressure?sku=BMP180_I2CS#tabs-0-product_tabset-2
  6. import smbus
  7. import threading
  8. import time
  9. from . import logger
  10. class bmp_180(object):
  11. KEY_TEMPERATURE = 'temperature'
  12. KEY_PRESSURE = 'pressure'
  13. KEY_TIME = 'time'
  14. def __init__(self, data_callback=None):
  15. self.__data_callback__ = data_callback
  16. # Initial the dht device, with data pin connected to:
  17. self.__active__ = True
  18. self.__thread__ = threading.Thread(target=self.run, args=())
  19. self.__thread__.daemon = True # Daemonize thread
  20. self.__thread__.start() # Start the execution
  21. def run(self):
  22. while self.__active__:
  23. data = self.__bmp_data_transmission__()
  24. if data is not None:
  25. logger.debug('BMP-Communication: Successfully: %s', repr(data))
  26. if self.__data_callback__ is not None:
  27. self.__data_callback__(**data)
  28. time.sleep(0.5)
  29. def close(self):
  30. self.__active__ = False
  31. self.__thread__.join()
  32. def __del__(self):
  33. self.close()
  34. def __bmp_data_transmission__(self):
  35. rv = {}
  36. # Get I2C bus
  37. bus = smbus.SMBus(1)
  38. # BMP180 address, 0x77(119)
  39. # Read data back from 0xAA(170), 22 bytes
  40. data = bus.read_i2c_block_data(0x77, 0xAA, 22)
  41. # Convert the data
  42. AC1 = data[0] * 256 + data[1]
  43. if AC1 > 32767 :
  44. AC1 -= 65535
  45. AC2 = data[2] * 256 + data[3]
  46. if AC2 > 32767 :
  47. AC2 -= 65535
  48. AC3 = data[4] * 256 + data[5]
  49. if AC3 > 32767 :
  50. AC3 -= 65535
  51. AC4 = data[6] * 256 + data[7]
  52. AC5 = data[8] * 256 + data[9]
  53. AC6 = data[10] * 256 + data[11]
  54. B1 = data[12] * 256 + data[13]
  55. if B1 > 32767 :
  56. B1 -= 65535
  57. B2 = data[14] * 256 + data[15]
  58. if B2 > 32767 :
  59. B2 -= 65535
  60. MB = data[16] * 256 + data[17]
  61. if MB > 32767 :
  62. MB -= 65535
  63. MC = data[18] * 256 + data[19]
  64. if MC > 32767 :
  65. MC -= 65535
  66. MD = data[20] * 256 + data[21]
  67. if MD > 32767 :
  68. MD -= 65535
  69. time.sleep(0.5)
  70. # BMP180 address, 0x77(119)
  71. # Select measurement control register, 0xF4(244)
  72. # 0x2E(46) Enable temperature measurement
  73. bus.write_byte_data(0x77, 0xF4, 0x2E)
  74. time.sleep(0.5)
  75. # BMP180 address, 0x77(119)
  76. # Read data back from 0xF6(246), 2 bytes
  77. # temp MSB, temp LSB
  78. data = bus.read_i2c_block_data(0x77, 0xF6, 2)
  79. # Convert the data
  80. temp = data[0] * 256 + data[1]
  81. # BMP180 address, 0x77(119)
  82. # Select measurement control register, 0xF4(244)
  83. # 0x74(116) Enable pressure measurement, OSS = 1
  84. bus.write_byte_data(0x77, 0xF4, 0x74)
  85. time.sleep(0.5)
  86. # BMP180 address, 0x77(119)
  87. # Read data back from 0xF6(246), 3 bytes
  88. # pres MSB1, pres MSB, pres LSB
  89. data = bus.read_i2c_block_data(0x77, 0xF6, 3)
  90. rv[self.KEY_TIME] = time.time()
  91. # Convert the data
  92. pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
  93. # Callibration for Temperature
  94. X1 = (temp - AC6) * AC5 / 32768.0
  95. X2 = (MC * 2048.0) / (X1 + MD)
  96. B5 = X1 + X2
  97. rv[self.KEY_TEMPERATURE] = ((B5 + 8.0) / 16.0) / 10.0
  98. # Calibration for Pressure
  99. B6 = B5 - 4000
  100. X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
  101. X2 = AC2 * B6 / 2048.0
  102. X3 = X1 + X2
  103. B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
  104. X1 = AC3 * B6 / 8192.0
  105. X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
  106. X3 = ((X1 + X2) + 2) / 4.0
  107. B4 = AC4 * (X3 + 32768) / 32768.0
  108. B7 = ((pres - B3) * (25000.0))
  109. pressure = 0.0
  110. if B7 < 2147483648:
  111. pressure = (B7 * 2) / B4
  112. else :
  113. pressure = (B7 / B4) * 2
  114. X1 = (pressure / 256.0) * (pressure / 256.0)
  115. X1 = (X1 * 3038.0) / 65536.0
  116. X2 = ((-7357) * pressure) / 65536.0
  117. rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
  118. return rv