Konubinix' opinionated web of thoughts

Plant Moisture Sensor

Fleeting

on the server

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import logging
import os
from datetime import datetime, timedelta

import requests
from quart import Blueprint, request

blueprint = Blueprint("basilic", __name__)
logger = logging.getLogger(__name__)


schedules = [(8, 30), (12, 0), (18, 0), (22, 0)]
threshold = int(os.environ.get("BASILIC_THRESHOLD", "200"))

if os.environ.get("BASILIC_DEBUG") == "true":
    schedules = [
        (hour, minute)
        for hour in range(23)
        for minute in (
            0,
            # 15,
            30,
            # 45,
            # 50,
            # 55
        )
    ]


@blueprint.route("/basilic")
async def record():
    # called with http://home:9911/basilic?value={value}
    value = int(request.args.get("value"))
    priority = "low" if value > threshold else "high"
    requests.post(
        "http://home:9705/basilic",
        headers={
            "title": "mp",
            "priority": priority,
        },
        data=str(value),
    )

    now = datetime.now()
    shifted = now + timedelta(hours=1)  # let's pretend we are actually past that date
    times = [
        now.replace(hour=hour, minute=minute, second=0, microsecond=0)
        for hour, minute in schedules
    ]
    found = None
    for prev, next in zip(times, times[1:]):
        if prev <= shifted < next:
            found = next
            break
    else:
        if now <= times[0]:
            found = times[0]
        else:
            found = times[0] + timedelta(days=1)
    wait_seconds = (found - now).total_seconds() if priority == "low" else 600
    logger.info(f"Waiting for {wait_seconds} (at {found})")
    return str(wait_seconds)

on the MCU

import time
from machine import Pin, ADC

import requests
from comm import wifi

from helpers import deep_sleep, dump_exception, lowpower, runner

lowpower()
wifi.reset()

debug = Pin(5, Pin.IN)
sensor = ADC(0)
# latching = LatchingRelay(
#     4, # d2
#     0, # d3
# )
switch = Pin(1, Pin.OUT)

def wait(waiting_seconds):
    if debug.value():
        time.sleep(waiting_seconds)
    else:
        deep_sleep(waiting_seconds)

@runner(debugpin=debug)
def run():
    try:
        if debug.value():
            wifi.on()
        while True:
            switch.value(1) # on
            time.sleep(2)
            value = sensor.read()
            switch.value(0) # off
            print(f"Uploading the value {value}")
            with wifi:
                waiting_seconds = int(float(requests.get(f"http://home:9911/basilic?value={value}").text))
            print(f"going to sleep for {waiting_seconds}")
            wait(waiting_seconds)
    except Exception as e:
        dump_exception(e, "error2.log")
        waiting_seconds = 3600
        print(f"dumped this error, waiting for {waiting_seconds}s before restarting")
        wait(waiting_seconds)
Fri Nov 28 18:52:10 CET 2025
Checking the current installation
Reading install.json from mpremote
Writing into install.json in mpremote
Writing into error.log in mpremote
Writing into resetdeepsleep in mpremote