I am trying to make web application which takes temperature and (motor)RPM from sensors coming through my computer which connected serially to a mechanical machine and display it on web page.
I am using Python Flask with AJAX. What I've tried so far now is took jsonify data from back-end and displayed on html page. But I am not getting or seeing any real time data changing on web page without reloading the page. I need to reload the page every time to see data changing.
How can I and what are the possible ways to get this data displayed on web page.
This is my python script for flask app:
from flask import Flask, render_template, request, jsonify
import random
import time
import serial
app = Flask(__name__)
@app.route('/')
def hello_world():
return render_template("index.html")
@app.route('/ret_num', methods = ['POST', 'GET'])
def ret_num():
s = serial.Serial('COM7')
res = s.read()
time.sleep(1)
return jsonify(res)
if __name__ == '__main__':
app.run(debug = True)
And HTML code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<script>
$(function(){
$.ajax({
url: '/ret_num',
type: 'POST',
success: function(response) {
console.log(response);
$("#num").html(response);
},
error: function(error) {
console.log(error);
}
});
});
</script>
<h1>Output</h1>
<h1 id="num"></h1>
</body>
</head>
</html>