title: Link Raspberry Pi #1 to Rapsberry Pi #2 using AWS as MQTT Broker
body: <p>So I have a Raspberry Pi #1 that will publish a MQTT Message to AWS through a topic <code>sensors/Button</code>. This will trigger when a button is pressed, as shown below.</p>
  
  <pre><code># Import SDK packages
  from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
  from time import sleep
  from gpiozero import Button
  from signal import pause
  
  button = Button(13, pull_up=False)
  
  def callMQTT():
      print("button is pressed.Sending to 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 and subscribe to AWS IoT
      my_rpi.connect()
      print("Connection Succesful")
  except:
      print("Unexpected error:", sys.exc_info()[0])
  
  button.when_pressed = callMQTT
  pause()
  </code></pre>
  
  <p>On Raspberry Pi #2, it will try to subscribe the MQTT from AWS using the same host, same Thing, same key and same certificate as Raspbery Pi #1. If a message is received, it will sound the buzzer and light up the LED as shown below.</p>
  
  <pre><code># Import SDK packages
  from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
  from time import sleep
  from gpiozero import Buzzer,LED
  import random
  import sys
  from datetime import datetime
  
  bz = Buzzer(22)
  led = LED(18)
  
  # Custom MQTT message callback
  def customCallback(client, userdata, message):
      print("Received a 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 and subscribe to AWS IoT
      my_rpi.connect()
  except:
      print("Unexpected error:", sys.exc_info()[0])
  
  while True:
  my_rpi.subscribe("sensors/Button", 1, customCallback)
  sleep(2)
  </code></pre>
  
  <p>However, this is not possible. When both programs run at the same time, Rasberry Pi #2 will always timeout. For some reason it allows 1 connection at a time only.
  I try to subscribe the topic directly through AWS when I run Raspberry Pi #1 code. It displays the message on AWS. Also, if I try to publish a message directly on AWS and run Raspberry Pi #2 code only, it works as well, but not when both running the code. The error I got on Raspberry Pi #2 is this:</p>
  
  <blockquote>
    <p>No handlers could be found for 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</p>
  </blockquote>
  
  <p>Anyone knows how to fix this? Thanks in Advance!</p>