kichen proj — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by mamamamp35.

Components

Sketch code

#include "DHT.h"
#include <WiFi.h>
#include <HTTPClient.h>

#define DHT_PIN 15
#define DHT_TYPE DHT22

#define GAS_PIN 34
#define LED_PIN 13

#define GAS_LIMIT 2000
#define TEMP_LIMIT 30.0

// ===== بيانات الواي فاي (محاكي Velxio) =====
const char* WIFI_SSID = "Velxio-GUEST";
const char* WIFI_PASSWORD = "";

// ===== رابط الـ Firebase =====
const char* FIREBASE_URL = "https://kitchen-monitor-8f349-default-rtdb.firebaseio.com/sensors.json";

DHT dht(DHT_PIN, DHT_TYPE);

void connectWiFi() {
  Serial.print("Connecting to WiFi");
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  int attempts = 0;
  while (WiFi.status() != WL_CONNECTED && attempts < 30) {
    delay(500);
    Serial.print(".");
    attempts++;
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println();
    Serial.println("WiFi connected!");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println();
    Serial.println("WiFi connection FAILED after 15 seconds.");
  }
}

void sendData(float temperature, float humidity, int gasValue) {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("WiFi not connected, skipping send.");
    return;
  }

  String json = "{";
  json += "\"temperature\":" + String(temperature) + ",";
  json += "\"humidity\":" + String(humidity) + ",";
  json += "\"gas\":" + String(gasValue);
  json += "}";

  int httpResponseCode = -1;
  int retries = 0;

  while (httpResponseCode <= 0 && retries < 3) {
    HTTPClient http;
    http.begin(FIREBASE_URL);
    http.addHeader("Content-Type", "application/json");

    httpResponseCode = http.PUT(json);

    Serial.print("Attempt ");
    Serial.print(retries + 1);
    Serial.print(" -> HTTP Response code: ");
    Serial.println(httpResponseCode);

    if (httpResponseCode > 0) {
      String response = http.getString();
      Serial.println("Response: " + response);
    }

    http.end();

    if (httpResponseCode <= 0) {
      retries++;
      delay(1000); // ننتظر ثانية قبل المحاولة تاني
    }
  }

  if (httpResponseCode <= 0) {
    Serial.println("Failed to send data after 3 attempts.");
  }
}

void setup() {
  Serial.begin(115200);

  dht.begin();

  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW);

  connectWiFi();

  Serial.println("================================");
  Serial.println("       SYSTEM STARTED");
  Serial.println("================================");
}

void loop() {

  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  int gasValue = analogRead(GAS_PIN);

  Serial.println();
  Serial.println("--------- READINGS ---------");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  Serial.print("Gas Value: ");
  Serial.println(gasValue);

  // إرسال البيانات مع كل قراءة
  sendData(temperature, humidity, gasValue);

  bool highTemperature = false;
  bool highGas = false;

  if (!isnan(temperature) && temperatu