Cameteo/circuitpython/code/main.py

123 lines
3.6 KiB
Python

# 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/>.
"""
##########################
# Weather and GPS logger #
##########################
Use with:
* Adafruit Feather M4 Express (CircuitPython firmware)
* Adafruit Ultimate GPS FeatherWing
* Bosch BME280 sensor (air temperature, humidity, atmospheric pressure) on I2C
Pins used:
* TX/RX : Ultimate GPS FeatherWing (data)
* A5 : Ultimate GPS FeatherWing EN (enabe/disable GPS power)
* I2C SDA/SCL : BME280 sensor
* D5 : RW / RO jumper
* A2, A3 : UART TX & RX to Raspberry pi (115200 baudrate)
* D9 : to EN pin on Raspberry Lipo SHIM
* D13 : onboard LED (as RW/RO indicator)
Back pins diagramm:
D05 A2 A3 D09 NC_
GND GND SCL SDA 3V3
Git repository :
https://framagit.org/arofarn/Cameteo
TODO for v1 :
* write data on flash drive (work-in-progress)
* send data through UART (work-in-progress)
"""
__author__ = "arofarn"
__version__ = 0.2
#######################
import time
import rtc
import gc
import board
from busio import UART
# import micropython
import neopixel
import cameteo
##########
# config #
##########
PRINT_DATA = True # Print data on USB UART / REPL output ?
DATA_TO_NEOPIXEL = True # Display atmospheric data as color on onboard neopixel ?
GPS_ENABLE = True # Use GPS module ?
UPDATE_INTERVAL = 10 # Interval between data acquisition (in seconds)
WRITE_INTERVAL = 60 # Interval between data written on flash Memory (-1 to disable)
SEND_INTERVAL = 60 # Interval between packet of data sent to Rpi (-1 to disable)
NEOPIXEL_MAX_VALUE = 70 # max value instead of brightness to spare some mem
#########
# Setup #
#########
gc.collect()
# micropython.mem_info()
# Enable RTC of the feather M0 board
CLOCK = rtc.RTC()
# Second UART to communicate with raspberry pi (throught GPIO)
if SEND_INTERVAL >= 0:
rpi_uart = UART(board.A2, board.A3, baudrate=115200, timeout=2000)
# Set onboard Neopixel : used as atmo data output
# brightness is fixed to 1 to spare some memory. Use NEOPIXEL_MAX_VALUE instead
if DATA_TO_NEOPIXEL:
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
else:
# if neopixel is disable : turn off the LED
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
pixel[0] = (0, 0, 0)
del pixel
sys_data = cameteo.SysData(debug=False)
bme_data = cameteo.BME280Data()
gps_data = cameteo.GPSData(rtc=CLOCK)
data = [gps_data, sys_data, bme_data]
# Init timers
last_update = last_sent_packet = last_written_data = time.monotonic()
#############
# Main loop #
#############
while True:
current_time = time.monotonic()
for src in data:
src.update(current_time, verbose=PRINT_DATA)
for src in data:
src.write_on_flash(current_time)
src.send_json(current_time, uart=rpi_uart, verbose=PRINT_DATA)
# # First check if files need to rotate
# last_rotation = rotate_files(last_rotation)
gc.collect()
# micropython.mem_info(1)
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))