Created
April 20, 2023 10:43
-
-
Save gbit-is/cbf99bc492742143a47cdd39f0111386 to your computer and use it in GitHub Desktop.
grodurhusid
This file contains 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 os | |
import json | |
import time | |
import ipaddress | |
from analogio import AnalogIn | |
import wifi | |
import socketpool | |
import busio | |
import board | |
import microcontroller | |
import displayio | |
import terminalio | |
from digitalio import DigitalInOut, Direction | |
from adafruit_httpserver.server import HTTPServer | |
from adafruit_httpserver.request import HTTPRequest | |
from adafruit_httpserver.response import HTTPResponse | |
from adafruit_httpserver.methods import HTTPMethod | |
from adafruit_httpserver.mime_type import MIMEType | |
import adafruit_dht | |
moisture_sensor_1 = AnalogIn(board.GP28) | |
dht22_1_pin = board.GP16 | |
dht22_sensor_1 = adafruit_dht.DHT22(dht22_1_pin) | |
HTML_REFRESH_RATE = 90 | |
soil_dry = 1.8 | |
soil_wet = 1.1 | |
min_temp = 10 | |
max_temp = 40 | |
min_humi = 20 | |
max_humi = 101 | |
def read_dht(dht_device): | |
color_red = False | |
color_blue = False | |
temp = dht_device.temperature | |
humi = dht_device.humidity | |
if temp > max_temp: | |
temp_status = "Of heitt" | |
color_red = True | |
elif temp < min_temp: | |
temp_status = "Of kalt" | |
color_blue = True | |
else: | |
temp_status = "Góður hiti" | |
if humi > max_humi: | |
humi_status = "Of rakt loft" | |
color_red = True | |
elif humi < min_humi: | |
humi_status = "Of þurrt loft" | |
color_blue = True | |
else: | |
humi_status = "Godur loftraki" | |
if color_red: | |
color = "red" | |
elif color_blue: | |
color = "blue" | |
else: | |
color = "green" | |
return temp,temp_status,humi,humi_status,color | |
def read_moisture_sensor(pin): | |
pin_voltage = (pin.value * 3.3) / 65536 | |
if pin_voltage > soil_dry: | |
soil_status = "Of þurrt" | |
soil_color = "red" | |
elif pin_voltage < soil_wet: | |
soil_status = "Of blautt" | |
soil_color = "blue" | |
else: | |
soil_status = "Góður raki" | |
soil_color = "green" | |
return pin_voltage,soil_status,soil_color | |
# onboard LED setup | |
led = DigitalInOut(board.LED) | |
led.direction = Direction.OUTPUT | |
led.value = False | |
# connect to network | |
print() | |
print("Connecting to WiFi") | |
connect_text = "Connecting..." | |
# set static IP address | |
ipv4 = ipaddress.IPv4Address("192.168.24.25") | |
netmask = ipaddress.IPv4Address("255.255.255.0") | |
gateway = ipaddress.IPv4Address("192.168.24.1") | |
wifi.radio.set_ipv4_address(ipv4=ipv4,netmask=netmask,gateway=gateway) | |
# connect to your SSID | |
wifi.radio.connect(os.getenv('CIRCUITPY_WIFI_SSID'), os.getenv('CIRCUITPY_WIFI_PASSWORD')) | |
print("Connected to WiFi") | |
pool = socketpool.SocketPool(wifi.radio) | |
server = HTTPServer(pool) | |
font_family = "monospace" | |
def webpage(): | |
ms1_value,ms1_status,ms1_color = read_moisture_sensor(moisture_sensor_1) | |
temp_1,temp_1_status,humi_1,humi_1_status,temp_1_color = read_dht(dht22_sensor_1) | |
html = f""" | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="refresh" content="{HTML_REFRESH_RATE}"> | |
<title>GrodurHusid</title> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1"> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script> | |
</head> | |
<body> | |
<div class="jumbotron text-center"> | |
<h1>Gróðurhúsið</h1> | |
</div> | |
<div class="container"> | |
<div class="row"> | |
<div class="col-sm-6" style="background-color:{temp_1_color}"> | |
<h3>Lofthiti / Raki</h3> | |
<p> | |
Hiti í Gróðurhúsi: {temp_1} °C ( {temp_1_status} ) | |
<br> | |
Raki í Gróðurhúsi: {humi_1} % ( {humi_1_status} ) | |
</p> | |
</div> | |
<div class="col-sm-6" style="background-color:{ms1_color}"> | |
<h3>Rakaskynjari 1</h3> | |
<p> | |
Gildi rakaskynjara: {ms1_value} | |
<br> | |
Staða: {ms1_status} | |
</p> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> | |
""" | |
return html | |
# route default static IP | |
@server.route("/") | |
def base(request: HTTPRequest): # pylint: disable=unused-argument | |
# serve the HTML f string | |
# with content type text/html | |
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response: | |
response.send(f"{webpage()}") | |
@server.route("/api") | |
def base(request: HTTPRequest): | |
ms1_value,ms1_status,ms1_color = read_moisture_sensor(moisture_sensor_1) | |
temp_1,temp_1_status,humi_1,humi_1_status,temp_1_color = read_dht(dht22_sensor_1) | |
data = { | |
"soil_sensors" : { | |
"1" : { | |
"moisture_value" : ms1_value | |
} | |
}, | |
"air_sensor" : { | |
"1" : { | |
"tempature" : temp_1, | |
"humidity" : humi_1 | |
} | |
} | |
} | |
jdat = json.dumps(data) | |
with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response: | |
response.send(jdat) | |
print("starting server..") | |
# startup the server | |
try: | |
server.start(str(wifi.radio.ipv4_address)) | |
print("Listening on http://%s:80" % wifi.radio.ipv4_address) | |
# if the server fails to begin, restart the pico w | |
except OSError: | |
time.sleep(5) | |
print("restarting..") | |
microcontroller.reset() | |
ping_address = ipaddress.ip_address("8.8.4.4") | |
# text objects for screen | |
# connected to SSID text | |
ssid_text = "%s" % os.getenv('WIFI_SSID') | |
# display ip address | |
ip_text = "IP: %s" % wifi.radio.ipv4_address | |
# display temp reading | |
clock = time.monotonic() # time.monotonic() holder for server ping | |
parrot = False # parrot state | |
party = 0 # time.monotonic() holder for party parrot | |
p = 0 # index for tilegrid | |
while True: | |
try: | |
if (clock + 30) < time.monotonic(): | |
if wifi.radio.ping(ping_address) is None: | |
print("lost connection") | |
else: | |
print("connected") | |
clock = time.monotonic() | |
server.poll() | |
except Exception as e: | |
print(e) | |
continue |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment