Now that your ESP8266 chip is ready to be used and you can connect it to your Wi-Fi network, we can build some basic projects with it. This will help you understand the basics of the ESP8266.
We are going to see three projects in this chapter: 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. 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 what 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 to it. The HIGH state means that the pin is active, and that positive voltage (3.3V) is applied to the pin. A LOW state would mean that the output is at 0V.
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.18.217.150.123