I'm new to JavaScript and HTML in general so I may have some silly questions...
I'm using Flask to display a graph with some values from a sensor on a Raspberry pi3. The sensor is connected through a serial port. I want to pass the values I'm reading from the serial port to the index.html, that is the template where I have the javascript code for the graph (made with Rgraph).
This is the main.py with flask and the port reading.
from flask import Flask,render_template
import serial
app = Flask(__name__)
ser = serial.Serial('/dev/ttyACM0', 9600)
@app.route('/')
def hello_world():
return render_template("index.html")
#here I read one value from the serial port
@app.route('/getdata')
def test():
return ser.readline()
if __name__ == '__main__':
app.run()
The index.html template that I'm trying to use is this here (on github). The javascript generates some random number (from line 66 to 72) but I want it to read a number from the page '/getdata' instead and push that in the data array.
I thought about using the GET-POST methods but it gets quite complicated. Maybe there's an easier solution? Or, maybe (but I think it's impossible), I could read the serial port directly from the javascript? Or, maybe, I'm doing everything wrong and I need to throw everything out of the window?