mqqt test v4 (with web)
This commit is contained in:
54
aaaSonstiges/micropython/testmicpywithmqtt/main.py
Normal file
54
aaaSonstiges/micropython/testmicpywithmqtt/main.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import urequests
|
||||
import json
|
||||
import socket
|
||||
import network
|
||||
import time
|
||||
import ubinascii
|
||||
from umqtt.simple import MQTTClient
|
||||
import machine
|
||||
host = "lires.de"
|
||||
|
||||
# one time dns resolve, damit der arme ESP und nicht wegkocht.
|
||||
def resolve_ip(hostname):
|
||||
try:
|
||||
print(f"🌐 Resolvieren von '{hostname}' ...")
|
||||
addr_info = socket.getaddrinfo(hostname, 80)
|
||||
ip = addr_info[0][4][0]
|
||||
print(f"🔎 IP-Adresse gefunden: {ip}")
|
||||
return ip
|
||||
except Exception as e:
|
||||
print("❌ Fehler beim Resolvieren:", e)
|
||||
return None
|
||||
|
||||
ip = resolve_ip(host)
|
||||
if ip is None:
|
||||
print("❌ Fehler: IP-Adresse konnte nicht aufgelöst werden.")
|
||||
raise SystemExit(1)
|
||||
MQTT_BROKER = ip
|
||||
MQTT_TOPIC = b'iot/testdata'
|
||||
|
||||
#callback
|
||||
def mqtt_callback(topic, msg):
|
||||
print("Neue Nachricht:", msg.decode())
|
||||
|
||||
#send data to the server on the given url
|
||||
def main():
|
||||
client_id = ubinascii.hexlify(machine.unique_id())
|
||||
client = MQTTClient(client_id, MQTT_BROKER)
|
||||
client.set_callback(mqtt_callback)
|
||||
client.connect()
|
||||
client.subscribe(MQTT_TOPIC)
|
||||
print("Warte auf Nachrichten...")
|
||||
|
||||
try:
|
||||
while True:
|
||||
client.wait_msg() # blockiert, bis Nachricht ankommt
|
||||
finally:
|
||||
client.disconnect()
|
||||
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,10 +1,30 @@
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
import routes.shared as shared
|
||||
from flask import Flask, jsonify, request
|
||||
import paho.mqtt.client as mqtt
|
||||
esp = Blueprint('eps', __name__, url_prefix='/unsecure/esp')
|
||||
|
||||
MQTT_BROKER = "localhost" # oder IP/Domain
|
||||
MQTT_PORT = 1883
|
||||
MQTT_TOPIC = "iot/machine"
|
||||
|
||||
@esp.route('/')
|
||||
def fetch_command():
|
||||
pCd = shared.pending_command
|
||||
shared.reset_command()
|
||||
return jsonify(pCd)
|
||||
|
||||
@esp.route('/toggle-machine', methods=['POST'])
|
||||
def toggle_machine():
|
||||
pCd = shared.pending_command
|
||||
pCd['command'] = 'machineToggle'
|
||||
pCd['command-URL'] = 'NO_URL'
|
||||
pCd['command-expected'] = 'machineStatusResponse'
|
||||
pCd['command-expected-URL'] = 'http://lires.de/unsecure/esp/machine-status'
|
||||
|
||||
client = mqtt.Client()
|
||||
client.connect(MQTT_BROKER, MQTT_PORT, 60)
|
||||
client.publish(MQTT_TOPIC, pCd)
|
||||
client.disconnect()
|
||||
|
||||
return jsonify({"status": pCd})
|
||||
@@ -23,16 +23,16 @@
|
||||
<div class="info">ESP-Conn Infos</div>
|
||||
<div class="action-button"> MAKE ME A COFFEE...</div>
|
||||
<div class="button-grid">
|
||||
<div class="grid-button">
|
||||
<div class="top-left-text">Maschiene</div>
|
||||
<div class="center-number">AUS</div>
|
||||
<div class="grid-button" onclick="toggleMachine()">
|
||||
<div class="top-left-text">Maschine</div>
|
||||
<div class="center-number" id="machine-status">AUS</div>
|
||||
</div>
|
||||
<div class="grid-button">
|
||||
<div class="top-left-text">Maschiene</div>
|
||||
<div class="center-number">Nicht Bereit</div>
|
||||
</div>
|
||||
<div class="grid-button">
|
||||
<div class="top-left-text">irgendwas kommt hier nocht</div>
|
||||
<div class="top-left-text">irgendwas kommt hier noch</div>
|
||||
<div class="center-number">XXX</div>
|
||||
</div>
|
||||
<div class="grid-button">
|
||||
@@ -75,6 +75,15 @@
|
||||
</section>
|
||||
|
||||
</main>
|
||||
<script>
|
||||
function toggleMachine() {
|
||||
fetch('/unsecure/esp/toggle-machine', { method: 'POST' })
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
document.getElementById("machine-status").innerText = data.status;
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user