ESP8266 WEBSERVER RELAY CONTROL and alarm on off

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

// 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

int 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

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

Toss the code in a blender, and hit puree, then. Who knows what you'll get.

If you have some requirements for the resulting program, feel free to share them.

The codes have been combined, but the sensor did not work, the motion was detected in a message that did not output the serial connection on the monitor.
how do I change these codes?

I want to put a button in webserver, I want to make the alarm status open or closed,

#include <ESP8266WiFi.h>

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
int Balarm = 13;   //Buzzer  alarm  connected to GPIO-13 or D7 of nodemcu
int PIRsensor = 15; //PIR sensor output connected to GPIO-15 or D8

void setup() {
  Serial.begin(115200);
  // pinlerin durumu giriş/çıkış
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
   pinMode(PIRsensor, INPUT); // PIR sensor as input  
  pinMode(Balarm, OUTPUT);   // Buzzer alaram as output
  digitalWrite (Balarm, LOW);// Initially buzzer off
  
  // çı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() {

  int state = digitalRead(PIRsensor); 
  delay(500);                         //
  
  
  WiFiClient client = server.available(); 

  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);
            }
            else if (state == HIGH){
             digitalWrite (Balarm, HIGH);    //If intrusion detected ring the buzzer
      delay(5000);                    
      delay(5000);
      delay(5000);                    //Ring buzzer for 15 seconds 
      //Serial.println("Motion detected!");
    }
    else {
      digitalWrite (Balarm, LOW);     //No intrusion Buzzer off
      //Serial.println("Motion absent!");
      
            }

            // 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("");
  }
}

I have a project with WebServer and relays and functions activated by relays, but it is at different level.

  int state = digitalRead(PIRsensor);
  delay(500);                         //

Check for motion, and then stuff your head in the sand for a while. Why?

Why is there an empty comment? Mindless cut and paste sucks.

            else if (state == HIGH){
             digitalWrite (Balarm, HIGH);    //If intrusion detected ring the buzzer

You only do this if there is a client connected, AND the client asked for the initial page. Why is this code HERE?

I'm a bit novice, I'm doing with trial and error. Can you help?

I have reviewed, but this level exceeds me :slight_smile: How can I make changes in my codes? can you help me?

4 relay
1 motion sensor

I can control the relays, but I want to add the motion detector and set the alarm, the alarm is activated and the alarm is disabled.

How can I make changes in my codes?

With any text editor.

can you help me?

No, because you have not defined (adequately) your requirements.

but I want to add the motion detector and set the alarm, the alarm is activated and the alarm is disabled.

Those are not requirements. They are goals, at best. A requirement defines when to do something (like read the motion sensor or activate the alarm or disable the alarm), as well as the condition(s) under which that should happen.

Dear friend, how do I make changes to this system to control the motion sensor? I did the merge, but it didn't work, could you please review and edit the code

Dear friend, how do I make changes to this system to control the motion sensor?

You can't. Motion sensor do what they want. You can't control them. ALL you can do is react to the output when you read a motion sensor.

I did the merge, but it didn't work

You STILL haven't properly defined any requirements, do you can NOT write any code.

could you please review and edit the code

We can review the code, WHEN YOU HAVE SOME REQUIREMENTS. YOU get to edit the code to properly implement those as-yet-undefined requirements.

1 Like