Publishing sensor data in IOT Protocols - Time & Space Complexity
When devices send sensor data repeatedly, it is important to understand how the time to send data changes as the number of sensors grows.
We want to know how the work needed to publish data increases when more sensors are involved.
Analyze the time complexity of the following code snippet.
for sensor in sensors:
data = sensor.read()
protocol.publish(topic=sensor.id, message=data)
wait(interval)
This code reads data from each sensor and publishes it one by one with a wait time between each.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all sensors to read and publish data.
- How many times: Once for each sensor in the list.
As the number of sensors increases, the total time to publish data grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads and publishes |
| 100 | 100 reads and publishes |
| 1000 | 1000 reads and publishes |
Pattern observation: Doubling the number of sensors roughly doubles the work needed.
Time Complexity: O(n)
This means the time to publish data grows linearly with the number of sensors.
[X] Wrong: "Publishing data from multiple sensors happens instantly no matter how many sensors there are."
[OK] Correct: Each sensor's data must be read and sent, so more sensors mean more work and more time.
Understanding how work grows with more sensors helps you design efficient IoT systems and shows you can think about scaling real devices.
"What if we batch all sensor data and publish it in one message? How would the time complexity change?"
Practice
Solution
Step 1: Understand publishing in IoT
Publishing means sending data from a sensor to a topic so other devices can get it.Step 2: Identify the main goal
The goal is to share sensor data, not store or delete it locally or encrypt it by default.Final Answer:
To send sensor data to a topic so others can receive it -> Option AQuick Check:
Publishing = sending data to topic [OK]
- Confusing publishing with storing data locally
- Thinking publishing encrypts data automatically
- Assuming publishing deletes old data
home/livingroom/temp?Solution
Step 1: Check MQTT publish method syntax
The correct syntax uses the client object (e.g., mqtt) and calls publish with topic and message as string.Step 2: Validate options
mqtt.publish('home/livingroom/temp', '22.5') uses mqtt.publish with topic and string message, which is correct. publish('home/livingroom/temp', 22.5) misses client object, C misses message, B uses invalid keyword argument.Final Answer:
mqtt.publish('home/livingroom/temp', '22.5') -> Option AQuick Check:
mqtt.publish(topic, message) = correct syntax [OK]
- Omitting the client object before publish
- Not providing the message argument
- Using keyword arguments not supported by publish
import paho.mqtt.client as mqtt
client = mqtt.Client()
client.connect('broker.hivemq.com', 1883)
result = client.publish('sensor/room1/humidity', '45')
print(result.rc)Solution
Step 1: Understand publish return value
client.publish returns a MQTTMessageInfo object with rc attribute showing result code: 0 means success.Step 2: Check printed value
Printing result.rc will print 0 if publish succeeded without error.Final Answer:
0 -> Option BQuick Check:
Publish success code = 0 [OK]
- Assuming publish returns null
- Expecting printed message payload
- Confusing rc with error code
client.publish('sensor/temp', 25)
What is the likely problem?Solution
Step 1: Check payload data type
MQTT publish requires message payload as string or bytes, not integer directly.Step 2: Identify issue with given code
Passing 25 as integer causes publish to fail silently or send empty data, so subscriber gets nothing.Final Answer:
The message payload must be a string or bytes, not an integer -> Option CQuick Check:
Payload must be string/bytes, not int [OK]
- Sending integer payload without conversion
- Thinking subscribe is needed before publish
- Assuming topic name is invalid without checking
Solution
Step 1: Understand QoS levels
QoS=1 ensures message delivery at least once, making it reliable.Step 2: Understand retain flag
retain=true keeps the last message on the broker so new subscribers get it immediately.Step 3: Combine for reliable and persistent data
Setting QoS=1 and retain=true meets both requirements.Final Answer:
Set QoS=1 and retain=true -> Option DQuick Check:
QoS=1 + retain=true = reliable & persistent [OK]
- Using QoS=0 which is unreliable
- Not setting retain so new subscribers miss data
- Confusing QoS=2 as always best choice
Ready to prove it?
Pass the test and this pattern moves unknown
