So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By Baman313v
#81611 My goal is to have the relay turn on while the sensor is HIGH/ detecting motion but only if the button on the webpage is set to "on" (When R1 == "on")
It connects to wifi, displays the webpage and has no problems with any of the buttons or anything. The one problem is that when R1 == "on" the PIR sensor isnt activating the relay.

Code: Select all/*********
RedSpade
*********/
#include <ESP8266WiFi.h>

const char* ssid     = "WIRELESS NAME";
const char* password = "PASSWORD";
WiFiServer server(80);
String header;

// Auxiliar variables to store the current output state
String R1State = "off";
String R2State = "off";
String R3State = "off";
String R4State = "off";

// Assign output variables to GPIO pins
const int R1 = D1;
const int R2 = D2;
const int R3 = D3;
const int R4 = D4;
const int S1 = D5;

void setup() {
  Serial.begin(115200);
  pinMode(R1, OUTPUT);
  pinMode(R2, OUTPUT);
  pinMode(R3, OUTPUT);
  pinMode(R4, OUTPUT);
  pinMode(S1, INPUT);
  digitalWrite(R1, LOW);
  digitalWrite(R2, LOW);
  digitalWrite(R3, LOW);
  digitalWrite(R4, LOW);
  // Connect to Wi-Fi network with SSID and password
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.hostname("Barrel1");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println(WiFi.hostname());
  server.begin();
}

void loop() {
  web();
}

void web(){
  WiFiClient client = server.available();
  if (client) {
    Serial.println("New Client.");
    String currentLine = "";
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        header += c;
        if (c == '\n') {
          if (currentLine.length() == 0) {
            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 /Skeleton/on") >= 0) {
              Serial.println("Skeleton on");
              R1State = "on";
              sensor();
              // digitalWrite(R1, HIGH);
            } else if (header.indexOf("GET /Skeleton/off") >= 0) {
              Serial.println("Skeleton off");
              R1State = "off";
              sensor();
              // digitalWrite(R1, LOW);
            } else if (header.indexOf("GET /Siren/on") >= 0) {
              Serial.println("Siren on");
              R2State = "on";
              sensor();
              // digitalWrite(R2, HIGH);
            } else if (header.indexOf("GET /Siren/off") >= 0) {
              Serial.println("Siren off");
              R2State = "off";
              sensor();
              // digitalWrite(R2, LOW);
            } else if (header.indexOf("GET /Lights/on") >= 0) {
              Serial.println("Siren on");
              R3State = "on";
              sensor();
              // digitalWrite(R3, HIGH);
            } else if (header.indexOf("GET /Lights/off") >= 0) {
              Serial.println("Siren off");
              R3State = "off";
              sensor();
              // digitalWrite(R3, LOW);
            } else if (header.indexOf("GET /Extra/on") >= 0) {
              Serial.println("Siren on");
              R4State = "on";
              sensor();
              // digitalWrite(R4, HIGH);
            } else if (header.indexOf("GET /Extra/off") >= 0) {
              Serial.println("Siren off");
              R4State = "off";
              sensor();
              // digitalWrite(R4, LOW);
            }
           
            // Display the HTML web page
            //html and css
            client.println("<!DOCTYPE html><html>");
            client.println("<head>");
            client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            client.println("<style>html { font-family: OCR A Std; font-size: 40px; color: #42b3f4; display: inline-block; margin: 0px auto; text-align: center; display: inline-block;}");
            client.println(".btn-group button { background-color: #0069b5; border: 1px #002642; color: white; padding: 18px 35px; cursor: pointer; float: center; font-family: Courier New;font-size: 17px;}");
            client.println(".btn-group button:hover { background-color: #002642;}</style></head>");
            client.println("<body style=\"background-color: #61686d;\"><h1>DEVICE NAME</h1><h2 style=\"font-size: 30px;\">Barrel #1</h2>");
            client.println("<div class=\"btn-group column\">");
            // Display current state of Skeleton
            if (R1State=="on") {
              client.println("<a href=\"/Skeleton/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Skeleton On</button></a>");
            } else {
              client.println("<p><a href=\"/Skeleton/on\"><button class=\"btn-group\">Turn Skeleton Off</button></a>");
            }
               
            // Display current state of Siren
            if (R2State=="on") {
              client.println("<a href=\"/Siren/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Siren On</button></a>");
            } else {
              client.println("<a href=\"/Siren/on\"><button class=\"btn-group\">Turn Siren Off</button></a>");
            }

            // Display current state of Lights
            if (R3State=="on") {
              client.println("<a href=\"/Lights/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Lights On</button></a>");
            } else {
              client.println("<a href=\"/Lights/on\"><button class=\"btn-group\">Turn Lights Off</button></a>");
            }

            // Display current state of Extra
            if (R4State=="on") {
              client.println("<a href=\"/Extra/off\"><button class=\"btn-group\" style=\"background-color: #63799b\">Turn Extra On</button></a>");
            } else {
              client.println("<a href=\"/Extra/on\"><button class=\"btn-group\">Turn Extra Off</button></a>");
            }
            client.println("</div></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("Client disconnected.");
    Serial.println("");
  }
}

void sensor(){
  int sensorval = digitalRead(S1);
  Serial.println(sensorval);
  if ((sensorval == HIGH) && (R1State == "on")) {
    digitalWrite(R1, HIGH);
    }
    else {
    digitalWrite(R1, LOW);
    }
}


Im pretty sure the code for the relay should be something along the lines of
Code: Select allint sensorval = digitalRead(S1);
Serial.println(sensorval);
if ((sensorval == HIGH) && (R1State == "on")) {
  digitalWrite(R1, HIGH);
  }
  else {
  digitalWrite(R1, LOW);
  }

S1 is the PIR state
R1 is the relay
R1State is the state of the relay