Show code
viewof sensorPayload = Inputs.range([1, 100], {
value: 10,
step: 1,
label: "Payload size (bytes):",
width: 400
})
viewof reportingInterval = Inputs.range([1, 3600], {
value: 60,
step: 1,
label: "Reporting interval (seconds):",
width: 400
})
viewof topicLength = Inputs.range([5, 50], {
value: 20,
step: 1,
label: "MQTT topic length (bytes):",
width: 400
})
viewof txPower = Inputs.range([1, 100], {
value: 10,
step: 1,
label: "TX power (mW):",
width: 400
})
viewof dataRate = Inputs.range([50, 1000], {
value: 250,
step: 10,
label: "Data rate (kbps):",
width: 400
})
coapPacketSize = {
const header = 4;
const token_options = 6;
const udp = 8;
const ipv6 = 40;
return header + token_options + sensorPayload + udp + ipv6;
}
mqttPacketSize = {
const header = 2;
const topic = 2 + topicLength;
const tcp = 20;
const ipv6 = 40;
const ack = 60;
return header + topic + sensorPayload + tcp + ipv6 + ack;
}
messagesPerYear = Math.floor((365 * 24 * 3600) / reportingInterval)
coapAnnualMB = ((messagesPerYear * coapPacketSize) / (1024 * 1024)).toFixed(2)
mqttAnnualMB = ((messagesPerYear * mqttPacketSize) / (1024 * 1024)).toFixed(2)
bandwidthSavings = (((mqttPacketSize - coapPacketSize) / mqttPacketSize) * 100).toFixed(1)
coapTxTime = ((coapPacketSize * 8) / (dataRate * 1000) * 1000).toFixed(2)
mqttTxTime = ((mqttPacketSize * 8) / (dataRate * 1000) * 1000).toFixed(2)
coapEnergy = (coapTxTime * txPower / 1000).toFixed(1)
mqttEnergy = (mqttTxTime * txPower / 1000).toFixed(1)
energyRatio = (mqttEnergy / coapEnergy).toFixed(2)
dailyMessages = (86400 / reportingInterval).toFixed(0)
coapDailyEnergyMj = (coapEnergy * (86400 / reportingInterval) / 1000).toFixed(1)
mqttDailyEnergyMj = (mqttEnergy * (86400 / reportingInterval) / 1000).toFixed(1)
html`<div style="margin-top: 18px; padding: 22px; border-radius: 14px; background: linear-gradient(135deg, #f4fbf9 0%, #eef6fb 100%); border: 1px solid rgba(22, 160, 133, 0.18);">
<div style="display: flex; justify-content: space-between; gap: 12px; align-items: baseline; flex-wrap: wrap;">
<div style="font-size: 1.1rem; font-weight: 700; color: #17324d;">Protocol overhead comparison</div>
<div style="font-size: 0.88rem; color: #51657a;">${messagesPerYear.toLocaleString()} messages/year</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-top: 16px;">
<div style="padding: 16px; border-radius: 12px; background: white; border-left: 4px solid #16A085;">
<div style="font-size: 0.8rem; color: #6b7d90; text-transform: uppercase; letter-spacing: 0.04em;">CoAP packet</div>
<div style="font-size: 1.8rem; font-weight: 700; color: #17324d;">${coapPacketSize} bytes</div>
<div style="font-size: 0.9rem; color: #51657a;">${sensorPayload} payload + 58 overhead</div>
</div>
<div style="padding: 16px; border-radius: 12px; background: white; border-left: 4px solid #E67E22;">
<div style="font-size: 0.8rem; color: #6b7d90; text-transform: uppercase; letter-spacing: 0.04em;">MQTT packet</div>
<div style="font-size: 1.8rem; font-weight: 700; color: #17324d;">${mqttPacketSize} bytes</div>
<div style="font-size: 0.9rem; color: #51657a;">${sensorPayload} payload + ${mqttPacketSize - sensorPayload} overhead</div>
</div>
<div style="padding: 16px; border-radius: 12px; background: white; border-left: 4px solid #3498DB;">
<div style="font-size: 0.8rem; color: #6b7d90; text-transform: uppercase; letter-spacing: 0.04em;">Bandwidth savings</div>
<div style="font-size: 1.8rem; font-weight: 700; color: #17324d;">${bandwidthSavings}%</div>
<div style="font-size: 0.9rem; color: #51657a;">CoAP versus MQTT</div>
</div>
<div style="padding: 16px; border-radius: 12px; background: white; border-left: 4px solid #9B59B6;">
<div style="font-size: 0.8rem; color: #6b7d90; text-transform: uppercase; letter-spacing: 0.04em;">Energy ratio</div>
<div style="font-size: 1.8rem; font-weight: 700; color: #17324d;">${energyRatio}x</div>
<div style="font-size: 0.9rem; color: #51657a;">MQTT energy versus CoAP</div>
</div>
</div>
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; margin-top: 16px;">
<div style="padding: 16px; border-radius: 12px; background: rgba(255,255,255,0.8);">
<div style="font-size: 0.92rem; font-weight: 700; color: #17324d;">Annual bandwidth</div>
<div style="margin-top: 8px; color: #51657a; line-height: 1.65;">
<div>CoAP: <strong style="color:#17324d;">${coapAnnualMB} MB/year</strong></div>
<div>MQTT: <strong style="color:#17324d;">${mqttAnnualMB} MB/year</strong></div>
<div>Difference: <strong style="color:#17324d;">${(mqttAnnualMB - coapAnnualMB).toFixed(2)} MB/year</strong></div>
</div>
</div>
<div style="padding: 16px; border-radius: 12px; background: rgba(255,255,255,0.8);">
<div style="font-size: 0.92rem; font-weight: 700; color: #17324d;">Energy per message</div>
<div style="margin-top: 8px; color: #51657a; line-height: 1.65;">
<div>CoAP: <strong style="color:#17324d;">${coapTxTime} ms</strong> → <strong style="color:#17324d;">${coapEnergy} uJ</strong></div>
<div>MQTT: <strong style="color:#17324d;">${mqttTxTime} ms</strong> → <strong style="color:#17324d;">${mqttEnergy} uJ</strong></div>
<div>At ${dailyMessages} messages/day: <strong style="color:#17324d;">${coapDailyEnergyMj} mJ/day</strong> vs <strong style="color:#17324d;">${mqttDailyEnergyMj} mJ/day</strong></div>
</div>
</div>
</div>
</div>`