From 1f6d92a54cac5dffde60173876604b149258326c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maik=20Mu=CC=88ller?= Date: Sat, 4 May 2024 15:38:36 +0200 Subject: [PATCH] add setup --- device_info.py | 7 +++ engine.py | 23 +++++++++ grow_system.py | 20 +++++++- grow_system_api.py | 2 +- pump.py | 7 +++ setup.py | 113 +++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 engine.py create mode 100644 pump.py create mode 100644 setup.py diff --git a/device_info.py b/device_info.py index 65ab51a..a61681e 100644 --- a/device_info.py +++ b/device_info.py @@ -32,6 +32,13 @@ class DeviceInfo: # } ] + engines = [ + { + 'type': 'pump', + 'pins': [15] + } + ] + read_secs = 5 # Device Infos End diff --git a/engine.py b/engine.py new file mode 100644 index 0000000..38bf29c --- /dev/null +++ b/engine.py @@ -0,0 +1,23 @@ +from machine import Pin + + +class Engine(): + + pins = [] + + engine = None + + def __init__(self, engine): + print("Hello from Engine parent class") + print(engine) + self.pins = engine['pins'] + self.engine = Pin(self.pins[0], Pin.OUT) + + def on(self): + print("engine on") + self.engine.value(1) + + def off(self): + print("engine off") + self.engine.value(0) + diff --git a/grow_system.py b/grow_system.py index 3528ea9..3861c5a 100644 --- a/grow_system.py +++ b/grow_system.py @@ -2,6 +2,7 @@ import time from moisture_sensor import MoistureSensor from dht22 import TemperatureHumiditySensor from ambilight_sensor import AmbilightSensor +from pump import Pump from sensor_data_manager import SensorDataManager from grow_system_api import GrowSystemApi from device_info import DeviceInfo @@ -15,6 +16,8 @@ class GrowSystem: # temperature_humidity_sensor = None sensors = [] + + engines = [] most_recent_values = [] @@ -24,7 +27,8 @@ class GrowSystem: device_info = DeviceInfo() - def __init__(self, settings): + def __init__(self, settings): + # Init sensors for sensor in self.device_info.sensors: print("") print("Initialize sensor:") @@ -41,6 +45,20 @@ class GrowSystem: self.sensors.append(AmbilightSensor(sensor)) else: print("No sensor type configured for: " + sensor['type']) + + # Init engines + for engine in self.device_info.engines: + print("") + print("Initialize engine:") + print(engine) + engine_type = engine['type'] + if engine_type == 'pump': + print("Found egine of type pump") + self.engines.append(Pump(engine)) + self.engines[0].on() + time.sleep(15) + self.engines[0].off() + #if not self.moisture_sensor: # self.moisture_sensor = MoistureSensor(settings['moisture_sensor']) diff --git a/grow_system_api.py b/grow_system_api.py index 5b9ccb3..0e3376c 100644 --- a/grow_system_api.py +++ b/grow_system_api.py @@ -15,7 +15,7 @@ class GrowSystemApi: self.base_url = self.device_info.server_url def say_hello(self): - response = self.http_client.post(self.base_url + "/api/device/hello", self._get_device_data()) + response = self.http_client.post(self.base_url + "/api/device", self._get_device_data()) print(response.text) jsonResult = json.loads(response.text) print(jsonResult) diff --git a/pump.py b/pump.py new file mode 100644 index 0000000..fc39e6d --- /dev/null +++ b/pump.py @@ -0,0 +1,7 @@ +from engine import Engine + + +class Pump(Engine): + + def __init__(self, engine): + super().__init__(engine) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..2aa6cee --- /dev/null +++ b/setup.py @@ -0,0 +1,113 @@ +import network +import ujson +import ure as re +import usocket as socket +import time + +class Setup: + def __init__(self): + self.ap_ssid = "Growsystem" + self.ap_password = "12345678" + self.wlans = [] + self.selected_ssid = "" + self.wlan_password = "" + self.pin = "" + self.html = """ + + Pico Setup + +

Setup Pico

+
+ SSID:
+ Password:
+ PIN:
+ +
+ + + """ + + def scan_wlans(self): + print("Scan WLANs") + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + self.wlans = [w[0].decode() for w in wlan.scan()] + print("Found:", self.wlans) + wlan.active(False) + + def start_ap_mode(self): + self.ap = network.WLAN(network.AP_IF) + self.ap.active(True) + while self.ap.active() == False: + pass + self.ap.config(essid=self.ap_ssid, password=self.ap_password) + print("AP SSID:", self.ap.config("essid")) + # print("AP Password:", self.ap.config("password")) + + def stop_ap_mode(self): + ap = network.WLAN(network.AP_IF) + ap.active(False) + + def start_webserver(self): + addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] + s = socket.socket() + s.bind(addr) + s.listen(1) + print("Listening on", addr) + while True: + conn, addr = s.accept() + print("Got a connection from", addr) + request = conn.recv(1024) + print("Content =", request) + conn.send("HTTP/1.1 200 OK\n") + conn.send("Content-Type: text/html\n") + conn.send("Connection: close\n\n") + conn.sendall(self.html.format(''.join([''.format(w, w) for w in self.wlans]))) + conn.close() + + def parse_request(self, request): + request = request.decode() + ssid_match = re.search("ssid=([^&]+)", request) + if ssid_match: + self.selected_ssid = ssid_match.group(1) + password_match = re.search("password=([^&]+)", request) + if password_match: + self.wlan_password = password_match.group(1) + pin_match = re.search("pin=([^&]+)", request) + if pin_match: + self.pin = pin_match.group(1) + + def save_config(self): + config = { + "ssid": self.selected_ssid, + "password": self.wlan_password, + "pin": self.pin + } + with open("initial_config.py", "w") as f: + f.write("config = " + ujson.dumps(config)) + + def switch_to_client_mode(self): + ap = network.WLAN(network.AP_IF) + ap.active(False) + wlan = network.WLAN(network.STA_IF) + wlan.active(True) + wlan.connect(self.selected_ssid, self.wlan_password) + while not wlan.isconnected(): + time.sleep(1) + print("Connected to", self.selected_ssid) + + def setup_pico(self): + print("Start PICO setup") + self.scan_wlans() + self.start_ap_mode() + self.start_webserver() + while True: + conn, addr = self.s.accept() + request = conn.recv(1024) + self.parse_request(request) + self.save_config() + conn.close() + break # Assuming only one request is handled + self.stop_ap_mode() + self.switch_to_client_mode() +