Intermediate 25 min

From Sensor Reading to JSON Message

The sensor reads 23.4°C. That’s just a number. But to send it to the cloud, we need more. We need to tell the cloud which device sent it, when it was read, and what it means.

The Raw Reading

The sensor gives us a number: 23.4

That’s it. Just a number. The device doesn’t know:

  • Which room this is
  • What time it was read
  • What unit it’s in (Celsius? Fahrenheit?)

Adding Context

The device needs to add context. It creates a message with:

device_id: Which device sent this? (e.g., “room-101”) timestamp: When was it read? (e.g., “2025-06-01T10:00:00Z”) temperature_c: The actual reading (e.g., 23.4)

The JSON Message

Here’s what the message looks like:

{
  "device_id": "room-101",
  "timestamp": "2025-06-01T10:00:00Z",
  "temperature_c": 23.4
}

This is JSON (JavaScript Object Notation). It’s a simple way to structure data. The cloud can read it, understand it, and store it.

Why Each Field Matters

device_id

  • Tells the cloud which device sent this
  • Important when you have many devices
  • Cloud uses it to organize data

timestamp

  • When the reading was taken
  • Cloud uses it to show trends over time
  • Usually in UTC (coordinated universal time)

temperature_c

  • The actual sensor reading
  • The “_c” suffix means Celsius
  • Makes units clear

Try It Yourself

Move the slider below to see how the JSON message changes:

Loading...

Adding More Fields

Sometimes you want to send more data. Let’s add humidity and battery level:

Loading...

The Code That Does This

Here’s what the device code looks like (pseudo-code):

# Read sensor
temperature = read_temperature_sensor()  # Returns 23.4

# Get current time
timestamp = get_current_time()  # Returns "2025-06-01T10:00:00Z"

# Build message
message = {
    "device_id": "room-101",
    "timestamp": timestamp,
    "temperature_c": temperature
}

# Convert to JSON string
json_message = json.dumps(message)

# Send to cloud (we'll see how in the next page)
send_to_cloud(json_message)

That’s it. Read sensor, add context, make JSON, send it.

What is Telemetry?

Telemetry is the data that devices send to the cloud. It’s usually:

  • Measurements (temperature, humidity, pressure)
  • Status (on/off, battery level)
  • Events (motion detected, door opened)

The word comes from Greek: “tele” (distant) + “metron” (measure). You’re measuring something from far away.

Common Telemetry Fields

Most IoT messages include:

  • device_id or deviceId: Identifies the device
  • timestamp: When the data was collected
  • sensor readings: The actual measurements
  • metadata: Battery, signal strength, firmware version

What’s Next?

Now you know how to create a message. Next, we’ll see how that message travels from the device to the cloud. We’ll look at networks and protocols.