58 lines
1.4 KiB
Python
Executable File

#!/usr/bin/env python
from paho.mqtt import client as mqtt_client
import time
mqtt_broker = "mqtt-dev-server.westus2-1.ts.eventgrid.azure.net"
mqtt_port = 8883
mqtt_topic = "wellnuotopics/topic1"
mqtt_client_id = "client1-authn-ID"
mqtt_client_id = "292"
connected = False
def connect_mqtt(client_id):
def on_connect(client, userdata, flags, rc):
global connected
if rc == 0:
print("Connected to MQTT Broker!")
connected = True
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION1, "asdasxzxdadaswd")
client.tls_set(
ca_certs='eventgrid.azure_full.pem',
certfile='../certs/client1-authn-ID.pem',
keyfile='../certs/client1-authn-ID.key'
)
client.username_pw_set(client_id, "")
client.on_connect = on_connect
client.connect(mqtt_broker, mqtt_port)
return client
def publish(client, topic, msg):
result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Sent `{msg}` to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
def main() -> None:
client = connect_mqtt(mqtt_client_id)
client.loop_start()
while(connected == False):
time.sleep(1)
publish(client, "/000013ed", "s")
time.sleep(2)
client.disconnect()
if __name__ == "__main__":
main()