Skip to main content
ESPBoards

Programming ESP8266 Hello World Code Examples for Beginners and Advanced Users

This article provides Hello World code examples to get started with ESP8266 programming. Learn how to start programming the ESP8266 and ESP32 boards.


Introduction #

ESP8266 Development Board

Looking at the ESP8266 price one might think it will be some cheap microcontroller that has no power, wastes energy and does not provide any useful peripherals, compared to the microcontrollers available today. However, it has a is a built-in WiFi module and enough processing power for any hobbyist-grade project.

Today we will learn how to start developing with the ESP8266 and you probably already know there is no better place to start than Hello World.

ESP8266 Programming basics #

To start with development, we expect that our first program is going to write the famous "Hello World" in the console. However, ESP8266 is a separate device from your computer and also it does not even have a screen, so where will it print out what we want? The answer is Serial Communication. We will connect the ESP8266 to our computer using a USB port, which translates to Universal Serial Bus. Did you notice, it includes the word Serial in it? It means that we communicate to the devices connected to USB ports using earlier mentioned Serial Communication.

The first step is to set up the ESP8266. Assuming you have the Arduino IDE set up, we will simply need to connect the ESP8266 board to our computer with the USB port.

So to begin with programming, we need to set up the serial port on the ESP8266 board. We can do this by using the function Serial.begin(), which takes two parameters: the baudrate (the speed at which the data is sent through the serial port) and the data bits (determining how much data will be sent at a time).

For this example, we'll use a baudrate of 9600 and 8 data bits.

Serial.begin(9600, SERIAL_8N1);

Writing the Message #

Once the serial port is set up, we can write the message. We will do this using the Serial.print() function, which takes a provided string as a parameter and simply prints it to the serial port.

For this example, as you may guess, we'll print the message "Hello World!".

Serial.print("Hello World!");

Combining setup and loop together #

Now to see what we did in action, we actually need to place the code in setup() function, which will be run once after the ESP8266 boots up.

void setup() {
// Initialize serial port with baud rate of 9600 and 8N1 configuration
Serial.begin(9600, SERIAL_8N1);

// Print message to serial console
Serial.print("Hello World!");
}

void loop() {
// This program does not require any code in the loop function
}

If you upload this program to the ESP8266 board and will open a Serial Monitor, you should see the message "Hello World!" printed out in the Serial Monitor once the ESP8266 boots.

Running the Program #

Once we are finished with the code, we can upload and run the program. Simply press the "Upload" button in the IDE in the right upper corner. You should start seeing some output in the terminal (do not confuse it with the serial monitor), telling you about compiling the code and uploading it to the ESP8266. Once the program done uploaded, the ESP8266 will reset and the program will start running automatically.

Hello World for Beginners #

Hello World is a classic programming exercise that is used to introduce new programmers to the basics of coding. It is a simple program that prints out the phrase "Hello World" to the console. We have done that already, but that is not enough with the microcontrollers.

With the microcontrollers the de-facto "Hello World" application is blinking a LED. And we are lucky because ESP8266 has a built-in LED. Let's jump straight into the code:

// Define the LED pin number
const int LED_PIN = 2;

void setup() {
// Set the LED pin as an output
pinMode(LED_PIN, OUTPUT);
}

void loop() {
// Turn the LED on
digitalWrite(LED_PIN, HIGH);

// Wait for 500ms
delay(500);

// Turn the LED off
digitalWrite(LED_PIN, LOW);

// Wait for 500ms
delay(500);
}

In this example, we have defined the pin number of the built-in LED, which is usually pin 2 on ESP8266 boards. Then in the setup() function, we set the LED pin as an output using the pinMode() function.

In the loop() function, we turn the LED on by setting the LED pin to HIGH using the digitalWrite() function. Then we wait for 500ms using the delay() function. After that, we turn the LED off by setting the LED pin to LOW, and wait for another 500ms.

If you click the upload button in the Arduino IDE and wait for it to finish uploading, the ESP8266 board will restart automatically and after it starts up, you should see the built-in LED on the board start blinking. To get a better understanding of the code, try adjusting the delays between the LED on and off status.

Hello World for Intermediate Users #

Now that we have the ESP8266 programming basics, let's step up the game a bit by playing around with other basic functions before we jump into the actual ESP8266 programming.

