In the following examples, I will be using the NodeMCU development board and will program it with the Arduino software. Hopefully you have already set-up your ESP8266 development environment and were able to load the blink sketch. If not please follow this tutorial first.
ESP8266 WiFi station mode example
The ESP8266 can communicate over WiFi in two different modes. It can connect to an existing wireless hot spot, or access point, similar to the way you connect your phone or computer to the Internet. This is called “station” mode. In station mode the ESP8266 board can reach out to the internet and may also communicate with other devices connected to the same network, including other ESP8266 modules. Here is a simple example. Remember to replace the values for the ssid and password variables with the name and password for your wireless network!
Once you load the code on your ESP8266 module, open the serial monitor and restart the module (pressing the reset button on the NodeMCU board will do the trick). All of the code executes in the set-up function, so it will only run once. You should see something like this, if all is well:
If you have access to your router admin screen, you should see the ESP8266 module in the list of connected devices.
ESP8266 WiFi Soft Access Point (Soft AP) example
Wi-Fi access points (AP) act as a hub for one or more stations (like your phone, or PC). An access point is connected to a wired network and often integrated with a router to provide access to stations connected to it to the Internet. If an access point is not connected to the Internet through a wired network, it is called a soft access point
ESP8266 module can operate as a soft access point (Soft AP) and support up to 5 Wi-Fi client devices, or stations, connected to it. Here is basic example code to turn the ESP8266 into a Soft Access Point:
When you load this script to the ESP8266 module, you should see the following serial output:
Now if you open the Wi-Fi connection menu of your phone and check the available networks, you should see a new one in the list. If you left the default settings in the sketch above, it will be called ESP8266. Connect to it, using the password in the same sketch and check the serial output again – you should see the count for devices connected increment.
That is all! The ESP8266 is also able to operate both in station and soft access point mode at the same time.
If you have two ESP8266 devices, you can make one be a station and the other an access point and they will be able to communicate with each other.
In the next tutorial, we will set-up a basic web-server on the ESP8266 module and serve HTML pages that will allow us to
– read data from the ESP8266 GPIO pins and display it in a browser window on your phone, or computer
– control the pins of the ESP8266 (turn an LED on and off) from a mobile phone, over Wi-Fi