ESP8266 Hello World! Your first ESP8266 code

In this post we will:

As a requirement, make sure you have the Arduino IDE up and running on your system. Refer to this awesome guide by lastminuteengineers.com to learn how to set it up to work with your ESP8266.

The code

Assuming you now have your Arduino IDE open and your ESP8266 connected, create a new sketch and paste the following code:

#include <ESP8266WiFi.h>

const char* ssid = "YOUR_SSID";     // Replace with your WiFi network name
const char* password = "YOUR_PASSWORD"; // Replace with your WiFi network password

void setup() {
  Serial.begin(115200); // Set the baud rate of the serial monitor to 115200
  WiFi.begin(ssid, password); // Connect to WiFi network
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.print("WiFi connected. IP address: ");
  Serial.println(WiFi.localIP()); // Output the IP address to the serial monitor
}

void loop() {
  // Your code here
}

You won’t need to install any external libraries for the following code to run.

Let’s break it down:

We first include the ESP8266WiFi.h library, which provides us with functions for connecting to WiFi networks.

#include <ESP8266WiFi.h>

We then define the ssid and password variables to store our WiFi network name and password:

const char* ssid = "YOUR_SSID";     // Replace with your WiFi network name
const char* password = "YOUR_PASSWORD"; // Replace with your WiFi network password

The setup() and loop() are the two standard callbacks your ESP8266 will run automagically. setup() will be called only once, while loop() will be called repeatedly. In the setup() function, we start the serial communication (specifying the Baud Rate, which is the bit rate your computer and your ESP8266 agree to use) and connect to the WiFi network using WiFi.begin(). We then use a while loop to wait for the connection to be established, and once connected, output the IP address to the serial monitor using WiFi.localIP():

void setup() {
  Serial.begin(115200); // Set the baud rate of the serial monitor to 115200
  WiFi.begin(ssid, password); // Connect to WiFi network
  Serial.println("Connecting to WiFi...");
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting...");
  }
  Serial.print("WiFi connected. IP address: ");
  Serial.println(WiFi.localIP()); // Output the IP address to the serial monitor
}

In the loop() function, you can add your own code as needed.

Troubleshooting

If your Arduino IDE serial panel is showing weird stuff / doesn’t show anything:

Serial Monitor with Old Editor (docs.arduino.cc)

 

Serial Monitor with New Editor (docs.arduino.cc)

 

I’m using Arch Linux, and I had to add my user to the dialout group to be able to read / write from the USB port. If you have the same issues, open your terminal and issue this command:

stat /dev/ttyUSB0

In the output, find the Gid value (Gid: ( 20/ dialout) in my case) and add your user to that group:

sudo usermod -a -G dialout <user>

That should do it!


Made with ❤️ by Mikepicker