mirror of
https://gitlab.fdmci.hva.nl/technische-informatica-sm3/ti-projectten/rooziinuubii79.git
synced 2025-08-04 04:14:58 +00:00
receive images from mqtt server and display on endpoint
This commit is contained in:
@@ -1,18 +1,24 @@
|
|||||||
from flask import Flask, request, render_template, jsonify
|
from flask import Flask, Response, request, render_template, jsonify
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
def on_message(client, userdata, message):
|
||||||
global kobuki_message #set scope for this variable
|
global kobuki_message, latest_image
|
||||||
kobuki_message = str(message.payload.decode("utf-8"))
|
if message.topic == "kobuki/data":
|
||||||
|
kobuki_message = str(message.payload.decode("utf-8"))
|
||||||
|
elif message.topic == "kobuki/cam":
|
||||||
|
latest_image = message.payload
|
||||||
|
|
||||||
|
|
||||||
# Create an MQTT client instance
|
# Create an MQTT client instance
|
||||||
mqtt_client = mqtt.Client()
|
mqtt_client = mqtt.Client()
|
||||||
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
mqtt_client.username_pw_set("server", "serverwachtwoordofzo")
|
||||||
mqtt_client.connect("145.92.224.21/ws", 80, 60)
|
mqtt_client.connect("localhost", 1884, 60)
|
||||||
mqtt_client.loop_start()
|
mqtt_client.loop_start()
|
||||||
mqtt_client.subscribe("kobuki/data")
|
mqtt_client.subscribe("kobuki/data")
|
||||||
|
mqtt_client.subscribe("kobuki/cam")
|
||||||
|
|
||||||
mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use
|
mqtt_client.on_message = on_message # this lines needs to be under the function definition otherwise it cant find which function it needs to use
|
||||||
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
@@ -43,6 +49,13 @@ def move():
|
|||||||
def data():
|
def data():
|
||||||
return kobuki_message
|
return kobuki_message
|
||||||
|
|
||||||
|
@app.route('/image')
|
||||||
|
def image():
|
||||||
|
global latest_image
|
||||||
|
if latest_image is not None:
|
||||||
|
return Response(latest_image, mimetype='image/jpeg')
|
||||||
|
else:
|
||||||
|
return "No image available", 404
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user