Jump to content

[Python] Python MQTT


Recommended Posts

Ich möchte Temperatur Daten mit einer Callback Funktion an meinen MQTT Broker senden.

Welche Variable des example_callback scripts enthält denn die Temperaturwerte?

Mit t.CALLBACK_TEMPERATURE bekomme ich immer nur den Wert 8?

 

meine Skript sieht so aus:

 

HOST = "10.208.216.149"

PORT = 4223

UID = "t8u" # Change to your UID

 

import time

 

import paho.mqtt.client as mqtt

 

from tinkerforge.ip_connection import IPConnection

from tinkerforge.bricklet_temperature import BrickletTemperature

 

def on_connect(client, userdata, flags, rc):

    print("Connected with result code " + str(rc))

 

# Callback function for temperature callback (parameter has unit °C/100)

def cb_temperature(temperature):

    print("Temperature: " + str(temperature/100.0) + " °C")

 

   

if __name__ == "__main__":

    ipcon = IPConnection() # Create IP connection

    t = BrickletTemperature(UID, ipcon) # Create device object

 

    ipcon.connect(HOST, PORT) # Connect to brickd

    # Don't use device before ipcon is connected

 

    # Register temperature callback to function cb_temperature

    t.register_callback(t.CALLBACK_TEMPERATURE, cb_temperature)

 

    # Set period for temperature callback to 1s (1000ms)

    # Note: The temperature callback is only called every second

    #      if the temperature has changed since the last call!

    t.set_temperature_callback_period(1000)

 

client = mqtt.Client()

client.on_connect = on_connect

 

client.connect("vmiot01srv", 1883, 60)

 

#client.loop_start()

 

while True:

    time.sleep(2)

    client.publish("tinkerforge/bricklet/temperature/t8u/temperature", str(t.CALLBACK_TEMPERATURE))

 

 

raw_input()

ipcon.disconnect()

Link to comment
Share on other sites

Du registrierst hier die cb_temperature Funktion für den Temperatur Callback:

 

# Register temperature callback to function cb_temperature
t.register_callback(t.CALLBACK_TEMPERATURE, cb_temperature)

 

Die t.CALLBACK_TEMPERATURE Konstante identifiziert den Temperatur Callback, damit register_callback weiss worum es geht.

 

Die Bindings rufen dann deine cb_temperature Funktion auf und übergeben die aktuelle Temperatur sobald das Bricklet eine Änderung der Temperatur meldet.

 

Du kannst also deinen publish() Aufruf in die cb_temperature() Funktion verlegen.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...