ddydytyddtdtddt — ESP32 simulation

trsrtnsrtdtrdhtrstyd

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

Components

Sketch code

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

// Velxio Simulator Wi-Fi Credentials
const char* ssid = "Velxio-GUEST";
const char* password = "";

// Telegram Bot Details (Paste your Token and ID here)
String BOT_TOKEN = "8776151324:AAGxEtHnOPCfToe8bqnv3Z-zJYWCXiyfS1E";
String CHAT_ID = "8691861646";

// Pin Definitions based on your Velxio circuit
const int PIR_PIN = 13;
const int LED_PIN = 25;

// Variable to track motion state and prevent spamming messages
bool motionDetected = false;

void setup() {
  Serial.begin(115200);
  
  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);
  digitalWrite(LED_PIN, LOW); // Ensure LED is off at start

  Serial.println("\nStarting Security System...");
  
  // Connect to Velxio Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWi-Fi Connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
  
  // Send a startup message to your phone
  sendTelegramMessage("System Armed: Security system is online.");
}

void loop() {
  int pirState = digitalRead(PIR_PIN);

  // If motion is detected and we haven't already registered it
  if (pirState == HIGH && !motionDetected) {
    digitalWrite(LED_PIN, HIGH); // Turn on LED
    Serial.println("ALERT: Motion Detected!");
    
    // Send alert to Telegram
    sendTelegramMessage("🚨 ALERT: Motion detected at home!");
    
    motionDetected = true; // Update state
  } 
  // If motion stops
  else if (pirState == LOW && motionDetected) {
    digitalWrite(LED_PIN, LOW); // Turn off LED
    Serial.println("Motion stopped.");
    motionDetected = false; // Reset state ready for next movement
  }

  delay(100); // Small delay to keep the simulation running smoothly
}

// Function to send the HTTP GET request to Telegram
void sendTelegramMessage(String message) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    
    // Replace spaces with URL encoding so the web request doesn't break
    message.replace(" ", "%20");
    
    // Construct the Telegram API URL
    String url = "https://api.telegram.org/bot" + BOT_TOKEN + "/sendMessage?chat_id=" + CHAT_ID + "&text=" + message;
    
    http.begin(url);
    int httpResponseCode = http.GET();
    
    if (httpResponseCode == 200) {
      Serial.println("Telegram message sent successfully!");
    } else {
      Serial.print("Error sending message. HTTP Code: ");
      Serial.println(httpResponseCode);
    }
    
    http.end();
  } else {
    Serial.println("Error: Wi-Fi not connected.");
  }
}