Hello,
I have a Galactic Unicorn and a BME280 plugged on it.
I’ve modified the “clock.py” example to display both clock and temp/pressure/humidity level.
My program is working well, but after a few minutes, it’s blocked.
The problem seem to come when I read BME280 value, but I don’t understand why.
Could you help me ?
import time
import math
import machine
import network
import ntptime
from galactic import GalacticUnicorn
from picographics import PicoGraphics, DISPLAY_GALACTIC_UNICORN as DISPLAY
import breakout_bme280 as bme280
from pimoroni_i2c import PimoroniI2C
PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5}
i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN)
bme = bme280.BreakoutBME280(i2c)
bme.configure(bme280.FILTER_COEFF_OFF, bme280.STANDBY_TIME_1000_MS,
bme280.OVERSAMPLING_16X, bme280.OVERSAMPLING_2X, bme280.OVERSAMPLING_1X,
bme280.FORCED_MODE)
try:
from secrets import WIFI_SSID, WIFI_PASSWORD
wifi_available = True
except ImportError:
print("Create secrets.py with your WiFi credentials to get time from NTP")
wifi_available = False
# create galactic object and graphics surface for drawing
gu = GalacticUnicorn()
graphics = PicoGraphics(DISPLAY)
# create the rtc object
rtc = machine.RTC()
width = GalacticUnicorn.WIDTH
height = GalacticUnicorn.HEIGHT
# set up some pens to use later
WHITE = graphics.create_pen(255, 255, 255)
BLACK = graphics.create_pen(0, 0, 0)
# Start connection
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)
# Wait for connect success or failure
max_wait = 100
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(0.2)
if max_wait > 0:
print("Connected")
# function for drawing outlined text
def outline_text(text, x, y):
graphics.set_pen(BLACK)
graphics.text(text, x - 1, y - 1, -1, 1)
graphics.text(text, x, y - 1, -1, 1)
graphics.text(text, x + 1, y - 1, -1, 1)
graphics.text(text, x - 1, y, -1, 1)
graphics.text(text, x + 1, y, -1, 1)
graphics.text(text, x - 1, y + 1, -1, 1)
graphics.text(text, x, y + 1, -1, 1)
graphics.text(text, x + 1, y + 1, -1, 1)
graphics.set_pen(WHITE)
graphics.text(text, x, y, -1, 1)
# Connect to wifi and synchronize the RTC time from NTP
def sync_time():
try:
ntptime.settime()
print("Time set")
except OSError:
pass
# NTP synchronizes the time to UTC, this allows you to adjust the displayed time
# by one hour increments from UTC by pressing the volume up/down buttons
#
# We use the IRQ method to detect the button presses to avoid incrementing/decrementing
# multiple times when the button is held.
utc_offset = 0
up_button = machine.Pin(GalacticUnicorn.SWITCH_VOLUME_UP, machine.Pin.IN, machine.Pin.PULL_UP)
down_button = machine.Pin(GalacticUnicorn.SWITCH_VOLUME_DOWN, machine.Pin.IN, machine.Pin.PULL_UP)
def adjust_utc_offset(pin):
global utc_offset
if pin == up_button:
utc_offset += 1
if pin == down_button:
utc_offset -= 1
up_button.irq(trigger=machine.Pin.IRQ_FALLING, handler=adjust_utc_offset)
down_button.irq(trigger=machine.Pin.IRQ_FALLING, handler=adjust_utc_offset)
year, month, day, wd, hour, minute, second, _ = rtc.datetime()
last_second = second
last_minute = minute
t,p,h=bme.read()
print(t,p,h)
# Check whether the RTC time has changed and if so redraw the display
def redraw_display_if_reqd():
global year, month, day, wd, hour, minute, second, last_second, last_minute,t,p,h
year, month, day, wd, hour, minute, second, _ = rtc.datetime()
if second != last_second:
#print(second)
hour += utc_offset
if hour>23:
hour-=24
if hour<0:
hour+=24;
graphics.set_pen(BLACK)
graphics.clear()
clock = "{:02}:{:02}".format(hour, minute)
# set the font
graphics.set_font("bitmap8")
# calculate text position so that it is centred
w = graphics.measure_text(clock, 1)
x = int(width / 2 - w / 2 + 1)
x = 2
y = 2
outline_text(clock, x, y)
graphics.set_font("bitmap6")
if minute != last_minute:
print("refresh bme")
t,p,h=bme.read()
print("refreshed")
time.sleep(0.5)
last_minute=minute
graphics.set_pen(WHITE)
if second>=40:
text=str(int(round(h,0)))+"%"
elif second>=20:
text=str(int(round(p/100,0)))+"m"
else:
text=str(int(round(t,0)))+"°C"
w = graphics.measure_text(text, 1)
x=width-w
y=2
graphics.text(text, x, y, -1, 1)
last_second = second
# update the display
gu.update(graphics)
gu.set_brightness(0.3)
sync_time()
while True:
if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_UP):
gu.adjust_brightness(+0.01)
if gu.is_pressed(GalacticUnicorn.SWITCH_BRIGHTNESS_DOWN):
gu.adjust_brightness(-0.01)
if gu.is_pressed(GalacticUnicorn.SWITCH_A):
sync_time()
redraw_display_if_reqd()
time.sleep(0.1)