AWS IoT Temperature Logger (Simulated)

Create and Register an IoT "Thing" (Device)

Go to AWS IoT Core → Manage → Things

Click Create things → Choose Create single thing

Give it a name: TempSensor001

Leave default settings (no shadow, no groups)

Click Next

Create Certificates and Attach Policies

AWS will prompt to create certificates for the Thing.

Download:

  • certificate.pem.crt
  • private.pem.key
  • AmazonRootCA1.pem (link provided by AWS)
  • public.pem.key (optional)

Save them securely.

Click Attach policy → Create a policy:

{
  "Version": "2025-04-20",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "iot:Connect",
        "iot:Publish",
        "iot:Subscribe",
        "iot:Receive"
      ],
      "Resource": "*"
    }
  ]
}

Attach this policy to the certificate.

Write the Simulator Script

iot_simulator.py

import ssl
import time
import json
import random
import paho.mqtt.client as mqtt

ENDPOINT = "your-iot-endpoint.amazonaws.com"  # Replace with your AWS IoT Core endpoint
PORT = 8883
TOPIC = "iot/temperature"

CA = "AmazonRootCA1.pem"
CERT = "certificate.pem.crt"
KEY = "private.pem.key"

client = mqtt.Client()
client.tls_set(ca_certs=CA,
                certfile=CERT,
                keyfile=KEY,
                tls_version=ssl.PROTOCOL_TLSv1_2)

client.connect(ENDPOINT, PORT)
client.loop_start()

while True:
    temperature = round(random.uniform(20, 35), 2)
    payload = {
        "device": "TempSensor001",
        "temperature": temperature,
        "timestamp": int(time.time())
    }
    print("Sending:", payload)
    client.publish(TOPIC, json.dumps(payload), qos=1)
    time.sleep(5)

Replace your-iot-endpoint.amazonaws.com with the endpoint from AWS IoT Core → Settings.

Create an IoT Rule to Log Data

Go to AWS IoT → Message Routing → Rules:

  • Click Create rule
  • Rule name: LogTemperature
  • SQL:
  • SELECT * FROM 'iot/temperature'
  • Add an action → Insert into DynamoDB (or CloudWatch Logs)
    • Table name: TemperatureLogs
    • Partition key: device (String)
    • Sort key: timestamp (Number)
    • Create the table first in DynamoDB

    Alternatively: Use CloudWatch Logs for easier setup.

View Logged Data

Option 1: DynamoDB

  • Go to DynamoDB → Tables → TemperatureLogs
  • Click Explore Table Items to see entries

Option 2: CloudWatch Logs

  • Go to CloudWatch → Logs → Log groups → Look for /aws/iot/LogTemperature
Testing

Run your simulator locally:

python iot_simulator.py

Go to AWS IoT → MQTT Test Client and subscribe to topic:

iot/temperature

You’ll see your messages arriving in real time.