mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 04:14:58 +00:00
wrote message and connect function for the mqtt connection to pi
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -16,4 +16,5 @@ src/C++/Driver/Makefile
|
|||||||
src/C++/Driver/vgcore*
|
src/C++/Driver/vgcore*
|
||||||
src/C++/Driver/cmake_install.cmake
|
src/C++/Driver/cmake_install.cmake
|
||||||
src/C++/Driver/Makefile
|
src/C++/Driver/Makefile
|
||||||
src/Python/flask/web/_pycache_
|
src/Python/flask/web/_pycache_
|
||||||
|
venv
|
@@ -1,4 +1,5 @@
|
|||||||
from flask import Flask, render_template
|
from flask import Flask, request, render_template
|
||||||
|
import paho.mqtt.publish as publish
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
@@ -6,12 +7,11 @@ app = Flask(__name__)
|
|||||||
def index():
|
def index():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
@app.route('/control', methods=['POST'])
|
@app.route('/move', methods=['POST'])
|
||||||
def control():
|
def move():
|
||||||
return("hello")
|
direction = request.form['direction']
|
||||||
|
publish.single("home/commands", direction, hostname="ishak.ishakpi.ddns.net")
|
||||||
|
return "Message sent"
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
||||||
|
|
||||||
|
|
@@ -4,13 +4,15 @@
|
|||||||
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
|
<img src="kobuki.jpg" alt="Kobuki Robot" id="robot-image" />
|
||||||
</div>
|
</div>
|
||||||
<div class="button-section">
|
<div class="button-section">
|
||||||
<button class="btn">←</button>
|
<form action="/move" method="post">
|
||||||
<button class="btn">↑</button>
|
<button class="btn" name="direction" value="left">←</button>
|
||||||
<button class="btn">→</button>
|
<button class="btn" name="direction" value="up">↑</button>
|
||||||
<button class="btn">↓</button>
|
<button class="btn" name="direction" value="right">→</button>
|
||||||
|
<button class="btn" name="direction" value="down">↓</button>
|
||||||
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>Sensor Data</h1>
|
<h1>Sensor Data</h1>
|
||||||
</div>
|
</div>
|
||||||
{%endblock%}
|
{%endblock%}
|
||||||
|
@@ -1,20 +1,17 @@
|
|||||||
import socket
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
HOST = "127.0.0.1" # Listen on all available interfaces
|
def on_connect(client, userdata, flags, rc):
|
||||||
PORT = 4024 # Port to listen on (non-privileged ports are > 1023)
|
print("Connected with result code " + str(rc))
|
||||||
|
client.subscribe("move")
|
||||||
|
|
||||||
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
def on_message(client, userdata, msg):
|
||||||
s.bind((HOST, PORT))
|
command = msg.payload.decode()
|
||||||
s.listen()
|
print(f"Received command: {command}")
|
||||||
print(f"Server listening on {HOST}:{PORT}")
|
# Voeg hier je code toe om het commando uit te voeren
|
||||||
conn, addr = s.accept()
|
|
||||||
with conn:
|
client = mqtt.Client()
|
||||||
print(f"Connected by {addr}")
|
client.on_connect = on_connect
|
||||||
conn.sendall(b"hallo\n")
|
client.on_message = on_message
|
||||||
while True:
|
|
||||||
data = conn.recv(2048)
|
client.connect("ishak.ishakpi.ddns.net", 1883, 60)
|
||||||
if data:
|
client.loop_forever()
|
||||||
print("Received:", repr(data))
|
|
||||||
conn.sendall(b"message received\n")
|
|
||||||
if not data:
|
|
||||||
break
|
|
Reference in New Issue
Block a user