-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
200 additions
and
133 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
.github/workflows/publish-release.yml → .github/workflows/publish.yml
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: Publish GitHub Release | ||
name: Publish Release | ||
|
||
on: | ||
push: | ||
|
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
INFLUX_URL="http://localhost:8086" | ||
INFLUX_URL="http://192.168.42.1:8086" | ||
INFLUX_USER="admin" | ||
INFLUX_PASSWORD="dietpi" | ||
MQTT_SERVER="tcp://localhost:1883" | ||
MQTT_SERVER="tcp://192.168.42.1:1883" | ||
MQTT_USER="mosquitto" | ||
MQTT_PASSWORD="dietpi" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[mqtt] | ||
host = 192.168.42.1 | ||
username = mosquitto | ||
password = dietpi | ||
|
||
[log] | ||
format = [%(name)s] [%(levelname)s] %(message)s |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,103 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import logging | ||
import os | ||
import struct | ||
import sys | ||
from configparser import ConfigParser | ||
from datetime import timedelta | ||
|
||
from gpiozero import DigitalInputDevice, CPUTemperature, LoadAverage, DiskUsage | ||
from paho.mqtt.client import Client | ||
from smbus2 import SMBus | ||
from timeloop import Timeloop | ||
|
||
# -------------------------- Configuration -------------------------- # | ||
|
||
MQTT_PUBLISH_FREQ_HZ = 0.1 | ||
LOG_PRINT_FREQ_HZ = 0.1 | ||
|
||
MQTT_CLIENT_ID = "sailtrack-status" | ||
CONFIG_FILE_PATH = "/etc/sailtrack/sailtrack.conf" | ||
|
||
I2C_BUS_NUM = 1 | ||
I2C_ADDR = 0x36 | ||
I2C_BATTERY_VOLTAGE_REG = 0x02 | ||
I2C_BATTERY_CAPACITY_REG = 0x04 | ||
|
||
CHARGE_PIN = 6 | ||
|
||
MQTT_JOB_INTERVAL_SEC = 1 / MQTT_PUBLISH_FREQ_HZ | ||
LOG_JOB_INTERVAL_SEC = 1 / LOG_PRINT_FREQ_HZ | ||
|
||
# ------------------------------------------------------------------- # | ||
|
||
published_messages = 0 | ||
received_messages = 0 | ||
|
||
|
||
def on_publish_callback(client, userdata, mid): | ||
global published_messages | ||
published_messages += 1 | ||
|
||
|
||
def get_battery_voltage(): | ||
regval = bus.read_word_data(I2C_ADDR, I2C_BATTERY_VOLTAGE_REG) | ||
regval = struct.unpack("<H", struct.pack(">H", regval))[0] | ||
return regval * 1.25 / 1000 / 16 | ||
|
||
|
||
def get_battery_capacity(): | ||
regval = bus.read_word_data(I2C_ADDR, I2C_BATTERY_CAPACITY_REG) | ||
regval = struct.unpack("<H", struct.pack(">H", regval))[0] | ||
return regval / 256 | ||
|
||
|
||
config = ConfigParser() | ||
config.read(CONFIG_FILE_PATH) | ||
bus = SMBus(I2C_BUS_NUM) | ||
mqtt = Client(MQTT_CLIENT_ID) | ||
mqtt.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"]) | ||
mqtt.on_publish = on_publish_callback | ||
mqtt.connect(config["mqtt"]["host"]) | ||
tl = Timeloop() | ||
formatter = logging.Formatter(config.get("log", "format", raw=True)) | ||
logging.getLogger("timeloop").handlers[0].setFormatter(formatter) | ||
logger = logging.getLogger(MQTT_CLIENT_ID) | ||
logger.setLevel(logging.INFO) | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
battery_charging = DigitalInputDevice(CHARGE_PIN) | ||
cpu_temperature = CPUTemperature() | ||
cpu_load = LoadAverage() | ||
disk_load = DiskUsage() | ||
|
||
|
||
@tl.job(interval=timedelta(seconds=MQTT_JOB_INTERVAL_SEC)) | ||
def mqtt_job(): | ||
sys.stdout = open(os.devnull, 'w') | ||
mqtt.publish("status/core", json.dumps({ | ||
"battery": { | ||
"voltage": get_battery_voltage(), | ||
"capacity": get_battery_capacity(), | ||
"charging": not battery_charging.value | ||
}, | ||
"cpu": { | ||
"temperature": cpu_temperature.temperature, | ||
"load": cpu_load.load_average | ||
}, | ||
"disk": { | ||
"load": disk_load.value | ||
} | ||
})) | ||
sys.stdout = sys.__stdout__ | ||
|
||
|
||
@tl.job(interval=timedelta(seconds=LOG_JOB_INTERVAL_SEC)) | ||
def log_job(): | ||
logger.info(f"Published messages: {published_messages}, Received messages: {received_messages}") | ||
|
||
|
||
tl.start(block=True) |
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,41 @@ | ||
#!/bin/bash | ||
#!/usr/bin/env python3 | ||
|
||
import json | ||
import logging | ||
import time | ||
from configparser import ConfigParser | ||
|
||
from paho.mqtt.client import Client | ||
|
||
# -------------------------- Configuration -------------------------- # | ||
|
||
MQTT_CLIENT_ID = "sailtrack-timesync" | ||
CONFIG_FILE_PATH = "/etc/sailtrack/sailtrack.conf" | ||
|
||
# ------------------------------------------------------------------- # | ||
|
||
|
||
def on_message_callback(client, userdata, message): | ||
doc = json.loads(message.payload) | ||
if "epoch" in doc and doc["epoch"]: | ||
logger.info(f"Received epoch: {doc['epoch']}") | ||
time.clock_settime(time.CLOCK_REALTIME, doc["epoch"]) | ||
logger.info("Time synced successfully!") | ||
logger.info("Exiting...") | ||
exit() | ||
|
||
|
||
config = ConfigParser() | ||
config.read(CONFIG_FILE_PATH) | ||
mqtt = Client(MQTT_CLIENT_ID) | ||
mqtt.username_pw_set(config["mqtt"]["username"], config["mqtt"]["password"]) | ||
mqtt.on_message = on_message_callback | ||
mqtt.connect(config["mqtt"]["host"]) | ||
mqtt.subscribe("sensor/gps0") | ||
logger = logging.getLogger(MQTT_CLIENT_ID) | ||
logger.setLevel(logging.INFO) | ||
handler = logging.StreamHandler() | ||
handler.setFormatter(logging.Formatter(config.get("log", "format", raw=True))) | ||
logger.addHandler(handler) | ||
logger.info("Waiting for epoch...") | ||
mqtt.loop_forever() |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,15 @@ | ||
#!/bin/bash | ||
|
||
POWEROFF_VOLTAGE=2.5 | ||
I2C_BUS_NUM=1 | ||
I2C_ADDR=0x36 | ||
I2C_BATTERY_VOLTAGE_REG=0x02 | ||
SLEEP_TIME_MS=10 | ||
|
||
while : ; do | ||
read=$(i2cget -y 1 0x36 0x02 w) | ||
swapped=${read:4:2}${read:2:2} | ||
voltage=$(awk 'BEGIN {printf "%.2f", ARGV[1] * 1.25 / 1000 / 16}' "$(( 16#$swapped ))") | ||
if awk "BEGIN {exit ARGV[1] > ARGV[2]}" "$voltage" "$POWEROFF_VOLTAGE"; then sudo /usr/sbin/poweroff; fi | ||
sleep 10 | ||
regval=$(i2cget -y $I2C_BUS_NUM $I2C_ADDR $I2C_BATTERY_VOLTAGE_REG w) | ||
regval=${regval:4:2}${regval:2:2} | ||
voltage=$(awk 'BEGIN {printf "%.2f", ARGV[1] * 1.25 / 1000 / 16}' "$((16#$regval))") | ||
if awk "BEGIN {exit ARGV[1] > ARGV[2]}" "$voltage" "$POWEROFF_VOLTAGE"; then poweroff; fi | ||
sleep $SLEEP_TIME_MS | ||
done |
Oops, something went wrong.