environment sensor library for raspberry dependencies to adafruit exist
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

bmp.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 = 0.5
  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.debug('BMP-Communication: Successfully: %s', repr(data))
  31. if self.__data_callback__ is not None:
  32. self.__data_callback__(**data)
  33. def __bmp_data_transmission__(self):
  34. if not DEBUG:
  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(self.SMBUS_DELAY)
  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(self.SMBUS_DELAY)
  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(self.SMBUS_DELAY)
  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] = int(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
  119. else:
  120. time.sleep(3 * self.SMBUS_DELAY)
  121. return {
  122. self.KEY_PRESSURE: 1717.,
  123. self.KEY_TEMPERATURE: 17.17,
  124. self.KEY_TIME: int(time.time())
  125. }