HTTP Polling Battery Life Calculator

HTTP Polling Battery Life Calculator

animation
http
app-protocols
Try Change Polling interval (seconds) while keeping battery capacity fixed. Then press Calculate battery life.
Observe After pressing Calculate battery life, watch HTTP days and connections/day as Polling interval (seconds) changes.
Explain Explain why Battery capacity (mAh) changes days for both protocols after you press Calculate battery life.

Technical boundaries. Battery days use only HTTP connection overhead or the fixed daily MQTT keep-alive cost.

Calculate battery life impact of HTTP polling vs MQTT persistent connections:

viewof pollingInterval = Inputs.range([1, 3600], {
  value: 600,
  step: 1,
  label: "Polling interval (seconds)"
})

viewof batteryCapacity = Inputs.range([500, 10000], {
  value: 3000,
  step: 100,
  label: "Battery capacity (mAh)"
})

viewof connectionOverhead = Inputs.range([1, 10], {
  value: 2.85,
  step: 0.1,
  label: "HTTP connection overhead (mAs)"
})

viewof mqttKeepalive = Inputs.range([0.1, 5], {
  value: 0.5,
  step: 0.1,
  label: "MQTT keep-alive cost (mAh/day)"
})
{
  const connectionsPerDay = (24 * 3600) / pollingInterval;
  const httpDailyMah = (connectionOverhead * connectionsPerDay) / 3600;
  const httpBatteryDays = batteryCapacity / httpDailyMah;
  const mqttBatteryDays = batteryCapacity / mqttKeepalive;
  const improvement = ((mqttBatteryDays - httpBatteryDays) / httpBatteryDays * 100).toFixed(1);

  const data = [
    {protocol: "HTTP Polling", days: httpBatteryDays.toFixed(0), color: "#E67E22"},
    {protocol: "MQTT Persistent", days: mqttBatteryDays.toFixed(0), color: "#16A085"}
  ];

  return html`
    <div style="font-family: Arial, sans-serif; padding: 15px; background: #f8f9fa; border-radius: 8px; border-left: 4px solid #2C3E50;">
      <p style="margin-top: 0; margin-bottom: 0.8em; color: #2C3E50; font-weight: 700; font-size: 1.1em;">Battery Life Comparison</p>
      <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin: 20px 0;">
        ${data.map(d => `
          <div style="background: white; padding: 15px; border-radius: 6px; border-top: 3px solid ${d.color};">
            <div style="font-size: 0.9em; color: #7F8C8D; margin-bottom: 5px;">${d.protocol}</div>
            <div style="font-size: 2em; font-weight: bold; color: ${d.color};">${d.days}</div>
            <div style="font-size: 0.9em; color: #7F8C8D;">days</div>
          </div>
        `).join('')}
      </div>
      <div style="background: white; padding: 15px; border-radius: 6px;">
        <strong style="color: #16A085;">MQTT improvement: ${improvement}% longer battery life</strong><br/>
        <span style="color: #7F8C8D; font-size: 0.9em;">
          HTTP: ${connectionsPerDay.toFixed(0)} connections/day (${httpDailyMah.toFixed(1)} mAh/day)<br/>
          MQTT: Persistent connection (${mqttKeepalive} mAh/day)
        </span>
      </div>
    </div>
  `;
}

Back to the chapter