import time from machine import Pin from machine import ADC from net import logging from net import ntp from net import Server from net import templates from net import util from net.config import config class Marshaller(Server): def __init__(self): self.switch = Pin(8, Pin.IN, Pin.PULL_UP) self.red_led = Pin(11, Pin.OUT) self.blue_led = Pin(13, Pin.OUT) self.green_led = Pin(20, Pin.OUT) self.yellow_led = Pin(18, Pin.OUT) self.v = Pin(27, Pin.OUT) self.a = ADC(2) self.last_value = -1 self.v.on() super().__init__() self.last_http_activation_ticks = time.ticks_ms() self.http_activation_interval_in_seconds = 2 * 60 self.is_http_activation = False self.ntp_interval_in_seconds = 3 * 60 * 60 self.ntp_ticks = time.ticks_ms() self.distance_interval_in_milliseconds = 50 self.distance_ticks = time.ticks_ms() self.isWaterPresent = False ntp.sync() def work(self): ticks = time.ticks_ms() if self.is_scanning(): self.v.on() if util.millisecondsElapsed(ticks, self.distance_ticks) > self.distance_interval_in_milliseconds: self.distance_ticks = ticks self.show_color() if self.is_http_activation and util.secondsElapsed(ticks, self.last_http_activation_ticks) > self.http_activation_interval_in_seconds: self.is_http_activation = False else: self.v.off() self.leds_off() super().work() if util.secondsElapsed(ticks, self.ntp_ticks) > self.ntp_interval_in_seconds: self.ntp_ticks = ticks ntp.sync() def is_scanning(self): return self.switch.value() == 0 or self.is_http_activation def get_buffered_distance_in_inches(self): distance = self.get_distance_in_inches() if abs(distance - self.last_value) > 0.25: self.last_value = distance return distance return self.last_value # TODO - don't convert distances to inches, convert thresholds in the opposite direction def get_distance_in_inches(self): return self.a.read_u16() / 65535 * 1024 * 5 * 0.03937008 def handlePath(self, path): if (path == 'on'): return self.http_activation() return self.getPathData(path) def getPathData(self, path): if path.endswith('.txt') or path.endswith('.ico'): with open(f'www/{path}', 'rb') as f: return f.read() return templates.render( f'www/{path or self.default_path}', hostname=config['hostname'], datetime=util.datetime(), is_active=self.is_scanning() ) def http_activation(self): self.is_http_activation = True self.last_http_activation_ticks = time.ticks_ms() # todo - no content status return "" def show_color(self): distance_in_inches = self.get_buffered_distance_in_inches() if distance_in_inches < 15: self.leds_off() self.red_led.on() elif distance_in_inches < 20: self.leds_off() self.green_led.on() self.yellow_led.on() elif distance_in_inches < 25: self.leds_off() self.green_led.on() elif distance_in_inches < 40: self.leds_off() self.yellow_led.on() else: self.leds_off() self.blue_led.on() def leds_off(self): self.red_led.off() self.blue_led.off() self.green_led.off() self.yellow_led.off() def cleanup(self): self.v.off() self.leds_off() super().cleanup() def main(): logging.log_file = 'www/log.txt' Marshaller().run() if __name__ == '__main__': main()