text: import board
  import RPi.GPIO as GPIO
  import paho.mqtt.client as mqtt
  import time
  
  from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTClient
  import logging
  import time
  import argparse
  import json
  
  AllowedActions = ['both', 'publish', 'subscribe']
  
  
  # 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")
  
  
  # Read in command-line parameters
  parser = argparse.ArgumentParser()
  parser.add_argument("-e", "--endpoint", action="store", required=True, dest="host", help="Your AWS IoT custom endpoint")
  parser.add_argument("-r", "--rootCA", action="store", required=True, dest="rootCAPath", help="Root CA file path")
  parser.add_argument("-c", "--cert", action="store", dest="certificatePath", help="Certificate file path")
  parser.add_argument("-k", "--key", action="store", dest="privateKeyPath", help="Private key file path")
  parser.add_argument("-p", "--port", action="store", dest="port", type=int, help="Port number override")
  parser.add_argument("-w", "--websocket", action="store_true", dest="useWebsocket", default=False,
                      help="Use MQTT over WebSocket")
  parser.add_argument("-id", "--clientId", action="store", dest="clientId", default="basicPubSub",
                      help="Targeted client id")
  parser.add_argument("-t", "--topic", action="store", dest="topic", default="tns/bot/garage", help="Targeted topic")
  parser.add_argument("-m", "--mode", action="store", dest="mode", default="both",
                      help="Operation modes: %s" % str(AllowedActions))
  parser.add_argument("-M", "--message", action="store", dest="message", default="MSG001 Garage door opened!",
                      help="Message to publish")
  
  args = parser.parse_args()
  host = args.host
  rootCAPath = args.rootCAPath
  certificatePath = args.certificatePath
  privateKeyPath = args.privateKeyPath
  port = args.port
  useWebsocket = args.useWebsocket
  clientId = args.clientId
  topic = args.topic
  
  if args.mode not in AllowedActions:
      parser.error("Unknown --mode option %s. Must be one of %s" % (args.mode, str(AllowedActions)))
      exit(2)
  
  if args.useWebsocket and args.certificatePath and args.privateKeyPath:
      parser.error("X.509 cert authentication and WebSocket are mutual exclusive. Please pick one.")
      exit(2)
  
  if not args.useWebsocket and (not args.certificatePath or not args.privateKeyPath):
      parser.error("Missing credentials for authentication.")
      exit(2)
  
  # Port defaults
  if args.useWebsocket and not args.port:  # When no port override for WebSocket, default to 443
      port = 443
  if not args.useWebsocket and not args.port:  # When no port override for non-WebSocket, default to 8883
      port = 8883
  
  # Configure logging
  logger = logging.getLogger("AWSIoTPythonSDK.core")
  logger.setLevel(logging.DEBUG)
  streamHandler = logging.StreamHandler()
  formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  streamHandler.setFormatter(formatter)
  logger.addHandler(streamHandler)
  
  # Init AWSIoTMQTTClient
  iotClient = None
  if useWebsocket:
      # print("websocket!")
      iotClient = AWSIoTMQTTClient(clientId, useWebsocket=True)
      iotClient.configureEndpoint(host, port)
      iotClient.configureCredentials(rootCAPath)
  else:
      # print("NON-websocket!")
      iotClient = AWSIoTMQTTClient(clientId)
      iotClient.configureEndpoint(host, port)
      iotClient.configureCredentials(rootCAPath, privateKeyPath, certificatePath)
  
  # AWSIoTMQTTClient connection configuration
  iotClient.configureAutoReconnectBackoffTime(1, 32, 20)
  iotClient.configureOfflinePublishQueueing(-1)  # Infinite offline Publish queueing
  iotClient.configureDrainingFrequency(2)  # Draining: 2 Hz
  iotClient.configureConnectDisconnectTimeout(10)  # 10 sec
  iotClient.configureMQTTOperationTimeout(5)  # 5 sec
  
  # Connect and subscribe to AWS IoT
  iotClient.connect()
  if args.mode == 'both' or args.mode == 'subscribe':
      iotClient.subscribe(topic, 1, customCallback)
  time.sleep(2)
  
  CONTACT_SENSOR = 21
  GPIO.setmode(GPIO.BCM)
  
  # Configure the contact sensor
  GPIO.setup(CONTACT_SENSOR, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  
  while True:
      if GPIO.input(CONTACT_SENSOR):
          print("open!")
          time.sleep(5)
          if args.mode == 'both' or args.mode == 'publish':
              message = dict(message=args.message)
              messageJson = json.dumps(message)
              iotClient.publish(topic, messageJson, 1)
              if args.mode == 'publish':
                  print('Published topic %s: %s\n' % (topic, messageJson))
              time.sleep(600)
      else:
          pass
          # print("closed")
      time.sleep(1)
  