Hi all,
Could someone help with the following:
I have a breakout board fitted with AHT20 and BMP280 sensors. This board communicates with an ESP32 C6 mini therefore I have selected pin 1 and pin 2 as my I2C pins.
My question is, how do I amend my code below, so both sensors can be controlled by I2C using my ESP32 C6 MINI board?
FYI, the addresses of the sensors are:0x38 and 0x77.
#include <WiFi.h>
#include <WebServer.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_AHTX0.h>
#define I2C_SDA 1
#define I2C_SCL 2
Adafruit_BMP280 bmp;
Adafruit_AHTX0 aht;
// Replace with your network credentials
const char* ssid = “xxxx”; // Enter SSID here
const char* password = “xxxxx”; //Enter Password here
WebServer server(80);
void setup() {
Serial.begin(9600); // setting next to band rate; Needs to be set to New Line to display correct
while (!Serial); // Wait for Serial Monitor to open
// Initialize AHT20 sensor
if (!aht.begin()) {
Serial.println(“Failed to initialize AHT20 sensor!”);
while (1);
}
Serial.println(“AHT20 sensor initialized.”);
// Initialize BMP280 sensor
if (!bmp.begin(0x77)) { // Default I²C address for BMP280 is 0x76
Serial.println(“Failed to initialize BMP280 sensor!”);
while (1);
}
Serial.println(“BPM280 sensor initialized.”);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected.”);
Serial.println(“IP address: “);
Serial.println(WiFi.localIP());
server.on(“/”, handle_OnConnect);
server.onNotFound(handle_NotFound);
server.begin();
Serial.println(“HTTP server started”);
}
void loop() {
server.handleClient();
}
void handle_OnConnect() {
// Read data from AHT20
sensors_event_t humidity, temp;
aht.getEvent(&humidity, &temp);
// Read data from BMP280
float pressure = bmp.readPressure() / 100.0F; // Convert to hPa
float temperature = temp.temperature;
float humidity1 = humidity.relative_humidity; // shows an error
//delay(1000); // Added by me so have delay beween reads
server.send(200, “text/html”, SendHTML(temperature,humidity1,pressure));
}
void handle_NotFound(){
server.send(404, “text/plain”, “Not found”);
}
String SendHTML(float temperature,float humidity1,float pressure){
String ptr = “<!DOCTYPE html>”;
ptr +=”<html>”;
ptr +=”<head>”;
ptr +=”<title>ESP32 Weather Station</title>”;
ptr +=”<meta name=’viewport’ content=’width=device-width, initial-scale=1.0′>”;
ptr +=”<link href=’https://fonts.googleapis.com/css?family=Open+Sans:300,400,600′ rel=’stylesheet’>”;
ptr +=”<style>”;
ptr +=”html { font-family: ‘Open Sans’, sans-serif; display: block; margin: 0px auto; text-align: center;color: #444444;}”;
ptr +=”body{margin: 0px;} “;
ptr +=”h1 {margin: 50px auto 30px;} “;
ptr +=”.side-by-side{display: table-cell;vertical-align: middle;position: relative;}”;
ptr +=”.text{font-weight: 600;font-size: 19px;width: 200px;}”;
ptr +=”.reading{font-weight: 300;font-size: 50px;padding-right: 25px;}”;
ptr +=”.temperature .reading{color: #F29C1F;}”;
ptr +=”.humidity .reading{color: #3B97D3;}”;
ptr +=”.pressure .reading{color: #26B99A;}”;
ptr +=”.superscript{font-size: 17px;font-weight: 600;position: absolute;top: 10px;}”;
ptr +=”.data{padding: 10px;}”;
ptr +=”.container{display: table;margin: 0 auto;}”;
ptr +=”.icon{width:65px}”;
ptr +=”</style>”;
ptr +=”</head>”;
ptr +=”<body>”;
ptr +=”<h1>ESP32 Weather Station</h1>”;
ptr +=”<div class=’container’>”;
ptr +=”<div class=’data temperature’>”;
ptr +=”<div class=’side-by-side icon’>”;
ptr +=”<svg enable-background=’new 0 0 19.438 54.003’height=54.003px id=Layer_1 version=1.1 viewBox=’0 0 19.438 54.003’width=19.438px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d=’M11.976,8.82v-2h4.084V6.063C16.06,2.715,13.345,0,9.996,0H9.313C5.965,0,3.252,2.715,3.252,6.063v30.982″;
ptr +=”C1.261,38.825,0,41.403,0,44.286c0,5.367,4.351,9.718,9.719,9.718c5.368,0,9.719-4.351,9.719-9.718″;
ptr +=”c0-2.943-1.312-5.574-3.378-7.355V18.436h-3.914v-2h3.914v-2.808h-4.084v-2h4.084V8.82H11.976z M15.302,44.833″;
ptr +=”c0,3.083-2.5,5.583-5.583,5.583s-5.583-2.5-5.583-5.583c0-2.279,1.368-4.236,3.326-5.104V24.257C7.462,23.01,8.472,22,9.719,22″;
ptr +=”s2.257,1.01,2.257,2.257V39.73C13.934,40.597,15.302,42.554,15.302,44.833z’fill=#F29C21 /></g></svg>”;
ptr +=”</div>”;
ptr +=”<div class=’side-by-side text’>Temperature</div>”;
ptr +=”<div class=’side-by-side reading’>”;
ptr += temperature;
ptr +=”<span class=’superscript’>°C</span></div>”;
ptr +=”</div>”;
ptr +=”<div class=’data humidity’>”;
ptr +=”<div class=’side-by-side icon’>”;
ptr +=”<svg enable-background=’new 0 0 29.235 40.64’height=40.64px id=Layer_1 version=1.1 viewBox=’0 0 29.235 40.64’width=29.235px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><path d=’M14.618,0C14.618,0,0,17.95,0,26.022C0,34.096,6.544,40.64,14.618,40.64s14.617-6.544,14.617-14.617″;
ptr +=”C29.235,17.95,14.618,0,14.618,0z M13.667,37.135c-5.604,0-10.162-4.56-10.162-10.162c0-0.787,0.638-1.426,1.426-1.426″;
ptr +=”c0.787,0,1.425,0.639,1.425,1.426c0,4.031,3.28,7.312,7.311,7.312c0.787,0,1.425,0.638,1.425,1.425″;
ptr +=”C15.093,36.497,14.455,37.135,13.667,37.135z’fill=#3C97D3 /></svg>”;
ptr +=”</div>”;
ptr +=”<div class=’side-by-side text’>Humidity</div>”;
ptr +=”<div class=’side-by-side reading’>”;
ptr +=(int)humidity1;
ptr +=”<span class=’superscript’>%</span></div>”;
ptr +=”</div>”;
ptr +=”<div class=’data pressure’>”;
ptr +=”<div class=’side-by-side icon’>”;
ptr +=”<svg enable-background=’new 0 0 40.542 40.541’height=40.541px id=Layer_1 version=1.1 viewBox=’0 0 40.542 40.541’width=40.542px x=0px xml:space=preserve xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink y=0px><g><path d=’M34.313,20.271c0-0.552,0.447-1,1-1h5.178c-0.236-4.841-2.163-9.228-5.214-12.593l-3.425,3.424″;
ptr +=”c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293c-0.391-0.391-0.391-1.023,0-1.414l3.425-3.424″;
ptr +=”c-3.375-3.059-7.776-4.987-12.634-5.215c0.015,0.067,0.041,0.13,0.041,0.202v4.687c0,0.552-0.447,1-1,1s-1-0.448-1-1V0.25″;
ptr +=”c0-0.071,0.026-0.134,0.041-0.202C14.39,0.279,9.936,2.256,6.544,5.385l3.576,3.577c0.391,0.391,0.391,1.024,0,1.414″;
ptr +=”c-0.195,0.195-0.451,0.293-0.707,0.293s-0.512-0.098-0.707-0.293L5.142,6.812c-2.98,3.348-4.858,7.682-5.092,12.459h4.804″;
ptr +=”c0.552,0,1,0.448,1,1s-0.448,1-1,1H0.05c0.525,10.728,9.362,19.271,20.22,19.271c10.857,0,19.696-8.543,20.22-19.271h-5.178″;
ptr +=”C34.76,21.271,34.313,20.823,34.313,20.271z M23.084,22.037c-0.559,1.561-2.274,2.372-3.833,1.814″;
ptr +=”c-1.561-0.557-2.373-2.272-1.815-3.833c0.372-1.041,1.263-1.737,2.277-1.928L25.2,7.202L22.497,19.05″;
ptr +=”C23.196,19.843,23.464,20.973,23.084,22.037z’fill=#26B999 /></g></svg>”;
ptr +=”</div>”;
ptr +=”<div class=’side-by-side text’>Pressure</div>”;
ptr +=”<div class=’side-by-side reading’>”;
ptr +=(int)pressure;
ptr +=”<span class=’superscript’>hPa</span></div>”;
ptr +=”</div>”;
ptr +=”</div>”;
ptr +=”</body>”;
ptr +=”</html>”;
return ptr;
}
Hi.
Are both sensors connected to the same pins?
I recommend taking a look at this I2C tutorial to see how to use multiple peripherals on the same I2C bus: https://randomnerdtutorials.com/esp32-i2c-communication-arduino-ide/
Regards,
Sara