Adding User Input #

Once you have the basic "Hello World" program working, we can start adding more advanced and exciting features. One of the most common features to add is user input, which will allow the user to enter a message that will be printed out. Here is an example of a program that adds user input:

#include <ESP8266WiFi.h>

void setup() {
Serial.begin(115200);
Serial.println("Hello World!");
Serial.println("Please enter a message:");
}

void loop() {
if (Serial.available()) {
String message = Serial.readString();
Serial.println("You entered: " + message);
}
}

This program prints out the message "Hello World!" and then prompts the user to enter a message. When the user enters a message, it is printed out to the serial port. This is a great way to get user input from ESP8266.

Hello World for ESP8266 #

Once you have blinked the built-in LED on the ESP8266, you can move on to more advanced projects. Now the same as the Hello World by blinking the built-in LED is a default first program for microcontrollers, the ESP8266 equivalent of Hello World is connecting it to the WiFi network. Therefore next, we will explore the esp8266 wifi example.

Connect to your WiFi network #

Once we are ready with the ESP8266 setup, the next step is to write the Hello World code. This code is slightly more complex than the code for beginners, but it is still relatively simple. Here is the esp8266 wifi example:

#include <ESP8266WiFi.h>

// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

void setup() {
Serial.begin(9600);
delay(10);

// Connect to Wi-Fi network with SSID and password
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}

void loop() {
// Do nothing here
}

This code will connect your ESP8266 board to the provided WiFi network in the configuration (SSID and password) and will print out the IP address of the device.

Create a WEB Server on ESP8266 #

However, again we are just seeing something printed out into the serial monitor. That is not exciting!

The next step that we all have been waiting for... We will create a WEB Server on ESP8266:

#include <ESP8266WiFi.h>

const char* ssid = "YourNetworkName";
const char* password = "YourNetworkPassword";

WiFiServer server(80);

void setup() {
Serial.begin(115200);
Serial.println();

WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi network: " + String(ssid));

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: " + WiFi.localIP().toString());

server.begin();
Serial.println("Server started");
}

void loop() {
WiFiClient client = server.available();

if (client) {
Serial.println("New client");
String currentLine = "";

while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
if (c == '\n') {
if (currentLine.length() == 0) {
client.println("HTTP/1.1 200 OK");
client.println("Content-type:text/html");
client.println();
client.println("<h1>Hello World!</h1>");
break;
} else {
currentLine = "";
}
} else if (c != '\r') {
currentLine += c;
}
}
}

client.stop();
Serial.println("Client disconnected");
}
}

This code example connects the ESP8266 board to a WiFi network and sets it up to act as a web server and respond to HTTP requests from clients. The board connects to a WiFi network using your provided SSID and password and then starts a web server on port 80.

In the loop() function, the code continuously checks for incoming client connections using the server.available() function. If you will open a browser and connect to the web server using the outputted IP address in the serial monitor, you will connect to the web server residing in ESP8266 and the server will read the incoming HTTP request line by line, and wait for an empty line, which will indicate the end of the request.

When the end of the request is detected, we will send an HTTP response back to the client with an HTTP status code of 200, which means success and the message in the body is "Hello World!". After sending the response, we will disconnect the client and wait for the next user to connect.

Conclusions #

Overall, we have covered a broad range of topics related to ESP8266 development for beginners. We have created a basic Hello World application that prints out the famous words in the console, but in our case to the serial monitor.

We have blinked the built-in LED, which is a Hello World equivalent message for the microcontrollers.

We have connected the ESP8266 board to our WiFi network, which represented the ESP8266 Hello World equivalent program.

Additionally, we went above and beyond and created a simple HTTP Web Server that resides inside the ESP8266.

Libraries and Frameworks #

  • The Arduino Core is a collection of libraries and tools for programming the Arduino, but can also be used for ESP8266. It is included by default in the Arduino IDE, which is a development environment for writing code for the ESP8266.

  • The ESP8266 Arduino Core is another collection of libraries for Arduino IDE, but includes tools for ESP8266 specifically:

    • the ESP8266WiFi library provides an easy way to connect to WiFi networks.
    • The ESP8266WebServer library provides an easy way to create a web server on the ESP8266.
    • The ESP8266HTTPClient library provides an easy way to make HTTP requests to web servers.