fedex Posted November 11, 2015 at 12:30 PM Share Posted November 11, 2015 at 12:30 PM 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() Quote Link to comment Share on other sites More sharing options...
photron Posted November 11, 2015 at 01:13 PM Share Posted November 11, 2015 at 01:13 PM 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.