mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
feat: moisture and relay support
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
from typing import Sized
|
import json
|
||||||
from machine import Pin, ADC
|
from machine import Pin, ADC
|
||||||
from micropython import const
|
from micropython import const
|
||||||
|
|
||||||
@@ -11,11 +11,11 @@ relays = [
|
|||||||
[Pin(2, mode=Pin.OUT), False]
|
[Pin(2, mode=Pin.OUT), False]
|
||||||
]
|
]
|
||||||
|
|
||||||
moisture_sensors = const([
|
moisture_sensors = [
|
||||||
ADC(Pin(28)),
|
ADC(Pin(28)),
|
||||||
ADC(Pin(27)),
|
ADC(Pin(27)),
|
||||||
ADC(Pin(26))
|
ADC(Pin(26))
|
||||||
])
|
]
|
||||||
|
|
||||||
|
|
||||||
min_moisture = const(0)
|
min_moisture = const(0)
|
||||||
@@ -24,7 +24,7 @@ max_moisture = const(65535)
|
|||||||
|
|
||||||
def read_moisture_sensors():
|
def read_moisture_sensors():
|
||||||
try:
|
try:
|
||||||
data = map(read_moisture_sensor, moisture_sensors)
|
data = list(map(read_moisture_sensor, moisture_sensors))
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": "Successfully read moisture sensors",
|
"message": "Successfully read moisture sensors",
|
||||||
@@ -41,7 +41,7 @@ def read_moisture_sensor(sensor: ADC):
|
|||||||
return (max_moisture-sensor.read_u16())*100/(max_moisture-min_moisture)
|
return (max_moisture-sensor.read_u16())*100/(max_moisture-min_moisture)
|
||||||
|
|
||||||
|
|
||||||
def assert_index(array: Sized, index: int):
|
def assert_index(array: list, index: int):
|
||||||
return not (len(array) >= index or index < 0)
|
return not (len(array) >= index or index < 0)
|
||||||
|
|
||||||
|
|
||||||
@@ -65,4 +65,4 @@ def try_switch_relay(relay_index: int, value: bool):
|
|||||||
# Then set the pin
|
# Then set the pin
|
||||||
relays[index][0].value()
|
relays[index][0].value()
|
||||||
|
|
||||||
return {"status": "success", "message": "Successfully turned on relay", "data": map(lambda x: x[1], relays)}
|
return {"status": "success", "message": "Successfully turned on relay", "data": list(map(lambda x: x[1], relays))}
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import socket
|
import socket
|
||||||
from config import led
|
from config import led
|
||||||
from decorators import Endpoint, ServerHandler
|
from decorators import Endpoint, ServerHandler
|
||||||
|
from pin import read_moisture_sensors, try_switch_relay
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
def initalize_app():
|
def initalize_app():
|
||||||
@@ -13,19 +15,25 @@ def initalize_app():
|
|||||||
print('listening on', addr)
|
print('listening on', addr)
|
||||||
|
|
||||||
# Listen for connections
|
# Listen for connections
|
||||||
Handler(s)
|
try:
|
||||||
|
Handler(s)
|
||||||
|
except any as e:
|
||||||
|
print("Closing server because of crash", e)
|
||||||
|
s.close()
|
||||||
|
|
||||||
|
|
||||||
class Handler(ServerHandler):
|
class Handler(ServerHandler):
|
||||||
|
|
||||||
@Endpoint('/light/on', 'POST')
|
@Endpoint('/moisture/read', 'GET')
|
||||||
def light_on(self):
|
def read_moisture():
|
||||||
print("led on")
|
return read_moisture_sensors()
|
||||||
led.value(1)
|
|
||||||
return {"led": "on"}
|
|
||||||
|
|
||||||
@Endpoint('/light/off', 'POST')
|
@Endpoint('/relay/activate', 'POST')
|
||||||
def light_off(self):
|
def activate_relay(self, **kwargs):
|
||||||
print("led off")
|
body: dict = json.loads(str(kwargs.get('body')))
|
||||||
led.value(0)
|
return try_switch_relay(int(body.get('relay')), True)
|
||||||
return {"led": "off"}
|
|
||||||
|
@Endpoint('/relay/deactivate', 'POST')
|
||||||
|
def deactivate_relay(self, **kwargs):
|
||||||
|
body: dict = json.loads(str(kwargs.get('body')))
|
||||||
|
return try_switch_relay(int(body.get('relay')), False)
|
||||||
|
|||||||
Reference in New Issue
Block a user