python - Link Raspberry Pi #1 to Rapsberry Pi #2 using AWS as MQTT Broker -
so have raspberry pi #1 publish mqtt message aws through topic sensors/button
. trigger when button pressed, shown below.
# import sdk packages awsiotpythonsdk.mqttlib import awsiotmqttclient time import sleep gpiozero import button signal import pause button = button(13, pull_up=false) def callmqtt(): print("button pressed.sending mqtt") mqtt_message = "{\"message\":\"button_pressed\"}" print(mqtt_message) my_rpi.publish("sensors/button", mqtt_message, 1) print("message published!") sleep(5) host="host.amazonaws.com" rootcapath = "rootca.pem" certificatepath = "certificate.pem.crt" privatekeypath = "private.pem.key" try: my_rpi = awsiotmqttclient("basicpubsub") my_rpi.configureendpoint(host,8883) my_rpi.configurecredentials(rootcapath, privatekeypath, certificatepath) my_rpi.configureofflinepublishqueueing(-1) # infinite offline publish queueing my_rpi.configuredrainingfrequency(2) # draining: 2 hz # connect , subscribe aws iot my_rpi.connect() print("connection succesful") except: print("unexpected error:", sys.exc_info()[0]) button.when_pressed = callmqtt pause()
on raspberry pi #2, try subscribe mqtt aws using same host, same thing, same key , same certificate raspbery pi #1. if message received, sound buzzer , light led shown below.
# import sdk packages awsiotpythonsdk.mqttlib import awsiotmqttclient time import sleep gpiozero import buzzer,led import random import sys datetime import datetime bz = buzzer(22) led = led(18) # custom mqtt message callback def customcallback(client, userdata, message): print("received new message: ") print(message.payload) print("from topic: ") print(message.topic) print("--------------\n\n") timestring = str(datetime.now()) print("doorbell pressed") bz.on() led.blink() sleep(1) bz.off() led.off() host="host.amazonaws.com" rootcapath = "rootca.pem" certificatepath = "certificate.pem.crt" privatekeypath = "private.pem.key" try: my_rpi = awsiotmqttclient("basicpubsub") my_rpi.configureendpoint(host, 8883) my_rpi.configurecredentials(rootcapath, privatekeypath, certificatepath) my_rpi.configureofflinepublishqueueing(-1) # infinite offline publish queueing my_rpi.configuredrainingfrequency(2) # draining: 2 hz my_rpi.configureconnectdisconnecttimeout(10) # 10 sec my_rpi.configuremqttoperationtimeout(5) # 5 sec # connect , subscribe aws iot my_rpi.connect() except: print("unexpected error:", sys.exc_info()[0]) while true: my_rpi.subscribe("sensors/button", 1, customcallback) sleep(2)
however, not possible. when both programs run @ same time, rasberry pi #2 timeout. reason allows 1 connection @ time only. try subscribe topic directly through aws when run raspberry pi #1 code. displays message on aws. also, if try publish message directly on aws , run raspberry pi #2 code only, works well, not when both running code. error got on raspberry pi #2 this:
no handlers found logger "awsiotpythonsdk.core.protocol.mqttcore" traceback (most recent call last): file "doorbell_indoor.py", line 72, in my_rpi.subscribe("sensors/button", 1, customcallback) file "/usr/local/lib/python2.7/dist-packages/awsiotpythonsdk/mqttlib.py", line 491, in subscribe return self._mqttcore.subscribe(topic, qos, callback) file "/usr/local/lib/python2.7/dist-packages/awsiotpythonsdk/core/protocol/mqttcore.py", line 416, in subscribe raise subscribetimeoutexception() awsiotpythonsdk.exception.awsiotexceptions.subscribetimeoutexception
anyone knows how fix this? in advance!
i don't see documented in python sdk docs, in java sdk docs says:
clientid - client id uniquely identify mqtt connection. 2 clients same client id not allowed connected concurrently same endpoint.
try using unique clientid
value each connection instead of "basicpubsub"
.
Comments
Post a Comment