diff --git a/pico-w/src/decorators.py b/pico-w/src/decorators.py index a1f524f..c11903a 100644 --- a/pico-w/src/decorators.py +++ b/pico-w/src/decorators.py @@ -40,7 +40,7 @@ class Endpoint: return try: body = get_body(request) - if body: + if body is str and body != "": kwargs = dict(**kwargs, body=body) val = function(instance, *args, **kwargs) @@ -71,6 +71,7 @@ def get_body(request: str): return content_type_index = index if (content_type_index + 4) == index: + return item diff --git a/pico-w/src/pin.py b/pico-w/src/pin.py index 58efac0..9ef4b79 100644 --- a/pico-w/src/pin.py +++ b/pico-w/src/pin.py @@ -1,3 +1,4 @@ +import time import json from machine import Pin, ADC from micropython import const @@ -16,15 +17,18 @@ moisture_sensors = [ ADC(Pin(27)), ADC(Pin(26)) ] +moisture_pwr = Pin(22, mode=Pin.OUT) - -min_moisture = const(0) +min_moisture = const(20800) max_moisture = const(65535) def read_moisture_sensors(): try: + moisture_pwr.value(1) + time.sleep_ms(400) data = list(map(read_moisture_sensor, moisture_sensors)) + moisture_pwr.value(0) return { "status": "success", "message": "Successfully read moisture sensors", @@ -36,9 +40,16 @@ def read_moisture_sensors(): def read_moisture_sensor(sensor: ADC): - + reads = 3 + val = 0 + for i in range(reads): + val += sensor.read_u16() + val /= reads # Reads from sensor with the help of this tutorial: https://www.diyprojectslab.com/raspberry-pi-pico-with-moisture-sensor/ - return (max_moisture-sensor.read_u16())*100/(max_moisture-min_moisture) + return { + "precentage": min([(max_moisture-val)*100/(max_moisture-min_moisture), 100]), + "read": val + } def assert_index(array: list, index: int): diff --git a/pico-w/src/server.py b/pico-w/src/server.py index e6c2224..99256ce 100644 --- a/pico-w/src/server.py +++ b/pico-w/src/server.py @@ -25,7 +25,7 @@ def initalize_app(): class Handler(ServerHandler): @Endpoint('/moisture/read', 'GET') - def read_moisture(): + def read_moisture(self): return read_moisture_sensors() @Endpoint('/relay/activate', 'POST')