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.
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.
This is how it should look like at the end:
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.
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.
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.
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:
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/.
#include "ESP8266WiFi.h"
#include <aREST.h>
#include "DHT.h"
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE, 15);
const char* ssid = "wifi-name";
const char* password = "wifi-pass";
float temperature;
float humidity;
dht.begin();
rest.variable("temperature",&temperature);
rest.variable("humidity",&humidity);
humidity = dht.readHumidity();
temperature = dht.readTemperature();
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.
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.
Further resources on this subject: