         delay(4);
       }
     }
-    else if (strcmp((char *)onoffsw.lastread, "{\"On\":0}") == 0) {
+    else if (strcmp((char *)onoffsw.lastread, "{\"On\":0}") == 0 && led_on) {
+      led_on = false;
       Serial.println(F("Off"));
       while (i > 10) {
         analogWrite(LED, i);

  mqtt.subscribe(&onoffsw);
}

uint32_t x = 0;
unsigned int i = 0;
void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect

#define LED         4
#define DS18B20     5 // Dallas DS18B20 digital thermometer


unsigned int i = 0;
unsigned int brightness = 1023;
bool led_on = false;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

