Created
December 23, 2022 17:11
-
-
Save DanEdens/7df817f5af197aec6ea283636b843a6b to your computer and use it in GitHub Desktop.
Get mqtt variable in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
def set_variable_from_mqtt_topic(name: str, awsip: str, awsport: str, topic: str) -> str: | |
""" | |
Subscribes to the specified MQTT topic and sets the value of the variable with the specified name to the value | |
received from the topic. | |
""" | |
# Replace any equals signs in the name with colons | |
name = name.replace("=", ":") | |
# Get the last character of the name | |
last = name[-1] | |
# Subscribe to the MQTT topic and get the first message received | |
output = subprocess.run(["mosquitto_sub", "-h", awsip, "-p", awsport, "-t", topic, "-C", "1"], capture_output=True) | |
get = output.stdout.decode().strip() | |
# Set the variable with the specified name to the value received from the MQTT topic | |
globals()[name] = get | |
return get | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment