// Wifi and MQTT library #include #include #include // Load the DHT library #include // Define macros // DHTPIN specifies the physical pin to which the sensor is connected (previously we had connected the sensor's DAT pin to ESP32 pin no. 14) #define DHTPIN 4 // Sensor type #define DHTTYPE DHT22 // Define the DHT sensor DHT dht(DHTPIN, DHTTYPE); // This section is executed once when the device starts up void setup() { Serial.begin(115200); Serial.println("Serial port opened"); dht.begin(); Serial.println("DHT sensor initialized"); } // Repeat endlessly after the setup() function execution void loop() { // Wait before the first measurement because the sensor is quite slow delay(2000); // Define variables, float indicates a decimal number (see explanations below) float temperature, humidity; // Store the temperature reading in the 'temperature' variable using the readTemperature() function from the dht library. temperature = dht.readTemperature(); // TODO: Store the humidity reading in the 'humidity' variable humidity = dht.readHumidity(); /////////////////////////////////////////////////////////////////////////////////////////////////////// // // Look for a function similar to readTemperature() for reading humidity in the dht library documentation // from the link provided below. Add it after "humidity = " in the line above. // https://adafruit.github.io/DHT-sensor-library/html/class_d_h_t.html // /////////////////////////////////////////////////////////////////////////////////////////////////////// // Check if reading values from the sensor was successful, if not, print an error message to the serial port if (isnan(humidity) || isnan(temperature)) { // Print to the serial port Serial.println("Failed to read from the sensor"); // Interrupt the execution of the loop() function return; } // Printing the temperature // Print the text "Temperature: " to the serial port Serial.print("Temperature: "); // Print the content of the 'temperature' variable Serial.print(temperature); // Print the °C symbol Serial.print("°C "); // Print a line break (= new line) Serial.println(); // TODO: Printing the humidity //////////////////////////////////////////////////////////////////////////////////////////////////// // // Write the necessary lines of code above to print humidity. Retrieve the humidity value from the // 'humidity' variable using the Serial.println() function. You may consider the temperature // printing codes above as an example. // //////////////////////////////////////////////////////////////////////////////////////////////////// // Print the text "Temperature: " to the serial port Serial.print("Humidity: "); // Print the content of the 'temperature' variable Serial.print(humidity); // Print the °C symbol Serial.print("% "); // Print a line break (= new line) Serial.println(); }