Created
October 20, 2022 12:23
-
-
Save austin-barrington/0feedfb426620310f1b1d64677ada346 to your computer and use it in GitHub Desktop.
Raspberry Pi - Pico W - Enviro+ Board
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import network | |
| import urequests as requests | |
| from picographics import PicoGraphics, DISPLAY_ENVIRO_PLUS | |
| from pimoroni import RGBLED | |
| from breakout_bme68x import BreakoutBME68X, STATUS_HEATER_STABLE | |
| from breakout_ltr559 import BreakoutLTR559 | |
| from pimoroni_i2c import PimoroniI2C | |
| temp_adjustment = 7 | |
| altitude = 17 # meteres | |
| your_name = "" | |
| # InfluxDB | |
| post_data = "greenhouse,sensor=enviropicow " # Space is important | |
| header_data = {'Authorization': 'Token <token>',} | |
| bucket = "" | |
| org = "" | |
| token = "" | |
| url = "https://europe-west1-1.gcp.cloud2.influxdata.com/api/v2/write?bucket="+bucket+"&org="+org+"&precision=s" # change depending on your influx url | |
| def write_influxdb_point(temp_value, press_value, humid_value, lux_value): | |
| try: | |
| data = post_data+"temperature="+ str(temp_value)+",pressure="+ str(press_value)+",humidity="+ str(humid_value)+",light="+str(lux_value) | |
| response = requests.post(url, headers=header_data, data = data) | |
| response.close() | |
| except: | |
| return | |
| # Networking | |
| global wlan | |
| SSID = "" | |
| PASSWORD = "" | |
| def wifi_init(): | |
| global wlan | |
| wlan = network.WLAN(network.STA_IF) | |
| wlan.active(True) | |
| wlan.connect(SSID, PASSWORD) | |
| wifi_wait() | |
| def wifi_wait(): | |
| screen_init() | |
| screen_wifi_init() | |
| global wlan | |
| while True: | |
| if wlan.status() < 0 or wlan.status() >= 3: | |
| break | |
| print('waiting for connection...') | |
| time.sleep(1) | |
| # Handle connection error | |
| if wlan.status() != 3: | |
| screen_wifi_failed() | |
| raise RuntimeError('network connection failed') | |
| else: | |
| screen_init() | |
| print('connected') | |
| status = wlan.ifconfig() | |
| print( 'ip = ' + status[0] ) | |
| def wifi_check(): | |
| global wlan | |
| if wlan.status() < 0 or wlan.status() >= 3: | |
| return | |
| else: | |
| wifi_init() | |
| # Sensors | |
| PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} | |
| PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} | |
| i2c = PimoroniI2C(**PINS_BREAKOUT_GARDEN) | |
| bmp = BreakoutBME68X(i2c, address=0x77) | |
| ltr = BreakoutLTR559(i2c) | |
| display = PicoGraphics(display=DISPLAY_ENVIRO_PLUS) | |
| display.set_backlight(1.0) | |
| led = RGBLED(6, 7, 10, invert=True) # setting pins for the RGB led | |
| led.set_rgb(0, 0, 0) | |
| # setup background | |
| BG = display.create_pen(0, 0, 0) | |
| TEXT = display.create_pen(255, 255, 255) | |
| TEMP = display.create_pen(255, 0, 0) | |
| PRESS = display.create_pen(255, 255, 0) | |
| HUMID = display.create_pen(0, 255, 0) | |
| LUX = display.create_pen(0,0,255) | |
| def screen_wifi_init(): | |
| display.set_pen(TEXT) | |
| display.text("Wifi Connecting...", 5, 10, scale=2) | |
| display.update() | |
| return | |
| def screen_wifi_failed(): | |
| display.set_pen(TEXT) | |
| display.text("Wifi Failed", 5, 10, scale=2) | |
| display.update() | |
| return | |
| def screen_init(): | |
| display.set_pen(BG) | |
| display.clear() | |
| def screen_graph_draw(temp_value, press_value, humid_value,lux_value): | |
| display.set_pen(BG) | |
| display.clear() | |
| display.set_pen(TEMP) | |
| display.text("TEMP: {:0.2f}c".format(temp_value), 5, 60, scale=2) | |
| display.set_pen(PRESS) | |
| display.text("PRESS: {:0.2f}Pa".format(press_value), 5, 100, scale=2) | |
| display.set_pen(HUMID) | |
| display.text("HUMID: {:0.2f}%".format(humid_value), 5, 150, scale=2) | |
| display.set_pen(LUX) | |
| display.text("LUX: {:0.2f} lux".format(lux_value), 5, 200, scale=2) | |
| display.set_pen(TEXT) | |
| display.text(your_name+"'s GreenHouse", 5, 10, scale=2) | |
| display.text(wlan.config('ssid')+": "+ str(wlan.config('txpower'))+" dBm", 5, 30, scale=2) | |
| display.update() | |
| def adjust_to_sea_pressure(pressure_hpa, temperature, altitude): | |
| # Adjusted-to-the-sea barometric pressure | |
| adjusted_hpa = pressure_hpa + ((pressure_hpa * 9.80665 * altitude) / (287 * (273 + temperature + (altitude / 400)))) | |
| return adjusted_hpa | |
| def main(): | |
| wifi_init() | |
| screen_init() | |
| while True: | |
| temperature, pressure, humidity, gas, status, _, _ = bmp.read() | |
| # correct temperature and humidity using an offset | |
| corrected_temperature = temperature - temp_adjustment | |
| dewpoint = temperature - ((100 - humidity) / 5) | |
| corrected_humidity = 100 - (5 * (corrected_temperature - dewpoint)) | |
| # convert pressure into hpa | |
| pressure_hpa = pressure / 100 | |
| # correct pressure | |
| pressure_hpa = adjust_to_sea_pressure(pressure_hpa, corrected_temperature, altitude) | |
| reading = ltr.get_reading() | |
| if reading is not None: | |
| lux = reading[BreakoutLTR559.LUX] | |
| print(wlan.config('txpower')) | |
| screen_graph_draw(corrected_temperature, pressure_hpa, corrected_humidity, lux) | |
| heater = "Stable" if status & STATUS_HEATER_STABLE else "Unstable" | |
| print("{:0.2f}c, {:0.2f}Pa, {:0.2f}%, {:0.2f} Ohms, {:05.02f} Lux, Heater: {}".format(corrected_temperature, pressure_hpa, corrected_humidity, gas, lux, heater)) | |
| if status & STATUS_HEATER_STABLE: | |
| wifi_check() | |
| write_influxdb_point(corrected_temperature, pressure_hpa, corrected_humidity, lux) | |
| time.sleep(5.0) | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment