0

0

I wrote a code in the Arduino IDE. With this code, i can control 4 leds on and off from the web page. In the web page 9 buttons.

2 buttons for led-1 on and off. 2 buttons for led-2 on and off. 2 buttons for led-3 on and off. 2 buttons for led-4 on and off. 1 button for toggle all leds on and off.

I want also add a real button to the circuit. When i press this button, i want to run that toggle code. How can i do that?

This is my code;

#include <ESP8266WiFi.h>

const char* ssid = "UnderSpeed";
const char* password = "q1w2e3r4t5__";
const char* host = "192.168.1.185"; //it will tell you the IP once it starts up
                                        //just write it here afterwards and upload
int r0 = D0;
int r1 = D1;
int r2 = D2;
int r3 = D3;
int btn0 = D5;

WiFiServer server(301); //just pick any port number you like

void setup() {
  Serial.begin(115200);
  delay(10);
Serial.println(WiFi.localIP());
  // prepare GPIO2
  pinMode(r0, OUTPUT);
  pinMode(r1, OUTPUT);
  pinMode(r2, OUTPUT);
  pinMode(r3, OUTPUT);
  pinMode(btn0, INPUT_PULLUP);
  digitalWrite(r0, HIGH);
  digitalWrite(r1, HIGH);
  digitalWrite(r2, HIGH);
  digitalWrite(r3, HIGH);

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected

  WiFiClient client = server.available();
  if (!client) {
    return;
  }

  // Wait until the client sends some data
  while (!client.available()) {
    delay(1);
  }

  // Read the first line of the request
  String req = client.readStringUntil('\r');
  client.flush();

  // Match the request
  if (req.indexOf("") != -10) {  //checks if you're on the main page

    if (req.indexOf("/r0-OFF") != -1) { //checks if you clicked OFF
      digitalWrite(r0, HIGH);
      Serial.println("Relay-0 OFF");
    }
    if (req.indexOf("/r0-ON") != -1) { //checks if you clicked ON
      digitalWrite(r0, LOW);
      Serial.println("Relay-0 ON");
    }

    if (req.indexOf("/r1-OFF") != -1) { //checks if you clicked OFF
      digitalWrite(r1, HIGH);
      Serial.println("Relay-1 OFF");
    }
    if (req.indexOf("/r1-ON") != -1) { //checks if you clicked ON
      digitalWrite(r1, LOW);
      Serial.println("Relay-1 ON");
    }

    if (req.indexOf("/r2-OFF") != -1) { //checks if you clicked OFF
      digitalWrite(r2, HIGH);
      Serial.println("Relay-2 OFF");
    }
    if (req.indexOf("/r2-ON") != -1) { //checks if you clicked ON
      digitalWrite(r2, LOW);
      Serial.println("Relay-2 ON");
    }

    if (req.indexOf("/r3-OFF") != -1) { //checks if you clicked OFF
      digitalWrite(r3, HIGH);
      Serial.println("Relay-3 OFF");
    }
    if (req.indexOf("/r3-ON") != -1) { //checks if you clicked ON
      digitalWrite(r3, LOW);
      Serial.println("Relay-3 ON");
    }

    if (req.indexOf("/btn0") != -1) { //checks if you clicked OFF
      buton_islemi();
    }

  }

  else {
    Serial.println("invalid request");
   client.stop();
    return;
  }

  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\n";
  s += "Content-Type: text/html\r\n\r\n";
  s += "<!DOCTYPE HTML>\r\n<html>\r\n";

  s += "<br><input type=\"button\" name=\"rly0-on\" value=\"Relay-0 ON \" onclick=\"location.href='/r0-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly0-off\" value=\"Relay-0 OFF\" onclick=\"location.href='/r0-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly1-on\" value=\"Relay-1 ON \" onclick=\"location.href='/r1-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly1-off\" value=\"Relay-1 OFF\" onclick=\"location.href='/r1-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly2-on\" value=\"Relay-2 ON \" onclick=\"location.href='/r2-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly2-off\" value=\"Relay-2 OFF\" onclick=\"location.href='/r2-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"rly3-on\" value=\"Relay-3 ON \" onclick=\"location.href='/r3-ON'\">";
  s += "<br>";
  s += "<br><input type=\"button\" name=\"rly3-off\" value=\"Relay-3 OFF\" onclick=\"location.href='/r3-OFF'\">";

  s += "<br><br><br>";

  s += "<br><input type=\"button\" name=\"btn0\" value=\"Button-0 \" onclick=\"location.href='/btn0'\">";
  s += "<br>";

  s += "</html>\n";

  client.flush();


  // Send the response to the client
  client.print(s);
  delay(1);  
}

void buton_islemi() {
  digitalWrite(r0, !digitalRead(r0));
  delay(500);
  digitalWrite(r1, !digitalRead(r1));
  delay(500);
  digitalWrite(r2, !digitalRead(r2));
  delay(500);
  digitalWrite(r3, !digitalRead(r3));
  Serial.println("Button Pressed");
}
  • you really should use the built-in http lib to make it simpler, more readable, and more flexible, like turning on and off many pins per request, like if(server.arg("r1")=="OFF") – dandavis May 1 at 21:49
  • @dandavis could you write simple example? I’m newbie. – Onder Yilmaz May 1 at 21:56
  • i can't do it much better than the examples at File > Examples > ESP8266WebServer > – dandavis May 1 at 22:11
1

Just add your button check into the section where you're not handling a client. For example:

static uint8_t buttonState = LOW;
if (!client) {

  uint8_t bs = digitalRead(BUTTON_PIN);
  if (bs != buttonState) {
    buttonState = bs;
    if (buttonState == HIGH) {
      buton_islemi();
    }
  }

  return;
}
  • Thank you so much, it's working!! – Onder Yilmaz May 1 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.