Navigation

    Logo
    • Register
    • Login
    • Search
    • Recent
    • Tags
    • Unread
    • Categories
    • Unreplied
    • Popular
    • GitHub
    • Docu
    • Hilfe
    1. Home
    2. Deutsch
    3. ioBroker Allgemein
    4. [Gelöst] MQTT-Verbindung via Python script

    NEWS

    • Neuer Blog: Fotos und Eindrücke aus Solingen

    • ioBroker@Smart Living Forum Solingen, 14.06. - Agenda added

    • ioBroker goes Matter ... Matter Adapter in Stable

    [Gelöst] MQTT-Verbindung via Python script

    This topic has been deleted. Only users with topic management privileges can see it.
    • S
      Steinmetz_4 @Steinmetz_4 last edited by

      @steinmetz_4

      Ich denke mir fehlt der Punkt "Topic"

      4326e3fa-f434-4c82-b36b-d7e160e3c9c2-image.png

      Aber was genau ist der, die, das, Topic?

      586e2e08-8a87-4631-a152-d3bd206ba3e6-image.png

      Codierknecht 1 Reply Last reply Reply Quote 0
      • Codierknecht
        Codierknecht Developer Most Active @Steinmetz_4 last edited by

        @steinmetz_4
        Wenn der Zero an den ioBroker senden soll, dann ist der ioB der Client.
        Der hat eine IP und einen Port, an den der Zero die Daten sendet. Das musst Du dann im Script entsprechend einstellen.
        Der Adapter erzeugt dann selbst beim Empfang der Daten die passenden DP.

        Homoran 1 Reply Last reply Reply Quote 0
        • Homoran
          Homoran Global Moderator Administrators @Codierknecht last edited by

          @codierknecht sagte in MQTT-Verbindung:

          dann ist der ioB der Client.

          aber irgendwo muss ein Broker sein.
          der ioBroker MQTT Client abonniert dann dort seine Topics

          Codierknecht 1 Reply Last reply Reply Quote 0
          • Codierknecht
            Codierknecht Developer Most Active @Homoran last edited by

            @homoran sagte in MQTT-Verbindung:

            aber irgendwo muss ein Broker sein.

            Ich sehe das so, dass das Python-Script sendet - also den Broker spielt.

            Aber das ist diese komische "Sprache", die Blöcke über die Einrückung definiert, oder?
            Die ist in meiner internen Hirn-Firewall automatisch geblockt 😁

            Marc Berg 1 Reply Last reply Reply Quote 0
            • Marc Berg
              Marc Berg Most Active @Codierknecht last edited by Marc Berg

              @codierknecht sagte in MQTT-Verbindung:

              @homoran sagte in MQTT-Verbindung:

              aber irgendwo muss ein Broker sein.

              Ich sehe das so, dass das Python-Script sendet - also den Broker spielt.

              Nein, das Skript ist der MQTT-Client, der an den Broker (der ja schon erfolgreich läuft) senden/publish will. Allerdings ist der Server in der Variable falsch hinterlegt. (FQDN / IP-Adresse). Der Port 8883 scheint übereinzustimmen.

              @Steinmetz_4 Ist das richtig, dass bereits eine Broker-Instanz läuft? Dann benötigst du keine zweite. Zeig' mal bitte die Einstellungen dieser Instanz.

              S 1 Reply Last reply Reply Quote 0
              • S
                Steinmetz_4 @Marc Berg last edited by

                @marc-berg

                Da bin ich wieder...

                Das Python script soll senden (temperatur) an IoBroker MQTT

                c031f7f0-a911-4540-b894-6ad740d0b4a2-image.png

                35e4a3db-7aa9-40c5-9eb7-a9805924384e-image.png

                Ich bin auch schon ein Stück weiter...
                das script versucht zu senden, kommt aber nicht durch...

                600597d5-8853-4ef8-90f4-d4d4fd45556b-image.png !

                #!/usr/bin/python3
                import sys
                import paho.mqtt.client as mqtt
                import ssl
                 
                # set the variables
                # Path to the Sensor systempath
                # 28-01142f7ba71a has to be changed to you sensor path!
                sensor = '/sys/bus/w1/devices/28-3c01f096929e/w1_slave'
                broker='FQDN / 192.168.178.185'
                port=1879
                publish_topic="house/pi-ds18b20"
                clientid='python-mqtt-ds18b20'
                username='Tom'
                password='xxxxxxxxxx'
                insecure=True
                qos=1
                retain_message=True
                
                # do the stuff
                def readTempSensor(sensorName) :
                    f = open(sensorName, 'r')
                    lines = f.readlines()
                    f.close()
                    return lines
                 
                def readTempLines(sensorName) :
                    lines = readTempSensor(sensorName)
                    while lines[0].strip()[-3:] != 'YES':
                        time.sleep(0.2)
                        lines = readTempSensor(sensorName)
                    temperaturStr = lines[1].find('t=')
                    if temperaturStr != -1 :
                        tempData = lines[1][temperaturStr+2:]
                        tempCelsius = float(tempData) / 1000.0
                        tempKelvin = 273 + float(tempData) / 1000
                        tempFahrenheit = float(tempData) / 1000 * 9.0 / 5.0 + 32.0
                        return [tempCelsius, tempKelvin, tempFahrenheit]
                
                #MQTT Connection
                client=mqtt.Client(clientid)
                client.username_pw_set(username, password)
                client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
                client.tls_insecure_set(insecure)
                client.connect(broker, port)
                client.loop_start()
                
                client.publish("{}/temperature".format(publish_topic),"{:.2f}".format(readTempLines(sensor)[0]),qos,retain_message)
                
                client.disconnect()
                client.loop_stop()
                

                Mfg

                Homoran 1 Reply Last reply Reply Quote 0
                • Homoran
                  Homoran Global Moderator Administrators @Steinmetz_4 last edited by

                  @steinmetz_4 sagte in MQTT-Verbindung:

                  Das Python script soll senden (temperatur) an IoBroker MQTT

                  und der ist als Broker konfiguriert?
                  und unter der IP

                  @steinmetz_4 sagte in MQTT-Verbindung:

                  192.168.178.185

                  zu erreichen?

                  S 1 Reply Last reply Reply Quote 0
                  • S
                    Steinmetz_4 @Homoran last edited by

                    @homoran

                    Wen meinst Du?
                    MQTT Adapter? läuft in IoBroker unter 192.168.178.185

                    Homoran 1 Reply Last reply Reply Quote 0
                    • Homoran
                      Homoran Global Moderator Administrators @Steinmetz_4 last edited by

                      @steinmetz_4 sagte in MQTT-Verbindung:

                      MQTT Adapter

                      ja, den!

                      @homoran sagte in MQTT-Verbindung:

                      der ist als Broker konfiguriert?

                      als Client nutzt er dir nichts.

                      S 1 Reply Last reply Reply Quote 0
                      • S
                        Steinmetz_4 @Homoran last edited by

                        @homoran

                        Ich denke schon.

                        dc56de23-c039-4973-a8cb-1af0e632e691-image.png

                        Watermeter läuft damit.

                        929a75b2-d2f7-4d76-b907-59b92e585d8e-image.png

                        Homoran 1 Reply Last reply Reply Quote 0
                        • Homoran
                          Homoran Global Moderator Administrators @Steinmetz_4 last edited by

                          @steinmetz_4 sagte in MQTT-Verbindung:

                          Ich denke schon.

                          jepp! dann ist die Frage auch eindeutig geklärt

                          S 2 Replies Last reply Reply Quote 0
                          • S
                            Steinmetz_4 @Homoran last edited by

                            @homoran

                            Ich kann Dir nicht mehr sagen.
                            Wenn ich wüsste was falsch ist, würde ich hier nicht fragen.

                            🤷‍♂️

                            Marc Berg 1 Reply Last reply Reply Quote 0
                            • Marc Berg
                              Marc Berg Most Active @Steinmetz_4 last edited by Marc Berg

                              @steinmetz_4 ist das das aktive Skript???

                              b45847f4-bad6-48c9-a223-360125309150-grafik.png

                              S 1 Reply Last reply Reply Quote 0
                              • S
                                Steinmetz_4 @Marc Berg last edited by

                                @marc-berg

                                Sorry, nein !

                                ich tüftel an allen Ecken und Enden!#

                                aktuel:

                                #!/usr/bin/python3
                                import sys
                                import paho.mqtt.client as mqtt
                                import ssl
                                
                                # set the variables
                                # Path to the Sensor systempath
                                # 28-01142f7ba71a has to be changed to you sensor path!
                                sensor = '/sys/bus/w1/devices/28-3c01f096929e/w1_slave'
                                broker='192.168.178.185'
                                port=1879
                                publish_topic="house/pi-ds18b20"
                                clientid='python-mqtt-ds18b20'
                                username='Tom'
                                password='Atomameise'
                                insecure=True
                                qos=1
                                retain_message=True
                                
                                # do the stuff
                                def readTempSensor(sensorName) :
                                    f = open(sensorName, 'r')
                                    lines = f.readlines()
                                    f.close()
                                    return lines
                                
                                def readTempLines(sensorName) :
                                    lines = readTempSensor(sensorName)
                                    while lines[0].strip()[-3:] != 'YES':
                                        time.sleep(0.2)
                                        lines = readTempSensor(sensorName)
                                    temperaturStr = lines[1].find('t=')
                                    if temperaturStr != -1 :
                                        tempData = lines[1][temperaturStr+2:]
                                        tempCelsius = float(tempData) / 1000.0
                                        tempKelvin = 273 + float(tempData) / 1000
                                        tempFahrenheit = float(tempData) / 1000 * 9.0 / 5.0 + 32.0
                                        return [tempCelsius, tempKelvin, tempFahrenheit]
                                
                                #MQTT Connection
                                client=mqtt.Client(clientid)
                                client.username_pw_set(username, password)
                                client.tls_set(cert_reqs=ssl.CERT_NONE) #no client certificate needed
                                client.tls_insecure_set(insecure)
                                client.connect(broker, port)
                                client.loop_start()
                                
                                client.publish("{}/temperature".format(publish_topic),"{:.2f}".format(readTempLines(sensor)[0]),qos,retain_message)
                                
                                client.disconnect()
                                client.loop_stop()
                                
                                
                                

                                Mfg

                                Marc Berg 1 Reply Last reply Reply Quote 0
                                • S
                                  Steinmetz_4 @Homoran last edited by Steinmetz_4

                                  @homoran sagte in MQTT-Verbindung:

                                  @steinmetz_4 sagte in MQTT-Verbindung:

                                  Ich denke schon.

                                  jepp! dann ist die Frage auch eindeutig geklärt

                                  Verzeiht

                                  Ich weiß, dass ich nichts weiß (https://de.wikipedia.org/wiki/Ich_weiß,_dass_ich_nichts_weiß)

                                  1 Reply Last reply Reply Quote 0
                                  • Marc Berg
                                    Marc Berg Most Active @Steinmetz_4 last edited by Marc Berg

                                    @steinmetz_4
                                    Wenn ich die Fehlermeldung richtig interpretiere, versucht dein Skript eine verschlüsselte Verbindung, die du hier:

                                    client.tls_set….
                                    

                                    auch so aktivierst. Dein Broker ist aber nicht mit SSL konfiguriert.
                                    Schalte SSL im Skript ab.

                                    Und: Du musst ruhiger werden … 🙂

                                    S 1 Reply Last reply Reply Quote 0
                                    • S
                                      Steinmetz_4 @Marc Berg last edited by

                                      @marc-berg

                                      Tada, es funktioniert!
                                      Ich danke Dir!
                                      War ein holpriger Weg aber es läuft...

                                      Habe beide Punkte auskommentiert.

                                      afad0031-d184-471a-b744-48fb1d2b13a4-image.png

                                      P.S. Ich bin die Ruhe in Person...

                                      Mfg
                                      Tom

                                      1 Reply Last reply Reply Quote 0
                                      • First post
                                        Last post

                                      Support us

                                      ioBroker
                                      Community Adapters
                                      Donate
                                      FAQ Cloud / IOT
                                      HowTo: Node.js-Update
                                      HowTo: Backup/Restore
                                      Downloads
                                      BLOG

                                      853
                                      Online

                                      31.9k
                                      Users

                                      80.1k
                                      Topics

                                      1.3m
                                      Posts

                                      4
                                      21
                                      2285
                                      Loading More Posts
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      Community
                                      Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
                                      The ioBroker Community 2014-2023
                                      logo