I have deployed a datalogger for condition monitoring using Arduino microcontroller. This is further connected to Raspberry Pi to use the data for visualization. So far I have been able to collect data to an SQLite database.
I am planning to put a python script to put last 50 readings to a json file at constant intervals and fetch json from javscript for making a chart.
Is there a better way to accomplish the same?
Asked
Active
Viewed 104 times
0

Sumeet Patel
- 23
- 4
-
You can build a small flask application like this one: https://pythonspot.com/flask-and-great-looking-charts-using-chart-js/ Inside of this application you can directly connect to your database and get the last 50 entries. So you don't have to deal with an additional json file. – SerAlejo Mar 20 '19 at 20:33
-
Thanks. That is pretty sufficient for the job. – Sumeet Patel Mar 20 '19 at 20:42
-
The only thing I can't figure out is getting data updated. As far as I know, I need to put function inside setInterval method to fetch data. I am blank on how the function will be in JavaScript and on flask. – Sumeet Patel Mar 20 '19 at 21:00
-
Which setInterval method do you mean exactly? If your data gets automatically inserted into the database you can just ouput the last 50 entries. Depending on your database design you wold do something like sorting your selection descending and limiting your output to 50. That should always display the newest data. You then just need to reload the webpage to get the updated data. – SerAlejo Mar 20 '19 at 21:22
-
I wanted to make the chart update automatically without the user reloading. For that I meant to use setInterval in JavaScript of the page which could run some function to update the data. – Sumeet Patel Mar 20 '19 at 21:34
-
You can simply do that by adding a refresh inside the html header. With flask you can do that by adding ' http-equiv="refresh" content="5" ' inside your meta tag, like this answer explains: https://stackoverflow.com/questions/45666664/how-to-refresh-the-flask-web-page Edit: But this will reload the page (without user interaction). If wan't to totally avoid any reloading, you would need to write a dynamic web application. – SerAlejo Mar 20 '19 at 21:41
-
@SerAlejo Thank you. Chart.js and flask application really worked out. Dealing with json was little tricky, there I understood there's json array and json object which needs to be handled differently. But overall the solution worked out well. However, I'm hesitant to refresh html as it will have total of 6 charts and refreshing repeatedly is a lot of work to raspberry pi, therefore, I intend to use jQuery to push new data. – Sumeet Patel Mar 24 '19 at 03:43