digitalRead in esp8266

For another code, I need to know the state of the digital pins on esp. However, I can't with this simple example to make it run.

I have tried with D5,0,4 and all the possible pinouts but nothing. All the time the serial prints 0... >:(

Anyone can help me?thanks!

void setup() {
  pinMode(1, OUTPUT);
  Serial.begin(9600);
}


void loop() {
  digitalWrite(1, LOW);
  Serial.println(digitalRead(1));
  delay(1000);
  digitalWrite(1, HIGH);
  Serial.println(digitalRead(1));
  delay(2000);
}

What is connected to pin zero?

A relay

alvaroboven:
A relay

?

TheMemberFormerlyKnownAsAWOL:
?

.
I am using the supla software to change the state of a relay and i need to know how to use digitalRead on esp8266

thanks

alvaroboven:
pinMode(1, OUTPUT);
Serial.begin(9600);

GPIO1 is one of the UART pins. You can't use it as a normal IO pin and have Serial working at the same time.

Did you mean to use "D1" instead of "1"? Boards — ESP8266 Arduino Core 3.1.2-21-ga348833 documentation

Pieter

PieterP:
Did you mean to use "D1" instead of "1"? Boards — ESP8266 Arduino Core 3.1.2-21-ga348833 documentation

Yes, The pin mapping on the ESP8266 is handled differently and more efficiently than on many other arduino boards.
How it is done varies depending on the specific ESP8266 board. (they don't all do it the same way)
On all esp8266 Arduino boards, naked constants like 0, 1, 2, etc... map directly to the GPIO bit number.
But some boards also use Dn defines to map between the Arduino pin numbers and GPIO bit numbers.

If you have a board that uses Dn defines (D0, D1, D2, D15, etc...) then as PieterP pointed out, pin Dn is not necessarily, and quite often, not the same as pin n.
i.e. on boards that use Dn defines, D1 is often not the same physical pin on the board as 1

Some esp8266 boards use the Dn pin mappings, and some done.
And boards that use Dn pin mapping don't all do it the same way.
So it is a bit of a wreck.
The one saving grace is that ESP8266 boards tend to label the pins in a way that if you use the name on the label for the pin,
D1, D2, ... ,D7or 1, 2, ... 7, etc... then you will access the pin with that label.

--- bill