Skip to content

Instantly share code, notes, and snippets.

View DavesCodeMusings's full-sized avatar

Dave Horton DavesCodeMusings

  • Planet Earth
View GitHub Profile
@DavesCodeMusings
DavesCodeMusings / SQLiteTimeSeriesDB.txt
Last active July 14, 2023 02:37
SQLite Time Series Database for sensor data
Make a time series database using SQLite.
Time series DBs are useful for storing data taken from the environment. For example,
weather readings from a station at fifteen minute intervals throughout the day.
Rather than using a popular, but more resource intensive purpose built time series
database for your small scale project, you can just as easily use good old SQLite.
# Sample Eclipse Mosquitto config with MQTT Secure and MQTT Secure Websockets.
# You need to provide the server.crt, server.key, and ca-certificates.crt
# Global Options
allow_anonymous true
persistence true
persistence_location /mosquitto/data/
log_dest stdout
# Standard MQTT
@DavesCodeMusings
DavesCodeMusings / boot.py
Created June 3, 2023 19:10
MQTT topic on an OLED display with ESP32-C3 and SSD1306
from machine import Pin, SoftI2C, Timer
from ssd1306 import SSD1306_I2C
from network import WLAN, STA_IF
from time import ticks_diff, ticks_ms
from config import WIFI_NAME, WIFI_PASS, WIFI_TIMEOUT
BOOT_BUTTON_GPIO = 9 # ESP32 is 0, ESP32-C3 is 9
SCREEN_TIMEOUT = 30 # Seconds before blanking OLED
i2c = SoftI2C(scl=Pin(4), sda=Pin(5))
@DavesCodeMusings
DavesCodeMusings / setup.sh
Last active July 7, 2025 23:41
Easily create a Home Assistant install using Docker containers
#!/bin/sh
# Create initial directories and files for home automation stack.
BASE_DIR=$(pwd)
ESPHOME_DIR=$BASE_DIR/esphome
HASS_DIR=$BASE_DIR/hass
MOSQUITTO_DIR=$BASE_DIR/mosquitto
NODE_RED_DIR=$BASE_DIR/node-red
@DavesCodeMusings
DavesCodeMusings / README.txt
Last active June 3, 2023 18:24
Not too bad backup to external disk
This shell script will back up files from remote systems as well as local
using rsync. Default behavior is to prune any backups older than 90 days,
except for Sundays. This results in daily copies for up to 90 days and weekly
copies for anything older.
Sample crontab entries to back up multiple hosts on a schedule:
0 6 * * * /media/backup/backup.sh lorum.home
0 7 * * * /media/backup/backup.sh ipsum.home
0 8 * * * /media/backup/backup.sh dolor.home
@DavesCodeMusings
DavesCodeMusings / gap_buffer.py
Created November 23, 2022 04:29
Python class to implement a gap buffer for accepting user input with editing features.
class GapBuffer:
def __init__(self, size, prefill=None):
self._buffer = bytearray(size)
self._gap_start = 0 # first element of gap
self._gap_end = len(self._buffer) # first element after gap
if prefill != None:
prefill_end = len(prefill) if (len(prefill) <= size) else size
self._buffer[0:prefill_end] = prefill[0:prefill_end]
self._gap_start = len(prefill)
@DavesCodeMusings
DavesCodeMusings / uasyncio_http_echo_server.py
Last active October 26, 2022 06:10
Echo-back HTTP server to demonstrate MicroPython uasyncio
# Example of using uasyncio to run a web server.
# The server does nothing but echo back what was sent to it.
# But the point here is to demonstrate the use of uasyncio, callbacks,
# and the mixing of async and regular (synchronous) functions.
import uasyncio
# This is a synchronous function that gets called from inside the async function.
def create_http_response(body):
res_lines = []
@DavesCodeMusings
DavesCodeMusings / boot_button_as_input.py
Last active June 3, 2023 19:04
Use the BOOT button on ESP boards as a regular input.
# After booting, the BOOT button attached to GPIO 0 is available to use as a regular input for your program.
# GPIO 9 is used for BOOT on the ESP32-C3. Check the Espressif docs for other ESP variations.
from machine import Pin
p0 = Pin(0, Pin.IN, Pin.PULL_UP) # Normal state is logic high, pressing the button will pull it low.
pressed = False # Flag to track if button was pressed.
def irq_handler(pin):
global pressed
pressed = True
@DavesCodeMusings
DavesCodeMusings / boot.py
Created October 4, 2022 15:15
MicroPython WebREPL with IP address on OLED and screen saver function
# Enable MicroPython WebREPL (Console over Websockets)
from machine import Pin, SoftI2C
from ssd1306 import SSD1306_I2C
from network import WLAN, STA_IF
from webrepl import start
from netcfg import AP_NAME, AP_PASS, WEBREPL_PASS
from ubinascii import a2b_base64
from time import sleep
i2c = SoftI2C(scl=Pin(4), sda=Pin(5)) # Wemos board with built-in OLED uses pins 4 (clock) & 5 (data)
@DavesCodeMusings
DavesCodeMusings / nodebug.js
Created May 30, 2022 19:03
Suppress showing of debug messages by replacing console.debug() with a custom function that checks a 'debug' flag before logging.
#!/usr/bin/env node
let _console_debug = console.debug
console.debug = function(message) {
if (debug) {
_console_debug.apply(console, arguments)
}
}
let debug = true