void reading_data () { soilmoisturevalue = analogRead(34); // soil moisture reading connected to pin 34, an analog-to-digital converter pin on the esp32 Serial.print("soilmoisture"); Serial.println(soilmoisturevalue); String moisture = String(soilmoisturevalue); // for MQTT transmission moisture.toCharArray(soilmoisturearray, moisture.length() + 1); client.publish(soilmoisture_topic, soilmoisturearray); // the soil moisture values are published under the topic soil moisture temperaturevalue = sht31.readTemperature(); // temperature reading obtained by the SHT31 sensor humidityvalue = sht31.readHumidity(); // humidity reading obtained by the SHT31 sensor if (! isnan(temperaturevalue)) { // check if 'is not a number', then convert to a char array String temperature = String(temperaturevalue);// for MQTT transmission temperature.toCharArray(temperaturearray, temperature.length() + 1); client.publish(temperature_topic, temperaturearray);// To publish the topic under Serial.print("Temp *C = "); Serial.print(temperature); Serial.print("\t\t"); // gives the temperature in degree celcius } else { Serial.println("Failed to read temperature"); } if (! isnan(humidityvalue)) { // check if 'is not a number' String humidity = String(humidityvalue); humidity.toCharArray(humidityarray, humidity.length() + 1); client.publish(humidity_topic, humidityarray); // the humidity values are published under the topic humidity Serial.print("Hum. % = "); Serial.println(humidity); // gives the humidity value as a percentage } else { Serial.println("Failed to read humidity"); } if (soilmoisturevalue > 2800) { digitalWrite (PIN, HIGH); // if soil moisture value is higher than 2800, switch on the water pump delay(1000); // The pump is switch on for 1 sec until the moisture level drops below 2800 } else { } digitalWrite (PIN, LOW); //Water pump turned off if soil moisture value is lower than 2800 }