Skip to content

Instantly share code, notes, and snippets.

@shijaz
Created July 15, 2018 20:03
Show Gist options
  • Save shijaz/1f5e3d5d67517d5a7252466e31313954 to your computer and use it in GitHub Desktop.
Save shijaz/1f5e3d5d67517d5a7252466e31313954 to your computer and use it in GitHub Desktop.
shadow_updater.py for the Lambda Function 'SeeRobot'
# shadow_updater.py for the Lambda Function 'SeeRobot'
# Updates the AWS IOT Shadow for the robot
# Shijaz Abdulla - www.awsomenow.com
import os
import time
import json
import boto3
# Get the environment variables so we know where/how to update the shadow
clientId = os.environ.get('AWS_IOT_MQTT_CLIENT_ID')
thingName = os.environ.get("AWS_IOT_THING_NAME")
host = os.environ.get("AWS_IOT_MQTT_HOST")
port = os.environ.get("AWS_IOT_MQTT_PORT_UPDATE")
# update the shadow
def update_shadow(new_value_dict):
topic = "$aws/things/{}/shadow/update".format(thingName)
payload_dict = {
"state": {
"desired" : new_value_dict
}
}
JSON_payload = json.dumps(payload_dict)
shadow_client = boto3.client('iot-data', 'eu-west-1')
response = shadow_client.update_thing_shadow(thingName=thingName,
payload=JSON_payload)
res_payload = json.loads(response['payload'].read().decode('utf-8'))
print("SeeObject: {0}".format(res_payload.get("state").get("desired").get("cheese_option")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment