Skip to content

Instantly share code, notes, and snippets.

@877dev
Last active March 15, 2021 20:46
Show Gist options
  • Save 877dev/58b8b2dc57912eabda79a8acea293df7 to your computer and use it in GitHub Desktop.
Save 877dev/58b8b2dc57912eabda79a8acea293df7 to your computer and use it in GitHub Desktop.
IOTstack support for apcupsd-mqtt

File structure is :

IOTstack/services/apcupsd-mqtt/Dockerfile
IOTstack/services/apcupsd-mqtt/service.yml
IOTstack/services/apcupsd-mqtt/apcupsd-mqtt.env (may not be required?) IOTstack/services/apcupsd-mqtt/src/apcupsd-mqtt.py
IOTstack/services/apcupsd-mqtt/src/requirements.txt

Things to fix:

  1. Where to enter MQTT credentials? Use env file?

Currently I added them directly to apcupsd-mqtt.py in services/apcupsd-mqtt/src dir, which is not ideal:

Here are my settings from apcupsd-mqtt.py

MQTT_USER = os.getenv('MQTT_USER')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD')
MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
MQTT_HOST = os.getenv('MQTT_HOST', '192.168.107.99')
INTERVAL = float(os.getenv('INTERVAL', 15))
UPS_ALIAS = os.getenv('UPS_ALIAS',socket.gethostname())
APCUPSD_HOST = os.getenv('APCUPSD_HOST','192.168.107.99')
LOG_LEVEL = os.getenv('LOG_LEVEL',logging.INFO)
logger.setLevel(LOG_LEVEL)
  1. Installation error to fix (add to Dockerfile?) " Fatal Python error: can't initialize time":

Manual fix for now (https://docs.linuxserver.io/faq) :

wget http://ftp.us.debian.org/debian/pool/main/libs/libseccomp/libseccomp2_2.4.4-1~bpo10+1_armhf.deb
then
sudo dpkg -i libseccomp2_2.4.4-1~bpo10+1_armhf.deb

#!/usr/bin/python
import os
import time
from prettytable import PrettyTable
import paho.mqtt.client as paho
from apcaccess import status as apc
import logging
import socket
import pyfiglet
logger = logging.getLogger()
handler = logging.StreamHandler()
formatter = logging.Formatter(
'%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
MQTT_USER = os.getenv('MQTT_USER')
MQTT_PASSWORD = os.getenv('MQTT_PASSWORD')
MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
MQTT_HOST = os.getenv('MQTT_HOST', 'localhost')
INTERVAL = float(os.getenv('INTERVAL', 15))
UPS_ALIAS = os.getenv('UPS_ALIAS',socket.gethostname())
APCUPSD_HOST = os.getenv('APCUPSD_HOST','127.0.0.1')
LOG_LEVEL = os.getenv('LOG_LEVEL',logging.INFO)
logger.setLevel(LOG_LEVEL)
t = PrettyTable(['Key','Value'])
t.add_row(['MQTT_USER', MQTT_USER])
t.add_row(['MQTT_PASSWORD', MQTT_PASSWORD])
t.add_row(['MQTT_HOST', MQTT_HOST])
t.add_row(['INTERVAL', INTERVAL])
t.add_row(['UPS_ALIAS', UPS_ALIAS])
t.add_row(['ACPUPSD_HOST', APCUPSD_HOST])
t.add_row(['LOG_LEVEL', LOG_LEVEL])
def pub_mqtt( topic, value):
"""
Publishes a new value to a mqtt topic.
:param topic:
:param value:
:return: nothing
"""
value = str(value)
client1 = paho.Client("control1") # create client object
#if MQTT_USER is not None and MQTT_PASSWORD is not None:
client1.username_pw_set(MQTT_USER,MQTT_PASSWORD)
try:
client1.connect(MQTT_HOST, MQTT_PORT) # establish connection
except:
logger.error("unable to connect to mqtt on %s:%i" % (MQTT_HOST, MQTT_PORT))
#logger.info("mqtt topic updated: topic: " + topic + " | value: " + value)
return client1.publish(topic, value)
def main():
MQTT_TOPIC_PREFIX="/apcupsd"
ups = apc.parse(apc.get(host=APCUPSD_HOST))
HOSTNAME = ups.get('HOSTNAME', 'apcupsd-mqtt')
MQTT_TOPIC_PREFIX = MQTT_TOPIC_PREFIX + "/" + UPS_ALIAS + "/"
first_run = True
logger.info("Printing first submission for debug purposes:")
result = pyfiglet.figlet_format("apcupsd-mqtt")
print( result )
while True:
ups_data = apc.parse(apc.get(host=APCUPSD_HOST), strip_units=True)
try:
max_watts = float(ups_data.get('NOMPOWER', 0.0))
current_percent = float(ups_data.get('LOADPCT', 0.0))
current_watts = ((max_watts*current_percent)/100)
ups_data['WATTS'] = current_watts
except:
logger.error("unable to conjure up the watts. @brandon you're bad at maths.")
# watts = float(os.getenv('WATTS', ups.get('NOMPOWER', 0.0))) * 0.01 * float(ups.get('LOADPCT', 0.0))
for k in ups_data:
topic_id = MQTT_TOPIC_PREFIX + str(k)
if first_run:
logger.info("%s = %s" % (topic_id, str(ups_data[k])))
pub_mqtt( topic_id, str(ups_data[k]) )
if first_run:
logger.info("end first_run debug")
first_run = False
time.sleep(INTERVAL)
if __name__ == '__main__':
main()
FROM python:3-alpine
MAINTAINER Brandon <hey@bgulla.dev>
USER root
COPY ./src /src
RUN pip install -r /src/requirements.txt \
&& chown -R 1001:1001 /src
USER 1001
WORKDIR ["/src"]
CMD ["python", "/src/apcupsd-mqtt.py"]
PrettyTable
apcaccess
paho-mqtt
pyfiglet
TZ=Europe/London
MQTT_USER =
MQTT_PASSWORD =
MQTT_PORT = 1883
MQTT_HOST = localhost
INTERVAL = 15
UPS_ALIAS = socket.gethostname()
APCUPSD_HOST = 127.0.0.1
apcupsd-mqtt:
build: ./services/apcupsd-mqtt/.
container_name: apcupsd-mqtt
restart: unless-stopped
# Volumes not used as no persistent data required
#volumes:
# - ./volumes/apcupsd-mqtt/data:/src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment