Code: Select all
# python3.6
import random
import time
from paho.mqtt import client as mqtt_client
broker = '192.168.1.10'
port = 1883
topic = "test/image"
# Generate a Client ID with the subscribe prefix.
client_id = f'subscribe-{random.randint(0, 100)}'
# username = 'emqx'
# password = 'public'
def connect_mqtt() -> mqtt_client:
def on_connect(client, userdata, flags, rc):
if rc == 0:
print("Connected to MQTT Broker!")
else:
print("Failed to connect, return code %d\n", rc)
client = mqtt_client.Client(client_id)
# client.username_pw_set(username, password)
client.on_connect = on_connect
client.connect(broker, port)
return client
def publish(client):
msg_count = 1
while True:
time.sleep(1)
#msg = f"messages: {msg_count}"
f = open("/Users/darrylscott/Pictures/testimage.jpg", "rb")
imagestring = f.read()
byteArray = bytes(imagestring)
result = client.publish(topic, byteArray, 0)
#result = client.publish(topic, msg)
# result: [0, 1]
status = result[0]
if status == 0:
print(f"Send image to topic `{topic}`")
else:
print(f"Failed to send message to topic {topic}")
msg_count += 1
if msg_count > 2:
break
def run():
client = connect_mqtt()
client.loop_start()
publish(client)
client.loop_stop()
if __name__ == '__main__':
run()