2021-09-30 18:37:53 +01:00
# This example lets you plug a BME680 or BME688 breakout into your Pico Explorer to make a little indoor weather station, with barometer style descriptions.
2022-07-07 09:37:47 +01:00
import time
2021-09-30 18:37:53 +01:00
from breakout_bme68x import BreakoutBME68X
from pimoroni_i2c import PimoroniI2C
2022-05-12 10:35:35 +01:00
from pimoroni import PICO_EXPLORER_I2C_PINS
2022-07-07 09:37:47 +01:00
from picographics import PicoGraphics , DISPLAY_PICO_EXPLORER
2021-09-30 18:37:53 +01:00
2022-07-07 09:37:47 +01:00
# set up the hardware
display = PicoGraphics ( display = DISPLAY_PICO_EXPLORER )
2022-05-12 10:35:35 +01:00
i2c = PimoroniI2C ( * * PICO_EXPLORER_I2C_PINS )
2022-07-07 09:37:47 +01:00
bme = BreakoutBME68X ( i2c , address = 0x76 )
2021-09-30 18:37:53 +01:00
# lets set up some pen colours to make drawing easier
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 0 , 0 ) # this colour will get changed in a bit
2022-05-28 18:09:59 +01:00
WHITE = display . create_pen ( 255 , 255 , 255 )
BLACK = display . create_pen ( 0 , 0 , 0 )
RED = display . create_pen ( 255 , 0 , 0 )
GREY = display . create_pen ( 125 , 125 , 125 )
2021-09-30 18:37:53 +01:00
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# converts the temperature into a barometer-type description and pen colour
def describe_temperature ( temperature ) :
2022-05-28 18:09:59 +01:00
global TEMPCOLOUR
2021-09-30 18:37:53 +01:00
if temperature < 10 :
description = " very cold "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 255 , 255 )
2021-09-30 18:37:53 +01:00
elif 10 < = temperature < 20 :
description = " cold "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 0 , 255 )
2021-09-30 18:37:53 +01:00
elif 20 < = temperature < 25 :
description = " temperate "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 255 , 0 )
2021-09-30 18:37:53 +01:00
elif 25 < = temperature < 30 :
description = " warm "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 255 , 255 , 0 )
2021-09-30 18:37:53 +01:00
elif temperature > = 30 :
description = " very warm "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 255 , 0 , 0 )
2021-09-30 18:37:53 +01:00
else :
description = " "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 0 , 0 )
2021-09-30 19:05:11 +01:00
return description
2021-09-30 18:37:53 +01:00
# comment out the function above and uncomment this one for yorkshire mode
"""
def describe_temperature ( temperature ) :
2022-05-28 18:09:59 +01:00
global TEMPCOLOUR
2021-09-30 18:37:53 +01:00
if temperature < 10 :
description = " frozzed "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 255 , 255 )
2021-09-30 18:37:53 +01:00
elif 10 < = temperature < 20 :
description = " nithering "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 0 , 255 )
2021-09-30 18:37:53 +01:00
elif 20 < = temperature < 25 :
description = " fair t ' middlin "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 255 , 0 )
2021-09-30 18:37:53 +01:00
elif 25 < = temperature < 30 :
description = " chuffing warm "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 255 , 255 , 0 )
2021-09-30 18:37:53 +01:00
elif temperature > = 30 :
description = " crackin t ' flags "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 255 , 0 , 0 )
2021-09-30 18:37:53 +01:00
else :
description = " "
2022-07-07 09:37:47 +01:00
TEMPCOLOUR = display . create_pen ( 0 , 0 , 0 )
2021-09-30 18:37:53 +01:00
return description
"""
2021-09-30 19:05:11 +01:00
# converts pressure into barometer-type description
2021-09-30 18:37:53 +01:00
def describe_pressure ( pressure ) :
if pressure < 970 :
description = " storm "
elif 970 < = pressure < 990 :
description = " rain "
elif 990 < = pressure < 1010 :
description = " change "
elif 1010 < = pressure < 1030 :
description = " fair "
elif pressure > = 1030 :
description = " dry "
else :
description = " "
return description
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# converts humidity into good/bad description
def describe_humidity ( humidity ) :
if 40 < humidity < 60 :
description = " good "
else :
description = " bad "
return description
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
while True :
2022-05-28 18:09:59 +01:00
display . set_pen ( BLACK )
display . clear ( )
2021-09-30 18:37:53 +01:00
# read the sensors
temperature , pressure , humidity , gas_resistance , status , gas_index , meas_index = bme . read ( )
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa
2021-09-30 19:05:11 +01:00
pressurehpa = pressure / 100
2021-09-30 18:37:53 +01:00
# draw a thermometer/barometer thingy
2022-05-28 18:09:59 +01:00
display . set_pen ( GREY )
2021-09-30 18:37:53 +01:00
display . circle ( 190 , 190 , 40 )
display . rectangle ( 180 , 45 , 20 , 140 )
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# switch to red to draw the 'mercury'
2022-05-28 18:09:59 +01:00
display . set_pen ( RED )
2021-09-30 18:37:53 +01:00
display . circle ( 190 , 190 , 30 )
2021-09-30 19:05:11 +01:00
thermometerheight = int ( 120 / 30 * temperature )
2021-09-30 18:37:53 +01:00
if thermometerheight > 120 :
thermometerheight = 120
if thermometerheight < 1 :
thermometerheight = 1
display . rectangle ( 186 , 50 + 120 - thermometerheight , 10 , thermometerheight )
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# drawing the temperature text
2022-05-28 18:09:59 +01:00
display . set_pen ( WHITE )
2021-09-30 18:37:53 +01:00
display . text ( " temperature: " , 10 , 10 , 240 , 3 )
2022-05-28 18:09:59 +01:00
display . set_pen ( TEMPCOLOUR )
2021-09-30 18:37:53 +01:00
display . text ( ' {:.1f} ' . format ( temperature ) + ' C ' , 10 , 30 , 240 , 5 )
2022-05-28 18:09:59 +01:00
display . set_pen ( WHITE )
2021-09-30 18:37:53 +01:00
display . text ( describe_temperature ( temperature ) , 10 , 60 , 240 , 3 )
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# and the pressure text
display . text ( " pressure: " , 10 , 90 , 240 , 3 )
display . text ( ' {:.0f} ' . format ( pressurehpa ) + ' hPa ' , 10 , 110 , 240 , 5 )
display . text ( describe_pressure ( pressurehpa ) , 10 , 140 , 240 , 3 )
2021-09-30 19:05:11 +01:00
2021-09-30 18:37:53 +01:00
# and the humidity text
display . text ( " humidity: " , 10 , 170 , 240 , 3 )
display . text ( ' {:.0f} ' . format ( humidity ) + ' % ' , 10 , 190 , 240 , 5 )
display . text ( describe_humidity ( humidity ) , 10 , 220 , 240 , 3 )
# time to update the display
display . update ( )
2022-05-28 18:09:59 +01:00
# waits for 1 second and clears to BLACK
2022-07-07 09:37:47 +01:00
time . sleep ( 1 )