I'm having some issues with esp8266 connecting to a wifi network. I'm testing with a hotspot from a mobile phone. If I set the network to open (no security) my code below works without any issue. If I enable security, WPA2 PSK (I'm assuming that means the personal version) and add the password to the code it fails to connect. Any suggestions as to what the issue might be?

//load softserial library
#include <SoftwareSerial.h>

//set a boolean constant variable to true
//#define DEBUG true
const boolean DEBUG = true;

//RX (pin 2) goes to TX on esp8266, TX (pin 3) goes to RX on esp8266
SoftwareSerial esp8266(2, 3);

//LED pin anode (long leg) is 13 and cathode (short leg) GND
int LED = 13;

//LED status array - led 0 is OFF (0) at startup.
int ledarray[] = {0};

void setup()
  //set the LED pin as output
  pinMode(LED, OUTPUT);

  //open the serial port

  //start esp8266 module (note: your esp's baud rate might be different)

  //reset esp8266 module
  senddata("AT+RST\r\n", 2000, DEBUG); 

  //set esp8266 as access point mode
  //senddata("AT+CWMODE=1\r\n", 1000, DEBUG); 

  //set esp8266 as wireless station mode (web server)
  senddata("AT+CWMODE=1\r\n", 1000, DEBUG);

  //set network SSID and password if esp8266 is activate as station mode else comment out
  senddata("AT+CWJAP=\"test\", \"\"\r\n", 6000, DEBUG);

  //get ip address for esp8266
  senddata("AT+CIFSR\r\n", 2000, DEBUG);

  //configure esp8266 for multiple connections
  senddata("AT+CIPMUX=1\r\n", 1000, DEBUG);

  //turn on esp8266 server on port 80
  senddata("AT+CIPSERVER=1,80\r\n", 1000, DEBUG);

void loop()
  //is the esp8266 sending a message 
    //if received data from esp8266
    if (esp8266.find("+IPD,"))
      //subtract 48 because the read() function returns the ASCII decimal 
      //value and 0 (the first decimal number) starts at 48
      int connectionid = esp8266.read() - 48;

      //read the url sent by the client, look for the variable (?)
      String msg;
      msg = esp8266.readStringUntil(' ');
      String command = msg.substring(0);

      //HTML served to browser
      String webpage = "<html><head><title>LED WEB SWITCH</title>";
      webpage += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"><style>.button {background-color: orange;border: none;color: white;padding: 15px 32px;text-align: center;display: inline-block;font-size: 16px;} .centre {text-align: center;}</style>";
      webpage += "</head><body class=\"centre\"><h1 class=\"centre\">LED WEB SWITCH</h1>";

      //if command is T (variable passed in button link on webpage)
      if (command == "T")
        //if position 0 of ledarray is 1
        if (ledarray[0] == 1)
          //turn LED off
          digitalWrite(LED, LOW);

          //set position 0 of ledarray to 0
          ledarray[0] = 0;

          //add text to webpage
          webpage += "<p>LED STATUS OFF</p>";
          //turn the LED on
          digitalWrite(LED, HIGH);

          //set position 0 of ledarray to 1
          ledarray[0] = 1;

          //add text to webpage
          webpage += "<p>LED STATUS ON</p>";
        //add text to webpage
        webpage += "<p>LED STATUS OFF</p>";

      //add the button (notice the href has a '?T')
      webpage += "<a class=\"button\" href=\"?T\">TAP</a></body></html>";

      //create a senddata string to send the wenbpage to the esp8266
      String cipsend = "AT+CIPSEND=";
      cipsend += connectionid;
      cipsend += ",";
      cipsend += webpage.length();
      cipsend += "\r\n";
      senddata(cipsend, 500, DEBUG);
      senddata(webpage, 500, DEBUG);

      //create a string closecommand, append the connection id, a return and newline
      String closecommand = "AT+CIPCLOSE=";
      closecommand += connectionid;
      closecommand += "\r\n";

      //send the closecommane
      senddata(closecommand, 500, DEBUG);

void senddata(String command, const int timeout, boolean debug)
  //send the received command to the esp8266

  //set int variable to the number of millisends since Arduino began
  long int time = millis();

  //while the time and the timeout is less than the number of millisends since Arduino began
  while((time + timeout) > millis())
    //while the esp8266 is sending messages
      //display output in the serial window 
  • fwiw, it a lot easier to debug if you program the esp instead of the uno.
    – dandavis
    Feb 11, 2020 at 17:46
  • SoftwareSerial doesn't work reliable at 115200 baud. change the baud rate in AT firmware and in sketch to 9600 baud
    – Juraj
    Feb 11, 2020 at 18:07
  • I've tried following the advice here: arduino.stackexchange.com/questions/24156/… //start at 115200 esp8266.begin(115200); //set to 9600 and start again senddata("AT+UART_DEF=9600,8,1,0,0\r\n", 2000, DEBUG); esp8266.begin(9600); This does not seem to work, at 9600 it refuses to connect to the network and doesn't give any errors. Mar 2, 2020 at 10:29

1 Answer 1


The solution to this was nothing to do with WPA2 PSK security or the baud rate being used. In my code I had a space between the WIFI server name and password (me correctly formatting). Apparently the ESP8266 does not like this one bit and the space needs to be removed - like so:

//set network SSID and password if esp8266 is activate as station mode else comment out
senddata("AT+CWJAP=\"test\",\"\"\r\n", 6000, DEBUG);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.