environment sensor library for raspberry dependencies to adafruit exist
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

bmp.py 3.7KB

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