import machine
import time
import json
import dht
DEVICE_ID = "SK_61"
LOCATION = "Server_Room"
sensor = dht.DHT22(machine.Pin(16))
print("Experiment 4.1: JSON Packaging Started (DHT22)...")
print("-" * 40)
while True:
try:
sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
data_dictionary = {
"sensor_id": DEVICE_ID,
"location": LOCATION,
"telemetry": {
"temp": round(temperature, 2),
"humidity": round(humidity, 2),
"unit": "Celsius"
},
"timestamp_ms": time.ticks_ms(),
"status": "OK"
}
json_packet = json.dumps(data_dictionary)
print(f"RAW DICTIONARY: {data_dictionary}")
print(f"JSON PACKET: {json_packet}")
print(f"DATA TYPE: {type(json_packet)}")
except OSError:
print("sensor read error")
print("-" * 40)
time.sleep(5)