Skip to content

Instantly share code, notes, and snippets.

@anggras
Last active April 22, 2021 05:43
Show Gist options
  • Save anggras/96eb12ef506338e879b8d08d2e300a7b to your computer and use it in GitHub Desktop.
Save anggras/96eb12ef506338e879b8d08d2e300a7b to your computer and use it in GitHub Desktop.
Note: This function access IoT Data Plane directly, not through the usual MQTT protocol. Useful to simulate an IoT device / sensor using Lambda
import json
import boto3
import random
MAX_TEMP = 42
MIN_TEMP = 36
def lambda_handler(event, context):
client = boto3.client('iot-data')
temp = round(random.uniform(MIN_TEMP, MAX_TEMP), 2)
payload = {
'temperature': temp
}
response = client.publish(
topic='test/topic',
qos=0,
payload=json.dumps(payload)
)
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment