Initial Module implementation

This commit is contained in:
Dirk Alders 2022-08-21 07:11:32 +01:00
parent 61eb4ddd85
commit 2ba5d7bf49
9 changed files with 141 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
config.py
# ---> Linux # ---> Linux
*~ *~

6
.gitmodules vendored Normal file
View File

@ -0,0 +1,6 @@
[submodule "rpi_envsens"]
path = rpi_envsens
url = https://git.mount-mockery.de/pylib/rpi_envsens.git
[submodule "report"]
path = report
url = https://git.mount-mockery.de/pylib/report.git

41
__install__.py Normal file
View File

@ -0,0 +1,41 @@
#!/bin/python
#
import os
import sys
SERVICE_FILE = """
[Unit]
Description=Smarthome Ambient Information Service
After=network-online.target
Wants=network-online.target
[Service]
User=%(UID)d
Group=%(GID)d
ExecStart=%(MY_PATH)s/ambient_info.sh
Type=simple
[Install]
WantedBy=default.target
"""
def help():
print("Usage: prog <UID> <GID> <TARGET_PATH>")
if __name__ == "__main__":
if len(sys.argv) == 4:
try:
uid = int(sys.argv[1])
gid = int(sys.argv[2])
except ValueError:
help()
else:
if os.path.isdir(sys.argv[3]):
with open(os.path.join(sys.argv[3], 'ambient_info.service'), "w") as fh:
fh.write(SERVICE_FILE % {
"MY_PATH": os.path.dirname(os.path.abspath(__file__)),
"UID": uid,
"GID": gid})
else:
help()
else:
help()

47
ambient_info.py Normal file
View File

@ -0,0 +1,47 @@
import config
import logging
import paho.mqtt.client as paho
import report
import time
from rpi_envsens.dht import dht_22
from rpi_envsens.bmp import bmp_180
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
def send_data_to_mqtt(data):
client= paho.Client("temp_sens")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883)
for key in data:
topic = config.MQTT_TOPIC + "/" + key
logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
client.publish(topic, data[key])
def dht_callback(**data):
del(data["time"])
send_data_to_mqtt(data)
def bmp_callback(**data):
del(data["time"])
del(data["temperature"])
send_data_to_mqtt(data)
if __name__ == '__main__':
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
#
# Initialise data collectors
d = dht_22(config.DHT_22_PORT, dht_callback)
b = bmp_180(bmp_callback)
while True:
time.sleep(1)

4
ambient_info.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
#
BASEPATH=`dirname $0`
$BASEPATH/venv/bin/python $BASEPATH/ambient_info.py

36
config_example/config.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import report
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
MQTT_USER = "mqtt_username"
MQTT_PASS = "mqtt_password"
MQTT_SERVER = "mqtt_server"
MQTT_TOPIC = "mqtt_topic"
DAT_PATH_DHT = None # os.path.join(__BASEPATH__, 'dat', 'dht')
DAT_PATH_BMP = None # os.path.join(__BASEPATH__, 'dat', 'bmp')
try:
import board
except ImportError:
DHT_22_PORT = 4
USE_PULSE_IO = True
else:
# DHT-PORT
DHT_22_PORT = board.D4
#
# Logging
#
APP_NAME = "ambient_info"
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
LOGLVL = 'DEBUG'
LOGHOST = 'cutelog'
LOGPORT = 19996
formatter = report.SHORT_FMT

1
report Submodule

@ -0,0 +1 @@
Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
paho-mqtt
adafruit-circuitpython-dht
smbus

1
rpi_envsens Submodule

@ -0,0 +1 @@
Subproject commit 2ffcdf2d59b5621b46973b16130e410ba146c949