ESP8266 01 wifi module
I take your code as a test sketch and will not comment on the terrible way you read Serial input.

The AT firmware requires \r\n as command ending characters. You can set them in Serial Monitor as option LN &amp; CR, but then use print to send the command to esp8266.

Or set in Serial Monitor "No line ending" and let in sketch the println to send the command. println adds \r\n to printed string.

If you had only "New line" set in Serial Monitor and used println the resulting command was for AT firmware disturbing "AT\n\r\n".