Python (Flask) and MQTT listening

2024/10/13 21:10:58

I'm currently trying to get my Python (Flask) webserver to display what my MQTT script is doing. The MQTT script, In essence, it's subscribed to a topic and I would really like to categorize the info it gets and display/update it in real time. Something like a simple list displaying various the settings that gets updated regularly.

Setting1 = 9
Setting2 = 2
Setting3 = 5

To begin with, I have a connect to page, so that you can fill-in an IP to which the Python (Flask) should connect to:

@app.route("/")
def my_form():return render_template("connect.html")@app.route("/", methods=["POST"])
def my_form_post():text = request.form["text"]processed_text = text.upper()

To which the connect.html looks like:

<!DOCTYPE html>
<html lang="en">
<body><h2>Fill in the IP in below:</h2><form action="." method="POST"><input type="text" name="text"><input type="submit" name="my-form" value="Connect"></form>
</body>
</html>

The connecting to the MQTT part is where the tricky part comes in and I will require some assistance. I was thinking of just adding the connect underneath but this just doesn't work right, it will just come back with an Internal Server Error.

Any help would really be appreciated!

Thanks!

Answer

I would make a separate service for the MQTT message handling. This service could process the messages received and store them (database, redis, simple inprogram memory) for access.

When a page in your flask app gets hit, you would connect to the service (or its storage) and process/display the information since the last request.

This can be done in the reverse as well where your post information from your flask app to the backend service to send MQTT messages.

https://en.xdnf.cn/q/118032.html

Related Q&A

I dont show the image in Tkinter

The image doesnt show in Tkinter. The same code work in a new window, but in my class it does not. What could be the problem ?import Tkinterroot = Tkinter.Tkclass InterfaceApp(root):def __init__(self,…

Read temperature with MAX31855 Thermocouple Sensor on Windows IoT

I am working on a Raspberry Pi 2 with Windows IoT. I want to connect the Raspberry Pi with a MAX31855 Thermocouple Sensor which I bought on Adafruit. Theres a Python libary available on GitHub to read …

PIP: How to Cascade Requirements Files and Use Private Indexes? [duplicate]

This question already has an answer here:Installing Packages from Multiple Servers from One or More Requirements File(1 answer)Closed 9 years ago.I am trying to deploy a Django app to Heroku where one …

Python error: TypeError: cannot compare a dtyped [float64] array with a scalar of type [bool]

Below is a small sample of my dataframe. In [10]: dfOut[10]:TXN_KEY Send_Agent Pay_Agent Send_Amount Pay_Amount 0 13272184 AWD120279 AEU002152 85.99 85.04 1 13272947 ARA03012…

How to remove grey boundary lines in a map when plotting a netcdf using imshow in matplotlib?

Is it possible to remove the grey boundary lines around the in following map? I am trying to plotting a netcdf using matplotlib.from netCDF4 import Dataset # clarify use of Dataset import matplotlib.p…

function that takes one column value and returns another column value

Apologies, if this is a duplicate please let me know, Ill gladly delete.My dataset: Index Col 1 Col 20 1 4, 5, 6 1 2 7, 8, 9 2 3 10, 11, 12 3 …

Python write value from dictionary based on match in range of columns

From my df showing employees with multiple levels of managers (see prior question here), I want to map rows to a department ID, based on a manager ID that may appear across multiple columns:eid, mid…

Merge two dataframes based on a column

I want to compare name column in two dataframes df1 and df2 , output the matching rows from dataframe df1 and store the result in new dataframe df3. How do i do this in Pandas ? df1place name qty unit…

Python/Pandas - building a new column based in columns comparison

I have this dataframe:df:CNPJ Revenues 2016 Revenues 2015 Revenues 2014 0 01.637.895/0001-32 R$ 12.696.658 NaN R$ 10.848.213 1 02.916.265/0001-60 …

Present blank screen, wait for key press -- how?

lo,I am currently trying to code a simple routine for an experiment we are planning to run. The experiment starts by entering a subject number and creating a bunch of files. I got that part working. Ne…