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 random, time # A random programmatic shadow client ID. SHADOW_CLIENT = "myShadowClient" # The unique hostname that &IoT; generated for # this device. HOST_NAME = "" # The relative path to the correct root CA file for &IoT;, # which you have already saved onto this device. ROOT_CA = "AmazonRootCA1.pem" # The relative path to your private key file that # &IoT; generated for this device, which you # have already saved onto this device. PRIVATE_KEY = "yourkeyid-private.pem.key" # The relative path to your certificate file that # &IoT; generated for this device, which 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) # Keep generating random test data until this script # stops running. # To stop running this script, press Ctrl+C. while True: # Generate random True or False test data to represent # okay or low moisture levels, respectively. moisture = random.choice([True, False]) if moisture: myDeviceShadow.shadowUpdate( '{"state":{"reported":{"moisture":"okay"}}}', myShadowUpdateCallback, 5) else: myDeviceShadow.shadowUpdate( '{"state":{"reported":{"moisture":"low"}}}', 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: