Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
matteocarnelos committed May 2, 2022
2 parents d3003af + bc8311e commit 0a46185
Show file tree
Hide file tree
Showing 16 changed files with 200 additions and 133 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Publish GitHub Release
name: Publish Release

on:
push:
Expand Down
13 changes: 8 additions & 5 deletions Automation_Custom_Script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,27 @@
# Configure X708 HAT
G_CONFIG_INJECT "alias poweroff=/boot/sailtrack/sailtrack-x708_softsd" "alias poweroff=/boot/sailtrack/sailtrack-x708_softsd" /etc/bash.bashrc

# Remove unused services
# Remove unused components
G_EXEC rm /etc/systemd/system/dietpi-vpn.service
G_EXEC rm /etc/systemd/system/dietpi-cloudshell.service

# Uninstall OpenSSH Client
/boot/dietpi/dietpi-software uninstall 0

# Install Telegraf
# Install required packages
G_AGI telegraf
G_EXEC pip3 install gpiozero
G_EXEC pip3 install paho-mqtt
G_EXEC pip3 install smbus2
G_EXEC pip3 install timeloop

# Enable services
G_EXEC systemctl enable sailtrack-x708_asd
G_EXEC systemctl enable sailtrack-x708_pwr
G_CONFIG_INJECT "+ telegraf" "+ telegraf" /boot/dietpi/.dietpi-services_include_exclude
G_CONFIG_INJECT "+ sailtrack-status" "+ sailtrack-status" /boot/dietpi/.dietpi-services_include_exclude
G_CONFIG_INJECT "+ sailtrack-timesync" "+ sailtrack-timesync" /boot/dietpi/.dietpi-services_include_exclude
G_EXEC /boot/dietpi/dietpi-services dietpi_controlled telegraf
G_EXEC /boot/dietpi/dietpi-services dietpi_controlled sailtrack-status
G_EXEC /boot/dietpi/dietpi-services dietpi_controlled sailtrack-timesync

# Configure DietPi Banner
settings=(1 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0)
Expand All @@ -29,4 +33,3 @@ done

# Reboot after first boot is completed
(while [ -f "/root/AUTO_CustomScript.sh" ]; do sleep 1; done; /usr/sbin/reboot) > /dev/null 2>&1 &

3 changes: 2 additions & 1 deletion dietpi.txt
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ AUTO_SETUP_GLOBAL_PASSWORD=dietpi
# - DietPi will automatically install all dependencies, like ALSA/X11 for desktops etc.
# - E.g. the following (without the leading "#") will install the LXDE desktop automatically on first boot:
#AUTO_SETUP_INSTALL_SOFTWARE_ID=23
AUTO_SETUP_INSTALL_SOFTWARE_ID=130 # Python 3 + Pip
AUTO_SETUP_INSTALL_SOFTWARE_ID=72 # I2C
AUTO_SETUP_INSTALL_SOFTWARE_ID=60 # WiFi Hotspot
AUTO_SETUP_INSTALL_SOFTWARE_ID=123 # Mosquitto
Expand Down Expand Up @@ -240,7 +241,7 @@ SOFTWARE_OWNCLOUD_DATADIR=/mnt/dietpi_userdata/owncloud_data
SOFTWARE_NEXTCLOUD_DATADIR=/mnt/dietpi_userdata/nextcloud_data

# WiFi Hotspot
SOFTWARE_WIFI_HOTSPOT_SSID=SailTrack-Net
SOFTWARE_WIFI_HOTSPOT_SSID=SailTrack-CoreNet
# - Key requires a minimum of 8 characters
SOFTWARE_WIFI_HOTSPOT_KEY=sailtracknet
SOFTWARE_WIFI_HOTSPOT_CHANNEL=1
Expand Down
4 changes: 2 additions & 2 deletions rootfs/etc/default/telegraf
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"
7 changes: 7 additions & 0 deletions rootfs/etc/sailtrack/sailtrack.conf
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
1 change: 1 addition & 0 deletions rootfs/etc/systemd/system/sailtrack-status.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Description=SailTrack-Status

[Service]
Restart=on-failure
ExecStart=/boot/sailtrack/sailtrack-status

[Install]
Expand Down
1 change: 1 addition & 0 deletions rootfs/etc/systemd/system/sailtrack-timesync.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Description=SailTrack-TimeSync

[Service]
Restart=on-failure
ExecStart=/boot/sailtrack/sailtrack-timesync

[Install]
Expand Down
1 change: 1 addition & 0 deletions rootfs/etc/systemd/system/sailtrack-x708_asd.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ After=dietpi-ramlog.service
Before=dietpi-preboot.service

[Service]
Restart=on-failure
ExecStart=/boot/sailtrack/sailtrack-x708_asd

[Install]
Expand Down
1 change: 1 addition & 0 deletions rootfs/etc/systemd/system/sailtrack-x708_pwr.service
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ After=dietpi-ramlog.service
Before=dietpi-preboot.service

[Service]
Restart=on-failure
ExecStart=/boot/sailtrack-x708_pwr

[Install]
Expand Down
2 changes: 1 addition & 1 deletion rootfs/etc/telegraf/telegraf.conf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ omit_hostname = true
[[inputs.mqtt_consumer]]
servers = ["${MQTT_SERVER}"]
topics = ["#"]
client_id = "sailtrack-core"
client_id = "telegraf"
username = "${MQTT_USER}"
password = "${MQTT_PASSWORD}"
data_format = "json"
Expand Down
103 changes: 102 additions & 1 deletion sailtrack/sailtrack-status
100644 → 100755
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)
80 changes: 0 additions & 80 deletions sailtrack/sailtrack-status.py

This file was deleted.

41 changes: 40 additions & 1 deletion sailtrack/sailtrack-timesync
100644 → 100755
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()
14 changes: 9 additions & 5 deletions sailtrack/sailtrack-x708_asd
100644 → 100755
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
Loading

0 comments on commit 0a46185

Please sign in to comment.