Commit 5c73cc54 authored by Gezer's avatar Gezer
Browse files

Es tut endlich mit JSON, aber muss noch weiter angepasst werden

parent f0183116
No related merge requests found
Showing with 360 additions and 43 deletions
+360 -43
...@@ -5,5 +5,5 @@ INFLUXDB_BUCKET=co2-test ...@@ -5,5 +5,5 @@ INFLUXDB_BUCKET=co2-test
INFLUXDB_TOKEN=jTTnLJmL5w2Z1IlkKJzkwDreWJ62HdDWT5_RS0J3Rbm28AxPkUZfU_sIPaG3aoLQ5MklO_Gn9y23HtU38yR0Nw== INFLUXDB_TOKEN=jTTnLJmL5w2Z1IlkKJzkwDreWJ62HdDWT5_RS0J3Rbm28AxPkUZfU_sIPaG3aoLQ5MklO_Gn9y23HtU38yR0Nw==
# (Optional) MQTT config, falls du das auch variabel machen willst # (Optional) MQTT config, falls du das auch variabel machen willst
MQTT_BROKER_URL="192.168.178.61" MQTT_BROKER_URL="172.20.10.12"
MQTT_TOPIC="co2/esp32" MQTT_TOPIC="co2/#"
# Ignoriere die virtuelle Umgebung #############
.venv/ # python ignores:
venv/ #############
# Ignoriere die __pycache__-Ordner # Byte-compiled / optimized / DLL files
__pycache__/ __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 # Unit test / coverage reports
*.pyc htmlcov/
*.pyo .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) # Translations
.vscode/ *.mo
.idea/ *.pot
# Ignoriere andere temporäre Dateien # Django stuff:
*.log *.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?
...@@ -17,10 +17,15 @@ class MQTTClientHandler: ...@@ -17,10 +17,15 @@ class MQTTClientHandler:
client.subscribe(self.topic) client.subscribe(self.topic)
def on_message(self, client, userdata, msg): 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: try:
data = json.loads(msg.payload.decode())
value = float(data.get("value")) value = float(data.get("co2"))
self.influx_writer.write_point( self.influx_writer.write_point(
measurement="temperature", measurement="temperature",
......
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
}"
[project] [project]
name = "mqtt-influx-backend" name = "mqtt-influx-backend"
version = "0.1.0" version = "0.1.0"
description = "MQTT subscriber that stores data in InfluxDB" description = "Backend to write MQTT sensor data to InfluxDB"
authors = [ readme = "README.md"
{ name = "Emre Gezer", email = "21geem1bif@hft-stuttgart.de" } requires-python = ">=3.10"
]
dependencies = [ dependencies = [
"influxdb-client",
"paho-mqtt", "paho-mqtt",
"python-dotenv" "influxdb-client",
] ]
requires-python = ">=3.8"
[tool.uv] [build-system]
# Optional: für zukünftige Konfigurationen requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[tool.setuptools] [tool.setuptools]
package-dir = {"" = "src"} package-dir = {"" = "src"}
packages = ["mqtt_influx_backend"]
[tool.setuptools.packages.find]
where = ["src"]
Metadata-Version: 2.4 Metadata-Version: 2.4
Name: mqtt-influx-backend Name: mqtt-influx-backend
Version: 0.1.0 Version: 0.1.0
Summary: MQTT subscriber that stores data in InfluxDB Summary: Backend to write MQTT sensor data to InfluxDB
Author-email: Emre Gezer <21geem1bif@hft-stuttgart.de> Requires-Python: >=3.10
Requires-Python: >=3.8 Description-Content-Type: text/markdown
Requires-Dist: influxdb-client
Requires-Dist: paho-mqtt Requires-Dist: paho-mqtt
Requires-Dist: python-dotenv Requires-Dist: influxdb-client
README.md
pyproject.toml pyproject.toml
src/mqtt_influx_backend/InfluxDBWriter.py src/mqtt_influx_backend/InfluxDBWriter.py
src/mqtt_influx_backend/MQTTClientHandler.py src/mqtt_influx_backend/MQTTClientHandler.py
src/mqtt_influx_backend/Main.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/PKG-INFO
src/mqtt_influx_backend.egg-info/SOURCES.txt src/mqtt_influx_backend.egg-info/SOURCES.txt
src/mqtt_influx_backend.egg-info/dependency_links.txt src/mqtt_influx_backend.egg-info/dependency_links.txt
......
influxdb-client
paho-mqtt paho-mqtt
python-dotenv influxdb-client
...@@ -17,16 +17,21 @@ class MQTTClientHandler: ...@@ -17,16 +17,21 @@ class MQTTClientHandler:
client.subscribe(self.topic) client.subscribe(self.topic)
def on_message(self, client, userdata, msg): 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: try:
data = json.loads(msg.payload.decode())
value = float(data.get("value")) value = float(msg.get("co2"))
self.influx_writer.write_point( self.influx_writer.write_point(
measurement="temperature", measurement="sensor_data",
tags={"topic": msg.topic}, tags={"time": metadate["mac"]},
tags={"mac": metadate["mac"]},
fields={"value": value}, fields={"value": value},
timestamp=datetime.utcnow() timestamp=metadate["timestamp"]
) )
print("Wrote to InfluxDB:", value) print("Wrote to InfluxDB:", value)
except Exception as e: except Exception as e:
......
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
from mqtt_influx_backend.MQTTClientHandler import MQTTClientHandler from src.mqtt_influx_backend.MQTTClientHandler import MQTTClientHandler
from mqtt_influx_backend.InfluxDBWriter import InfluxDBWriter from src.mqtt_influx_backend.InfluxDBWriter import InfluxDBWriter
load_dotenv() load_dotenv()
......
uv.lock 0 → 100644
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 },
]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment