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.

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