AWS Code Sample
Catalog demonstrates how to simulate soil moisture readings by generating random data. You push that data into the related shadow in AWS IoT.

# # Copyright 2010-2019, Inc. or its affiliates. All Rights Reserved. # # This file is licensed under the Apache License, Version 2.0 (the "License"). # You may not use this file except in compliance with the License. A copy of # the License is located at # # # # This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR # CONDITIONS OF ANY KIND, either express or implied. See the License for the # specific language governing permissions and limitations under the License. # from AWSIoTPythonSDK.MQTTLib import AWSIoTMQTTShadowClient import RPi.GPIO as GPIO import time # A random programmatic shadow client ID. SHADOW_CLIENT = "myShadowClient" # The unique hostname that AWS IoT generated for # this device. HOST_NAME = "" # The relative path to the correct root CA file for AWS IoT, # that you have already saved onto this device. ROOT_CA = "AmazonRootCA1.pem" # The relative path to your private key file that # AWS IoT generated for this device, that you # have already saved onto this device. PRIVATE_KEY = "yourkeyid-private.pem.key" # The relative path to your certificate file that # AWS IoT generated for this device, that you # have already saved onto this device. CERT_FILE = "yourkeyid-certificate.pem.crt.txt" # A programmatic shadow handler name prefix. SHADOW_HANDLER = "MyRPi" # Automatically called whenever the shadow is updated. def myShadowUpdateCallback(payload, responseStatus, token): print() print('UPDATE: $aws/things/' + SHADOW_HANDLER + '/shadow/update/#') print("payload = " + payload) print("responseStatus = " + responseStatus) print("token = " + token) # Create, configure, and connect a shadow client. myShadowClient = AWSIoTMQTTShadowClient(SHADOW_CLIENT) myShadowClient.configureEndpoint(HOST_NAME, 8883) myShadowClient.configureCredentials(ROOT_CA, PRIVATE_KEY, CERT_FILE) myShadowClient.configureConnectDisconnectTimeout(10) myShadowClient.configureMQTTOperationTimeout(5) myShadowClient.connect() # Create a programmatic representation of the shadow. myDeviceShadow = myShadowClient.createShadowHandlerWithName( SHADOW_HANDLER, True) # Represents the GPIO21 pin on the Raspberry Pi. channel = 21 # Use the GPIO BCM pin numbering scheme. GPIO.setmode(GPIO.BCM) # Receive input signals through the pin. GPIO.setup(channel, GPIO.IN) while True: if GPIO.input(channel): myDeviceShadow.shadowUpdate( '{"state":{"reported":{"moisture":"low"}}}', myShadowUpdateCallback, 5) else: myDeviceShadow.shadowUpdate( '{"state":{"reported":{"moisture":"okay"}}}', myShadowUpdateCallback, 5) # Wait for this test value to be added. time.sleep(60)

Sample Details

Service: iot

Last tested: 2019-01-25

Author: pccornel

Type: full-example

On this page: