/***** ESP32-S3 + DHT22 + MQ2 + POT + DS18B20 + MPU6050 + OLED + Firebase *****/
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHTesp.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <MPU6050.h>
// ---------------- WIFI DETAILS ----------------
const char* WIFI_SSID = "Wokwi-GUEST";
const char* WIFI_PASSWORD = "";
// ------------- FIREBASE REALTIME DB ------------
// Example DB URL: https://your-project-id-default-rtdb.firebaseio.com/
const char* FIREBASE_HOST = "https://aurasensehack-default-rtdb.firebaseio.com/"; // no https://
const char* FIREBASE_AUTH = "YOUR_DB_SECRET_OR_ID_TOKEN"; // ?auth= value
const char* FIREBASE_PATH = "/multiSensorHub.json";
// ----------------- PIN CONFIG ------------------
#define DHT_PIN 15 // DHT22 data
#define MQ2_PIN 2 // MQ2 analog out
#define POT_PIN 1 // potentiometer analog
#define DS18B20_PIN 14 // one-wire
#define I2C_SDA_PIN 11 // OLED + MPU6050
#define I2C_SCL_PIN 12
// ----------- OBJECTS ---------------------------
// DHT22
DHTesp dht;
// DS18B20
OneWire oneWire(DS18B20_PIN);
DallasTemperature ds18b20(&oneWire);
// MPU6050
MPU6050 mpu;
// OLED
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// --------------- WIFI CONNECT ------------------
void connectWiFi() {
Serial.print("Connecting to WiFi");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi connected!");
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
// ------------- SEND TO FIREBASE ----------------
bool sendToFirebase(const String& json) {
WiFiClientSecure *client = new WiFiClientSecure;
client->setInsecure(); // ignore cert
HTTPClient https;
String url = "https://" + String(FIREBASE_HOST) + String(FIREBASE_PATH) +
"?auth=" + String(FIREBASE_AUTH);
Serial.print("Firebase URL: ");
Serial.println(url);
if (!https.begin(*client, url)) {
Serial.println("HTTPS begin failed");
delete client;
return false;
}
https.addHeader("Content-Type", "application/json");
int httpCode = https.PUT(json); // overwrite node
if (httpCode > 0) {
Serial.print("HTTP code: ");
Serial.println(httpCode);
} else {
Serial.print("Firebase error: ");
Serial.println(https.errorToString(httpCode));
}
https.end();
delete client;
return (httpCode == 200 || httpCode == 201);
}
// -------------- OLED DISPLAY -------------------
void showOnOLED(float t_dht, float h_dht,
float mq2, float pot,
float t_ds,
int16_t gx, int16_t gy, int16_t gz) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.println("Multi Sensor Hub");
display.setCursor(0, 12);
display.printf("DHT: %.1fC %.1f%%", t_dht, h_dht);
display.setCursor(0, 24);
display.printf("MQ2 : %.0f POT: %.0f", mq2, pot);
display.setCursor(0, 36);
display.printf("DS18B20: %.1fC", t_ds);
display.setCursor(0, 48);
display.printf("Gx:%d Gy:%d Gz:%d", gx, gy, gz);
display.display();
}
// -------------------- SETUP --------------------
void setup() {
Serial.begin(115200);
// I2C
Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN);
// DHT
dht.setup(DHT_PIN, DHTesp::DHT22);
// DS18B20
ds18b20.begin();
// MPU6050
Serial.println("Init MPU6050...");
mpu.initialize();
if (mpu.testConnection()) {
Serial.println("MPU6050 OK");
} else {
Serial.println("MPU6050 FAIL");
}
// OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println("SSD1306 allocation failed");
for (;;);
}
display.clearDisplay();
display.display();
// WiFi
connectWiFi();
}
// --------------------- LOOP --------------------
void loop() {
// --- DHT22 ---
TempAndHumidity dhtData = dht.getTempAndHumidity();
float t_dht = dhtData.temperature;
float h_dht = dhtData.humidity;
// --- MQ2 & POT (raw ADC values 0-4095) ---
float mq2Val = analogRead(MQ2_PIN);
float potVal = analogRead(POT_PIN);
// --- DS18B20 temperature ---
ds18b20.requestTemperatures();
float t_ds = ds18b20.getTempCByIndex(0);
// --- MPU6050 motion data ---
int16_t ax, ay, az, gx, gy, gz;
mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// Serial debug
Serial.printf("DHT: %.1fC %.1f%% | MQ2: %.0f | POT: %.0f | DS: %.1fC\n",
t_dht, h_dht, mq2Val, potVal, t_ds);
Serial.printf("MPU6050 Acc: %d,%d,%d Gyro: %d,%d,%d\n",
ax, ay, az, gx, gy, gz);
// --- Create JSON payload for Firebase ---
String json = "{";
json += "\"dht\":{";
json += "\"temperature\":" + String(t_dht, 2) + ",";
json += "\"humidity\":" + String(h_dht, 2) + "},";
json += "\"mq2\":" + String(mq2Val, 0) + ",";
json += "\"pot\":" + String(potVal, 0) + ",";
json += "\"ds18b20_temp\":" + String(t_ds, 2) + ",";
json += "\"mpu6050\":{";
json += "\"ax\":" + String(ax) + ",";
json += "\"ay\":" + String(ay) + ",";
json += "\"az\":" + String(az) + ",";
json += "\"gx\":" + String(gx) + ",";
json += "\"gy\":" + String(gy) + ",";
json += "\"gz\":" + String(gz) + "}";
json += "}";
// --- send to Firebase ---
if (WiFi.status() != WL_CONNECTED) {
connectWiFi();
}
bool ok = sendToFirebase(json);
// --- show on OLED ---
showOnOLED(t_dht, h_dht, mq2Val, potVal, t_ds, gx, gy, gz);
if (ok) {
Serial.println("Firebase update OK");
} else {
Serial.println("Firebase update FAILED");
}
delay(5000); // every 5 seconds
}