Triggering GPIO of a ESP8266 webserver from another ESP8266

Hello, my automation project is nearly complete. I just have this finial snag on implementing my project.

I have 3 Esp8266s. 1 is my main webserver so we can call that ESP-1. It is hardwired to the mailserver (ESP-2) when certain GPIO pins output from ESP-1 to the mailserver it sends an email and that works fine.

The snag is with the 3rd Esp8266 (ESP-3). That webserver works fine as a standalone but I would like to have some wireless connection bettween ESP-1 and ESP-3.

Currently on ESP-1, while connected to the same network as my laptop, I can type in a web browser Http://192.168.0.20/5/ON and it will set GPIO 5 of ESP-1 HIGH. I am able to have ESP-1 and ESP-3s websever have a button that can do this via the website but I am trying to get GPIO INPUTs to do it in the sketch.

I would like to have when ESP-3 GPIO 5 goes HIGH it will act like you are pressing a web site button on ESP-1 such as sending the request

"content += "<a><a href='Http://192.168.0.21/2/ON' target='inlineframe'><button class=\"button button2\">ON</button></a>";

which I have on the sketch from ESP-1. With that code on the website, the button press doesn't open a page or refresh, it will just send the command to the other webserver to trigger GPIO2 on ESP-3.

How would one use ESP-3's GPIO 5 as an INPUT connected to an external switch to send a request such as "Http://192.168.0.20/5/ON" to turn activate the GPIO 5 on ESP-1 while keeping ESP-3's webserver in tact?

I am sorry if its a confusing question. I am new to programming, I have tried searching for anything that was done similar to this but had no luck to see if this is even possible what I am trying to accomplish.

I am just looking to find how to approach this and if someone has an example (not looking for someone to write the code for me).

Thanks for any help given.

I find your description a bit confusing. I've tried to pick out the main points:

ESP-2 is configured to send mails. It has pin X? configured as an input pin. If it detects a specific state on pin X? ( HIGH ? LOW ? ) it triggers a mail. It has IP address 192.168.0.? in your local network.

ESP-1 is configured as a web server. It has IP address 192.168.0.20? in your local network. GPIO5 is an output pin and is set HIGH when you issue the command Http://192.168.0.20/5/ON in a web browser.

ESP-3 is configured as a web server. It has IP address 192.168.0.? in your local network. It has pin GPIO5 defined as an input pin. When it detects that GPIO5 goes HIGH, you want some action? to be applied to ESP-1. It is not clear if this is through an electrical connection - pin manipulation, or sending it a command via a URL.
GPIO2 is configured as an output pin. It's value changes when ? occurs.

Can you complete the missing (or unclear) information where a ? appears above.
Can you somehow represent your architecture (ESP-1, 2 and 3 with the pins you have mentioned) on a hand drawn sketch and attach a picture to this thread.
Distinguish between physical buttons (say attached to GPIO pins) and buttons which appear on a web page.

Sorry about the confusion again. Attached is the block diagram sketch to kinda show how its hooked up.

ESP-2 is currently using inputs on pins 3,2,0 for various email alarms being sent. Each one has a different message to know what is being triggered. It sends the email with any of those pins go LOW. Its IP is 192.168.0.22 on my network.

ESP-1 is my web server with address 192.168.0.20. It is physically connected to relays and to ESP-2 via 1k OHM resistors. It outputs a LOW on GPIO pins 3,2,0 depending on what relay gets triggered. I also triggered GPIO 4,5 HIGH to activate the relays via the website that ESP-1 serves and also by typing in 192.168.0.20/5/ON or /OFF depending on the state I need.

ESP-3 is also a web server. It has a magnetic door switch and a relay physically wired to it. Door switch on GPIO5 as a LOW input and relay on GPIO 2 output HIGH to trigger the relay. Its address is 192.168.0.21. I can connect to it to activate the relay by going to the website it serves and clicking the web button or 192.168.0.21/2/ON or /OFF.

On ESP-1s webpage, I have an HTML "inline frame" type button that when pressed it will trigger 192.168.0.21/ON or /OFF when toggled. Clicking this web button does not refresh anything on the page it just appears to output that request to that URL.

The part above works just fine.

What I would like to do is have the door switch that is physically connected to ESP-3 GPIO 5 INPUT send a request to ESP-1 GPIO3 OUTPUT so it will set ESP-2 GPIO INPUT LOW.

Since ESP-2 is only the mail server and ESP-3 is on the other side of the building from ESP-1 and ESP-2, I couldn't come up with any other way to send the email notifications besides going through ESP-1 to get to ESP-2.

I haven't figured out how to use a digitalRead of ESP-3 GPIO5 to send such a request. The only thing I could think of was to trigger ESP-1 GPIO3 via the URL 192.168.0.20/ON but I lack the coding knowledge.

I can upload my sketches too if you need them to help understand what I am trying to do.

Thanks again

2018-04-26.jpg

ESP-3 is, at the moment, capable of detecting the state of the door switch wired to GPIO5 and operating a relay which is connected to GPIO2. You now want to add a new action so that GPIO5, in addition to triggering the relay, also sends a command to ESP-1. Is that right ?

In that case, you are simply then using ESP-3 as a web client of ESP-1. It [ESP-3] has to read the state of GPIO5 in the loop() and, if a status change has been detected, issue an HTTP GET command to ESP-1 to notify it of the new state, maybe by sending the URL "Http://192.168.0.20/X/ON" or "Http://192.168.0.20/X/OFF" as required.

It would be good to post the sketches that are running in ESP-1, ESP-2 and ESP-3. You'll probably have to use an attachment for each .ino file because of th limits on code tags.

Okay thanks. I will look into how to make ESP-3 into a web client. I should still be able to trigger the ESP-3 relay via 192.168.0.21/2/ON with using a web client instead of server? Or will that need to be done differently with HTTP GET requests from ESP-1 to ESP-3? Also, not sure if it matters or not, the ESP-1 web server is a cookie authentication type server.

6f3865e452bfb239d4f5771fb2ed3d2612d7b347.jpg
I don't understand what esp-2 is for. Surely, esp-1 could do the things esp-2 is doing, freeing up esp-2 for another project, and 3 of esp-1's pins?

I couldn’t get the webserver and mailserver to work together on the same ESP. I’m sure it’s possible but with my limited coding knowledge that’s the best I could do right now to get the same results.

Why didn't you ask for help with the coding, instead of making the hardware more complex, and then asking for help with that? This is what's known as an "X-Y" problem. You are asking for help with Y when what you really need help with is X.

I think the OP could post his 3 sketches, but it looks like he has got quite far towards a solution using the methods which he has already chosen and has presumably also gained a useful learning experience. Achieving the missing functionality is probably only a relatively minor change to his existing code.

I would suggest he get something working first. After that, he can, of course look at optimising the architecture and minimising the component count etc.

PaulRB:
Why didn't you ask for help with the coding, instead of making the hardware more complex, and then asking for help with that? This is what's known as an "X-Y" problem. You are asking for help with Y when what you really need help with is X.

he was asking

The hardware isn’t really more complex, just a few resistors. Also I figured triggering a pin on an another web server would be easier than spending many more hours working on that mail server. Hardware is what I know, and not coding as Juraj posted before. I couldn’t get the web and mail server to work so I moved on a different way.

Juraj:
he was asking

Ah, never saw that, sorry.

slvrsky:
The hardware isn’t really more complex, just a few resistors.

In my book, you made it 100% more complex by doubling the number of ESPs.

I will reply to your other thread about that problem.

I posted the sketches here: Esp8266 webserver with email output? - Project Guidance - Arduino Forum

Thanks to PaulRB I was able to combine the webserver and email server all in ESP-1. I am still working on trying to get ESP-3 to request an url that ESP-1 serves to get the email to trigger. I believe ESP-3 will need to be a client & server. Would I need to do a HTTP POST or GET request to get ESP-3 to request the url 192.168.0.20/2? I have tried a few different ways and most the time I use 192.168.0.20 as the server address. It will say connected but I’m not sure how to get it to act like as if you would type 192.168.0.20/2 in a web browser?

Esp 3 needs to be a client only as far as I understand your description. Yes, it should send a GET or POST to esp 1 (does not matter which, I think).

You should post your updated esp 1 sketch here because it has been significantly altered. I remember it is quite long, so you may not be able to post it in code tags. I also remember quite a lot of repetitive code, so perhaps we can suggest some ways to shrink it.

I am attaching the changes I made to ESP-1 as of right now. I changed a couple things around on the web page side. I will work today on the connection between ESP-1 and ESP-3 to try and do the GET request for 2 commands.

Thanks!

UPDATED ESP1.txt (15.4 KB)

I guess I am most confused on how to get ESP-3 (the client) to ask ESP-1 (server) for the request of lets say IPADDRESS/GPIO/ON? On all the examples I see are like this

 Serial.printf("\n[Connecting to %s ... ", host);
  if (client.connect(host, 80))
  {
    Serial.println("connected]");

    Serial.println("[Sending a request]");
    client.print(String("GET /") + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Connection: close\r\n" +
                 "\r\n"
                );

    Serial.println("[Response:]");
    while (client.connected())
    {
      if (client.available())
      {
        String line = client.readStringUntil('\n');
        Serial.println(line);
      }
    }
    client.stop();
    Serial.println("\n[Disconnected]");
  }
  else
  {
    Serial.println("connection failed!]");
    client.stop();
  }
  delay(5000);

}

Where would I add the requested url?

client.print(String("GET /2/ON") + " HTTP/1.1\r\n" +

If that’s all it is wow lol! I’ll try that. Thanks