From 3bc5969391ee8666176a3d46da12a897913e0060 Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 24 Sep 2024 13:10:55 +0200 Subject: [PATCH] demo script --- GPIO-demo.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 GPIO-demo.py diff --git a/GPIO-demo.py b/GPIO-demo.py new file mode 100644 index 0000000..ea12611 --- /dev/null +++ b/GPIO-demo.py @@ -0,0 +1,39 @@ +''' + Demo Python GPIO on the MQ-Pro (requires a modified device tree) + See: https://github.com/easytarget/MQ-Pro-IO/ + For install requirements requirements look at: + - https://github.com/easytarget/MQ-Pro-IO/blob/main/GPIO-examples.md#python-demo +''' +from time import sleep +# oled libs +from luma.core.interface.serial import i2c +from luma.core.render import canvas +from luma.oled.device import ssd1306 +# sensor libs +from smbus2 import SMBus +from bme280 import BME280 + +# Hardware +i2c_bus = 0 +ssd1306_addr = 0x3c +bme280_addr = 0x76 + +# display init +serial = i2c(port=i2c_bus, address=ssd1306_addr) +device = ssd1306(serial) + +# sensor init +bus = SMBus(i2c_bus) +bme280 = BME280(i2c_addr=bme280_addr, i2c_dev=bus) +bme280.setup() + +while True: + t = round(bme280.get_temperature(),1) + h = round(bme280.get_humidity(),1) + p = round(bme280.get_pressure()) + out = ' Temp: {}°\n Humi: {}%\n Pres: {}mb'.format(t,h,p) + with canvas(device) as draw: + draw.rectangle(device.bounding_box, outline="white", fill="black") + draw.text((26, 12), out, fill="white") + print(out.replace('\n',' : ')) + sleep(1)