Created
December 23, 2022 17:03
-
-
Save DanEdens/bd598f956ae55e6df4d3ac980557634e to your computer and use it in GitHub Desktop.
get file from mqtt 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 os | |
import subprocess | |
def subscribe_to_mqtt_topic( | |
topic: str, path: str, awsip: str=os.getenv("AWSIP"), | |
awsport: str=os.getenv("AWSPORT")) -> str: | |
""" | |
Subscribes to the MQTT topic with the specified name and writes the received messages to a file with the same name. | |
Returns the file path of the created file. | |
""" | |
# Replace any equals signs in the name with colons | |
topic = topic.replace("=", ":") | |
# Get the last character of the name | |
last = topic[-1] | |
# Subscribe to the MQTT topic and write the received messages to a file with the same name | |
subprocess.run([ | |
"mosquitto_sub", "-h", awsip, "-p", awsport, | |
"-t", f"commands/{topic}", "-C", "1"], | |
stdout=open(f"{path}/{last}.cmd", "w") | |
) | |
# Return the file path | |
return f"{path}/{last}.cmd" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment