IoT Part B: Structured Questions (25%)

Exam preparation notes for Part B structured questions: MQTT protocol, JSON format, MicroPython WiFi, and Edge vs Cloud.

Question 1: MQTT Protocol

Q1(a): MQTT architecture with diagram [5 marks]

MQTT uses a Publish/Subscribe architecture with three main components:

┌─────────────┐         ┌─────────────┐         ┌─────────────┐
│  Publisher  │────────>│   Broker    │<────────│ Subscriber  │
│  (Sensor)   │  Publish│  (Server)   │Subscribe│ (Dashboard) │
└─────────────┘         └─────────────┘         └─────────────┘
                              │
                         Firewall
                              │
                    ┌─────────┴─────────┐
                    ▼                   ▼
              Publisher            Subscriber
              (Device)             (App)
  • Publisher: IoT devices/sensors; publish to topics (e.g. UMT/KKB/BK3-1/Sensor/Temperature).
  • Broker: Central server; routes messages; provides QoS; can retain messages.
  • Subscriber: Apps/devices; subscribe with exact topics or wildcards (+ and #).

Publishers and subscribers are decoupled; one device can be both.

Q1(b): Three QoS levels [6 marks]

QoSTypeDescriptionUse case
0At most onceFire and forget; may be lostNon-critical, high-frequency readings
1At least onceACK required; may duplicateImportant data (e.g. door lock status)
2Exactly oncePUBREC → PUBREL → PUBCOMP; no duplicatesCritical (e.g. emergency stop)

Q1(c): Topic structure and wildcards [6 marks]

Tree-like hierarchy, / delimiter, case-sensitive. Example: UMT/KKB/BK3-1/Sensor/Temperature

  • + (single-level): Replaces one level. UMT/+/BK3-1/Sensor/Temperature matches UMT/KKB/BK3-1/... or UMT/Library/BK3-1/...
  • # (multi-level): Entire branch from that point; must be last. UMT/KKB/# matches UMT/KKB/BK3-1/Sensor/Temperature and any deeper path.

Practical: Subscribe to UMT/KKB/+/Sensor/# to receive all sensors from all floors in KKB.

Q1(d): MQTT vs HTTP for IoT [5 marks]

FeatureMQTTHTTP
PatternPublish/Subscribe (1-to-many)Request/Response (1-to-1)
HeaderSmall, 2-byte fixed headerLarger (min ~71 bytes)
QoS3 levelsSame for all
Best forConstrained devices, sensor networksWeb, large data transfers

Question 2: JSON Data Format

Q2(a): What is JSON and why for IoT? [4 marks]

Lightweight, text-based, human-readable; flexible nested structures; language-independent; smaller than XML.

Q2(b): JSON for weather station [4 marks]

{
  "device_id": "WeatherStation_001",
  "location": "UMT_KKB_Rooftop",
  "timestamp": "2025-11-04T10:30:01Z",
  "sensors": {
    "temperature": { "value": 28.5, "unit": "Celsius" },
    "humidity": { "value": 65, "unit": "percent" },
    "pressure": { "value": 1013.25, "unit": "hPa" }
  },
  "battery_level": 87
}

timestamp: ISO 8601; sensors: value + unit; device_id and battery_level for identification and health.

Question 3: Python WiFi Connection (MicroPython)

Q3(a): Connect ESP32/Pico W to WiFi [8 marks]

import network
import time

WIFI_SSID = "YourWiFiName"
WIFI_PASSWORD = "YourPassword"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(WIFI_SSID, WIFI_PASSWORD)

max_wait = 10
while max_wait > 0:
    if wlan.status() >= 3:
        break
    max_wait -= 1
    time.sleep(1)

if wlan.status() == 3:
    print("Connected! IP:", wlan.ifconfig()[0])
else:
    print("Connection failed. Status:", wlan.status())

STA_IF = station (client) mode. status: 0=idle, 1=connecting, 2=wrong password, 3=connected. ifconfig() returns (IP, subnet, gateway, DNS).

Q3(b): Send temperature to ThingSpeak via HTTP GET [6 marks]

import network, time, urequests

# ... WiFi connect same as Q3a ...

THINGSPEAK_API_KEY = "YOUR_WRITE_API_KEY"
THINGSPEAK_URL = "http://api.thingspeak.com/update"

def send_to_thingspeak(temperature, humidity):
    url = f"{THINGSPEAK_URL}?api_key={THINGSPEAK_API_KEY}&field1={temperature}&field2={humidity}"
    response = urequests.get(url)
    if response.status_code == 200:
        print("Data sent:", response.text)
    response.close()

# Main: read sensor, send every 15s (ThingSpeak free limit)
while True:
    if wlan.isconnected():
        send_to_thingspeak(25.5, 60.0)
    time.sleep(15)

Question 4: Additional Part B Topics

Q4(a): Edge vs Cloud Computing [6 marks]

AspectEdgeCloud
LocationOn/near deviceRemote data center
LatencyVery lowHigher
Use caseReal-time, emergency stopML training, long-term analytics

Q4(b): LDR + LED (MicroPython) [6 marks]

from machine import Pin, ADC
import time

ldr = ADC(26)
led = Pin(25, Pin.OUT)
DARKNESS_THRESHOLD = 30000

while True:
    light_value = ldr.read_u16()
    if light_value < DARKNESS_THRESHOLD:
        led.on()
    else:
        led.off()
    time.sleep(1)

read_u16() returns 0–65535; threshold for darkness → turn LED on.