ich habe mal auf objektorientierung umschreiben lassen :)

This commit is contained in:
yaba-source
2025-05-16 06:40:15 +00:00
parent d108398603
commit 4618b3bb69

View File

@@ -5,34 +5,90 @@ import json
import time import time
import urequests import urequests
# MQTT-Konfiguration class CoffeeMachine:
MQTT_BROKER = "lires.de" def __init__(self):
MQTT_PORT = 1883
MQTT_CLIENT_ID = "esp8266_coffee"
MQTT_TOPIC_STATUS = b"coffee/status"
MQTT_TOPIC_COMMAND = b"coffee/command"
MQTT_TOPIC_RETURN = b"coffee/return"
# Other Constants
SERVER_URL = "http://lires.de/unsecure/esp/online"
# --- Eingänge --- # --- Eingänge ---
an = Pin(5, Pin.IN, Pin.PULL_UP) self.an = Pin(5, Pin.IN, Pin.PULL_UP)
bereit = Pin(4, Pin.IN, Pin.PULL_UP) self.bereit = Pin(4, Pin.IN, Pin.PULL_UP)
fehler = Pin(14, Pin.IN, Pin.PULL_UP) self.fehler = Pin(14, Pin.IN, Pin.PULL_UP)
bohnen_voll = Pin(12, Pin.IN, Pin.PULL_UP) self.bohnen_voll = Pin(12, Pin.IN, Pin.PULL_UP)
wasser_voll = Pin(13, Pin.IN, Pin.PULL_UP) self.wasser_voll = Pin(13, Pin.IN, Pin.PULL_UP)
# --- Ausgänge --- # --- Ausgänge ---
toggle_machine = Pin(0, Pin.OUT) self.toggle_machine = Pin(0, Pin.OUT)
starten = Pin(15, Pin.OUT) self.starten = Pin(15, Pin.OUT)
# --- Status --- # --- Status ---
kaffee_machen = 0 self.kaffee_machen = 0
vorbereitung = 0 self.vorbereitung = 0
kaffee_fertig = 0 self.kaffee_fertig = 0
self.gestartet = 0
def mqtt_callback(topic, msg): def get_status(self):
return {
"an": self.an.value(),
"bereit": self.bereit.value(),
"fehler": self.fehler.value(),
"bohnen_voll": self.bohnen_voll.value(),
"wasser_voll": self.wasser_voll.value(),
"einschalten": self.toggle_machine.value(),
"starten": self.starten.value(),
"kaffee_machen": self.kaffee_machen,
"vorbereitung": self.vorbereitung,
"kaffee_fertig": self.kaffee_fertig
}
def make_coffee(self):
if self.kaffee_machen == 1:
self.toggle_machine.value(1)
time.sleep(1)
self.toggle_machine.value(0)
if (self.kaffee_machen == 1 and
self.an.value() == 1 and
self.bereit.value() == 1 and
self.fehler.value() == 0):
self.starten.value(1)
time.sleep(1)
self.starten.value(0)
self.gestartet = 1
else:
self.starten.value(0)
self.gestartet = 0
def update_status(self):
if self.bereit.value() == 0 and self.an.value() == 1 and self.fehler.value() == 0:
self.vorbereitung = 1
if (self.bereit.value() == 1 and
self.an.value() == 1 and
self.fehler.value() == 0 and
self.gestartet == 1):
self.kaffee_fertig = 1
self.gestartet = 0
else:
self.kaffee_fertig = 0
class MQTTHandler:
BROKER = "lires.de"
PORT = 1883
CLIENT_ID = "esp8266_coffee"
TOPIC_STATUS = b"coffee/status"
TOPIC_COMMAND = b"coffee/command"
TOPIC_RETURN = b"coffee/return"
def __init__(self, coffee_machine):
self.coffee_machine = coffee_machine
self.client = None
def connect(self):
self.client = MQTTClient(self.CLIENT_ID, self.BROKER, port=self.PORT)
self.client.set_callback(self._callback)
self.client.connect()
print('MQTT verbunden')
self.client.subscribe(self.TOPIC_COMMAND)
def _callback(self, topic, msg):
print('-------------------------') print('-------------------------')
print('MQTT Nachricht empfangen:') print('MQTT Nachricht empfangen:')
print(f'Topic: {topic.decode()}') print(f'Topic: {topic.decode()}')
@@ -40,129 +96,48 @@ def mqtt_callback(topic, msg):
print('-------------------------') print('-------------------------')
try: try:
command = json.loads(msg.decode()) command = json.loads(msg.decode())
if topic == MQTT_TOPIC_COMMAND: if topic == self.TOPIC_COMMAND:
if command['command']=='toggle_machine': self._handle_command(command)
if starten.value() == 0:
command['status']='served'
print(command)
client.publish(MQTT_TOPIC_RETURN, json.dumps(command))
if command['command']=='make_coffee':
# Kaffe maschine antworten @TODO
print(command)
client.publish(MQTT_TOPIC_RETURN, json.dumps(command))
except Exception as e: except Exception as e:
print('Fehler bei Kommando-Verarbeitung:', e) print('Fehler bei Kommando-Verarbeitung:', e)
def connect_mqtt(): def _handle_command(self, command):
client = MQTTClient(MQTT_CLIENT_ID, MQTT_BROKER, port=MQTT_PORT) if command.get('command') == 'toggle_machine':
client.set_callback(mqtt_callback) if self.coffee_machine.starten.value() == 0:
client.connect() command['status'] = 'served'
print('MQTT verbunden') self.client.publish(self.TOPIC_RETURN, json.dumps(command))
client.subscribe(MQTT_TOPIC_COMMAND)
return client
def send_online_status(): elif command.get('command') == 'make_coffee':
try: self.coffee_machine.kaffee_machen = 1
ip = network.WLAN(network.STA_IF).ifconfig() self.client.publish(self.TOPIC_RETURN, json.dumps(command))
payload = json.dumps({"ip": ip})
headers = {'Content-Type': 'application/json'}
response = urequests.post(SERVER_URL, data=payload, headers=headers)
print("Antwort vom Server:", response.text)
response.close()
except Exception as e:
print("Fehler beim Senden:", e)
def publish_status(self):
if self.client:
status = self.coffee_machine.get_status()
self.client.publish(self.TOPIC_STATUS, json.dumps(status))
def check_messages(self):
if self.client:
self.client.check_msg()
# Objekte erstellen
coffee_machine = CoffeeMachine()
mqtt_handler = MQTTHandler(coffee_machine)
# MQTT-Verbindung herstellen # MQTT-Verbindung herstellen
try: try:
client = connect_mqtt() mqtt_handler.connect()
except Exception as e: except Exception as e:
print('MQTT Verbindungsfehler:', e) print('MQTT Verbindungsfehler:', e)
client = None
send_online_status()
# Hauptschleife # Hauptschleife
while True: while True:
try: try:
if client: mqtt_handler.check_messages()
client.check_msg() # Prüfe auf neue MQTT-Nachrichten mqtt_handler.publish_status()
coffee_machine.make_coffee()
# Status senden coffee_machine.update_status()
status = { time.sleep(5)
# ---IOs---
"an": an.value(),
"bereit": bereit.value(),
"fehler": fehler.value(),
"bohnen_voll": bohnen_voll.value(),
"Wasser_voll": wasser_voll.value(),
"einschalten": toggle_machine.value(),
"starten": starten.value(),
# ---komunikation---
"kaffee_machen": kaffee_machen,
"vorbereitung": vorbereitung,
"kaffee_fertig": kaffee_fertig,
}
client.publish(MQTT_TOPIC_STATUS, json.dumps(status))
else:
# Versuche Neuverbindung
try:
client = connect_mqtt()
except:
pass
time.sleep(5) # Warte 5 Sekunden zwischen den Status-Updates
except Exception as e: except Exception as e:
print('Fehler in Hauptschleife:', e) print('Fehler in Hauptschleife:', e)
time.sleep(5) time.sleep(5)
client = None
# Einschalten der Kaffeemaschine
if kaffee_machen == 1:
toggle_machine(1)
time.sleep(1)
toggle_machine(0)
# Starten der Kaffeemaschine
if kaffee_machen == 1 and an() == 1 and bereit() == 1 and fehler() == 0:
starten(1)
time.sleep(1)
starten(0)
gestartet = 1
else:
starten(0)
gestartet = 0
#Vorbereitung der Kaffeemaschine
if bereit == 0 and an==1 and fehler==0 :
vorbereitung=1
# Vorbereitung der Kaffeemaschine
if bereit() == 0 and an() == 1 and fehler() == 0:
vorbereitung = 1
# Kaffeemaschine fertig
if bereit() == 1 and an() == 1 and fehler() == 0 and gestartet == 1:
kaffee_fertig=1
gestartet = 0
else:
kaffee_fertig=0
# Fehlerbehandlung
if fehler() == 1:
fehler(1)
else:
fehler(0)
if bohnen_voll() == 1:
bohnen_voll(1)
if wasser_voll() == 1:
wasser_voll(1)