             case 'P':
                 // CO2
                 snprintf(valstr, sizeof(valstr), "%d PPM", value);
-                mqtt_send(TOPIC_CO2, valstr, true);
+                if (mqtt_send(TOPIC_CO2, valstr, true)) {
+                    errors = 0;
+                } else {
+                    errors++;
+                }
                 break;
             case 'A':
                 // humidity
                 snprintf(valstr, sizeof(valstr), "%d.%02d %%", value / 100, value % 100);
-                mqtt_send(TOPIC_HUMIDITY, valstr, true);
+                if (mqtt_send(TOPIC_HUMIDITY, valstr, true)) {
+                    errors = 0;
+                } else {
+                    errors++;
+                }
                 break;
             case 'B':
                 // temperature
                 temp10 = (5 * value - 21848) / 8;
                 snprintf(valstr, sizeof(valstr), "%d.%d °C", temp10 / 10, abs(temp10) % 10);
-                mqtt_send(TOPIC_TEMPERATURE, valstr, true);
+                if (mqtt_send(TOPIC_TEMPERATURE, valstr, true)) {
+                    errors = 0;
+                } else {
+                    errors++;
+                }
                 break;
             default:
                 // ignore unhandled packet

    // keep mqtt alive
    mqttClient.loop();

    // verify network connection and reboot on failure
    if (WiFi.status() != WL_CONNECTED) {
        Serial.println("Restarting ESP...");
        ESP.restart();
    }


void loop(void)
{
    static int errors = 0;
    static bool prev_clk = false;
    char valstr[16];
    int temp10;
