Start to move from Arduino (C/C++) to MicroPython / CircuitPython (teensy=>Feather M0 express)
This commit is contained in:
17
circuitpython/boot.py
Normal file
17
circuitpython/boot.py
Normal file
@ -0,0 +1,17 @@
|
||||
# Selectively setting readonly to False on boot
|
||||
|
||||
import board
|
||||
import digitalio
|
||||
import storage
|
||||
|
||||
switch = digitalio.DigitalInOut(board.D5)
|
||||
switch.direction = digitalio.Direction.INPUT
|
||||
switch.pull = digitalio.Pull.UP
|
||||
led = digitalio.DigitalInOut(board.D13)
|
||||
led.direction = digitalio.Direction.OUTPUT
|
||||
|
||||
# If the D0 is connected to ground with a wire
|
||||
# CircuitPython can write to the drive
|
||||
storage.remount("/", switch.value)
|
||||
print("Readonly : {}".format(switch.value))
|
||||
led.value = switch.value
|
BIN
circuitpython/lib/adafruit_bme280.mpy
Normal file
BIN
circuitpython/lib/adafruit_bme280.mpy
Normal file
Binary file not shown.
0
circuitpython/lib/adafruit_bus_device/__init__.py
Normal file
0
circuitpython/lib/adafruit_bus_device/__init__.py
Normal file
BIN
circuitpython/lib/adafruit_bus_device/i2c_device.mpy
Normal file
BIN
circuitpython/lib/adafruit_bus_device/i2c_device.mpy
Normal file
Binary file not shown.
BIN
circuitpython/lib/adafruit_bus_device/spi_device.mpy
Normal file
BIN
circuitpython/lib/adafruit_bus_device/spi_device.mpy
Normal file
Binary file not shown.
BIN
circuitpython/lib/adafruit_gps.mpy
Normal file
BIN
circuitpython/lib/adafruit_gps.mpy
Normal file
Binary file not shown.
BIN
circuitpython/lib/neopixel.mpy
Normal file
BIN
circuitpython/lib/neopixel.mpy
Normal file
Binary file not shown.
126
circuitpython/main.py
Normal file
126
circuitpython/main.py
Normal file
@ -0,0 +1,126 @@
|
||||
# Simple weather and GPS logger
|
||||
|
||||
import board
|
||||
from busio import I2C, UART
|
||||
from time import sleep
|
||||
from analogio import AnalogIn
|
||||
import microcontroller
|
||||
import gc
|
||||
import micropython
|
||||
import os
|
||||
|
||||
from adafruit_bme280 import Adafruit_BME280_I2C
|
||||
from adafruit_gps import GPS
|
||||
import neopixel
|
||||
|
||||
#########
|
||||
# Setup #
|
||||
#########
|
||||
|
||||
# BME280 sensors (I2C)
|
||||
i2c = I2C(board.SCL, board.SDA)
|
||||
bme280 = Adafruit_BME280_I2C(i2c)
|
||||
|
||||
# Integrated Neopixel
|
||||
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2)
|
||||
|
||||
# Battery voltage
|
||||
vbat = AnalogIn(board.D9, )
|
||||
|
||||
# GPS on Feather board
|
||||
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,2000') # 1000 ms refresh
|
||||
|
||||
gc.collect()
|
||||
micropython.mem_info()
|
||||
|
||||
temp, hum, press = 0.0, 0.0, 0.0
|
||||
rouge, vert, bleu = 0, 0, 0
|
||||
|
||||
# Check if data directory exists
|
||||
if 'data' not in os.listdir():
|
||||
os.mkdir('data')
|
||||
os.mkdir('data/hourly')
|
||||
os.mkdir('data/daily')
|
||||
|
||||
#############
|
||||
# Main loop #
|
||||
#############
|
||||
while True:
|
||||
sleep(5)
|
||||
|
||||
gc.collect()
|
||||
# micropython.mem_info(1)
|
||||
# print('Memory free: {} allocated: {}'.format(gc.mem_free(), gc.mem_alloc()))
|
||||
|
||||
temp = bme280.temperature
|
||||
hum = bme280.humidity
|
||||
press = bme280.pressure
|
||||
|
||||
print("Temperature: {:>+.1f} degC | Humidite: {:>.1f} % | Pression: {:>.1f} hPa".format(temp, hum, press))
|
||||
print("Tension batterie : {:>.2f} V | CPU Temp: {:>+.1f} degC".format((vbat.value*2*3.3/65536), microcontroller.cpu.temperature))
|
||||
# 0.00644531 = 2*3.3/1024 :
|
||||
# 2 : voltage is divided by 2
|
||||
# 3.3 : Vref = 3.3V
|
||||
# 1024 : 10bit ADC
|
||||
|
||||
# Conversion des donn?es en couleur
|
||||
# ROUGE => temp?rature : max = 35?C, min =10?C soit une amplitude de 25?C
|
||||
rouge = int((temp-10)*255/25)
|
||||
if rouge > 255:
|
||||
rouge = 255
|
||||
if rouge < 0:
|
||||
rouge = 0
|
||||
|
||||
# BLEU => humidit? : max= 100%, mini=0%
|
||||
bleu = int(hum*255/100)
|
||||
|
||||
# VERT => Pression : mini=960hPa, maxi = 1030hPa soit une amplitude 70hPa
|
||||
vert = int((press-960)*255/70)
|
||||
if vert > 255:
|
||||
vert = 255
|
||||
if vert < 0:
|
||||
vert = 0
|
||||
|
||||
rvb = (rouge, vert, bleu)
|
||||
print("Couleur : {}".format(rvb))
|
||||
pixel[0] = rvb
|
||||
|
||||
gps.update()
|
||||
if not gps.has_fix:
|
||||
# Try again if we don't have a fix yet.
|
||||
print('Waiting for fix... {} - {}'.format(gps.has_fix, gps.satellites))
|
||||
continue
|
||||
|
||||
print('Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}'.format(
|
||||
gps.timestamp_utc.tm_mon, # Grab parts of the time from the
|
||||
gps.timestamp_utc.tm_mday, # struct_time object that holds
|
||||
gps.timestamp_utc.tm_year, # the fix time. Note you might
|
||||
gps.timestamp_utc.tm_hour, # not get all data like year, day,
|
||||
gps.timestamp_utc.tm_min, # month!
|
||||
gps.timestamp_utc.tm_sec))
|
||||
if gps.altitude_m is not None:
|
||||
print('Latitude: {} deg | Longitude: {} deg | Altitude: {} m'.format(gps.latitude,
|
||||
gps.longitude,
|
||||
gps.altitude_m))
|
||||
else:
|
||||
print('Latitude: {} deg | Longitude: {} deg'.format(gps.latitude,
|
||||
gps.longitude))
|
||||
|
||||
print('Fix quality: {}'.format(gps.fix_quality))
|
||||
# Some attributes beyond latitude, longitude and timestamp are optional
|
||||
# and might not be present. Check if they're None before trying to use!
|
||||
if gps.satellites is not None:
|
||||
print('# satellites: {}'.format(gps.satellites))
|
||||
if gps.track_angle_deg is not None:
|
||||
print('Speed: {} knots'.format(gps.speed_knots))
|
||||
if gps.track_angle_deg is not None:
|
||||
print('Track angle: {} degrees'.format(gps.track_angle_deg))
|
||||
if gps.horizontal_dilution is not None:
|
||||
print('Horizontal dilution: {}'.format(gps.horizontal_dilution))
|
||||
if gps.height_geoid is not None:
|
||||
print('Height geo ID: {} meters'.format(gps.height_geoid))
|
||||
|
Reference in New Issue
Block a user