Skip to content

Instantly share code, notes, and snippets.

@mharsch
mharsch / mt_viki_remote
Created February 18, 2023 18:32
Arduino replaces MT-VIKI KVM remote module for changing / polling selected KVM input
#include <math.h>
// connected to 'D+' pin on mini USB
const int input_pin = 11;
// connected to 'D-' pin on mini USB
const int output_pin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
@mharsch
mharsch / gist:5188206
Last active February 8, 2024 02:43
serve HLS (HTTP Live Streaming) content from node.js

HLS streaming from node

Provided that you already have a file or stream segmenter generating your .m3u8 playlist and .ts segment files (such as the ffmpeg 'hls' muxer), this little node server will serve up those files to an HLS compatible client (e.g. Safari). If you're using node for your streaming app already, this obviates the need to serve the HLS stream from a separate web server.

loosely based on https://gist.github.com/bnerd/2011232

// loosely based on https://gist.github.com/bnerd/2011232
// requires node.js >= v0.10.0
// assumes that HLS segmenter filename base is 'out'
// and that the HLS playlist and .ts files are in the current directory
@mharsch
mharsch / gist:b1108372982a1921235563ee5f603807
Last active December 30, 2023 16:38
run a service on a schedule (e.g. off-peak electricity rates)
We want to run a long-running process during certain hours, days-of-the-week, etc.
We want the service to start on a schedule, and be stopped on a corresponding schedule.
Example: Residential electric rates are based on Time of Use (TOU) such that Weekdays
from 1p to 7p are considered 'Peak Hours' when rates are much higher than all other times.
Our long running process uses significant electricity, so we want to stop running it
once we reach the Peak Hours period, and resume running once the Peak period is over.
1.) We create a systemd service file for the long running process (foo.service)
2.) We create a systemd timer unit associated with the service (foo.timer). This timer handles
@mharsch
mharsch / prep.sh
Last active December 29, 2023 13:30
wipe and prep drives for replotting
#!/bin/bash
# Script for wiping old plot directories and prepping drives for new plots
# Pause between steps in case something goes wrong (Ctrl-C to bail out)
RANGE=sdb[g-j]
DEVS=/dev/$RANGE
DEVS_SCHED=/sys/block/${RANGE}/queue/scheduler
PARTS=/dev/${RANGE}1
OLD_DIRS=/plots/${RANGE}1
@mharsch
mharsch / gist:ce4ec394a3a058b7d0d3d25f52595f13
Created December 18, 2023 19:05
enable pc speaker control via beep when logged in remotely (Ubuntu 22.04)
This will save you from going down a rather deep documentation rabbit hole:
1.) enable pcspkr driver by removing blacklist entry for pcspkr in /etc/modprobe.d/blacklist.conf
2.) create user group 'beep' for controlling permissions to speaker
sudo addgroup --system beep
3.) Add your user account to the new beep group (logout and log back in after this step)
sudo usermod -aG beep $USER
4.) change default permissions on speaker device to allow members of group 'beep' to access speaker.
Comment out previous entry and add the following line to /usr/lib/udev/rules.d/70-pcspkr-beep.rules
@mharsch
mharsch / kasa_grafana.md
Created September 21, 2023 15:12
use kasa smart plug with prometheus / grafana

Uses this container to scrape power data from the reverse-engineered kasa API running on the smart plugs (e.g. HS100).

Docker compose example

compose.yml

version: '3.8'
@mharsch
mharsch / gist:e942fc0f0092f69ea5904a727542340f
Last active June 16, 2023 21:44
Configure systemd watchdog on raspberry pi to reboot if it can't ping a certain host

Most online instructions for Linux watchdog refer to the 'watchdog' package which is best described here

The watchdog configuration described here uses systemd, and the two methods are mutually exclusive, so if you've previously setup the above watchdog, you must first uninstall it.

sudo apt remove watchdog

Our goal is to add a systemd service that continuously pings a known host (or IP address)

@mharsch
mharsch / hostname_watchdog.md
Last active June 16, 2023 21:37
setup linux to reboot if the host loses access to hostname (not IP)

Deprecated

I no longer recommend the below method. For my updated recommendation, see this gist

Setup Linux to reboot if it loses access to a certain host by hostname (not IP)

The linux watchdog service can be configured to run certain 'liveness' tests periodically, and then take some action (such as reboot) if a test fails (and doesn't recover) for some period of time.

There is a built-in test for pinging an IP address (i.e. 'ping' directive), but you may instead need to test access to a hostname

/**
* V. Hunter Adams
* DDS of sine wave on MCP4822 DAC w/ ISR
*
* Modified example code from Raspberry Pi
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
*
* SPDX-License-Identifier: BSD-3-Clause
*
GPIO 8 (pico pin 11) MCP4725 SDA
@mharsch
mharsch / alpha4_sim_results.txt
Created April 22, 2023 17:40
alpha4 GPU harvesting simulate results for RTX 3080 Ti, 3070, 3060
RTX 3080 Ti System:
===================
OS="Ubuntu 22.04.2 LTS 5.15.0-70-generic"
SystemVendor/Model="Gigabyte Technology Co., Ltd. B550 AORUS MASTER"
CPU="1x AMD Ryzen 9 5950X 16-Core Processor (C:16|T:32)"
GPU="NVIDIA GeForce RTX 3080 Ti"
GPUSKU="Ampere"
GPUArch="N/A"
DriverVersion="530.30.02"
PCIeLink="PCIe 4x16"