Chat freely about anything...

User avatar
By ykilicaslan06
#79106 These codes work normally,

I want to add alarm motion detector to these codes, how can I do it? and I want to make the alarm active or passive with 1 button

I use the device esp8266 nodemcu

What I need to add to this code, I want to compile both the relay control and the alarm system, I would be very glad if you help. I want to activate or deactivate the alarm with a button


RELAY KONTROL CODE


Code: Select all// esp librari
#include <ESP8266WiFi.h>

// kablosuz ağ ayarları
const char* ssid = "okhan"; //ağ adı
const char* password = "atakan2009"; // parola

// server protum
WiFiServer server(80);

// http istekleri
String header;

// röle duurumu
String relay1State = "kapat";
String relay2State = "kapat";
String relay3State = "kapat";
String relay4State = "kapat";

// röle pinleri
const int relay1 = 5; // GPIO5 D1 nodemcu karşılıkları
const int relay2 = 4; // GPIO4 D2
const int relay3 = 0; // GPIO0 D3
const int relay4 = 2; // GPIO2 D4

void setup() {
  Serial.begin(115200);
  // pinlerin durumu giriş/çıkış
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  // çıkış ayaları
  digitalWrite(relay1, HIGH);
  digitalWrite(relay2, HIGH);
  digitalWrite(relay3, HIGH);
  digitalWrite(relay4, HIGH);

  // hadi ağa bağlanalım
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  // espnin ip adresini yazdıralım
  Serial.println("");
  Serial.println("Kablosuz Bağlandı.");
  Serial.println("Sunucu Ip Adresi: ");
  Serial.println(WiFi.localIP());
  server.begin();
}

void loop() {
  WiFiClient client = server.available();   // sisteme bağlı cihaz sayısı

  if (client) {                             // yeni bağlantı
    Serial.println("Yeni Veri.");          // seri porttan okuyalım
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

            // turns the GPIOs on and off
            if (header.indexOf("GET /5/ac") >= 0)
            {
              Serial.println("GPIO 5 ac");
              relay1State = "ac";
              digitalWrite(relay1, LOW);
            }
            else if (header.indexOf("GET /5/kapat") >= 0)
            {
              Serial.println("GPIO 5 kapat");
              relay1State = "kapat";
              digitalWrite(relay1, HIGH);
            }
            else if (header.indexOf("GET /4/ac") >= 0) {
              Serial.println("GPIO 4 ac");
              relay2State = "ac";
              digitalWrite(relay2, LOW);
            }
            else if (header.indexOf("GET /4/kapat") >= 0) {
              Serial.println("GPIO 4 kapat");
              relay2State = "kapat";
              digitalWrite(relay2, HIGH);
            }
            else if (header.indexOf("GET /0/ac") >= 0)
            {
              Serial.println("GPIO 0 ac");
              relay3State = "ac";
              digitalWrite(relay3, LOW);
            }
            else if (header.indexOf("GET /0/kapat") >= 0)
            {
              Serial.println("GPIO 0 kapat");
              relay3State = "kapat";
              digitalWrite(relay3, HIGH);
            }
            else if (header.indexOf("GET /2/ac") >= 0) {
              Serial.println("GPIO 2 ac");
              relay4State = "ac";
              digitalWrite(relay4, LOW);
            }
            else if (header.indexOf("GET /2/kapat") >= 0) {
              Serial.println("GPIO 2 kapat");
              relay4State = "kapat";
              digitalWrite(relay4, HIGH);
            }

            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 12px 24px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");

            // Web Page Heading
            client.println("<body><h1>Evoto Ev Kontrol Sistemi</h1>");

            // Display current state, and ON/OFF buttons for GPIO 5
            client.println("<p>Buton 1 Durumu " + relay1State + "</p>");
            // If the relay1State is off, it displays the ON button
            if (relay1State == "kapat") {
              client.println("<p><a href=\"/5/ac\"><button class=\"button\">Aktif</button></a></p>");
            } else {
              client.println("<p><a href=\"/5/kapat\"><button class=\"button button2\">Pasif</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 4
            client.println("<p>Buton 2 Durumu " + relay2State + "</p>");
            // If the relay2State is off, it displays the ON button
            if (relay2State == "kapat") {
              client.println("<p><a href=\"/4/ac\"><button class=\"button\">Aktif</button></a></p>");
            } else {
              client.println("<p><a href=\"/4/kapat\"><button class=\"button button2\">Pasif</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 0
            client.println("<p>Buton 3 Durumu " + relay3State + "</p>");
            // If the relay1State is off, it displays the ON button
            if (relay3State == "kapat") {
              client.println("<p><a href=\"/0/ac\"><button class=\"button\">Aktif</button></a></p>");
            } else {
              client.println("<p><a href=\"/0/kapat\"><button class=\"button button2\">Pasif</button></a></p>");
            }

            // Display current state, and ON/OFF buttons for GPIO 2
            client.println("<p>Buton 4 Durumu " + relay4State + "</p>");
            // If the relay2State is off, it displays the ON button
            if (relay4State == "kapat") {
              client.println("<p><a href=\"/2/ac\"><button class=\"button\">Aktif</button></a></p>");
            } else {
              client.println("<p><a href=\"/2/kapat\"><button class=\"button button2\">Pasif</button></a></p>");
            }

            client.println("</body></html>");

            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Sunucu Kapandı.");
    Serial.println("");
  }
}


motion dedektor code

Code: Select allint Status = 12;
int sensor = 13;

void setup() {
  pinMode(sensor, INPUT); // declare sensor as input
  pinMode(Status, OUTPUT);  // declare LED as output
}

void loop(){
  long state = digitalRead(sensor);
  delay(1000);
    if(state == HIGH){
      digitalWrite (Status, HIGH);
      Serial.println("Motion detected!");
    }
    else {
      digitalWrite (Status, LOW);
      Serial.println("Motion absent!");
      }
}



i want this codes, merge, and add alarm on/off button