diff --git a/.env b/.env index 7e8fe0903c19345776fb5cdbb38aec4ad1f16b12..5d8dad8d81272ceb5f1b28fb87e5d18e90d53839 100644 --- a/.env +++ b/.env @@ -5,5 +5,5 @@ INFLUXDB_BUCKET=co2-test INFLUXDB_TOKEN=jTTnLJmL5w2Z1IlkKJzkwDreWJ62HdDWT5_RS0J3Rbm28AxPkUZfU_sIPaG3aoLQ5MklO_Gn9y23HtU38yR0Nw== # (Optional) MQTT config, falls du das auch variabel machen willst -MQTT_BROKER_URL="192.168.178.61" -MQTT_TOPIC="co2/esp32" +MQTT_BROKER_URL="172.20.10.12" +MQTT_TOPIC="co2/#" diff --git a/.gitignore b/.gitignore index e7f1c73124997c9faea1847393cce5875cdb4126..103d74d1b3cdc94a7c42ee74b6bcaa1718dcc7ab 100644 --- a/.gitignore +++ b/.gitignore @@ -1,18 +1,205 @@ -# Ignoriere die virtuelle Umgebung -.venv/ -venv/ +############# +# python ignores: +############# -# Ignoriere die __pycache__-Ordner +# Byte-compiled / optimized / DLL files __pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt -# Ignoriere Python Bytecode-Dateien -*.pyc -*.pyo +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ -# Ignoriere IDE-spezifische Dateien (falls du zum Beispiel VSCode oder PyCharm verwendest) -.vscode/ -.idea/ +# Translations +*.mo +*.pot -# Ignoriere andere temporäre Dateien +# Django stuff: *.log -*.bak +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# UV +# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +#uv.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Ruff stuff: +.ruff_cache/ + +# PyPI configuration file +.pypirc + +############# +# vue ignores: +############# + +.DS_Store +node_modules +/dist + +# local env files +.env.local +.env.*.local + +# Log files +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + +# Editor directories and files +.idea +.vscode +*.suo +*.ntvs* +*.njsproj +*.sln +*.sw? diff --git a/build/lib/mqtt_influx_backend/MQTTClientHandler.py b/build/lib/mqtt_influx_backend/MQTTClientHandler.py index d42bf56f0ff674799be6a9c57270d71775eee13d..2772feec15e18a4a844677c6ee82ad1a4fc20983 100644 --- a/build/lib/mqtt_influx_backend/MQTTClientHandler.py +++ b/build/lib/mqtt_influx_backend/MQTTClientHandler.py @@ -17,10 +17,15 @@ class MQTTClientHandler: client.subscribe(self.topic) def on_message(self, client, userdata, msg): - print("hallo bin in der methofe") + utfMgs = msg.payload.decode("utf-8") + #dictionary = json.loads(utfMgs) + print(utfMgs) + print(utfMgs) + print(utfMgs) + data = utfMgs try: - data = json.loads(msg.payload.decode()) - value = float(data.get("value")) + + value = float(data.get("co2")) self.influx_writer.write_point( measurement="temperature", diff --git a/console.txt b/console.txt new file mode 100644 index 0000000000000000000000000000000000000000..4832f2c20cbf6572fa1db52c38351a6c9df7c05a --- /dev/null +++ b/console.txt @@ -0,0 +1,10 @@ +mosquitto_pub -h 172.20.10.12 -t co2/esp32 -m "{ + "metadata": { + "timestamp": "2025-04-12T14:22:35Z", + "mac": "AA:BB:CC:DD:EE:FF", + "room": "A123" + }, + "co2": 615.3, + "temperature": 21.8, + "humidity": 45.2 +}" diff --git a/pyproject.toml b/pyproject.toml index f9e7db84f2ead29489fb3bd36e784ffb8b8c169c..d74aeac5e542efcb1ff4f55aac2e0f4e85da325a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,20 +1,20 @@ [project] name = "mqtt-influx-backend" version = "0.1.0" -description = "MQTT subscriber that stores data in InfluxDB" -authors = [ - { name = "Emre Gezer", email = "21geem1bif@hft-stuttgart.de" } -] +description = "Backend to write MQTT sensor data to InfluxDB" +readme = "README.md" +requires-python = ">=3.10" dependencies = [ - "influxdb-client", "paho-mqtt", - "python-dotenv" + "influxdb-client", ] -requires-python = ">=3.8" -[tool.uv] -# Optional: für zukünftige Konfigurationen +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" [tool.setuptools] package-dir = {"" = "src"} -packages = ["mqtt_influx_backend"] + +[tool.setuptools.packages.find] +where = ["src"] diff --git a/src/mqtt_influx_backend.egg-info/PKG-INFO b/src/mqtt_influx_backend.egg-info/PKG-INFO index 60300eb2332aa05970c424755e4a79ecb7b32d9e..fecd44b41368c076cc73c85171e584a3d965fb76 100644 --- a/src/mqtt_influx_backend.egg-info/PKG-INFO +++ b/src/mqtt_influx_backend.egg-info/PKG-INFO @@ -1,9 +1,8 @@ Metadata-Version: 2.4 Name: mqtt-influx-backend Version: 0.1.0 -Summary: MQTT subscriber that stores data in InfluxDB -Author-email: Emre Gezer <21geem1bif@hft-stuttgart.de> -Requires-Python: >=3.8 -Requires-Dist: influxdb-client +Summary: Backend to write MQTT sensor data to InfluxDB +Requires-Python: >=3.10 +Description-Content-Type: text/markdown Requires-Dist: paho-mqtt -Requires-Dist: python-dotenv +Requires-Dist: influxdb-client diff --git a/src/mqtt_influx_backend.egg-info/SOURCES.txt b/src/mqtt_influx_backend.egg-info/SOURCES.txt index cd04f13f60475dbc2ae8e9caefecb73ea3040c82..01700c403825c5378cd373d8bbf4074d0fb5fc61 100644 --- a/src/mqtt_influx_backend.egg-info/SOURCES.txt +++ b/src/mqtt_influx_backend.egg-info/SOURCES.txt @@ -1,8 +1,8 @@ -README.md pyproject.toml src/mqtt_influx_backend/InfluxDBWriter.py src/mqtt_influx_backend/MQTTClientHandler.py src/mqtt_influx_backend/Main.py +src/mqtt_influx_backend/__init__.py src/mqtt_influx_backend.egg-info/PKG-INFO src/mqtt_influx_backend.egg-info/SOURCES.txt src/mqtt_influx_backend.egg-info/dependency_links.txt diff --git a/src/mqtt_influx_backend.egg-info/requires.txt b/src/mqtt_influx_backend.egg-info/requires.txt index 71b4ba31498e200ce0adf0b5c4319668288ca4c2..04cfe9868ee78421fc5675765c8d3d3d07400f58 100644 --- a/src/mqtt_influx_backend.egg-info/requires.txt +++ b/src/mqtt_influx_backend.egg-info/requires.txt @@ -1,3 +1,2 @@ -influxdb-client paho-mqtt -python-dotenv +influxdb-client diff --git a/src/mqtt_influx_backend/MQTTClientHandler.py b/src/mqtt_influx_backend/MQTTClientHandler.py index d42bf56f0ff674799be6a9c57270d71775eee13d..1048fc2dba0baae69193aab4f787d5eaaa9e2323 100644 --- a/src/mqtt_influx_backend/MQTTClientHandler.py +++ b/src/mqtt_influx_backend/MQTTClientHandler.py @@ -17,16 +17,21 @@ class MQTTClientHandler: client.subscribe(self.topic) def on_message(self, client, userdata, msg): - print("hallo bin in der methofe") + + msg = json.loads(msg.payload) + print(msg) + metadate = msg["metadata"] + print(metadate["mac"]) try: - data = json.loads(msg.payload.decode()) - value = float(data.get("value")) + + value = float(msg.get("co2")) self.influx_writer.write_point( - measurement="temperature", - tags={"topic": msg.topic}, + measurement="sensor_data", + tags={"time": metadate["mac"]}, + tags={"mac": metadate["mac"]}, fields={"value": value}, - timestamp=datetime.utcnow() + timestamp=metadate["timestamp"] ) print("Wrote to InfluxDB:", value) except Exception as e: diff --git a/src/mqtt_influx_backend/Main.py b/src/mqtt_influx_backend/Main.py index 4202370acfd55ac2839940c61537793464e1eb6c..22b69554eaf652c1addb8eda0a3a0b8327ba6d9d 100644 --- a/src/mqtt_influx_backend/Main.py +++ b/src/mqtt_influx_backend/Main.py @@ -1,8 +1,8 @@ from dotenv import load_dotenv import os -from mqtt_influx_backend.MQTTClientHandler import MQTTClientHandler -from mqtt_influx_backend.InfluxDBWriter import InfluxDBWriter +from src.mqtt_influx_backend.MQTTClientHandler import MQTTClientHandler +from src.mqtt_influx_backend.InfluxDBWriter import InfluxDBWriter load_dotenv() diff --git a/src/mqtt_influx_backend/__init__.py b/src/mqtt_influx_backend/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000000000000000000000000000000000000..f595e9a83c37791fb15cc3e69e7e18c0fb18aa11 --- /dev/null +++ b/uv.lock @@ -0,0 +1,112 @@ +version = 1 +revision = 1 +requires-python = ">=3.10" + +[[package]] +name = "certifi" +version = "2025.1.31" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/1c/ab/c9f1e32b7b1bf505bf26f0ef697775960db7932abeb7b516de930ba2705f/certifi-2025.1.31.tar.gz", hash = "sha256:3d5da6925056f6f18f119200434a4780a94263f10d1c21d032a6f6b2baa20651", size = 167577 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/38/fc/bce832fd4fd99766c04d1ee0eead6b0ec6486fb100ae5e74c1d91292b982/certifi-2025.1.31-py3-none-any.whl", hash = "sha256:ca78db4565a652026a4db2bcdf68f2fb589ea80d0be70e03929ed730746b84fe", size = 166393 }, +] + +[[package]] +name = "influxdb-client" +version = "1.48.0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "certifi" }, + { name = "python-dateutil" }, + { name = "reactivex" }, + { name = "setuptools" }, + { name = "urllib3" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/11/47/b756380917cb4b968bd871fc006128e2cc9897fb1ab4bcf7d108f9601e78/influxdb_client-1.48.0.tar.gz", hash = "sha256:414d5b5eff7d2b6b453f33e2826ea9872ea04a11996ba9c8604b0c1df57c8559", size = 386415 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/5c/b3/1edc89584b8d1bc5226cf508b67ab64da3ba83041cab348861e6f4392326/influxdb_client-1.48.0-py3-none-any.whl", hash = "sha256:410db15db761df7ea98adb333c7a03f05bcc2ceef4830cefb7071b888be2b827", size = 746177 }, +] + +[[package]] +name = "mqtt-influx-backend" +version = "0.1.0" +source = { editable = "." } +dependencies = [ + { name = "influxdb-client" }, + { name = "paho-mqtt" }, +] + +[package.metadata] +requires-dist = [ + { name = "influxdb-client" }, + { name = "paho-mqtt" }, +] + +[[package]] +name = "paho-mqtt" +version = "2.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/39/15/0a6214e76d4d32e7f663b109cf71fb22561c2be0f701d67f93950cd40542/paho_mqtt-2.1.0.tar.gz", hash = "sha256:12d6e7511d4137555a3f6ea167ae846af2c7357b10bc6fa4f7c3968fc1723834", size = 148848 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/c4/cb/00451c3cf31790287768bb12c6bec834f5d292eaf3022afc88e14b8afc94/paho_mqtt-2.1.0-py3-none-any.whl", hash = "sha256:6db9ba9b34ed5bc6b6e3812718c7e06e2fd7444540df2455d2c51bd58808feee", size = 67219 }, +] + +[[package]] +name = "python-dateutil" +version = "2.9.0.post0" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "six" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/66/c0/0c8b6ad9f17a802ee498c46e004a0eb49bc148f2fd230864601a86dcf6db/python-dateutil-2.9.0.post0.tar.gz", hash = "sha256:37dd54208da7e1cd875388217d5e00ebd4179249f90fb72437e91a35459a0ad3", size = 342432 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/ec/57/56b9bcc3c9c6a792fcbaf139543cee77261f3651ca9da0c93f5c1221264b/python_dateutil-2.9.0.post0-py2.py3-none-any.whl", hash = "sha256:a8b2bc7bffae282281c8140a97d3aa9c14da0b136dfe83f850eea9a5f7470427", size = 229892 }, +] + +[[package]] +name = "reactivex" +version = "4.0.4" +source = { registry = "https://pypi.org/simple" } +dependencies = [ + { name = "typing-extensions" }, +] +sdist = { url = "https://files.pythonhosted.org/packages/ef/63/f776322df4d7b456446eff78c4e64f14c3c26d57d46b4e06c18807d5d99c/reactivex-4.0.4.tar.gz", hash = "sha256:e912e6591022ab9176df8348a653fe8c8fa7a301f26f9931c9d8c78a650e04e8", size = 119177 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/69/3f/2ed8c1b8fe3fc2ed816ba40554ef703aad8c51700e2606c139fcf9b7f791/reactivex-4.0.4-py3-none-any.whl", hash = "sha256:0004796c420bd9e68aad8e65627d85a8e13f293de76656165dffbcb3a0e3fb6a", size = 217791 }, +] + +[[package]] +name = "setuptools" +version = "78.1.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/a9/5a/0db4da3bc908df06e5efae42b44e75c81dd52716e10192ff36d0c1c8e379/setuptools-78.1.0.tar.gz", hash = "sha256:18fd474d4a82a5f83dac888df697af65afa82dec7323d09c3e37d1f14288da54", size = 1367827 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/54/21/f43f0a1fa8b06b32812e0975981f4677d28e0f3271601dc88ac5a5b83220/setuptools-78.1.0-py3-none-any.whl", hash = "sha256:3e386e96793c8702ae83d17b853fb93d3e09ef82ec62722e61da5cd22376dcd8", size = 1256108 }, +] + +[[package]] +name = "six" +version = "1.17.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/94/e7/b2c673351809dca68a0e064b6af791aa332cf192da575fd474ed7d6f16a2/six-1.17.0.tar.gz", hash = "sha256:ff70335d468e7eb6ec65b95b99d3a2836546063f63acc5171de367e834932a81", size = 34031 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/b7/ce/149a00dd41f10bc29e5921b496af8b574d8413afcd5e30dfa0ed46c2cc5e/six-1.17.0-py2.py3-none-any.whl", hash = "sha256:4721f391ed90541fddacab5acf947aa0d3dc7d27b2e1e8eda2be8970586c3274", size = 11050 }, +] + +[[package]] +name = "typing-extensions" +version = "4.13.2" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/f6/37/23083fcd6e35492953e8d2aaaa68b860eb422b34627b13f2ce3eb6106061/typing_extensions-4.13.2.tar.gz", hash = "sha256:e6c81219bd689f51865d9e372991c540bda33a0379d5573cddb9a3a23f7caaef", size = 106967 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/8b/54/b1ae86c0973cc6f0210b53d508ca3641fb6d0c56823f288d108bc7ab3cc8/typing_extensions-4.13.2-py3-none-any.whl", hash = "sha256:a439e7c04b49fec3e5d3e2beaa21755cadbbdc391694e28ccdd36ca4a1408f8c", size = 45806 }, +] + +[[package]] +name = "urllib3" +version = "2.4.0" +source = { registry = "https://pypi.org/simple" } +sdist = { url = "https://files.pythonhosted.org/packages/8a/78/16493d9c386d8e60e442a35feac5e00f0913c0f4b7c217c11e8ec2ff53e0/urllib3-2.4.0.tar.gz", hash = "sha256:414bc6535b787febd7567804cc015fee39daab8ad86268f1310a9250697de466", size = 390672 } +wheels = [ + { url = "https://files.pythonhosted.org/packages/6b/11/cc635220681e93a0183390e26485430ca2c7b5f9d33b15c74c2861cb8091/urllib3-2.4.0-py3-none-any.whl", hash = "sha256:4e16665048960a0900c702d4a66415956a584919c03361cac9f1df5c5dd7e813", size = 128680 }, +]