Python Forum
live updated json file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: live updated json file (/thread-29191.html)



live updated json file - Nyanpasu - Aug-22-2020

Dear Python forum!

I've read quite a bit on how to extract data into a json file but the majority of the documentations found online will only save static data or data you yourself have added by hand.

I have decided to complicate my life a bit and use python to do my project since I had a raspberry pi lying around with a bluetooth I thought might as well....let me explain.

I'm using my Arduino to gather temperature data and via bluetooth send it to my pi.

#! /usr/bin/python
import serial
import datetime
ser = serial.Serial('/dev/rfcomm0' , 9600)
print(ser.name)
print("Connected")



while True:

    now = datetime.datetime.now()
    ser_bytes = ser.readline()
    print (now.strftime("%Y-%m-%d %H:%M:%S, ")), ser_bytes
Eventually I'd like to load this into a database and make a plot that is visible on a website where the x is the time and y is the temperature.

In order to achieve this first I have to somehow save whatever numbers I'm getting in the while function. My initial idea was to just save it as a txt file like this:

nohup python -u temp.py &
This would work but eventually the file would get too big and probably not the nicest way to solve this problem, so I have to somehow limit the number of lines allowed in the database.

After a bit of research I found out saving it to CSV or JSON would be a lot more desirable but no documentation I've read so far uses the variables as inputs into the JSON or CSV file. I've done quite a few attempts but I was unsuccessful.

As I am new to this and got nobody else to ask I'd appreciate any help.

Sorry for an entry level question and thanks if you have read through it.

Sincerely,
Nyanpasu


RE: live updated json file - ndc85430 - Aug-22-2020

It would help to see how you're attempting to write the values into whatever file. It really shouldn't be that hard.


RE: live updated json file - Nyanpasu - Aug-22-2020

Managed to use lists instead and solve the issue. Now I can work with the data.