-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGPIO-demo.py
57 lines (47 loc) · 1.5 KB
/
GPIO-demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'''
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
Tested using the LoRa HAT device tree:
- https://github.com/easytarget/MQ-Pro-IO/tree/main/alt-trees/lora-hat
'''
from time import sleep, ctime
# 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()
def read_sensor():
t = round(bme280.get_temperature(),1)
h = round(bme280.get_humidity(),1)
p = round(bme280.get_pressure())
return t, h, p
def show_text(s, x, y):
with canvas(device) as draw:
draw.rectangle(device.bounding_box, outline="white", fill="black")
draw.text((x, y), s, fill="white")
# initial reading to settle sensor
_, _, _ = read_sensor()
show_text('Initialising..', 36, 26)
print('Initialising')
# loop
while True:
sleep(1)
temp, humi, pres = read_sensor()
out = ' Temp: {}°C\n Humi: {}%\n Pres: {}mb'.format(temp, humi, pres)
show_text(out, 26, 12)
print('{} :: {}°C, {}%, {}mb'.format(ctime(),temp, humi, pres))