Simple switch read using ESP8266

Is there an example of using the ESP8266 in a stand alone mode in the Arduino IDE but not involving an Arduino module to simply read a switch state using an IPAD or similar device.

Dear Gaggymoon,

For ESP8266 in Stand alone Mode and Arduino IDE, generally you can set it up to read a state , exactly similar to Arduino, please remember that ESP8266 GPIOs are active LOW, and starts on High when ESP boots, Also remember that ESP volatge is 3.3v Please provide your setup or senario, what switch you want to monitor, and how it is connect, then i will try to help you accordingly

regards Samir Tafesh

Thanks Samir,

I am fully aware of hardware considerations and restraints for the ESP8266.

I have plenty of examples of using the ESP8266 to turn on a LED or a relay from another WiFi device (such as an IPAD) and have successfully got them to work.

However, I need to have a "reverse" situation where a switch changes the state (such as high to low) of an input pin (such as on GPIO2) of the ESP8266 and its state is observed on a WiFi device such as an IPAD.

I really need a code example for this basic task to get a starting point.


Gaggymoon: I have plenty of examples of using the ESP8266 to turn on a LED or a relay from another WiFi device (such as an IPAD) and have successfully got them to work.

Just curious. What firmware are you running on your ESP8266? And which version of ESP8266 do you have?

Dear Gaggymoon,

Thank you for the reply, i am currently working on a nearly similar setup, where i have a stand alone ESP which is connected to a relay to be controlled via web, the relay will be connected to GPIO2

a push button will be connected to GPIO0 initially connected as high (since the GPIO boots as high and to floating) so this way we insure that the GPIO0 is High by default, when the push button is triggered it will be set as low and thus we can read the status

So in your code some changes will be made as the example below:

Initially we prepare the GPIO0 as input

pinMode(0, INPUT);

and then in loop function where we check the availability of a client we update

// Check if a client has connected WiFiClient client = server.available(); if (!client) { // read the state of the pushbutton value: buttonState = digitalRead(0); //GPIO 0 // check if the pushbutton is pressed. // if it is, the buttonState is LOW: if (buttonState == LOW) { Serial.println("Button Pushed"); delay(100); }

in the coming days, Once i finalize my development i will upload the full setup and code

NB: i am using ESP8266-01 in the setup, and Arduino IDE as programing interface.

Regards Samir Tafesh return; }

Thanks again Samir,

I am using ESP8266-01 and nodemcu.

Not sure if your code is what I want as it appears to just check the state of a pushbutton and print it to the serial monitor. I want the state of a switch to be read by any WiFi device that is on the same network as the ESP8266.

Maybe I should further investigate "Instructibles" to see if there are any simple examples that do this although I have looked unsuccessfully so far.

The Button - a WiFi connected push button see has an example which is approximately what I need but it requires Lua and not the Arduino IDE.


I have found a pretty good example that works for me


This does what I want and a lot more than I need.

I can now observe the state of a switch on the ESP8266 using an Ipad.

All I have to do now is work through the example and understand the code.


Dear Gaggymoon,

Happy to see that you found what you are looking for, in fact at the beginning I did not understand well your setup, The code is easy to follow, but make sure to update the GPIOs according to ESP8266-01, and remember the ESP-01 has only two GPIOs not three as in the example, and both GPIOs are digital not analogue.

Regards Samir Tafesh

Thanks Samir for your help

My knowledge of networking and WiFi is very limited but hopefully I will get a better understanding soon.