# cPy - MQTT client
# provided by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-09-02  Initial release

import  time
from    paho                import  mqtt
import  paho.mqtt.client    as      paho

# https://pypi.org/project/paho-mqtt/#getting-started

# ==================================== Main ======================================

def main():

    def on_connect(client, userdata, flags, rc, properties=None):
        print(f'CONNACK received with code {rc}')


    def on_message(client, userdata, msg):
        print(f'{msg.topic} {msg.qos} {msg.payload}')


    client              = paho.Client(client_id="", userdata=None, protocol=paho.MQTTv5)
    client.on_connect   = on_connect
    client.on_message   = on_message
    client.tls_set(tls_version=mqtt.client.ssl.PROTOCOL_TLS)
    client.username_pw_set('pietje', 'Ab123456')
    client.connect('b3b6d663d3c241c3892ab7d10336xxxx.s1.eu.hivemq.cloud', 8883)
    client.subscribe('me', qos=1)

    client.loop_start()
    for i in range(10):
        client.publish('me', payload=f'from Linux {i}', qos=1)
        time.sleep(1)
    client.loop_stop()
    # client.loop_forever()


main()

# ==================================== End =====================================
