简体   繁体   English

使用 mqtt 将两个值与 python 进行比较

[英]compare two values with python using mqtt

I'm trying to compare two values received from mqtt and send a publish message depending of the comparation.我正在尝试比较从 mqtt 收到的两个值并根据比较发送发布消息。 After searching a lot I get it working when I only compar if the two values are equal o diferent.经过大量搜索后,当我仅比较两个值是否相等或不同时,我就可以正常工作。 But, what I really want to do is that when I receive a message, the program keep it (value of 230 for example), then if the new one is of the same condition( value of 250, still less than 300), so no action is required, till the value received is bigger than 300.但是,我真正想做的是,当我收到一条消息时,程序会保留它(例如值为 230),那么如果新的条件相同(值为 250,仍然小于 300),所以无需任何操作,直到收到的值大于 300。

any suggestion of how to do it?有什么建议吗? I don't need a direct answer but if you know where I can look to more information or examples like what I'm trying to do it will be helpfull.我不需要直接的答案,但是如果您知道我可以在哪里查找更多信息或示例,例如我正在尝试做的事情,那将会很有帮助。

import paho.mqtt.client as mqtt
import time

client = mqtt.Client()
client.connect("test.mosquitto.org", 1883)

def on_connect(client, userdata, flags, rc):
    client.subscribe("Medir")

client.publish("Valve_OC", "Cerrar")
print("cierre de la valvula")
#initialize the new and last value
new_value=0
last_value = 0 
print("initial values: ", new_value, last_value)

def on_message_medir(client, userdata, message):  # The callback for when a PUBLISH message is received from topic MEDIR.
    global new_value
    global last_value
    new_value=int(float(str(message.payload.decode("utf-8"))))
    print("new_value:", new_value)
    compar_value(new_value, last_value) #function to compare the values

def compar_value(new_value, last_value): 
    if new_value != last_value:
        if new_value<300:
            client.publish("Valve_OC", "Abrir,100")
            last_value = new_value
            time.sleep(3)
        
        elif (new_value>300): 
            client.publish("Valve_OC", "Abrir,60")
            last_value=new_value       
            time.sleep(3)
       
def on_publish(client, obj, mid):
    print("Mensaje: " + str(mid))
def on_subscribe(client, obj, mid, granted_qos):
    print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(client, obj, level, string):
    print(f"Log: {string}")
client = mqtt.Client()
client.on_connect = on_connect
client.on_publish = on_publish
client.message_callback_add("Medir", on_message_medir)
client.on_subscribe = on_subscribe
client.on_log = on_log
client.connect("test.mosquitto.org", 1883)

client.loop_forever()

You should just keep comparing the value, probably converting it to an int before comparing.您应该继续比较该值,可能在比较之前将其转换为 int。 Like this:像这样:

def compar_value(new_value): 
    if int(new_value) < 300:
        client.publish("Valve_OC", "Abrir,100")
        last_value = new_value
    else
        last_value = new_value

and when the message comes, you compare the value.当消息到来时,你比较值。 And if you want to keep the last value, just keep updating the last_value variable, that sould be global.如果你想保留最后一个值,只需不断更新 last_value 变量,它应该是全局的。

I think you don't need the global redeclaration part when inside the function, since your variables are already globally delcared.我认为在 function 内部时您不需要全局重新声明部分,因为您的变量已经被全局删除。 Besides the sleep part also doesn't does any help, since the MQTT messages are all asynchronous.除了睡眠部分也没有任何帮助,因为 MQTT 消息都是异步的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM