below is my code it will write only one value to the json file i need this to write 5150380
below is my code. It will write only one value to the json file. I need this to write a multiple of values over time but I can not get it to write more than one value. I think i am supposed to gather the information first and then dump it to the json file after I have collected the data but I do not know how to get it to collect more than one value. Example would be that I need the sensor data to record 1 value every min for say 10 min. I am only getting one value total recorded in the file. I think I would run a while loop over time but I am not sure.
from grovepi import *
from grove_rgb_lcd import *
from time import sleep
from math import isnan
import json
dht_sensor_port = 7 # connect the DHt sensor to port 7
dht_sensor_type = 0 # use 0 for the blue-colored sensor and 1 for the white-colored sensor
# set red as backlight color
# we need to do it just once
# setting the backlight color once reduces the amount of data transfer over the I2C line
setRGB(255,0,0)
while True:
try:
# get the temperature and Humidity from the DHT sensor
[ temp,hum ] = dht(dht_sensor_port,dht_sensor_type)
# change temp reading from celsius to fahrenheit
temp = ((temp/5.0)*9)+32
# round the temp to 2 decimal places so it will read on the LCD screen
new_temp = round(temp, 2)
print(“temp =”, temp , “Fthumidity =”, hum,”%”)
# check if we have nans
# if so, then raise a type error exception
if isnan(new_temp) is True or isnan(hum) is True:
raise TypeError('nan error')
t = str(new_temp)
h = str(hum)
# instead of inserting a bunch of whitespace, we can just insert a n
# we're ensuring that if we get some strange strings on one line, the 2nd one won't be affected
setText_norefresh(“Temp:” + t + “Fn” + “Humidity :” + h + “%”)
except (IOError, TypeError) as e:
print(str(e))
# and since we got a type error
# then reset the LCD's text
setText(“”)
except KeyboardInterrupt as e:
print(str(e))
# since we're exiting the program
# it's better to leave the LCD with a blank text
setText(“”)
break
# output to data file
outputData = {}
outputData['weather'] = []
outputData['weather'].append({
'temperature': t,
'humidity': h
})
# wait to update the LCD
sleep(0.05)
with open('outputData.txt', 'w') as outfile:
json.dump(outputData, outfile)