Azure / azure-sphere-gallery

A collection of inspirational, unmaintained Azure Sphere software and hardware designs ready to be reused.
37 stars 38 forks source link

Azure Sphere MQTT multiple device issue #86

Open nishithvpoojary opened 2 years ago

nishithvpoojary commented 2 years ago

Hi. I have tried the MQTT sample in the azure sphere gallery. It worked fine. Now I have been trying to connect 2 device. I got continuous error whenever I added more client on the python code. These are the changes done.

broker_address='test.mosquitto.org'

def on_message1(client1, userdata, message1): msg1=str(message1.payload.decode("utf-8")) print("received message =",msg1) msg1=msg1[len(msg1)-1]+msg1[0:len(msg1)-1] client1.publish("azuresphere/sample/device1",msg1)

def on_connect1(client1, userdata, flags, rc): print("Connected ") client1.subscribe("azuresphere/sample/host")

client1=paho.Client() client1.on_message=on_message1 client1.on_connect=on_connect1

def on_message2(client2, userdata, message2): msg2=str(message2.payload.decode("utf-8")) print("received message =",msg2) msg2=msg2[len(msg2)-1]+msg2[0:len(msg2)-1] client2.publish("azuresphere/sample/device2",msg2)

def on_connect2(client2, userdata, flags, rc): print("Connected ") client2.subscribe("azuresphere/sample/host")

client2=paho.Client() client2.on_message=on_message2 client2.on_connect=on_connect2 print("connecting to broker")

parentDir=os.path.abspath(os.path.join(Path(os.path.dirname(os.path.realpath(file))), os.pardir)) certsDir=os.path.join(parentDir,'HighLevelApp','Certs') print(certsDir) if (os.path.exists(os.path.join(certsDir,'mosquitto.org.crt'))): print('mosquitto cert exists') else: print("mosquitto cert doesn't exist :(")

client1.tls_set(ca_certs=os.path.join(certsDir,'mosquitto.org.crt'), certfile=os.path.join(certsDir,'client.crt'), keyfile=os.path.join(certsDir,'client.key'), cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2) client1.tls_insecure_set(False) client1.connect(broker_address, 8883, 60)

client2.tls_set(ca_certs=os.path.join(certsDir,'mosquitto.org.crt'), certfile=os.path.join(certsDir,'client.crt'), keyfile=os.path.join(certsDir,'client.key'), cert_reqs=ssl.CERT_REQUIRED, tls_version=ssl.PROTOCOL_TLSv1_2) client2.tls_insecure_set(False) client2.connect(broker_address, 8883, 60)

start loop to process received messages

client1.loop_forever() client2.loop_forever()

Thank You.

jamesadevine commented 2 years ago

Hi @nishithvpoojary

From reading the sample, you shouldn't need to create a second client for the host MQTT script. The host script acts as an intermediary between azure sphere devices and the mosquitto server.

I think all you need to do is load the HighLevel app on a second device and it should just work.