diff --git a/.gitignore b/.gitignore index d08faf7..2ff1af4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +config.py + # ---> Linux *~ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b4b6223 --- /dev/null +++ b/.gitmodules @@ -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 diff --git a/__install__.py b/__install__.py new file mode 100644 index 0000000..f14d251 --- /dev/null +++ b/__install__.py @@ -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 ") + +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() diff --git a/ambient_info.py b/ambient_info.py new file mode 100644 index 0000000..28a0b11 --- /dev/null +++ b/ambient_info.py @@ -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) diff --git a/ambient_info.sh b/ambient_info.sh new file mode 100755 index 0000000..15d4c5a --- /dev/null +++ b/ambient_info.sh @@ -0,0 +1,4 @@ +#!/bin/sh +# +BASEPATH=`dirname $0` +$BASEPATH/venv/bin/python $BASEPATH/ambient_info.py \ No newline at end of file diff --git a/config_example/config.py b/config_example/config.py new file mode 100644 index 0000000..6dbf30b --- /dev/null +++ b/config_example/config.py @@ -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 diff --git a/report b/report new file mode 160000 index 0000000..21bac82 --- /dev/null +++ b/report @@ -0,0 +1 @@ +Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..67e0f81 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +paho-mqtt +adafruit-circuitpython-dht +smbus diff --git a/rpi_envsens b/rpi_envsens new file mode 160000 index 0000000..2ffcdf2 --- /dev/null +++ b/rpi_envsens @@ -0,0 +1 @@ +Subproject commit 2ffcdf2d59b5621b46973b16130e410ba146c949