"""
"""
from machine import Pin, I2C
from machine import Timer
import utime
import sys
from bmp280 import BMP280
INTERVAL = 1000 # millisecond
BMP_addr = 0x76 # 預設位址 0x76
oled_width = 128
oled_height = 64
if(sys.platform=="esp8266"):
led_pin = 2 # GPIO2(D4)
I2C_SDA =4 # GPIO4(D2)
I2C_SCL =5 # GIPO5(D1)
i2c = I2C(scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=100000) #initializing the I2C method for ESP8266
button_pin = 16 # GPIO16(D0)
elif(sys.platform=="esp32"):
led_pin = 2
I2C_SDA =21 # GPIO21(I2C SDA)
I2C_SCL =22 # GPIO22(I2C SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=100000) #initializing the I2C method for ESP32
button_pin = 35
elif(sys.platform=="rp2"):
led_pin = "LED"
I2C_SDA =16 # GPIO16(I2C SDA)
I2C_SCL =17 # GPIO17(I2C SCL)
i2c = I2C(0, scl=Pin(I2C_SCL), sda=Pin(I2C_SDA), freq=100000) #initializing the I2C method for pi Pico
button_pin = 21
# Start Function
if __name__ == "__main__":
print("Hello, " + sys.platform + "!")
# Define GPIO pins for LED and push button
led1 = Pin(led_pin, Pin.OUT) # Initialize LED
# FORCEER een scan om te kijken of de bus leeft
devices = i2c.scan() # I2C scanning
if len(devices) != 0:
print(f"找到 I2C 設備,位址: {[hex(d) for d in devices]}")
if devices.count(BMP_addr) > 0:
print(f"BMP280 found at I2C address {BMP_addr:#x}")
else:
raise OSError(f"BMP280 not found at I2C address {BMP_addr:#x}")
else:
raise OSError("No device found")
try:
# 建立 BMP280 物件
bmp = BMP280(i2c, addr=BMP_addr)
utime.sleep_ms(1000)
lastTime = utime.ticks_ms()
print("--- Start measurement ---")
while True:
currTime = utime.ticks_ms()
if (currTime - lastTime > INTERVAL):
lastTime = currTime
led1.value(not led1.value())
temp = bmp.temperature()
pressure = bmp.pressure()
# 根據標準海平面氣壓 (1013.25 hPa) 估算海拔高度 (米)
altitude = 44330 * (1 - (pressure / 1013.25) ** (1 / 5.255))
print(f"溫度: {temp:.2f} °C | 氣壓: {pressure:.2f} hPa | 估計海拔: {altitude:.2f} m")
except Exception as e:
print(e)
except KeyboardInterrupt: # 使用者中斷執行(通常是輸入^C)
print("Stop")
print("Exit measurement.")