Add : write data on SPI flash memory if writable

This commit is contained in:
Pierrick C 2018-08-02 00:09:13 +02:00
parent 7c80336f9a
commit 37e77ebef6
1 changed files with 20 additions and 5 deletions

View File

@ -24,7 +24,7 @@ Use with:
TODO for v1 :
* write data on flash drive
* send data trough UART (new one !)
* send data through UART (one more !)
"""
__version__ = 0.1
__repo__ = "https://framagit.org/arofarn/Cameteo"
@ -33,9 +33,10 @@ __repo__ = "https://framagit.org/arofarn/Cameteo"
# config #
##########
print_data = True
backup_data = True
data_to_neopixel = True
gps_enable = True
update_interval = 5 # in seconds
update_interval = 60 # in seconds
send_json_data = True
datetime_format = "{:04}/{:02}/{:02}_{:02}:{:02}:{:02}"
neopixel_max_value =const(100) #max value instead of brightness to spare some mem
@ -78,9 +79,9 @@ class Data:
# 65536 : 16bit ADC
#Data from BME280
self.data['BME280'] = {'temp': { 'val': bme280.temperature, 'unit': '°C' },
'hum': { 'val': bme280.humidity, 'unit': '%' },
'press': { 'val': bme280.pressure, 'unit': 'hPa' }}
self.data['BME280'] = {'temp': { 'val': round(bme280.temperature, 1), 'unit': '°C' },
'hum': { 'val': int(bme280.humidity), 'unit': '%' },
'press': { 'val': round(bme280.pressure, 2), 'unit': 'hPa' }}
if gps_enable:
if gps.has_fix:
self.data['GPS'] = {'timestamp': {'val': datetime_format.format(gps.timestamp_utc.tm_year,
@ -127,6 +128,18 @@ class Data:
output = output + "}"
return output
def write_on_flash(self):
"""Save the current data as csv file on SPI flash"""
try:
with open("data/data.csv", "a") as csv_file:
csv_file.write("{};{};{};{}\n".format(self.data['SYS']['time']['val'],
self.data['BME280']['temp']['val'],
self.data['BME280']['hum']['val'],
self.data['BME280']['press']['val']))
except OSError as e:
print("Err. {} : R-O FS".format(e))
#############
# Functions #
#############
@ -244,6 +257,8 @@ while True:
if print_data:
data.show()
# print(data.json())
if backup_data:
data.write_on_flash()
if data_to_neopixel:
pixel[0] = update_neopixel(data.data)
gc.collect()