I have a python script that reads sensor data using pyserial and saves it on a .txt file. I have another python script that reads the data from a text file and puts it on an HTML page using Flask API. The HTML page is on a local network. I want to pass the sensor data from the HTML page to my User Interface code (.js) on another device. How can I make this communication possible? Here is my Python code that uses flask API to put the sensor data on an HTML page
global data
data='0'
@app.route('/')
def index():
with open('Sensor_Data_Storage.txt', 'rb') as File1:
line = File1.readline().decode()
print(line)
return render_template('sensor.html',dist=line)
if __name__ == '__main__':
try:
wsgi.server(eventlet.listen(('192.168.0.110',8000)),app)
except KeyboardInterrupt:
print("Keyboard interrupt received. Exiting.")
finally:
# clean up
File1.close()
Here is my HTML page code (sensor.HTML)
<div class="jumbotron jumbotron-fluid">
<div class="container">
<br>
<br>
<h3 class="Tem">{{dist}}</h3>
<br>
<br>
</div>
</div>
This is my Javascript code (the UI)
const sensorDataUrl = "http://192.168.0.110:8000";
function Sensor1Data() {
const [sensorData, setSensorData] = useState({});
useEffect(() => {
getSensorDataWithFetch();
}, []);
const getSensorDataWithFetch = async () => {
const response = await fetch(sensorDataUrl);
const jsonData = await response.json();
setSensorData(jsonData);
};
return(
<div>
<p> {sensorData.dist} </p>
</div>
);
}
export default Sensor1Data;