Created
April 26, 2023 23:29
-
-
Save joeycastillo/d6ba13c46b117ea105a44159209042b8 to your computer and use it in GitHub Desktop.
Low power Adafruit IO data logger with always-on LCD display
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import digitalio | |
import rtc | |
import time | |
import alarm | |
import ssl | |
import wifi | |
import socketpool | |
import adafruit_requests | |
from adafruit_io.adafruit_io import IO_HTTP | |
from adafruit_lc709203f import LC709203F | |
from adafruit_bme280 import basic as adafruit_bme280 | |
from oso_lcd.lcdwing_lite import LCDWingLite, Indicator | |
try: | |
from secrets import secrets | |
except ImportError: | |
print("WiFi secrets are kept in secrets.py, please add them there!") | |
raise | |
requests = None | |
display = LCDWingLite(board.I2C()) | |
bme280 = adafruit_bme280.Adafruit_BME280_I2C(board.I2C()) | |
clock = rtc.RTC() | |
def setup_wifi_if_needed(): | |
global requests | |
if requests is None: | |
display.print(" ") | |
display.set_indicator(Indicator.WIFI) | |
display.blink(3) | |
wifi.radio.connect(secrets["ssid"], secrets["password"]) | |
pool = socketpool.SocketPool(wifi.radio) | |
requests = adafruit_requests.Session(pool, ssl.create_default_context()) | |
display.blink(0) | |
def set_time(): | |
setup_wifi_if_needed() | |
aio_username = secrets["aio_username"] | |
aio_key = secrets["aio_key"] | |
location = secrets.get("timezone", None) | |
TIME_URL = f"https://io.adafruit.com/api/v2/{aio_username}/integrations/time/strftime?x-aio-key={aio_key}&tz={location}&fmt=%25Y-%25m-%25d+%25H%3A%25M%3A%25S" | |
response = requests.get(TIME_URL) | |
components = response.text.split(' ') | |
(year, month, day) = components[0].split('-') | |
(hour, minute, second) = components[1].split(':') | |
clock.datetime = time.struct_time((int(year), int(month), int(day), int(hour), int(minute), int(second), 0, -1, -1)) | |
def post_data(temperature, humidity, pressure, battery): | |
setup_wifi_if_needed() | |
aio_username = secrets["aio_username"] | |
aio_key = secrets["aio_key"] | |
io = IO_HTTP(aio_username, aio_key, requests) | |
try: | |
io.send_data("lcd-logger.temperature", temperature) | |
io.send_data("lcd-logger.humidity", humidity) | |
io.send_data("lcd-logger.pressure", pressure) | |
io.send_data("lcd-logger.battery", battery) | |
except Exception as e: # pylint: disable=broad-except | |
print(e) | |
display.print("Error") | |
go_to_sleep() | |
def go_to_sleep(): | |
# Clear all indicators except for the sleep indicator | |
display.clear_indicator(Indicator.ALL) | |
display.set_indicator(Indicator.MOON) | |
# Shut down I2C power | |
i2c_power = digitalio.DigitalInOut(board.I2C_POWER) | |
i2c_power.switch_to_input() | |
# Go to sleep | |
time_alarm = alarm.time.TimeAlarm(monotonic_time=time.monotonic() + 60 - clock.datetime.tm_sec) | |
alarm.exit_and_deep_sleep_until_alarms(time_alarm) | |
# Main loop begins here! | |
if alarm.wake_alarm is None: | |
set_time() | |
# Get temperature in fahrenheit; for celsius, remove the conversion factor | |
temperature = bme280.temperature * (9 / 5) + 32 | |
# At the top of the hour, upload data to adafruit.io | |
if clock.datetime.tm_min == 0 and clock.datetime.tm_hour != alarm.sleep_memory[0]: | |
# stash the current hour in alarm.sleep_memory[0] so we don't double-log if the clock was fast | |
alarm.sleep_memory[0] = clock.datetime.tm_hour | |
# Read new sensor data | |
humidity = bme280.relative_humidity | |
pressure = bme280.pressure | |
battery_voltage = LC709203F(board.I2C()).cell_voltage | |
# Show the DATA indicator to indicate that we're transferring data | |
display.set_indicator(Indicator.DATA) | |
post_data(temperature, humidity, pressure, battery_voltage) | |
set_time() | |
display.clear_indicator(Indicator.DATA) | |
# Always update the temperature display | |
display.print("{:.1f}#F".format(temperature)) | |
go_to_sleep() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment