In this article by Marco Schwartz, author Internet of Things with ESP8266, we will focus on the ESP8266 chip is ready to be used and you can connect it to your Wi-Fi network, we can now build some basic projects with it. This will help you understand the basics of the ESP8266.
(For more resources related to this topic, see here.)
We are going to see three projects in this article: how to control an LED, how to read data from a GPIO pin, and how to grab the contents from a web page. We will also see how to read data from a digital sensor.
Controlling an LED
First, we are going to see how to control a simple LED. Indeed, the GPIO pins of the ESP8266 can be configured to realize many functions: inputs, outputs, PWM outputs, and also SPI or I2C communications. This first project will teach you how to use the GPIO pins of the chip as outputs.
- The first step is to add an LED to our project. These are the extra components you will need for this project:
- 5mm LED (https://www.sparkfun.com/products/9590)
- 330 Ohm resistor (to limit the current in the LED) (https://www.sparkfun.com/products/8377)
- The next step is to connect the LED with the resistor to the ESP8266 board. To do so, the first thing to do is to place the resistor on the breadboard.
- Then, place the LED on the breadboard as well, connecting the longest pin of the LED (the anode) to one pin of the resistor.
- Then, connect the other end of the resistor to the GPIO pin 5 of the ESP8266, and the other end of the LED to the ground.
This is how it should look like at the end:
- We are now going to light up the LED by programming the ESP8266 chip, by connecting it to the Wi-Fi network.
This is the complete code for this section:
// Import required libraries #include <ESP8266WiFi.h> void setup() { // Set GPIO 5 as output pinMode(5, OUTPUT); // Set GPIO 5 on a HIGH state digitalWrite(5, HIGH); } void loop() { }
This code simply sets the GPIO pin as an output, and then applies a HIGH state on it. The HIGH state means that the pin is active, and that positive voltage (3.3V) is applied on the pin. A LOW state would mean that the output is at 0V.
- You can now copy this code and paste it in the Arduino IDE.
- Then, upload the code to the board using the instructions from the previous article. You should immediately see that the LED is lighting up. You can shut it down again by using digitalWrite(5, LOW) in the code. You could also, for example, modify the code so the ESP8266 switches the LED on and off every second.
Reading data from a GPIO pin
As a second project in this article, we are going to read the state of a GPIO pin. For this, we will use the same pin as in the previous project. You can therefore remove the LED and the resistor that we used in the previous project.
Now, simply connect this pin (GPIO 5) of the board to the positive power supply on your breadboard with a wire, therefore applying a 3.3V signal on this pin.
Reading data from a pin is really simple. This is the complete code for this part:
// Import required libraries
#include <ESP8266WiFi.h>
void setup(void)
{
// Start Serial (to display results on the Serial monitor)
Serial.begin(115200);
// Set GPIO 5 as input
pinMode(5, INPUT);}
void loop() {
// Read GPIO 5 and print it on Serial port
Serial.print("State of GPIO 5: ");
Serial.println(digitalRead(5));
// Wait 1 second
delay(1000);
}
We simply set the pin as an input, and then read the value of this pin, and print it out every second. Copy and paste this code into the Arduino IDE, then upload it to the board using the instructions from the previous article.
This is the result you should get in the Serial monitor:
State of GPIO 5: 1
We can see that the returned value is 1 (digital state HIGH), which is what we expected, because we connected the pin to the positive power supply. As a test, you can also connect the pin to the ground, and the state should go to 0.
Grabbing the content from a web page
As a last project in this article, we are finally going to use the Wi-Fi connection of the chip to grab the content of a page. We will simply use the www.example.com page, as it’s a basic page largely used for test purposes.
This is the complete code for this project:
// Import required libraries
#include <ESP8266WiFi.h>
// WiFi parameters
constchar* ssid = "your_wifi_network";
constchar* password = "your_wifi_password";
// Host
constchar* host = "www.example.com";
void setup() {
// Start Serial
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
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());
}
int value = 0;
void loop() {
Serial.print("Connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// This will send the request to the server
client.print(String("GET /") + " HTTP/1.1rn" +
"Host: " + host + "rn" + "Connection: closernrn");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
delay(5000);
}
The code is really basic: we first open a connection to the example.com website, and then send a GET request to grab the content of the page. Using the while(client.available()) code, we also listen for incoming data, and print it all inside the Serial monitor.
You can now copy this code and paste it into the Arduino IDE. This is what you should see in the Serial monitor:
This is basically the content of the page, in pure HTML code.
Reading data from a digital sensor
In this last section of this article, we are going to connect a digital sensor to our ESP8266 chip, and read data from it. As an example, we will use a DHT11 sensor that can be used to get ambient temperature and humidity.
You will need to get this component for this section, the DHT11 sensor (https://www.adafruit.com/products/386)
Let’s now connect this sensor to your ESP8266:
- First, place the sensor on the breadboard. Then, connect the first pin of the sensor to VCC, the second pin to pin #5 of the ESP8266, and the fourth pin of the sensor to GND.
This is how it will look like at the end:
Note that here I’ve used another ESP8266 board, the Adafruit ESP8266 breakout board.
We will also use the aREST framework in this example, so it’s easy for you to access the measurements remotely. aREST is a complete framework to control your ESP8266 boards remotely (including from the cloud), and we are going to use it several times in the article. You can find more information about it at the following URL: http://arest.io/.
- Let’s now configure the board. The code is too long to be inserted here, but I will detail the most important part of it now.
- It starts by including the required libraries:
#include "ESP8266WiFi.h" #include <aREST.h> #include "DHT.h"
- To install those libraries, simply look for them inside the Arduino IDE library manager. Next, we need to set the pin on which the DHT sensor is connected to:
#define DHTPIN 5 #define DHTTYPE DHT11
- After that we declare an instance of the DHT sensor:
DHT dht(DHTPIN, DHTTYPE, 15);
- As earlier, you will need to insert your own Wi-Fi name and password inside the code:
const char* ssid = "wifi-name"; const char* password = "wifi-pass";
- We also define two variables that will hold the measurements of the sensor:
float temperature; float humidity;
- In the setup() function of the sketch, we initialize the sensor:
dht.begin();
- Still in the setup() function, we expose the variables to the aREST API, so we can access them remotely via Wi-Fi:
rest.variable("temperature",&temperature); rest.variable("humidity",&humidity);
- Finally, in the loop() function, we make the measurements from the sensor:
humidity = dht.readHumidity(); temperature = dht.readTemperature();
- It’s now time to test the project! Simply grab all the code and put it inside the Arduino IDE. Also make sure to install the aREST Arduino library using the Arduino library manager.
- Now, put the ESP8266 board in bootloader mode, and upload the code to the board. After that, reset the board, and open the Serial monitor. You should see the IP address of the board being displayed:
- Now, we can access the measurements from the sensor remotely. Simply go to your favorite web browser, and type:
192.168.115.105/temperature
You should immediately get the answer from the board, with the temperature being displayed:
{ "temperature": 25.00, "id": "1", "name": "esp8266", "connected": true }
You can of course do the same with humidity.
Note that we used here the aREST API. You can learn more about it at: http://arest.io/.
Congratulations, you just completed your very first projects using the ESP8266 chip! Feel free to experiment with what you learned in this article, and start learning more about how to configure your ESP8266 chip.
Summary
In this article, we realized our first basic projects using the ESP8266 Wi-Fi chip. We first learned how to control a simple output, by controlling the state of an LED. Then, we saw how to read the state of a digital pin on the chip. Finally, we learned how to read data from a digital sensor, and actually grab this data using the aREST framework.
We are going to go right into the main topic of the article, and build our first Internet of Things project using the ESP8266.
Resources for Article:
Further resources on this subject:
- Sending Notifications using Raspberry Pi Zero [article]
- The Raspberry Pi and Raspbian [article]
- Working with LED Lamps [article]