Esp8266 check status of relay

Hi guys

I’m create a switch that can turn off and on the light, but I’m wondering I’m also creating a website portal for that where I could see the status of the relay from anywhere in the world.

How could I go about seein the status of the light is on or off on my website? Would I need to create a database where I’d store a 0 for off and 1 for on

or can I put a variable in the code on the esp8266 where it would store that 0 and 1 and then the website would be able to access that??

How can the light be controlled? Through your website? With a button attached to an esp pin? With a regular light switch? Does the esp control the relay or only monitor it?

To view the status of an esp pin on a website, I would suggest that when the pin changes, the esp should update the website using an http POST command to a script hosted on the site. The updated status could be attached either as a parameter or in the body of the message. The script could be written in php for example. It would receive the POST and update a file on the server, or a database if you wish, but a file would be simpler. A web page hosted on the server could include that file so that the status can be viewed.

PaulRB:
How can the light be controlled? Through your website? With a button attached to an esp pin? With a regular light switch? Does the esp control the relay or only monitor it?

To view the status of an esp pin on a website, I would suggest that when the pin changes, the esp should update the website using an http POST command to a script hosted on the site. The updated status could be attached either as a parameter or in the body of the message. The script could be written in php for example. It would receive the POST and update a file on the server, or a database if you wish, but a file would be simpler. A web page hosted on the server could include that file so that the status can be viewed.

Yes sorry so the node mcu esp8266 would be connected to the relay through the pin and then the website would send a post request to the ESP8266 IP address which would signal the relay to turn on.

Ah okay so instead of storing the status of the mcu on the database I would store it on a file in the server which would then be accessible by the website if I was on a computer or phone right?

You are saying that the website is the only way to switch the light on and off? Then you don't need the esp to monitor the relay status, it will be whatever status website last set it to. Correct?

PaulRB:
You are saying that the website is the only way to switch the light on and off? Then you don't need the esp to monitor the relay status, it will be whatever status website last set it to. Correct?

Yes so either the website and then if I decide to implement alexa or google home.

But what i was thinking was accessing my website even when your not home and to see if you switched off your lights since it would say the current status .

In example.

I run, on a RPi 4, a MQTT Broker that several ESP32's publish and subscribe. One ESP32 replaces my HVAC control box. I can go to my web site turn off/on the HVAC system, set hot or cold, turn the fan off/on, and set the desired temperature using my phone or an internet browser.

Remember, you are only monitoring the latest intention of your system, not the relay operation. That would require an extra set of contacts on the actual relay.

Paul

Tomstud122:
Yes so either the website and then if I decide to implement alexa or google home.

OK, thats different. That is why I asked about the different ways to switch the light on or off. If your website is the only way, then there is no need for the esp to inform your website of the relay status because the website always knows the status. But if there is another control, then the esp must inform your website when the relay status changes.

Actually, even without alexa, there is another way that the relay status could change. There could be a power outage at a time when the light was on. When power is restored, the light would be off, but your website would think it was on. So you do need the esp to inform the website that the light is off when the esp starts up.