Uploading BME280 Sensor Data to ThingSpeak Using ESP32

by CarlosVoltT in Circuits > Electronics

108 Views, 0 Favorites, 0 Comments

Uploading BME280 Sensor Data to ThingSpeak Using ESP32

portada-820x461.png

In this tutorial, we will show you how to connect a BME280 sensor to an ESP32 to read temperature, humidity, and atmospheric pressure data

In this tutorial, we will show you how to connect a BME280 sensor to an ESP32 to read temperature, humidity, and atmospheric pressure data, and upload this data to ThingSpeak in real time. This type of project is ideal for IoT applications such as home weather stations or environmental monitoring systems.




What is BME280?

The BME280 is an advanced sensor from Bosch that can measure three environmental variables: temperature , relative humidity and atmospheric pressure . Thanks to its low consumption and compact size, it is widely used in IoT projects.

Main features:

  1. Temperature range: -40 to 85 °C
  2. Humidity range: 0% to 100%
  3. Pressure range: 300 to 1100 hPa
  4. Communication: I2C (used in this tutorial) and SPI




Necessary components

Before you begin, make sure you have the following components:

  1. ESP32
  2. BME280 Sensor (I2C version)
  3. Connection cables
  4. Account on ThingSpeak

An Esp32




Features of the ESP32-T module




Connectivity

The ESP32 module has all the WiFi variants :

  1. 802.11 b/g/n/e/i/n
  2. Wi-Fi Direct (P2P), P2P Discovery, P2P Group Owner mode and P2P Power Management

This new version includes connectivity via low-consumption Bluetooth

  1. Bluetooth v4.2 BR/EDR and BLE
  2. BLE Beacon

In addition, it can communicate using SPI, I2C, UART, MAC Ethernet, Host SD protocols




Microcontroller Features

The CPU is made up of a Tensilica LX6 model SoC with the following features and memory

  1. Dual core 32-bit with 160MHz speed
  2. Memoria ROM de 448 kBytes
  3. 520kBytes SRAM memory

Dyspnea at 48 Pines

  1. 18 ADC de 12 bits
  2. 2 8-bit DACs
  3. 10 pin contact sensors
  4. 16 PWM
  5. 20 Digital inputs/outputs




Food and consumption patterns

For the correct operation of the ESP32, it is necessary to supply a voltage between 2.8V and 3.6V. The energy it consumes depends on the operating mode. It contains a mode, the Ultra Low Power Solution (ULP) , in which basic tasks (ADC, RTC...) continue to be performed in Sleep mode.

Female pins

Dupont cables female male

PCB

Download gerber file –> Gerber_esp32




Circuit





Connecting the BME280 Sensor to the ESP32

We will use the I2C interface to connect the BME280 to the ESP32. Below are the necessary connections:

I2C Connections:

  1. VCC (BME280) → 3.3V (ESP32)
  2. GND (BME280) → GND (ESP32)
  3. SDA (BME280) → GPIO 21 (ESP32)
  4. SCL (BME280) → GPIO 22 (ESP32)

Tip : If your BME280 sensor does not have built-in pull-up resistors on the SDA and SCL lines , add external 4.7kΩ resistors between each line and the 3.3V voltage.

Tip : If your BME280 sensor does not have built-in pull-up resistors on the SDA and SCL lines , add external 4.7kΩ resistors between each line and the 3.3V voltage.




ThingSpeak Setup

Before uploading the data, we need to configure ThingSpeak:

  1. Create a ThingSpeak account if you don't have one.
  2. Create a new channel with the required fields (e.g. “Temperature”, “Humidity” and “Pressure”).
  3. Once created, copy the channel write API key , which we will use in the code to send the data.


Source Code

#include <WiFi.h>
#include <HTTPClient.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// Definiciones para el sensor BME280
#define PRESION_NIVEL_MAR_HPA (1013.25)
// Reemplaza con tus credenciales Wi-Fi
const char* red_wifi = "Tu_Red_Wifi";
const char* contrasena_wifi = "Tu_Contraseña";
// API Key de ThingSpeak
const char* clave_api = "Tu_Clave_Api";
// Dirección URL de ThingSpeak
const char* servidor_thingspeak = "http://api.thingspeak.com/update";
// Objeto del sensor BME280
Adafruit_BME280 sensor_bme;
void setup() {
Serial.begin(115200);
// Inicializar WiFi
WiFi.begin(red_wifi, contrasena_wifi);
Serial.print("Conectando a WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConectado a WiFi");
// Inicializar el sensor BME280
if (!sensor_bme.begin(0x76)) {
Serial.println("No se encuentra el sensor BME280. Revisa la conexión.");
while (1);
}
}
void loop() {
// Leer datos del sensor BME280
float temperatura = sensor_bme.readTemperature();
float humedad = sensor_bme.readHumidity();
float presion = sensor_bme.readPressure() / 100.0F; // Conversión a hPa
// Mostrar datos en el monitor serial
Serial.print("Temperatura = ");
Serial.print(temperatura);
Serial.println(" *C");
Serial.print("Humedad = ");
Serial.print(humedad);
Serial.println(" %");
Serial.print("Presión = ");
Serial.print(presion);
Serial.println(" hPa");
// Enviar datos a ThingSpeak
if(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
String url = String(servidor_thingspeak) + "?api_key=" + clave_api + "&field1=" + String(temperatura) + "&field2=" + String(humedad) + "&field3=" + String(presion);
http.begin(url);
int codigo_http = http.GET();
if(codigo_http > 0) {
Serial.printf("Código de respuesta: %d\n", codigo_http);
if(codigo_http == HTTP_CODE_OK) {
String respuesta = http.getString();
Serial.println("Datos enviados a ThingSpeak");
}
} else {
Serial.printf("Error en la conexión: %s\n", http.errorToString(codigo_http).c_str());
}
http.end();
} else {
Serial.println("Error de conexión WiFi");
}
// Intervalo de actualización (15 segundos)
delay(15000);
}