From 67a4490180343f61fa0c1274961ad1da92811999 Mon Sep 17 00:00:00 2001 From: helgibbons <50950368+helgibbons@users.noreply.github.com> Date: Thu, 30 Sep 2021 18:37:53 +0100 Subject: [PATCH 1/2] add Pico Explorer tiny weather station examples --- .../pico_explorer/weatherstation_BME280.py | 145 +++++++++++++++++ .../pico_explorer/weatherstation_BME68X.py | 146 ++++++++++++++++++ 2 files changed, 291 insertions(+) create mode 100644 micropython/examples/pico_explorer/weatherstation_BME280.py create mode 100644 micropython/examples/pico_explorer/weatherstation_BME68X.py diff --git a/micropython/examples/pico_explorer/weatherstation_BME280.py b/micropython/examples/pico_explorer/weatherstation_BME280.py new file mode 100644 index 00000000..01a48dca --- /dev/null +++ b/micropython/examples/pico_explorer/weatherstation_BME280.py @@ -0,0 +1,145 @@ +# This example lets you plug a BME280 breakout into your Pico Explorer and make a little indoor weather station, with barometer style descriptions. + +import machine +import utime + +from breakout_bme280 import BreakoutBME280 +from pimoroni_i2c import PimoroniI2C + +# Pico Explorer boilerplate +import picoexplorer as display +width = display.get_width() +height = display.get_height() +display_buffer = bytearray(width * height * 2) +display.init(display_buffer) + +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_PICO_EXPLORER) +bme = BreakoutBME280(i2c) + +# lets set up some pen colours to make drawing easier +tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit +white = display.create_pen(255, 255, 255) +black = display.create_pen(0, 0, 0) +red = display.create_pen(255, 0, 0) + +# converts the temperature into a barometer-type description and pen colour +def describe_temperature(temperature): + global tempcolour + if temperature < 10: + description = "very cold" + tempcolour = display.create_pen(0, 255, 255) + elif 10 <= temperature < 20: + description = "cold" + tempcolour = display.create_pen(0, 0, 255) + elif 20 <= temperature < 25: + description = "temperate" + tempcolour = display.create_pen(0, 255, 0) + elif 25 <= temperature < 30: + description = "warm" + tempcolour = display.create_pen(255, 255, 0) + elif temperature >= 30: + description = "very warm" + tempcolour = display.create_pen(255, 0, 0) + else: + description = "" + tempcolour = display.create_pen(0, 0, 0) + return description + +# comment out the function above and uncomment this one for yorkshire mode +""" +def describe_temperature(temperature): + global tempcolour + if temperature < 10: + description = "frozzed" + tempcolour = display.create_pen(0, 255, 255) + elif 10 <= temperature < 20: + description = "nithering" + tempcolour = display.create_pen(0, 0, 255) + elif 20 <= temperature < 25: + description = "fair t' middlin" + tempcolour = display.create_pen(0, 255, 0) + elif 25 <= temperature < 30: + description = "chuffing warm" + tempcolour = display.create_pen(255, 255, 0) + elif temperature >= 30: + description = "crackin t' flags" + tempcolour = display.create_pen(255, 0, 0) + else: + description = "" + tempcolour = display.create_pen(0, 0, 0) + return description +""" + +# converts pressure into barometer-type description +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 + +# converts humidity into good/bad description +def describe_humidity(humidity): + if 40 < humidity < 60: + description = "good" + else: + description = "bad" + return description + +while True: + # read the sensors + temperature, pressure, humidity = bme.read() + # pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa + pressurehpa = pressure/100 + + # draw a thermometer/barometer thingy + display.set_pen(125, 125, 125) + display.circle(190, 190, 40) + display.rectangle(180, 45, 20, 140) + + # switch to red to draw the 'mercury' + display.set_pen(red) + display.circle(190, 190, 30) + thermometerheight = int(120/30 * temperature) + if thermometerheight > 120: + thermometerheight = 120 + if thermometerheight < 1: + thermometerheight = 1 + display.rectangle(186, 50 + 120 - thermometerheight, 10, thermometerheight) + + # drawing the temperature text + display.set_pen(white) + display.text("temperature:", 10, 10, 240, 3) + display.set_pen(tempcolour) + display.text('{:.1f}'.format(temperature) + 'C', 10, 30, 240, 5) + display.set_pen(white) + display.text(describe_temperature(temperature), 10, 60, 240, 3) + + # 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) + + # 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() + + # waits for 1 second and clears to black + utime.sleep(1) + display.set_pen(black) + display.clear() \ No newline at end of file diff --git a/micropython/examples/pico_explorer/weatherstation_BME68X.py b/micropython/examples/pico_explorer/weatherstation_BME68X.py new file mode 100644 index 00000000..491f9d3f --- /dev/null +++ b/micropython/examples/pico_explorer/weatherstation_BME68X.py @@ -0,0 +1,146 @@ +# 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. + +import machine +import utime + +from breakout_bme68x import BreakoutBME68X +from pimoroni_i2c import PimoroniI2C + +# Pico Explorer boilerplate +import picoexplorer as display +width = display.get_width() +height = display.get_height() +display_buffer = bytearray(width * height * 2) +display.init(display_buffer) + +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +i2c = PimoroniI2C(**PINS_PICO_EXPLORER) +bme = BreakoutBME68X(i2c) + +# lets set up some pen colours to make drawing easier +tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit +white = display.create_pen(255, 255, 255) +black = display.create_pen(0, 0, 0) +red = display.create_pen(255, 0, 0) + +# converts the temperature into a barometer-type description and pen colour +def describe_temperature(temperature): + global tempcolour + if temperature < 10: + description = "very cold" + tempcolour = display.create_pen(0, 255, 255) + elif 10 <= temperature < 20: + description = "cold" + tempcolour = display.create_pen(0, 0, 255) + elif 20 <= temperature < 25: + description = "temperate" + tempcolour = display.create_pen(0, 255, 0) + elif 25 <= temperature < 30: + description = "warm" + tempcolour = display.create_pen(255, 255, 0) + elif temperature >= 30: + description = "very warm" + tempcolour = display.create_pen(255, 0, 0) + else: + description = "" + tempcolour = display.create_pen(0, 0, 0) + return description + +# comment out the function above and uncomment this one for yorkshire mode +""" +def describe_temperature(temperature): + global tempcolour + if temperature < 10: + description = "frozzed" + tempcolour = display.create_pen(0, 255, 255) + elif 10 <= temperature < 20: + description = "nithering" + tempcolour = display.create_pen(0, 0, 255) + elif 20 <= temperature < 25: + description = "fair t' middlin" + tempcolour = display.create_pen(0, 255, 0) + elif 25 <= temperature < 30: + description = "chuffing warm" + tempcolour = display.create_pen(255, 255, 0) + elif temperature >= 30: + description = "crackin t' flags" + tempcolour = display.create_pen(255, 0, 0) + else: + description = "" + tempcolour = display.create_pen(0, 0, 0) + return description +""" + +# converts pressure into barometer-type description +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 + +# converts humidity into good/bad description +def describe_humidity(humidity): + if 40 < humidity < 60: + description = "good" + else: + description = "bad" + return description + +while True: + # read the sensors + temperature, pressure, humidity, gas_resistance, status, gas_index, meas_index = bme.read() + + # pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa + pressurehpa = pressure/100 + + # draw a thermometer/barometer thingy + display.set_pen(125, 125, 125) + display.circle(190, 190, 40) + display.rectangle(180, 45, 20, 140) + + # switch to red to draw the 'mercury' + display.set_pen(red) + display.circle(190, 190, 30) + thermometerheight = int(120/30 * temperature) + if thermometerheight > 120: + thermometerheight = 120 + if thermometerheight < 1: + thermometerheight = 1 + display.rectangle(186, 50 + 120 - thermometerheight, 10, thermometerheight) + + # drawing the temperature text + display.set_pen(white) + display.text("temperature:", 10, 10, 240, 3) + display.set_pen(tempcolour) + display.text('{:.1f}'.format(temperature) + 'C', 10, 30, 240, 5) + display.set_pen(white) + display.text(describe_temperature(temperature), 10, 60, 240, 3) + + # 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) + + # 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() + + # waits for 1 second and clears to black + utime.sleep(1) + display.set_pen(black) + display.clear() From de05483ca6e0ab73780d4b1394ee945a080da72e Mon Sep 17 00:00:00 2001 From: helgibbons <50950368+helgibbons@users.noreply.github.com> Date: Thu, 30 Sep 2021 19:05:11 +0100 Subject: [PATCH 2/2] linting Pico Explorer weather station examples --- .../pico_explorer/weatherstation_BME280.py | 32 +++++++++++-------- .../pico_explorer/weatherstation_BME68X.py | 32 +++++++++++-------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/micropython/examples/pico_explorer/weatherstation_BME280.py b/micropython/examples/pico_explorer/weatherstation_BME280.py index 01a48dca..07c539dd 100644 --- a/micropython/examples/pico_explorer/weatherstation_BME280.py +++ b/micropython/examples/pico_explorer/weatherstation_BME280.py @@ -1,6 +1,5 @@ # This example lets you plug a BME280 breakout into your Pico Explorer and make a little indoor weather station, with barometer style descriptions. -import machine import utime from breakout_bme280 import BreakoutBME280 @@ -20,11 +19,12 @@ i2c = PimoroniI2C(**PINS_PICO_EXPLORER) bme = BreakoutBME280(i2c) # lets set up some pen colours to make drawing easier -tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit +tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit white = display.create_pen(255, 255, 255) black = display.create_pen(0, 0, 0) red = display.create_pen(255, 0, 0) + # converts the temperature into a barometer-type description and pen colour def describe_temperature(temperature): global tempcolour @@ -46,7 +46,8 @@ def describe_temperature(temperature): else: description = "" tempcolour = display.create_pen(0, 0, 0) - return description + return description + # comment out the function above and uncomment this one for yorkshire mode """ @@ -73,7 +74,8 @@ def describe_temperature(temperature): return description """ -# converts pressure into barometer-type description + +# converts pressure into barometer-type description def describe_pressure(pressure): if pressure < 970: description = "storm" @@ -89,6 +91,7 @@ def describe_pressure(pressure): description = "" return description + # converts humidity into good/bad description def describe_humidity(humidity): if 40 < humidity < 60: @@ -97,40 +100,41 @@ def describe_humidity(humidity): description = "bad" return description + while True: # read the sensors temperature, pressure, humidity = bme.read() # pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa - pressurehpa = pressure/100 - + pressurehpa = pressure / 100 + # draw a thermometer/barometer thingy display.set_pen(125, 125, 125) display.circle(190, 190, 40) display.rectangle(180, 45, 20, 140) - + # switch to red to draw the 'mercury' display.set_pen(red) display.circle(190, 190, 30) - thermometerheight = int(120/30 * temperature) + thermometerheight = int(120 / 30 * temperature) if thermometerheight > 120: thermometerheight = 120 if thermometerheight < 1: thermometerheight = 1 display.rectangle(186, 50 + 120 - thermometerheight, 10, thermometerheight) - + # drawing the temperature text - display.set_pen(white) + display.set_pen(white) display.text("temperature:", 10, 10, 240, 3) - display.set_pen(tempcolour) + display.set_pen(tempcolour) display.text('{:.1f}'.format(temperature) + 'C', 10, 30, 240, 5) display.set_pen(white) display.text(describe_temperature(temperature), 10, 60, 240, 3) - + # 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) - + # and the humidity text display.text("humidity:", 10, 170, 240, 3) display.text('{:.0f}'.format(humidity) + '%', 10, 190, 240, 5) @@ -142,4 +146,4 @@ while True: # waits for 1 second and clears to black utime.sleep(1) display.set_pen(black) - display.clear() \ No newline at end of file + display.clear() diff --git a/micropython/examples/pico_explorer/weatherstation_BME68X.py b/micropython/examples/pico_explorer/weatherstation_BME68X.py index 491f9d3f..70e0d109 100644 --- a/micropython/examples/pico_explorer/weatherstation_BME68X.py +++ b/micropython/examples/pico_explorer/weatherstation_BME68X.py @@ -1,6 +1,5 @@ # 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. -import machine import utime from breakout_bme68x import BreakoutBME68X @@ -20,11 +19,12 @@ i2c = PimoroniI2C(**PINS_PICO_EXPLORER) bme = BreakoutBME68X(i2c) # lets set up some pen colours to make drawing easier -tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit +tempcolour = display.create_pen(255, 255, 255) # this colour will get changed in a bit white = display.create_pen(255, 255, 255) black = display.create_pen(0, 0, 0) red = display.create_pen(255, 0, 0) + # converts the temperature into a barometer-type description and pen colour def describe_temperature(temperature): global tempcolour @@ -46,7 +46,8 @@ def describe_temperature(temperature): else: description = "" tempcolour = display.create_pen(0, 0, 0) - return description + return description + # comment out the function above and uncomment this one for yorkshire mode """ @@ -73,7 +74,8 @@ def describe_temperature(temperature): return description """ -# converts pressure into barometer-type description + +# converts pressure into barometer-type description def describe_pressure(pressure): if pressure < 970: description = "storm" @@ -89,6 +91,7 @@ def describe_pressure(pressure): description = "" return description + # converts humidity into good/bad description def describe_humidity(humidity): if 40 < humidity < 60: @@ -97,41 +100,42 @@ def describe_humidity(humidity): description = "bad" return description + while True: # read the sensors temperature, pressure, humidity, gas_resistance, status, gas_index, meas_index = bme.read() - + # pressure comes in pascals which is a reight long number, lets convert it to the more manageable hPa - pressurehpa = pressure/100 - + pressurehpa = pressure / 100 + # draw a thermometer/barometer thingy display.set_pen(125, 125, 125) display.circle(190, 190, 40) display.rectangle(180, 45, 20, 140) - + # switch to red to draw the 'mercury' display.set_pen(red) display.circle(190, 190, 30) - thermometerheight = int(120/30 * temperature) + thermometerheight = int(120 / 30 * temperature) if thermometerheight > 120: thermometerheight = 120 if thermometerheight < 1: thermometerheight = 1 display.rectangle(186, 50 + 120 - thermometerheight, 10, thermometerheight) - + # drawing the temperature text - display.set_pen(white) + display.set_pen(white) display.text("temperature:", 10, 10, 240, 3) - display.set_pen(tempcolour) + display.set_pen(tempcolour) display.text('{:.1f}'.format(temperature) + 'C', 10, 30, 240, 5) display.set_pen(white) display.text(describe_temperature(temperature), 10, 60, 240, 3) - + # 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) - + # and the humidity text display.text("humidity:", 10, 170, 240, 3) display.text('{:.0f}'.format(humidity) + '%', 10, 190, 240, 5)