- The ESP8266 can act as an access point, network device, or both.
- PuTTY requires CR+LF when ending an AT command.
If you’ve been following along with the series, you’ve completed Part 1 and connected to your ESP8266 (with either an Arduino or USB-to-serial converter) to your computer.
At this point, it’s important to understand that when we connected to the ESP8266 and received the ‘ready’ serial response back from the chip as it booted up, that ‘ready’ response was a routine programmed into the ESP8266’s stock firmware. We’ll discuss the firmware implications in greater detail during the next post, but until then keep in mind that the rest of this post pertains to the stock firmware only. If you flash the firmware with your own brewed up code to make an LED flash, the AT commands that we’re about to discuss will mean nothing to the ESP8266.
AT Commands
Finally! It’s time to actually interact with the ESP8266. When I say interact, I mean send the ESP8266 an instruction, and have it respond accordingly. This is accomplished through the use of AT commands. What are those, you ask? AT commands are essentially instructions used to control modems. Here’s a reference guide for a cellular module that shows something like 300 commands, but the number of AT commands recognized by the ESP8266 is limited to about 30 function (+/- a couple depending on the firmware version). Just keep in mind that they’re the same commands. The big-picture takeaway here is that the ESP8266 is a wireless device, and it “speaks the same language” as gps modules and phones’ GSM modems.
Let’s try out some commands! First, let’s figure out exactly what firmware version we’re dealing with. To do that, we’ll use the following command:
[code]AT+GMR[/code]
For Arduino IDE Users
- Enter the AT command on the input line.
- Hit Enter on the keyboard, or click the Send button.
- The command will be displayed in the terminal window, followed by the ESP8266’s response!
For PuTTY Users
- Enter the AT command on the input line, then hit Enter. This sends a special CR (carriage return) character to the ESP8266.
- The command you just typed will show up below the initial line (depending on your PuTTY settings), and nothing will seem to happen! This is because the ESP8266 expects to see two special characters (CR+LF) before it acknowledges the AT command! The Arduino serial monitor does this by default, but PuTTY cannot be set up to accomplish this automatically. The LF (line feed) character can be sent by hitting ctrl+J+Enter.
- The ESP8266’s response will be displayed.
[code]
AT version:0.25.0.0(Jun 5 2015 16:27:16)
SDK version:1.1.1
Ai-Thinker Technology Co. Ltd.
Jun 23 2015 23:23:50
[/code]
There’s a very good chance your firmware version will differ from mine, but don’t worry about that now. The core set of commands won’t change from firmware version to version, so the next couple of steps will still be applicable.
Setting the ESP8266’s WiFi Mode
‘Mode’ is quite vague here, but as a WiFi device, the ESP8266 can operate in a couple of different ways: either as an access point (AP), a network device (client), or both. Without going into the networking details, as an access point, the ESP8266 acts something like a wireless router and can be connected to by network devices. As a network device, the ESP8266 can connect to an existing wireless network. When operating in both modes, the ESP8266 can act like a network repeater.
By default, the ESP8266’s stock firmware is set to AP mode. If you’d like to confirm that this is the case, send the following command:
[code]AT+CWMODE?[/code]
You should get this response: +CWMODE:2, where 2 corresponds to AP mode as seen in the table below.
Number | Network Mode |
---|---|
1 | Network Device (client) |
2 | Access Point (AP) |
3 | Both |
Since we’re attempting to connect the ESP8266 to an existing network, we’ll need to change ESP8266 to network device mode with the following command:
[code]AT+CWMODE=1[/code]
List the Wifi Networks in Range
Now that the ESP8266 is acting as a network device, it can scan the airwaves for WiFi signals in range. To do that, send:
[code]AT+CWLAP[/code]
The ESP8266 will return a list of all the access points in range, with each line item consisting of the security level of the access point, the network name, the signal strength, MAC address, and wireless channel. As far as the security level goes, the table below can be used for reference.
Number | Security Mode |
---|---|
0 | Open |
1 | WEP |
2 | WPA_PSK |
3 | WPA2_PSK |
4 | WPA_WPA2_PSK |
Connecting to a Wireless Network
If you knew me, and I hadn’t obscurred the MAC address, it still wouldn’t be tough to choose my wireless network out of the list. It’s RockChalk, for obvious reasons. As you can see from the security mode designation of 4, this wireless network is password protected. This is important, as the password will need to be included in the AT command, along with the network’s name (SSID). Be sure to include the quotation marks (string indicators)!
[code]AT+CWJAP="RockChalk","password"[/code]
If you’ve done everything correctly, after a few seconds you’ll get responses back from ESP8266:
[code]
WIFI CONNECTED
WIFI GOT IP
OK
[/code]
That second line WIFI GOT IP means that it’s been assigned an IP address, so let’s check and see exactly what that address is.
[code]
AT+CIFSR
[/code]
Cool! Everything looks good! Everyone’s router interface is going to be different, but here’s a screenshot of my router’s device list as it pertains to the ESP8266. You’ll notice that the IP and MAC addresses match!
As of right now, the firmware that I have installed on the ESP8266 unfortunately doesn’t allow you do set the name of the device as it will appear on the network – the name would typically go where the blank box to the right of the IP address is shown, above. Mark my words, though, that feature will be available on future firmware releases.
List of AT Commands
As I alluded to earlier, there are plenty more AT commands than the ones we used here to connect to a WiFi network. These sorts of lists are all over the internet, and lots of them are nowhere close to complete. Instead of making my own list here, and adding to the ESP8266 confusion that’s out there, I’ll link to the actual source that no one ever seems to cite!