Connecting your module to your Wi-Fi network
Now, we are going to check whether the ESP8266 and the Arduino IDE are working correctly, and connect your chip to your local Wi-Fi network.
To do so, let's perform the following steps:
- First, we need to write the code and then upload it to the board. The code is simple; we just want to connect to the local Wi-Fi network and print the IP address of the board. This is the code to connect to the network:
// Import required libraries #include <ESP8266WiFi.h> // WiFi parameters constchar* ssid = "your_wifi_name"; constchar* password = "your_wifi_password"; void setup(void) { // Start Serial Serial.begin(115200); // Connect to WiFi WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { }
You can simply copy the lines of the preceding code and then paste them into the ESP8266 Arduino IDE that you downloaded earlier. Of course, put your own Wi-Fi name and password in the code. Save this file with a name of your choice.
- Now, navigate to Tools | Boards and select Generic ESP8266 Module. Also, select the correct Serial port that corresponds to the FTDI converter that your are using.
- After that, we need to put the board in the bootloader mode, so we can program it. To do so, connect the pin GPIO 0 to the ground, via the cable we plugged into GPIO 0. Then, power cycle the board by switching the power supply off and then on again.
- Now, upload the code to the board and open the Serial monitor when this is done. Set the Serial monitor speed to 115200. Now, disconnect the cable between GPIO 0 and GND and power cycle the board again. You should see the following message:
WiFi connected 192.168.1.103
If you can see this message and an IP, congratulations, your board is now connected to your Wi-Fi network! You are now ready to build your first projects using the ESP8266 chip.