Browse Source

Initial Module implementation

master
Dirk Alders 2 years ago
parent
commit
2ba5d7bf49
9 changed files with 141 additions and 0 deletions
  1. 2
    0
      .gitignore
  2. 6
    0
      .gitmodules
  3. 41
    0
      __install__.py
  4. 47
    0
      ambient_info.py
  5. 4
    0
      ambient_info.sh
  6. 36
    0
      config_example/config.py
  7. 1
    0
      report
  8. 3
    0
      requirements.txt
  9. 1
    0
      rpi_envsens

+ 2
- 0
.gitignore View File

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

+ 6
- 0
.gitmodules View File

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

+ 41
- 0
__install__.py View File

@@ -0,0 +1,41 @@
1
+#!/bin/python
2
+#
3
+import os
4
+import sys
5
+
6
+SERVICE_FILE = """
7
+[Unit]
8
+Description=Smarthome Ambient Information Service
9
+After=network-online.target
10
+Wants=network-online.target
11
+[Service]
12
+User=%(UID)d
13
+Group=%(GID)d
14
+ExecStart=%(MY_PATH)s/ambient_info.sh
15
+Type=simple
16
+[Install]
17
+WantedBy=default.target
18
+"""
19
+
20
+
21
+def help():
22
+    print("Usage: prog <UID> <GID> <TARGET_PATH>")
23
+
24
+if __name__ == "__main__":
25
+    if len(sys.argv) == 4:
26
+        try:
27
+            uid = int(sys.argv[1])
28
+            gid = int(sys.argv[2])
29
+        except ValueError:
30
+            help()
31
+        else:
32
+            if os.path.isdir(sys.argv[3]):
33
+                with open(os.path.join(sys.argv[3], 'ambient_info.service'), "w") as fh:
34
+                    fh.write(SERVICE_FILE % {
35
+                        "MY_PATH": os.path.dirname(os.path.abspath(__file__)),
36
+                        "UID": uid,
37
+                        "GID": gid})
38
+            else:
39
+                help()
40
+    else:
41
+        help()

+ 47
- 0
ambient_info.py View File

@@ -0,0 +1,47 @@
1
+import config
2
+import logging
3
+import paho.mqtt.client as paho
4
+import report
5
+import time
6
+
7
+from rpi_envsens.dht import dht_22
8
+from rpi_envsens.bmp import bmp_180
9
+
10
+try:
11
+    from config import APP_NAME as ROOT_LOGGER_NAME
12
+except ImportError:
13
+    ROOT_LOGGER_NAME = 'root'
14
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
15
+
16
+
17
+def send_data_to_mqtt(data):
18
+    client= paho.Client("temp_sens")
19
+    client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
20
+    client.connect(config.MQTT_SERVER, 1883)
21
+    for key in data:
22
+        topic = config.MQTT_TOPIC + "/" + key
23
+        logger.info("Sending Ambient Information to mqtt %s=%s", topic, str(data[key]))
24
+        client.publish(topic, data[key])
25
+
26
+
27
+def dht_callback(**data):
28
+    del(data["time"])
29
+    send_data_to_mqtt(data)
30
+
31
+def bmp_callback(**data):
32
+    del(data["time"])
33
+    del(data["temperature"])
34
+    send_data_to_mqtt(data)
35
+    
36
+
37
+
38
+if __name__ == '__main__': 
39
+    report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
40
+
41
+    #
42
+    # Initialise data collectors
43
+    d = dht_22(config.DHT_22_PORT, dht_callback)
44
+    b = bmp_180(bmp_callback)
45
+
46
+    while True:
47
+        time.sleep(1)

+ 4
- 0
ambient_info.sh View File

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

+ 36
- 0
config_example/config.py View File

@@ -0,0 +1,36 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+import os
4
+import report
5
+
6
+__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
7
+
8
+MQTT_USER = "mqtt_username"
9
+MQTT_PASS = "mqtt_password"
10
+MQTT_SERVER = "mqtt_server"
11
+MQTT_TOPIC = "mqtt_topic"
12
+
13
+DAT_PATH_DHT = None     # os.path.join(__BASEPATH__, 'dat', 'dht')
14
+DAT_PATH_BMP = None     # os.path.join(__BASEPATH__, 'dat', 'bmp')
15
+
16
+try:
17
+    import board
18
+except ImportError:
19
+    DHT_22_PORT = 4
20
+    USE_PULSE_IO = True
21
+else:
22
+    # DHT-PORT
23
+    DHT_22_PORT = board.D4
24
+
25
+
26
+#
27
+# Logging
28
+#
29
+APP_NAME = "ambient_info"
30
+LOGTARGET = 'stdout'   # possible choices are: 'logfile' or 'stdout'
31
+LOGLVL = 'DEBUG'
32
+
33
+LOGHOST = 'cutelog'
34
+LOGPORT = 19996
35
+
36
+formatter = report.SHORT_FMT

+ 1
- 0
report

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

+ 3
- 0
requirements.txt View File

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

+ 1
- 0
rpi_envsens

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

Loading…
Cancel
Save