코드가 python 2.x 용인것 같아서 3.x로 되도록 대충 바꾸었고,
import smbus 에서 에러가 나서 아래 패키지를 설치하면 해결된다.
$ sudo apt-get install python3-smbus |
[링크 : https://forums.raspberrypi.com/viewtopic.php?t=192909]
BMP280 이지 BME280이 아니라 습도가 안되서 좀 아쉽..
$ cat bmp280.py # Distributed with a free-will license. # Use it any way you want, profit or free, provided it fits in the licenses of its associated works. # BMP280 # This code is designed to work with the BMP280_I2CS I2C Mini Module available from ControlEverything.com. # https://www.controleverything.com/content/Barometer?sku=BMP280_I2CSs#tabs-0-product_tabset-2 import smbus import time # Get I2C bus bus = smbus.SMBus(1) # BMP280 address, 0x76(118) # Read data back from 0x88(136), 24 bytes b1 = bus.read_i2c_block_data(0x76, 0x88, 24) # Convert the data # Temp coefficents dig_T1 = b1[1] * 256 + b1[0] dig_T2 = b1[3] * 256 + b1[2] if dig_T2 > 32767 : dig_T2 -= 65536 dig_T3 = b1[5] * 256 + b1[4] if dig_T3 > 32767 : dig_T3 -= 65536 # Pressure coefficents dig_P1 = b1[7] * 256 + b1[6] dig_P2 = b1[9] * 256 + b1[8] if dig_P2 > 32767 : dig_P2 -= 65536 dig_P3 = b1[11] * 256 + b1[10] if dig_P3 > 32767 : dig_P3 -= 65536 dig_P4 = b1[13] * 256 + b1[12] if dig_P4 > 32767 : dig_P4 -= 65536 dig_P5 = b1[15] * 256 + b1[14] if dig_P5 > 32767 : dig_P5 -= 65536 dig_P6 = b1[17] * 256 + b1[16] if dig_P6 > 32767 : dig_P6 -= 65536 dig_P7 = b1[19] * 256 + b1[18] if dig_P7 > 32767 : dig_P7 -= 65536 dig_P8 = b1[21] * 256 + b1[20] if dig_P8 > 32767 : dig_P8 -= 65536 dig_P9 = b1[23] * 256 + b1[22] if dig_P9 > 32767 : dig_P9 -= 65536 # BMP280 address, 0x76(118) # Select Control measurement register, 0xF4(244) # 0x27(39) Pressure and Temperature Oversampling rate = 1 # Normal mode bus.write_byte_data(0x76, 0xF4, 0x27) # BMP280 address, 0x76(118) # Select Configuration register, 0xF5(245) # 0xA0(00) Stand_by time = 1000 ms bus.write_byte_data(0x76, 0xF5, 0xA0) time.sleep(0.5) # BMP280 address, 0x76(118) # Read data back from 0xF7(247), 8 bytes # Pressure MSB, Pressure LSB, Pressure xLSB, Temperature MSB, Temperature LSB # Temperature xLSB, Humidity MSB, Humidity LSB data = bus.read_i2c_block_data(0x76, 0xF7, 8) # Convert pressure and temperature data to 19-bits adc_p = ((data[0] * 65536) + (data[1] * 256) + (data[2] & 0xF0)) / 16 adc_t = ((data[3] * 65536) + (data[4] * 256) + (data[5] & 0xF0)) / 16 # Temperature offset calculations var1 = ((adc_t) / 16384.0 - (dig_T1) / 1024.0) * (dig_T2) var2 = (((adc_t) / 131072.0 - (dig_T1) / 8192.0) * ((adc_t)/131072.0 - (dig_T1)/8192.0)) * (dig_T3) t_fine = (var1 + var2) cTemp = (var1 + var2) / 5120.0 fTemp = cTemp * 1.8 + 32 # Pressure offset calculations var1 = (t_fine / 2.0) - 64000.0 var2 = var1 * var1 * (dig_P6) / 32768.0 var2 = var2 + var1 * (dig_P5) * 2.0 var2 = (var2 / 4.0) + ((dig_P4) * 65536.0) var1 = ((dig_P3) * var1 * var1 / 524288.0 + ( dig_P2) * var1) / 524288.0 var1 = (1.0 + var1 / 32768.0) * (dig_P1) p = 1048576.0 - adc_p p = (p - (var2 / 4096.0)) * 6250.0 / var1 var1 = (dig_P9) * p * p / 2147483648.0 var2 = p * (dig_P8) / 32768.0 pressure = (p + (var1 + var2 + (dig_P7)) / 16.0) / 100 # Output data to screen print("Temperature in Celsius : %.2f C" %cTemp) print("Temperature in Fahrenheit : %.2f F" %fTemp) print("Pressure : %.2f hPa" %pressure) |
[링크 : http://www.pibits.net/code/raspberry-pi-and-bmp280-sensor-example.php]
+
[링크 : https://cdn-shop.adafruit.com/datasheets/BST-BMP280-DS001-11.pdf]
내가 가진거. HW-611 이라고 써있고 칩은 별다른 마킹이 없는데
i2c 주소는 0x76 이니 맞는거 같긴한데 아닌것 같기도 하고 모르겠다 -ㅁ-
'embeded > raspberry pi' 카테고리의 다른 글
서보 팬틸트 브라켓 구매 (0) | 2023.09.19 |
---|---|
DHT22 도착! (0) | 2023.09.19 |
라즈베리 파이 openOCD jtag? (0) | 2023.07.24 |
rpi bear metal (0) | 2023.07.10 |
라즈베리 3 전원 off (0) | 2023.07.02 |