Master the fundamental concepts of protocol implementation through this focused micro-challenge.
MQTT runs over TCP port 1883 (or 8883 with TLS). Every packet starts with a fixed header of 2-5 bytes. For example, a CONNECT packet has type nibble 1 in the high bits of byte 0, plus a variable-length "remaining length" field encoding how many bytes follow.
Common control packet types:
1): client requests connection, carries protocol name "MQTT" and level 4 for v3.1.12): server acknowledges3): message to a topic like sensors/temp8): client requests topic filters with QoS levelsThe variable header adds fields like packet identifier, connect flags, and keep-alive interval (e.g. 60 seconds). The payload carries client ID, credentials, topic names, and message data.
Each byte uses 7 data bits plus a continuation bit. Value 321 encodes as two bytes: 0xC1 0x02.
This task requires you to build MQTT CONNECT and PUBLISH packets. Mosquitto and AWS IoT Core parse this exact fixed-header format from millions of battery-powered sensors where minimal framing overhead matters on cellular links.
QoS levels add delivery guarantees: 0 is fire-and-forget, 1 is at-least-once with PUBACK, 2 is exactly-once with a four-step handshake.
Write a C program that builds MQTT CONNECT and PUBLISH packets. Show the packet bytes and a field breakdown.
Requirements:
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
All starter code and reference implementations are available for your local setup.
View on Github