WiFi on Arduino: Setting up the ESP8266

The ESP8266 is a system-on-a-chip which can be programmed independently using the Arduino IDE. It can also be used as a WiFi shield for an Arduino Uno by setting upa simple serial connection. Once connected, the ESP8266 can be controlled by issuing the appropriate AT commands. The instructions below explain how to do this.

The Autodesk graphics library includes an ESP8266 module, but it does not behave perfectly. In particular, the body of the component appears on top of the wires which makes it difficult to see the actual connections. The graphical component is also a different model from the one you are using, and has no breakout board. You will therefore need to interpret the diagram.

Notice that the power to the ESP8266 is 3.3V, and that the signal wire from the Arduino passes through a voltage divider.

Create the required circuit, and then upload the code shown below. Notice that the set_esp8266_baud_rate() method contains some AT commands which set the baud rate to 9600. This is called in setup().

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 #include <SoftwareSerial.h> SoftwareSerial espSerial(10, 11); // RX, TX void setup() { Serial.begin(9600); // Set baud rate of ESP8266 to 9600 regardless of original setting set_esp8266_baud_rate(9600); espSerial.begin(9600); } void loop() { if (espSerial.available()) { Serial.write(espSerial.read()); } if (Serial.available()) { espSerial.write(Serial.read()); } } void set_esp8266_baud_rate(long int baud_rate){ long int baud_rate_array[] = {1200,2400,4800,9600,19200,38400,57600,74880,115200,230400}; int i, j, pause=10; String response; Serial.println("Setting ESP8266 baud rate..."); for (j=0; j<5; j++){ for (int i=0; i<10; i++){ espSerial.begin(baud_rate_array[i]); espSerial.print("AT\r\n"); delay(pause); if (espSerial.available()) { response=espSerial.readString(); response.trim(); if (response.endsWith("OK")) { espSerial.print("AT+UART_CUR="); espSerial.print(baud_rate); espSerial.println(",8,1,0,0"); delay(pause); if (espSerial.available()) { response=espSerial.readString(); } espSerial.begin(9600); delay(pause); espSerial.println("AT"); delay(pause); if (espSerial.available()) { response=espSerial.readString(); response.trim(); if (response.endsWith("OK")) break; else { Serial.println("Trying again..."); } } else { Serial.println("Trying again..."); } } } } if (response.endsWith("OK")) break; } espSerial.begin(9600); delay(pause); espSerial.println("AT"); delay(pause); if (espSerial.available()) { response=espSerial.readString(); response.trim(); if (response.endsWith("OK")) { Serial.print("\r\nBaud rate is now 9600"); } else { Serial.println("Sorry - could not set baud rate"); Serial.println("Try powering off and on again"); Serial.println("Don't try to use 115200"); } } } 
Line Comment
24 This is an array containing all of the common baud rates
29 Outer loop: tries to set the baud rate up to a maximum of five times
30 Inner loop: goes through each baud rate in turn trying to get an OK response from the ESP
37-41 If we get an OK response, the current baud rate matches the ESP's current setting. We then issues the AT command to set the speed.
144-146 Set the configuration of the serial channel
45-53 These lines test that the configuration has been successful
54-59 If any of the tests fail, we try again
67-81 A final test after all attempts have been completed. A status message is written to the serial port so that the user knows the end result.

With this code running, you should be able to enter the following commands into the Arduino serial monitor and get the response from the module:

Command Response
AT OK
AT+GMR Version information
AT+CWLAP List of available WiFi access points

Please be careful when you are sending AT commands to the ESP8266. You may accidentally place the unit into an indefinite deep sleep...

ESP8266 AT instruction set