   mqttClient.publish(topicBuffer, msgBuffer);
 }
 
+void setup()
+{
+  // put your setup code here, to run once:
+  Serial.begin(115200);
+  Serial.println();
+  u8g2.begin();
+  dht.begin();
+  sensors.begin();
+
+  delay(10);
+
+  drawDisplay("ANAVI Thermometer");
+
+  connectWiFi();
+  mqttClient.setServer(mqtt_server, 1883);
+}
+
 void loop()
 {
-    // Reconnect if MQTT client is not connected.
-    if (!mqttClient.connected()) 
-    {
-      reconnect();
-    }
-  
-    float humidity = dht.readHumidity();
-    float temp = dht.readTemperature();
-    if (isnan(humidity) || isnan(temp))
-    {
-      delay(sensorInterval);
-      return;
-    }
-    
+  // Reconnect if MQTT client is not connected.
+  if (!mqttClient.connected())
+  {
+    reconnect();
+  }
+
+  float humidity = dht.readHumidity();
+  float temp = dht.readTemperature();
+  if (!isnan(humidity) && !isnan(temp))
+  {
     // Adjust temperature depending on the calibration coefficient
     temp = temp*temperatureCoef;
-    dhtTemperature = temp;
-    dhtHumidity = humidity;
     
-    String txtTemperature = String(dhtTemperature, 1);
-    String air="Air "+txtTemperature+"C ";
+    txtTemperature = String(temp, 1);
+    txtHumidity = String(humidity, 0);
+  }
+
+  String air = "";
+  if (0 < txtTemperature.length())
+  {
+    air="Air "+txtTemperature+"C ";
     Serial.println(air);
-    String txtHumidity = String(dhtHumidity, 0);
-    String hum="Humidity "+txtHumidity+"%";
+  }
+
+  String hum = "";
+  if (0 < txtHumidity.length())
+  {
+    hum="Humidity "+txtHumidity+"%";
     Serial.println(hum);
-
-    String txtWater="";
-    String water="";
-    if (0 < sensors.getDeviceCount())
+  }
+
+  String water="";
+  if (0 < sensors.getDeviceCount())
+  {
+    sensors.requestTemperatures();
+    txtWater = String(sensors.getTempCByIndex(0),1);
+    water="Water "+txtWater+"C";
+    Serial.println(water);
+  }
+  drawDisplay(air.c_str(), hum.c_str(), water.c_str());
+
+  // Publish data to thingspeak.com over MQTT
+  const unsigned long currentMillis = millis();
+  if (publishInterval <= (currentMillis - sensorPreviousMillis))
+  {
+    // Read light only if sensor is attached
+    if (isSensorAvailable(sensorBH1750))
     {
-        sensors.requestTemperatures();
-        float wtemp = sensors.getTempCByIndex(0);
-        dsTemperature = wtemp;
-        txtWater = String(dsTemperature,1);
-        water="Water "+String(dsTemperature,1)+"C";
-        Serial.println(water);
+        txtLight = readLight();
+        Serial.println("Light "+txtLight+" Lux");
     }
-    drawDisplay(air.c_str(), hum.c_str(), water.c_str());
-
-    // Publish data to thingspeak.com over MQTT
-    const unsigned long currentMillis = millis();
-    if (publishInterval <= (currentMillis - sensorPreviousMillis))
-    {
-        sensorPreviousMillis = currentMillis;
-        mqttpublish(txtTemperature, txtHumidity, txtWater);
-    }
-
-    // Read data from the sensor every 3 seconds
-    delay(sensorInterval);
-}
+
+    sensorPreviousMillis = currentMillis;
+    mqttpublish(txtTemperature, txtHumidity, txtWater, txtLight);
+  }
+
+  // Read data from the sensor every 3 seconds
+  delay(sensorInterval);
+}

// specfic DHT22 unit on your board
const float temperatureCoef = 1;

float dhtTemperature = 0;
float dhtHumidity = 0;
float dsTemperature = 0;

// For random generation of MQTT client ID
static const char alphanum[] ="0123456789"
                              "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                              "abcdefghijklmnopqrstuvwxyz";

void drawDisplay(const char *line1, const char *line2 = "", const char *line3 = "")
{
    // Write on OLED display
    // Clear the internal memory
    u8g2.clearBuffer();
    // Set appropriate font
    u8g2.setFont(u8g2_font_ncenR14_tr);
    u8g2.drawStr(0,14, line1);
    u8g2.drawStr(0,39, line2);
    u8g2.drawStr(0,64, line3);
    // Transfer internal memory to the display
    u8g2.sendBuffer();
}

void connectWiFi() {

  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();

#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

// Configure the interval in milliseconds to read data
// from the sensors and to publish it to thingspeak.com
