mirror of
https://github.com/eliasrenman/gardentron.git
synced 2026-03-16 20:46:07 +01:00
fix: improved moisture sensor
This commit is contained in:
@@ -40,7 +40,7 @@ class Endpoint:
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
body = get_body(request)
|
body = get_body(request)
|
||||||
if body:
|
if body is str and body != "":
|
||||||
kwargs = dict(**kwargs, body=body)
|
kwargs = dict(**kwargs, body=body)
|
||||||
|
|
||||||
val = function(instance, *args, **kwargs)
|
val = function(instance, *args, **kwargs)
|
||||||
@@ -71,6 +71,7 @@ def get_body(request: str):
|
|||||||
return
|
return
|
||||||
content_type_index = index
|
content_type_index = index
|
||||||
if (content_type_index + 4) == index:
|
if (content_type_index + 4) == index:
|
||||||
|
|
||||||
return item
|
return item
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import time
|
||||||
import json
|
import json
|
||||||
from machine import Pin, ADC
|
from machine import Pin, ADC
|
||||||
from micropython import const
|
from micropython import const
|
||||||
@@ -16,15 +17,18 @@ moisture_sensors = [
|
|||||||
ADC(Pin(27)),
|
ADC(Pin(27)),
|
||||||
ADC(Pin(26))
|
ADC(Pin(26))
|
||||||
]
|
]
|
||||||
|
moisture_pwr = Pin(22, mode=Pin.OUT)
|
||||||
|
|
||||||
|
min_moisture = const(20800)
|
||||||
min_moisture = const(0)
|
|
||||||
max_moisture = const(65535)
|
max_moisture = const(65535)
|
||||||
|
|
||||||
|
|
||||||
def read_moisture_sensors():
|
def read_moisture_sensors():
|
||||||
try:
|
try:
|
||||||
|
moisture_pwr.value(1)
|
||||||
|
time.sleep_ms(400)
|
||||||
data = list(map(read_moisture_sensor, moisture_sensors))
|
data = list(map(read_moisture_sensor, moisture_sensors))
|
||||||
|
moisture_pwr.value(0)
|
||||||
return {
|
return {
|
||||||
"status": "success",
|
"status": "success",
|
||||||
"message": "Successfully read moisture sensors",
|
"message": "Successfully read moisture sensors",
|
||||||
@@ -36,9 +40,16 @@ def read_moisture_sensors():
|
|||||||
|
|
||||||
|
|
||||||
def read_moisture_sensor(sensor: ADC):
|
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/
|
# 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):
|
def assert_index(array: list, index: int):
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ def initalize_app():
|
|||||||
class Handler(ServerHandler):
|
class Handler(ServerHandler):
|
||||||
|
|
||||||
@Endpoint('/moisture/read', 'GET')
|
@Endpoint('/moisture/read', 'GET')
|
||||||
def read_moisture():
|
def read_moisture(self):
|
||||||
return read_moisture_sensors()
|
return read_moisture_sensors()
|
||||||
|
|
||||||
@Endpoint('/relay/activate', 'POST')
|
@Endpoint('/relay/activate', 'POST')
|
||||||
|
|||||||
Reference in New Issue
Block a user