Used this example, and I had problem with clientMQTT.tls_set(certify.where()) ()
CERTIFICATE_VERIFY_FAILED
May be it was because I use virtualbox+ubuntuserver with OR and trying to connect from PC+windows over local net.
So, if you have same problems, you can try this code (visual studio code + python3.10):
import paho.mqtt.client as mqtt
import time
import certifi
import ssl
import json
def on_connect(client, userdata, flags, rc):
if rc == 0:
print(âConnected to brokerâ)
global Connected #Use global variable
Connected = True #Signal connection
else:
print(âConnection failedâ)
def on_publish(client, userdata, result):
print(fâData published. Data: {attribute_value}')
pass
def on_message(client, userdata, message):
msg = json.loads(message.payload.decode(âutf-8â))
id = msg[âattributeStateâ][ârefâ][âidâ]
att_name = msg[âattributeStateâ][ârefâ][ânameâ]
att_value = msg[âattributeStateâ][âvalueâ]
print(fâMessage received. Asset ID: {id}. Attribute name: {att_name}. Attribute value: {att_value}')
Connected = False
username = âmaster:mqttuserâ
secret = âUwNbCqV_YOUR_SECRET_DYDvzP5Zâ
host = âsalmon.localâ
port = 8883
clientID = âWQTTwrtâ
assetID = â7PaWocH_YOUR_THING_pq93hâ
attributeWr = âwriteAttributeâ
attribute_value = 10
attributeRd = âsubscribeAttributeâ
clientMQTT = mqtt.Client(clientID)
clientMQTT.username_pw_set(username, password = secret)
//# the key steps here
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
//# if you do not want to check the cert hostname, skip it
//# context.check_hostname = False
clientMQTT.tls_set_context(context)
clientMQTT.on_connect = on_connect
clientMQTT.on_publish = on_publish
clientMQTT.on_message = on_message
clientMQTT.connect(host, port=port)
clientMQTT.loop_start()
while Connected != True:
time.sleep(0.1)
while attribute_value <=42 :
clientMQTT.publish(f"master/{clientID}/writeattributevalue/{attributeWr}/{assetID}â, attribute_value)
clientMQTT.subscribe(f"master/{clientID}/attribute/{attributeRd}/{assetID}â)
time.sleep(5)
attribute_value += 1
clientMQTT.disconnect()
clientMQTT.loop_stop()
Here:
//# the key steps here
context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
//# if you do not want to check the cert hostname, skip it
//# context.check_hostname = False
you need to remove //