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
|
|
|
Use with:
|
2018-08-11 15:57:35 +02:00
|
|
|
* Adafruit Feather M4 Express (CircuitPython firmware)
|
2018-07-29 19:39:13 +02:00
|
|
|
* Adafruit Ultimate GPS FeatherWing
|
|
|
|
* Bosch BME280 sensor (air temperature, humidity, atmospheric pressure) on I2C
|
|
|
|
|
2018-08-12 17:09:47 +02:00
|
|
|
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
|
2018-08-15 18:44:13 +02:00
|
|
|
* A2, A3 : UART TX & RX to Raspberry pi (115200 baudrate)
|
2018-08-12 17:09:47 +02:00
|
|
|
* D9 : to EN pin on Raspberry Lipo SHIM
|
|
|
|
* D13 : onboard LED (as RW/RO indicator)
|
|
|
|
|
|
|
|
Back pins diagramm:
|
2018-08-15 18:44:13 +02:00
|
|
|
D05 A2 A3 D09 NC_
|
2018-08-12 17:09:47 +02:00
|
|
|
GND GND SCL SDA 3V3
|
|
|
|
|
2018-08-04 12:05:23 +02:00
|
|
|
Git repository :
|
2018-08-03 12:09:55 +02:00
|
|
|
https://framagit.org/arofarn/Cameteo
|
|
|
|
|
2018-07-29 19:39:13 +02:00
|
|
|
TODO for v1 :
|
2018-08-02 00:15:25 +02:00
|
|
|
* write data on flash drive (work-in-progress)
|
2018-08-03 22:44:41 +02:00
|
|
|
* send data through UART (work-in-progress)
|
2018-07-29 19:39:13 +02:00
|
|
|
"""
|
2018-08-25 11:38:05 +02:00
|
|
|
__author__ = "arofarn"
|
2018-09-10 11:03:28 +02:00
|
|
|
__version__ = 0.3
|
2018-07-29 13:02:16 +02:00
|
|
|
|
|
|
|
#######################
|
2018-08-25 11:38:05 +02:00
|
|
|
import time
|
2018-09-09 11:39:21 +02:00
|
|
|
import gc
|
2018-09-10 11:03:28 +02:00
|
|
|
import rtc
|
2018-08-25 11:38:05 +02:00
|
|
|
import board
|
2018-09-09 11:39:21 +02:00
|
|
|
from busio import UART
|
2018-07-29 16:19:44 +02:00
|
|
|
# import micropython
|
2018-07-28 18:42:27 +02:00
|
|
|
import neopixel
|
2018-09-09 11:39:21 +02:00
|
|
|
import cameteo
|
2018-08-25 11:38:05 +02:00
|
|
|
|
|
|
|
##########
|
|
|
|
# config #
|
|
|
|
##########
|
2018-08-25 11:44:35 +02:00
|
|
|
|
2018-09-10 10:00:26 +02:00
|
|
|
PRINT_DATA = False # Print data on USB UART / REPL output ?
|
2018-09-09 11:39:21 +02:00
|
|
|
DATA_TO_NEOPIXEL = True # Display atmospheric data as color on onboard neopixel ?
|
|
|
|
GPS_ENABLE = True # Use GPS module ?
|
2018-08-25 11:38:05 +02:00
|
|
|
UPDATE_INTERVAL = 10 # Interval between data acquisition (in seconds)
|
2018-09-09 11:39:21 +02:00
|
|
|
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
|
2018-08-02 00:11:57 +02:00
|
|
|
|
2018-07-28 18:42:27 +02:00
|
|
|
#########
|
|
|
|
# Setup #
|
|
|
|
#########
|
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
gc.collect()
|
2018-08-25 11:38:05 +02:00
|
|
|
# micropython.mem_info()
|
2018-07-29 16:19:44 +02:00
|
|
|
|
2018-08-25 11:38:05 +02:00
|
|
|
# Enable RTC of the feather M0 board
|
2018-09-09 11:39:21 +02:00
|
|
|
CLOCK = rtc.RTC()
|
2018-07-28 18:42:27 +02:00
|
|
|
|
2018-08-04 12:03:11 +02:00
|
|
|
# Second UART to communicate with raspberry pi (throught GPIO)
|
2018-09-09 11:39:21 +02:00
|
|
|
if SEND_INTERVAL >= 0:
|
|
|
|
rpi_uart = UART(board.A2, board.A3, baudrate=115200, timeout=2000)
|
2018-08-03 22:44:01 +02:00
|
|
|
|
2018-08-04 12:03:11 +02:00
|
|
|
# Set onboard Neopixel : used as atmo data output
|
2018-08-25 11:38:05 +02:00
|
|
|
# brightness is fixed to 1 to spare some memory. Use NEOPIXEL_MAX_VALUE instead
|
2018-09-09 11:39:21 +02:00
|
|
|
if DATA_TO_NEOPIXEL:
|
2018-08-11 15:57:35 +02:00
|
|
|
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
|
2018-07-29 13:02:16 +02:00
|
|
|
else:
|
2018-08-25 11:38:05 +02:00
|
|
|
# if neopixel is disable : turn off the LED
|
2018-08-11 15:57:35 +02:00
|
|
|
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=1)
|
2018-08-25 11:38:05 +02:00
|
|
|
pixel[0] = (0, 0, 0)
|
2018-09-10 09:58:31 +02:00
|
|
|
del pixel
|
2018-07-28 18:42:27 +02:00
|
|
|
|
2018-09-09 11:39:21 +02:00
|
|
|
sys_data = cameteo.SysData(debug=False)
|
2018-09-10 11:01:44 +02:00
|
|
|
bme_data = cameteo.BME280Data(debug=False)
|
|
|
|
gps_data = cameteo.GPSData(debug=False)
|
2018-09-09 11:39:21 +02:00
|
|
|
|
2018-09-10 11:01:44 +02:00
|
|
|
data = [sys_data, gps_data, bme_data]
|
2018-09-09 11:39:21 +02:00
|
|
|
|
2018-09-10 11:03:28 +02:00
|
|
|
rtc.set_time_source(gps_data)
|
|
|
|
CLOCK_SYNCED = False
|
2018-07-28 18:42:27 +02:00
|
|
|
|
|
|
|
#############
|
|
|
|
# Main loop #
|
|
|
|
#############
|
2018-07-29 13:02:16 +02:00
|
|
|
|
2018-09-09 11:39:21 +02:00
|
|
|
while True:
|
2018-07-29 16:19:44 +02:00
|
|
|
|
2018-09-09 11:39:21 +02:00
|
|
|
current_time = time.monotonic()
|
2018-08-19 23:39:58 +02:00
|
|
|
|
2018-09-09 11:39:21 +02:00
|
|
|
for src in data:
|
|
|
|
src.update(current_time, verbose=PRINT_DATA)
|
2018-09-10 11:03:28 +02:00
|
|
|
|
|
|
|
# Sync clocks
|
|
|
|
CLOCK_SYNCED = cameteo.set_clock_from_localtime(CLOCK, threshold=2.0, debug=True)
|
2018-07-29 13:02:16 +02:00
|
|
|
|
2018-09-09 11:39:21 +02:00
|
|
|
for src in data:
|
2018-09-10 11:03:28 +02:00
|
|
|
#Only write data when clocks are OK
|
|
|
|
if CLOCK_SYNCED:
|
|
|
|
src.write_on_flash(current_time)
|
2018-09-10 10:00:26 +02:00
|
|
|
src.send_json(current_time, uart=rpi_uart, verbose=True)
|
2018-08-03 12:09:55 +02:00
|
|
|
|
2018-07-29 13:02:16 +02:00
|
|
|
gc.collect()
|
|
|
|
# micropython.mem_info(1)
|
|
|
|
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
|