main #3
|
|
@ -32,6 +32,13 @@ class DeviceInfo:
|
|||
# }
|
||||
]
|
||||
|
||||
engines = [
|
||||
{
|
||||
'type': 'pump',
|
||||
'pins': [15]
|
||||
}
|
||||
]
|
||||
|
||||
read_secs = 5
|
||||
# Device Infos End
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
@ -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
|
||||
|
|
@ -16,6 +17,8 @@ class GrowSystem:
|
|||
|
||||
sensors = []
|
||||
|
||||
engines = []
|
||||
|
||||
most_recent_values = []
|
||||
|
||||
sensor_data_manager = None
|
||||
|
|
@ -25,6 +28,7 @@ class GrowSystem:
|
|||
device_info = DeviceInfo()
|
||||
|
||||
def __init__(self, settings):
|
||||
# Init sensors
|
||||
for sensor in self.device_info.sensors:
|
||||
print("")
|
||||
print("Initialize sensor:")
|
||||
|
|
@ -42,6 +46,20 @@ class GrowSystem:
|
|||
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'])
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,7 @@
|
|||
from engine import Engine
|
||||
|
||||
|
||||
class Pump(Engine):
|
||||
|
||||
def __init__(self, engine):
|
||||
super().__init__(engine)
|
||||
|
|
@ -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 = """
|
||||
<html>
|
||||
<head><title>Pico Setup</title></head>
|
||||
<body>
|
||||
<h1>Setup Pico</h1>
|
||||
<form method="post">
|
||||
SSID: <select name="ssid">{}</select><br>
|
||||
Password: <input type="password" name="password"><br>
|
||||
PIN: <input type="text" name="pin" maxlength="4"><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
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(['<option value="{}">{}</option>'.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()
|
||||
|
||||
Loading…
Reference in New Issue