import os import paho.mqtt.client as mqtt from influxdb_client import InfluxDBClient from influxdb_client import Point from influxdb_client.client.write_api import SYNCHRONOUS import json import logging logging.basicConfig(level=logging.DEBUG) # mqttc = mqtt.Client("client-id") # but note that the client id must be unique on the broker. Leaving the client # id parameter empty will generate a random id for you. mqttc = mqtt.Client() influx_client = InfluxDBClient(url="http://localhost:8086", token="7KIKWL-7REcvJzNnDHKmvTDWqtJ_vflOfDXwncM78SI80xLEo2aXrjiLspCxPhVTiWFn4V6VWfAqedaxLJX9nQ==", org="sensors") write_api = influx_client.write_api(write_options=SYNCHRONOUS) def messageReceived(client, userdata, message): print("received topic" + str(message.topic) + "with payload: " + str(message.payload) + "\n") sensorId = message.topic.replace("schreiomat/","") print("sensor id: " + sensorId) print("sending") result = "" write_api.write("dac77b8b5d0fd121", "sensors", Point("soundlevel").tag("sensor", sensorId).field("dbA", float(message.payload))) def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) if (rc == 0): print("(Success)") else: print("(Fail)") # Subscribing in on_connect() means that if we lose the connection and # reconnect then subscriptions will be renewed. client.subscribe("schreiomat/#", 0) mqttc.username_pw_set("knurps", "LEPEZ1ELYDUmjg") mqttc.on_message = messageReceived mqttc.on_connect = on_connect mqttc.connect("ccc-p.org", 1883, 60) mqttc.loop_forever()