Skip to content
Tutorials Center
My learning

Topics

Lesson 36 of 36 ยท Projects and Practice

IoT Practice: Calculations, Exercises and Interview Questions

Practice IoT engineering: estimate battery life, message volume and payload size with runnable calculators, solve design exercises and review common interview questions.

The last lesson of this chapter is a workout. IoT work is full of back-of-the-envelope calculations (how long does the battery last, how much will the cloud bill be, does the message fit in a LoRa frame) and of design decisions where you must justify a choice. Below you get calculators you can modify, exercises to solve, and the questions interviewers actually ask, with short model answers.

Calculation 1: battery life

A battery sensor spends most of its time asleep. Average current is a weighted mix of sleep and active time:

average = (I_sleep * t_sleep + I_active * t_active) / (t_sleep + t_active)

Battery life is capacity divided by average current. Real batteries lose capacity to self-discharge and cold, so apply a derating factor (0.7 is a sober guess).

Node.js Battery life calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Change one number at a time to learn what dominates. Often it is the active time (a slow Wi-Fi connect costs more than the measurement itself), which is why sensors keep the radio on for the shortest possible time.

Calculation 2: message volume and cost

Node.js Messages per month and payload overhead

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Two levers cut cost: send less often and batch readings into one message. On cellular links the per-message protocol overhead (TCP, TLS, MQTT headers) often exceeds the payload, so batching helps twice.

Exercises

Try each one before looking at the hint.

  1. Topic design. A greenhouse has 3 zones, each with soil moisture, air temperature and a valve. Design the topic tree for readings and commands, and a filter that selects all moisture readings in all zones. Hint: greenhouse/zone1/moisture, filter greenhouse/+/moisture; commands under .../valve/set.
  2. QoS choice. For each, pick QoS 0, 1 or 2: a temperature every 10 s; "open the garage door"; an electricity meter reading used for billing. Hint: 0, 1 with idempotent handling, 1 with a unique ID or 2.
  3. Offline design. A tracker in a shipping container has coverage only at ports. What do you store on the device, and what happens when the buffer is full? Hint: bounded store-and-forward, drop oldest low-priority samples, send events before bulk samples.
  4. Threat model. List three STRIDE threats for a smart lock and a defense for each. Hint: spoofed unlock command (signed commands with counters), tampering with firmware (secure boot and signatures), denial of service by battery drain (rate limits).
  5. Protocol choice. For a rural soil sensor with a 5-year battery and a hundred-byte message each hour, which link and why? Hint: LoRaWAN or NB-IoT, not Wi-Fi.
  6. Code. Modify the rules engine so a manual light switch disables the motion rule for 30 minutes.
  7. Code. Extend the monitor project with a rate-of-change alarm (more than 3 degrees in 5 minutes).

Interview questions with short answers

What is the difference between MQTT and HTTP for IoT? MQTT is a persistent, small-overhead publish/subscribe protocol where the server can push to clients; HTTP is request/response, heavier, and needs polling or long connections for pushes. MQTT suits constrained devices and many-to-many messaging; HTTP suits one-off calls and web integration.

Explain QoS levels. 0 at most once, 1 at least once (duplicates possible), 2 exactly once through a four-step handshake. The effective QoS per subscriber is the minimum of publish and subscribe levels. See QoS.

What does the retained flag do, and when would you not use it? The broker keeps the last message of a topic and sends it to new subscribers. Avoid it for events, which would look current to late joiners.

How do you detect a device that lost power? Keep-alive timeouts plus a Last Will, usually retained, on a status topic. A clean shutdown publishes its own offline message.

CoAP versus MQTT? CoAP is a REST-like protocol over UDP for very constrained devices, with resource URIs, observe, and DTLS; MQTT is broker-based pub/sub over TCP.

How would you secure a fleet of 100,000 devices? Unique per-device credentials (certificates in secure hardware), TLS with verification, default-deny ACLs per topic, signed OTA updates with anti-rollback and staged rollout, no shared defaults, logging and revocation. See identity and TLS.

Why use a device shadow? To let apps read and write device state while the device is offline, with desired/reported/delta and versioning. See shadows.

Edge or cloud? Process at the edge when latency, bandwidth, privacy or offline operation matter; use the cloud for storage, fleet-wide analytics and model training. See edge computing.

How do you handle duplicate and out-of-order data? Idempotent processing, dedupe by device and timestamp, event-time ordering, and a tolerance window for late data. See the pipeline lesson.

How do you reduce a battery device's power use? Sleep deeply, shorten active time, batch transmissions, use efficient radios (BLE, LoRa) and avoid slow connection setup (reuse sessions, static IP, shorter TLS handshakes).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A node uses 150 mA for 3 s every 10 minutes and sleeps otherwise. What most affects its battery life?

    Active current is thousands of times the sleep current, so awake time dominates the average.
  2. 2. Which change reduces billed messages the most for 1000 devices sending every 10 seconds?

    Billing is mostly per message, so fewer, larger messages cost less.
  3. 3. Which is NOT a good use of a retained message?

    Retained messages represent state; an event retained for later subscribers looks like it just happened.
  4. 4. Which link suits a rural battery sensor sending 100 bytes per hour for years?

    Low-power wide-area networks trade bandwidth for range and battery life.
  5. 5. Which pair gives a fleet the best protection against forged devices?

    Unique identities and least-privilege ACLs limit the damage of any single compromise.

Summary

  • Battery life follows average current: minimize active time and wake-ups.
  • Message volume and billing depend on interval, batching and payload size.
  • Practice topic design, QoS choice, offline behavior, threat modeling and protocol selection until each takes a minute to justify.
  • Interviewers look for trade-offs and reasoning: always say why, and what you give up.

Next: Back to the Internet of Things tutorial