made working endpoint for kobuki data

This commit is contained in:
2024-11-05 19:11:29 +01:00
parent 5f4a7606ce
commit 6326227be6

View File

@@ -3,11 +3,18 @@ import paho.mqtt.client as mqtt
app = Flask(__name__)
def on_message(client, userdata, message):
global kobuki_message #set scope for this variable
kobuki_message = str(message.payload.decode("utf-8"))
# Create an MQTT client instance
mqtt_client = mqtt.Client()
mqtt_client.username_pw_set("ishak", "kobuki")
mqtt_client.connect("localhost", 1883, 60)
mqtt_client.connect("145.92.224.21", 1883, 60)
mqtt_client.loop_start()
mqtt_client.subscribe("kobuki/data")
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('/', methods=["GET","POST"])
def index():
@@ -27,10 +34,10 @@ def move():
@app.route('/data', methods=['GET'])
def data():
data = 0
data = mqtt_client.subscribe("kobuki/data")
if data != 0:
return jsonify({data})
return jsonify({"kobuki_message": kobuki_message})
if __name__ == '__main__':
app.run(debug=True)