environment sensor library for raspberry dependencies to adafruit exist
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

bmp.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import time
  2. from . import logger, background_task, DEBUG
  3. if not DEBUG:
  4. import smbus
  5. class bmp_180(background_task):
  6. RUN_SLEEP_TIME = 0.5
  7. SMBUS_DELAY = 0.5
  8. MIN_REFRESH_RATE = RUN_SLEEP_TIME + 3 * SMBUS_DELAY
  9. KEY_TEMPERATURE = 'temperature'
  10. KEY_PRESSURE = 'pressure'
  11. KEY_TIME = 'time'
  12. def __init__(self, data_callback=None):
  13. self.__data_callback__ = data_callback
  14. # Initialise background_task
  15. background_task.__init__(self)
  16. def __run__(self):
  17. data = self.__bmp_data_transmission__()
  18. if data is not None:
  19. logger.debug('BMP-Communication: Successfully: %s', repr(data))
  20. if self.__data_callback__ is not None:
  21. self.__data_callback__(**data)
  22. def __bmp_data_transmission__(self):
  23. if not DEBUG:
  24. rv = {}
  25. # Get I2C bus
  26. bus = smbus.SMBus(1)
  27. # BMP180 address, 0x77(119)
  28. # Read data back from 0xAA(170), 22 bytes
  29. data = bus.read_i2c_block_data(0x77, 0xAA, 22)
  30. # Convert the data
  31. AC1 = data[0] * 256 + data[1]
  32. if AC1 > 32767 :
  33. AC1 -= 65535
  34. AC2 = data[2] * 256 + data[3]
  35. if AC2 > 32767 :
  36. AC2 -= 65535
  37. AC3 = data[4] * 256 + data[5]
  38. if AC3 > 32767 :
  39. AC3 -= 65535
  40. AC4 = data[6] * 256 + data[7]
  41. AC5 = data[8] * 256 + data[9]
  42. AC6 = data[10] * 256 + data[11]
  43. B1 = data[12] * 256 + data[13]
  44. if B1 > 32767 :
  45. B1 -= 65535
  46. B2 = data[14] * 256 + data[15]
  47. if B2 > 32767 :
  48. B2 -= 65535
  49. MB = data[16] * 256 + data[17]
  50. if MB > 32767 :
  51. MB -= 65535
  52. MC = data[18] * 256 + data[19]
  53. if MC > 32767 :
  54. MC -= 65535
  55. MD = data[20] * 256 + data[21]
  56. if MD > 32767 :
  57. MD -= 65535
  58. time.sleep(self.SMBUS_DELAY)
  59. # BMP180 address, 0x77(119)
  60. # Select measurement control register, 0xF4(244)
  61. # 0x2E(46) Enable temperature measurement
  62. bus.write_byte_data(0x77, 0xF4, 0x2E)
  63. time.sleep(self.SMBUS_DELAY)
  64. # BMP180 address, 0x77(119)
  65. # Read data back from 0xF6(246), 2 bytes
  66. # temp MSB, temp LSB
  67. data = bus.read_i2c_block_data(0x77, 0xF6, 2)
  68. # Convert the data
  69. temp = data[0] * 256 + data[1]
  70. # BMP180 address, 0x77(119)
  71. # Select measurement control register, 0xF4(244)
  72. # 0x74(116) Enable pressure measurement, OSS = 1
  73. bus.write_byte_data(0x77, 0xF4, 0x74)
  74. time.sleep(self.SMBUS_DELAY)
  75. # BMP180 address, 0x77(119)
  76. # Read data back from 0xF6(246), 3 bytes
  77. # pres MSB1, pres MSB, pres LSB
  78. data = bus.read_i2c_block_data(0x77, 0xF6, 3)
  79. rv[self.KEY_TIME] = int(time.time())
  80. # Convert the data
  81. pres = ((data[0] * 65536) + (data[1] * 256) + data[2]) / 128
  82. # Callibration for Temperature
  83. X1 = (temp - AC6) * AC5 / 32768.0
  84. X2 = (MC * 2048.0) / (X1 + MD)
  85. B5 = X1 + X2
  86. rv[self.KEY_TEMPERATURE] = ((B5 + 8.0) / 16.0) / 10.0
  87. # Calibration for Pressure
  88. B6 = B5 - 4000
  89. X1 = (B2 * (B6 * B6 / 4096.0)) / 2048.0
  90. X2 = AC2 * B6 / 2048.0
  91. X3 = X1 + X2
  92. B3 = (((AC1 * 4 + X3) * 2) + 2) / 4.0
  93. X1 = AC3 * B6 / 8192.0
  94. X2 = (B1 * (B6 * B6 / 2048.0)) / 65536.0
  95. X3 = ((X1 + X2) + 2) / 4.0
  96. B4 = AC4 * (X3 + 32768) / 32768.0
  97. B7 = ((pres - B3) * (25000.0))
  98. pressure = 0.0
  99. if B7 < 2147483648:
  100. pressure = (B7 * 2) / B4
  101. else :
  102. pressure = (B7 / B4) * 2
  103. X1 = (pressure / 256.0) * (pressure / 256.0)
  104. X1 = (X1 * 3038.0) / 65536.0
  105. X2 = ((-7357) * pressure) / 65536.0
  106. rv[self.KEY_PRESSURE] = (pressure + (X1 + X2 + 3791) / 16.0) / 100
  107. return rv
  108. else:
  109. time.sleep(3 * self.SMBUS_DELAY)
  110. return {
  111. self.KEY_PRESSURE: 1717.,
  112. self.KEY_TEMPERATURE: 17.17,
  113. self.KEY_TIME: int(time.time())
  114. }