Measure temperature and umidity

Even the powerful ESP32 module itself is not able to provide information like temperature and humidity. Specific sensors are needed to read physical properties. This example uses the BME280 device, that provides calibrated digital signal output of the temperature, pression and humidity.

The ESP32 module can be programmed to read temperature, pression and humidity from the BME280 device and then publish temperature, pression and humidity values towards an MQTT broker. In order to reduce power usage, the ESP32 sleeps between two consecutive measurements.

The following topics are published by this experiment:
  • esp32/bme280/temperature
  • esp32/bme280/humidity
  • esp32/bme280/pressure


Hardware set-up

Following hardware is required to build the experiment:
  • ESP32 module
  • BME280 device
Implement following connections:
  • Connect GPIO 5 to BME280.SDA
  • Connect GPIO 4 to BME280.SCL
  • Connect 3.3 to BME280.VIN
  • Connect GND to BME280.GND
You need also a working MQTT broker, I use Mosquitto on top of Raspberry PI.

ESP32 software

Before writing software make sure to have installed the software development environment, downlodable from Arduino Software web site. It may be needed to download some library if not already present. Libraries are required also to manage the external devices.

The sketch has to include the required libraries:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <PubSubClient.h>
#include <WiFi.h>
#include "esp_deep_sleep.h" //Library needed for ESP32 Sleep Functions


The setup() function performs all the logic for the IoT sensor and then it goes to sleep.

void setup() {
// SDA = 5
// SCL = 4
    Wire.begin(5, 4);
    Serial.begin(115200);
  
    bool status;
    status = bme.begin(0x76); // bme280 i2c address = 0x76
    if (!status) {
        Serial.println("Could not find a valid BME280 sensor, check wiring!");
        while (1);
    } else Serial.println("Valid BME280 sensor found!");

    setup_wifi();

    Serial.print("Client id = ");
    Serial.println(clientId);
    client.setServer(mqtt_server, 1883);

    measure_publish();//perform measurement and publish bme280 data via MQTT
    
    client.disconnect();//flush and close client before going to sleep, needed to avoid losing published messages
    
    esp_deep_sleep_enable_timer_wakeup(UpdateInterval);
    Serial.println("Going to sleep now...");
    esp_deep_sleep_start();    
}


In this example the loop() function is not used.

Download the complete sketch here, change the secret values and the MQTT broker address and deploy it on ESP32 board.

Testing the measurement device

After succesful deployment of software on ESP32 board, assuming that you have installed Mosquitto on your preferred platform, e.g. Raspberry PI, you can use the following commands to subscribe each parameter:

mosquitto_sub -d -t esp32/bme280/temperature
mosquitto_sub -d -t esp32/bme280/humidity
mosquitto_sub -d -t esp32/bme280/pressure
You can also subscribe to all measurements together by executing the following command:

mosquitto_sub -d -t esp32/bme280/#


Share Share on Facebook Share on Twitter Bookmark on Reddit Share via mail
Creative Commons Attribution-Share Alike | Terms and conditions | Privacy policy