Skip to content

Instantly share code, notes, and snippets.

@shijaz
Last active July 15, 2018 19:56
Show Gist options
  • Save shijaz/e5c19de86580d3e6e5055f5eb10004d8 to your computer and use it in GitHub Desktop.
Save shijaz/e5c19de86580d3e6e5055f5eb10004d8 to your computer and use it in GitHub Desktop.
shadow_updater.py for Lambda function 'MoveRobot'
# shadow_updater.py for Lambda function 'MoveRobot'
# 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
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")
# Updates the device 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("Move: {0}".format(res_payload.get("state").get("desired").get("move_direction")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment