示例#1
0
def start():

  # Make sure that Client ID is unique for each device
  myAWSIoTMQTTShadowClient = AWSIoTPyMQTT.AWSIoTMQTTShadowClient("IoTPublishDevice")
  myAWSIoTMQTTShadowClient.configureEndpoint("xxxxxxxxxxxxxxxxxx", 8883)  # Enter the endpoint for your thing
  myAWSIoTMQTTShadowClient.configureCredentials("xxxxxxxxx-root-ca.pem", "xxxxxxxx-private.pem.key", "xxxxxxx.pem.crt") # Enter the path to relevant Key and Root CA

  
  print(myAWSIoTMQTTShadowClient.connect())
  
  BotShadow = myAWSIoTMQTTShadowClient.createShadowHandlerWithName("YourThingName",True)  #Replace with your thing name
  
  
  payload = {"state":{"reported":{"key1":"value1","key2":"value2","key3":"value3","key4":"value4"}}}
    

  try:
     #Updating Device Shadow
     print(BotShadow.shadowUpdate(json.dumps(payload), customShadowCallback_Update, 2))
     time.sleep(2)
  except: 
     print("Error Connecting to AWS IOT")
示例#2
0
    port = 443
if not args.useWebsocket and not args.port:  # When no port override for non-WebSocket, default to 8883
    port = 8883

# Configure logging
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(
    logging.Formatter('%(asctime)s %(name)s %(levelname)s - %(message)s'))
logger = logging.getLogger("jd")
logger.setLevel(logging.DEBUG)
logger.addHandler(streamHandler)

# Init AWSIoTMQTTShadowClient
shadowClient = None
if useWebsocket:
    shadowClient = mqtt.AWSIoTMQTTShadowClient(clientId, useWebsocket=True)
    shadowClient.configureEndpoint(host, port)
    shadowClient.configureCredentials(rootCAPath)
else:
    shadowClient = mqtt.AWSIoTMQTTShadowClient(clientId)
    shadowClient.configureEndpoint(host, port)
    shadowClient.configureCredentials(rootCAPath, privateKeyPath,
                                      certificatePath)

# AWSIoTMQTTShadowClient configuration
shadowClient.configureAutoReconnectBackoffTime(1, 32, 20)
shadowClient.configureConnectDisconnectTimeout(10)  # 10 sec
shadowClient.configureMQTTOperationTimeout(5)  # 5 sec

mqttClient = shadowClient.getMQTTConnection()
mqttClient.configureOfflinePublishQueueing(-1, mqtt.DROP_OLDEST)
        "fields": {
            "red": red,
            "blue": blue,
            "green": green,
            "color": color
        }
    })

    client.write_points(data)
    print("--------------\n\n")


# Create an AWS IoT MQTT Client using TLSv1.2 Mutual Authentication

# Make sure that the Client ID is unique for each device
myAWSIoTMQTTShadowClient = AWSIoTPyMQTT.AWSIoTMQTTShadowClient("GrafanaClient")
myAWSIoTMQTTShadowClient.configureEndpoint(
    "xxxxxxxxxxxxxxxxxx", 8883)  # Enter the endpoint for your thing
myAWSIoTMQTTShadowClient.configureCredentials(
    "xxxxxxxxx-root-ca.pem", "xxxxxxxx-private.pem.key",
    "xxxxxxx.pem.crt")  # Enter the path to relevant Key and Root CA

print(myAWSIoTMQTTShadowClient.connect())

BotShadow = myAWSIoTMQTTShadowClient.createShadowHandlerWithName(
    "YourThingName", True)  #Replace with your thing name

while True:
    try:
        print(BotShadow.shadowGet(customCallback, 2))
        time.sleep(2)
def loop():
    color = "No Color"
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(signal, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(s2, GPIO.OUT)
    GPIO.setup(s3, GPIO.OUT)

    # Make sure that Client ID is unique for each device
    myAWSIoTMQTTShadowClient = AWSIoTPyMQTT.AWSIoTMQTTShadowClient(
        "RPIColorSensor")
    myAWSIoTMQTTShadowClient.configureEndpoint("XXXXXXXX.amazonaws.com", 8883)
    myAWSIoTMQTTShadowClient.configureCredentials("root-ca.pem",
                                                  "rpi-private.pem.key",
                                                  "rpi-certificate.pem.crt")

    print(myAWSIoTMQTTShadowClient.connect())

    BotShadow = myAWSIoTMQTTShadowClient.createShadowHandlerWithName(
        "RPI3", True)

    print("\n")
    while (1):
        print(" --- Reading Values ---")
        GPIO.output(s2, GPIO.LOW)
        GPIO.output(s3, GPIO.LOW)
        time.sleep(0.3)
        start = time.time()

        for impulse_count in range(NUM_CYCLES):
            GPIO.wait_for_edge(signal, GPIO.FALLING)
        duration = time.time() - start  #seconds to run for loop
        red = NUM_CYCLES / duration  #in Hz
        print("red value - ", red)
        if (red > 25000):
            color = "Red"
        GPIO.output(s2, GPIO.LOW)
        GPIO.output(s3, GPIO.HIGH)
        time.sleep(0.3)

        start = time.time()
        for impulse_count in range(NUM_CYCLES):
            GPIO.wait_for_edge(signal, GPIO.FALLING)
        duration = time.time() - start
        blue = NUM_CYCLES / duration
        print("blue value - ", blue)
        if (blue > 27000):
            color = "Blue"

        GPIO.output(s2, GPIO.HIGH)
        GPIO.output(s3, GPIO.HIGH)
        time.sleep(0.3)

        start = time.time()
        for impulse_count in range(NUM_CYCLES):
            GPIO.wait_for_edge(signal, GPIO.FALLING)
        duration = time.time() - start
        green = NUM_CYCLES / duration
        print("green value - ", green)
        if (green > 27000):
            color = "Green"
        if (green > 22000 and blue < 25000):
            color = "Green"

        if ((red < 19000) and (green < 19000) and (blue < 19000)):
            color = "NoColor"
        # Create message payload
        print(color)
        payload = {
            "state": {
                "reported": {
                    "red": str(red),
                    "blue": str(blue),
                    "green": str(green),
                    "color": str(color)
                }
            }
        }

        try:
            #Updating Device Shadow
            print(
                BotShadow.shadowUpdate(json.dumps(payload),
                                       customShadowCallback_Update, 2))
            time.sleep(2)
        except:
            print("Error Connecting to AWS IOT")