2018-07-29 18:48:25 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2018-07-29 19:39:13 +02:00
|
|
|
"""
|
2018-07-29 18:48:25 +02:00
|
|
|
##########################
|
|
|
|
# Weather and GPS logger #
|
|
|
|
##########################
|
2018-07-29 19:39:13 +02:00
|
|
|
Author : Pierrick Couturier
|
2018-07-29 18:48:25 +02:00
|
|
|
|
2018-07-29 19:39:13 +02:00
|
|
|
Use with:
|
|
|
|
* Adafruit Feather M0 Express (CircuitPython firmware 3.0.0)
|
|
|
|
* Adafruit Ultimate GPS FeatherWing
|
|
|
|
* Bosch BME280 sensor (air temperature, humidity, atmospheric pressure) on I2C
|
|
|
|
|
|
|
|
TODO for v1 :
|
|
|
|
* write data on flash drive
|
|
|
|
* send data trough UART (new one !)
|
|
|
|
"""
|
|
|
|
__version__ = 0.1
|
|
|
|
__repo__ = "https://framagit.org/arofarn/Cameteo"
|
2018-07-28 18:42:27 +02:00
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
##########
|
2018-07-29 16:25:37 +02:00
|
|
|
# config #
|
2018-07-29 13:02:16 +02:00
|
|
|
##########
|
|
|
|
print_data = True
|
|
|
|
data_to_neopixel = True
|
2018-07-29 15:27:37 +02:00
|
|
|
gps_enable = True
|
2018-07-29 16:19:44 +02:00
|
|
|
update_interval = 5 # in seconds
|
2018-07-29 13:02:16 +02:00
|
|
|
send_json_data = True
|
2018-07-29 16:19:44 +02:00
|
|
|
datetime_format = "{:04}/{:02}/{:02}_{:02}:{:02}:{:02}"
|
2018-07-29 16:45:19 +02:00
|
|
|
neopixel_max_value =const(100) #max value instead of brightness to spare some mem
|
2018-07-29 13:02:16 +02:00
|
|
|
|
|
|
|
#######################
|
|
|
|
|
2018-07-29 16:19:44 +02:00
|
|
|
import board, microcontroller
|
|
|
|
import gc, os
|
|
|
|
# import micropython
|
|
|
|
import time, rtc
|
2018-07-28 18:42:27 +02:00
|
|
|
from busio import I2C, UART
|
|
|
|
from analogio import AnalogIn
|
|
|
|
|
|
|
|
from adafruit_bme280 import Adafruit_BME280_I2C
|
|
|
|
from adafruit_gps import GPS
|
|
|
|
import neopixel
|
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
###########
|
|
|
|
# Classes #
|
|
|
|
###########
|
|
|
|
|
|
|
|
class Data:
|
2018-07-29 16:19:44 +02:00
|
|
|
"""Class for handling data"""
|
2018-07-29 13:02:16 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.data = {}
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Read the data from various sensors and update the data dict variable"""
|
|
|
|
#Data from Feather board
|
2018-07-29 16:19:44 +02:00
|
|
|
self.data['SYS'] = {'time': {'val': datetime_format.format(*clock.datetime[0:6]),
|
|
|
|
'unit': '' },
|
|
|
|
'v_bat': {'val': vbat.value*0.000100708,
|
|
|
|
'unit': 'V' },
|
|
|
|
'CPU_temp': {'val': microcontroller.cpu.temperature,
|
|
|
|
'unit': '°C' }}
|
2018-07-29 13:02:16 +02:00
|
|
|
# Note about v_bat calculation :
|
|
|
|
# 0.000100708 = 2*3.3/65536 with
|
|
|
|
# 2 : voltage is divided by 2
|
|
|
|
# 3.3 : Vref = 3.3V
|
|
|
|
# 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' }}
|
|
|
|
if gps_enable:
|
|
|
|
if gps.has_fix:
|
2018-07-29 16:19:44 +02:00
|
|
|
self.data['GPS'] = {'timestamp': {'val': datetime_format.format(gps.timestamp_utc.tm_year,
|
|
|
|
gps.timestamp_utc.tm_mon,
|
|
|
|
gps.timestamp_utc.tm_mday,
|
|
|
|
gps.timestamp_utc.tm_hour,
|
|
|
|
gps.timestamp_utc.tm_min,
|
|
|
|
gps.timestamp_utc.tm_sec),
|
|
|
|
'unit': ''},
|
2018-07-29 13:02:16 +02:00
|
|
|
'lat': {'val': gps.latitude, 'unit': 'deg'},
|
|
|
|
'lon': {'val': gps.longitude, 'unit': 'deg'},
|
|
|
|
'alt': {'val': gps.altitude_m, 'unit': 'm'},
|
2018-07-29 16:19:44 +02:00
|
|
|
'qual': {'val': gps.fix_quality, 'unit': ''}}
|
2018-07-29 13:02:16 +02:00
|
|
|
else:
|
|
|
|
self.data['GPS'] = {'lat': {'val': None, 'unit': 'deg'},
|
|
|
|
'lon': {'val': None, 'unit': 'deg'},
|
|
|
|
'alt': {'val': None, 'unit': 'm'}}
|
|
|
|
else:
|
|
|
|
self.data['GPS'] = None
|
|
|
|
|
|
|
|
def show(self):
|
2018-07-29 16:25:37 +02:00
|
|
|
"""Serialize data to json-formatted string for visualization on
|
|
|
|
serial console
|
2018-07-29 13:02:16 +02:00
|
|
|
"""
|
|
|
|
for source in self.data.keys():
|
|
|
|
print(source + ": ")
|
|
|
|
if not self.data[source] == None:
|
|
|
|
for d in self.data[source].items():
|
|
|
|
print("\t{0}: {val} {unit}".format(d[0], **d[1]))
|
|
|
|
|
|
|
|
def json(self):
|
|
|
|
"""Serialize data to json-formatted string"""
|
|
|
|
output = "{"
|
|
|
|
for source in self.data.keys():
|
|
|
|
output = "".join((output, "'", source, "': \n"))
|
|
|
|
if not self.data[source] == None:
|
|
|
|
for d in self.data[source].items():
|
|
|
|
output = "".join((output, "{",
|
|
|
|
"'{}': ".format(d[0]),
|
|
|
|
"{",
|
|
|
|
"'val': {val},'unit': {unit}".format(**d[1]),
|
|
|
|
"}}\n"))
|
|
|
|
output = output + "}, \n"
|
|
|
|
output = output + "}"
|
|
|
|
return output
|
|
|
|
|
|
|
|
#############
|
|
|
|
# Functions #
|
|
|
|
#############
|
|
|
|
|
|
|
|
def check_data_dir():
|
|
|
|
"""Check if data directories exists"""
|
|
|
|
if 'data' not in os.listdir():
|
|
|
|
os.mkdir('data')
|
|
|
|
os.mkdir('data/hourly')
|
|
|
|
os.mkdir('data/daily')
|
|
|
|
elif 'hourly' not in os.listdir('data'):
|
|
|
|
os.mkdir('data/hourly')
|
|
|
|
elif 'daily' not in os.listdir('data'):
|
|
|
|
os.mkdir('data/daily')
|
|
|
|
|
|
|
|
def update_neopixel(data):
|
2018-07-29 16:25:37 +02:00
|
|
|
"""Convert atmospheric data from BME280 sensor into NeoPixel color
|
|
|
|
* RED => temperature : max = 35degC, min =10degC (range 25°C)
|
|
|
|
* BLUE => humidity : max= 100%, mini=0%
|
|
|
|
* GREEN => pression : mini=960hPa, maxi = 1030hPa (range 70hPa)
|
2018-07-29 13:02:16 +02:00
|
|
|
"""
|
|
|
|
|
2018-07-29 16:45:19 +02:00
|
|
|
rouge = int((data['BME280']['temp']['val']-10)*neopixel_max_value/25)
|
|
|
|
if rouge > neopixel_max_value:
|
|
|
|
rouge = neopixel_max_value
|
2018-07-29 13:02:16 +02:00
|
|
|
if rouge < 0:
|
|
|
|
rouge = 0
|
|
|
|
|
2018-07-29 16:45:19 +02:00
|
|
|
bleu = int(data['BME280']['hum']['val']*neopixel_max_value/100)
|
2018-07-29 13:02:16 +02:00
|
|
|
|
2018-07-29 16:45:19 +02:00
|
|
|
vert = int((data['BME280']['press']['val']-960)*neopixel_max_value/70)
|
|
|
|
if vert > neopixel_max_value:
|
|
|
|
vert = neopixel_max_value
|
2018-07-29 13:02:16 +02:00
|
|
|
if vert < 0:
|
|
|
|
vert = 0
|
|
|
|
|
|
|
|
if print_data:
|
|
|
|
print("Col:{}".format((rouge, vert, bleu)))
|
|
|
|
|
|
|
|
return (rouge, vert, bleu)
|
|
|
|
|
2018-07-29 17:27:52 +02:00
|
|
|
def set_clock_from_GPS():
|
|
|
|
if gps_enable and gps.has_fix:
|
|
|
|
#Convert GPS timestamp into struct_time
|
|
|
|
gps_datetime = time.struct_time((gps.timestamp_utc.tm_year,
|
|
|
|
gps.timestamp_utc.tm_mon,
|
|
|
|
gps.timestamp_utc.tm_mday,
|
|
|
|
gps.timestamp_utc.tm_hour,
|
|
|
|
gps.timestamp_utc.tm_min,
|
|
|
|
gps.timestamp_utc.tm_sec, 0, 0, 0))
|
|
|
|
#Max difference between GPS and internal RTC (in seconds):
|
|
|
|
if abs(time.mktime(gps_datetime) - time.mktime(clock.datetime)) >= 5.0:
|
|
|
|
print("Clock difference with GPS!")
|
|
|
|
print("Previous date/time : " + datetime_format.format(*clock.datetime[0:6]))
|
|
|
|
clock.datetime = gps_datetime #Trust GPS if there is a bias
|
|
|
|
print("New date/time : " + datetime_format.format(*clock.datetime[0:6]))
|
2018-07-29 18:48:25 +02:00
|
|
|
|
2018-07-28 18:42:27 +02:00
|
|
|
#########
|
|
|
|
# Setup #
|
|
|
|
#########
|
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
gc.collect()
|
2018-07-29 16:19:44 +02:00
|
|
|
#micropython.mem_info()
|
|
|
|
|
|
|
|
#Enable RTC of the feather M0 board
|
|
|
|
clock = rtc.RTC()
|
2018-07-29 17:27:52 +02:00
|
|
|
#clock.datetime = time.struct_time((2018, 7, 29, 15, 31, 30, 0, 0, 0))
|
2018-07-29 13:02:16 +02:00
|
|
|
|
2018-07-28 18:42:27 +02:00
|
|
|
# BME280 sensors (I2C)
|
|
|
|
i2c = I2C(board.SCL, board.SDA)
|
2018-07-29 13:02:16 +02:00
|
|
|
# i2c addresses for BME280 breakout :
|
2018-07-29 16:25:37 +02:00
|
|
|
# 0x77 = adafruit breakout board
|
|
|
|
# 0x76 = tiny chinese board
|
2018-07-29 13:02:16 +02:00
|
|
|
bme280 = Adafruit_BME280_I2C(i2c, address=0x76)
|
2018-07-28 18:42:27 +02:00
|
|
|
|
|
|
|
# Battery voltage
|
|
|
|
vbat = AnalogIn(board.D9, )
|
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
# GPS on FeatherWing board
|
|
|
|
if gps_enable:
|
|
|
|
gps_uart = UART(board.TX, board.RX, baudrate=9600, timeout=3000)
|
|
|
|
gps = GPS(gps_uart)
|
|
|
|
# Turn on the basic GGA and RMC info
|
|
|
|
gps.send_command('PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
|
|
|
|
gps.send_command('PMTK220,1000') # 1000 ms refresh rate
|
2018-07-28 18:42:27 +02:00
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
# Integrated Neopixel
|
|
|
|
if data_to_neopixel:
|
2018-07-29 16:45:19 +02:00
|
|
|
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
|
2018-07-29 13:02:16 +02:00
|
|
|
else:
|
|
|
|
#if neopixel is disable : turn off the LED
|
2018-07-29 16:45:19 +02:00
|
|
|
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
|
2018-07-29 13:02:16 +02:00
|
|
|
pixel[0] = (0,0,0)
|
|
|
|
pixel = None
|
2018-07-28 18:42:27 +02:00
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
check_data_dir()
|
2018-07-28 18:42:27 +02:00
|
|
|
|
|
|
|
#############
|
|
|
|
# Main loop #
|
|
|
|
#############
|
2018-07-29 13:02:16 +02:00
|
|
|
|
|
|
|
data = Data()
|
|
|
|
last_update = time.monotonic()
|
2018-07-29 16:19:44 +02:00
|
|
|
|
2018-07-28 18:42:27 +02:00
|
|
|
while True:
|
2018-07-29 13:02:16 +02:00
|
|
|
|
|
|
|
if gps_enable:
|
|
|
|
gps.update()
|
|
|
|
|
|
|
|
current = time.monotonic()
|
|
|
|
if current - last_update >= update_interval:
|
|
|
|
last_update = current
|
2018-07-29 17:27:52 +02:00
|
|
|
set_clock_from_GPS()
|
2018-07-29 13:02:16 +02:00
|
|
|
data.update()
|
|
|
|
if print_data:
|
|
|
|
data.show()
|
|
|
|
# print(data.json())
|
|
|
|
if data_to_neopixel:
|
|
|
|
pixel[0] = update_neopixel(data.data)
|
|
|
|
gc.collect()
|
|
|
|
# micropython.mem_info(1)
|
|
|
|
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
|