Setting up the ESP8266 development environment and project demonstration

The ESP8266 is a low-cost Wi-Fi chip developed by Espressif Systems, featuring a complete TCP/IP protocol stack and microcontroller capabilities. It is designed for mobile devices, wearable electronics, and IoT applications, offering very low power consumption and an extremely affordable price.

There are multiple development environments available for the ESP8266; this article covers setting up the Arduino IDE environment.

The development board and other modules used in this article, along with connecting cables, can be purchased at the links provided at the bottom.

The NodeMCU board I am using here is equipped with this chip.

Please download and install the appropriate driver for the USB-to-TTL chip used on your development board; my board uses the CH340.

Install Arduino IDE

Arduino IDE is an integrated development environment provided by Arduino official, supporting C language programming primarily for Arduino series boards.

With simple configuration, you can add ESP8266 board support to your existing programming environment. For users familiar with Arduino libraries and development workflows, there is virtually no difference in usage.

Download Arduino IDE:

Add ESP8266 Support

Open Arduino IDE, click File in the top-left corner, then select Preferences. Locate Additional Board Manager URLs and enter the following link: http://arduino.esp8266.com/stable/package_esp8266com_index.json

Then click ToolsBoardBoards Manager to open the Boards Manager interface:

Search for esp8266 and install it:

After installation, restart the Arduino IDE software. You will now see ESP8266 board options under ToolsBoard:

Select the port: first check the port in Device Manager, then select the corresponding port in Arduino IDE.

Project Demo

Send “Hello World” via Serial Port

Create a new project in Arduino IDE and enter the following code:

void setup() {    // Initialization function; code inside runs only once at startup
  Serial.begin(115200);    // Set serial baud rate
}
/*
https://blog.zeruns.com
*/
void loop() {     // Loop function; code inside runs continuously
  Serial.println("Hello World"); // Print "Hello World" to serial port
}

After entering the code, click the Upload button to compile and upload to the board. Then open the Serial Monitor, set the baud rate to 115200, and you will see the output “Hello World”.

LED Blinking

The ESP8266 module has a built-in LED connected to pin D4.

Note: ESP8266 comes in various development boards; different boards have different GPIO pin assignments.

void setup() {          // Initialization function; code inside runs only once at startup
  pinMode(D4,OUTPUT);  // Set GPIO pin D4 to output mode
}

void loop() {             // Loop function; code inside runs continuously
  digitalWrite(D4,HIGH);  // Set GPIO pin D4 to high level (HIGH can be replaced with 1)
  delay(1000);            // Delay for 1000 milliseconds
  digitalWrite(D4,LOW);   // Set GPIO pin D4 to low level (LOW can be replaced with 0)
  delay(1000);            // https://blog.zeruns.com
}

Read DHT11 Data and Display It

In this example, we use a DHT11 temperature and humidity sensor to measure temperature and humidity, then display the results on a 0.96-inch OLED screen.

Project source code:

#include <Arduino.h>
#include <NTPClient.h>
#include <U8g2lib.h>
#include <DHT.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif

DHT dht(D3,DHT11, 15);
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ D1, /* data=*/ D2, /* reset=*/ U8X8_PIN_NONE);

void setup(){
  u8g2.begin();
  u8g2.enableUTF8Print();
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_unifont_t_chinese2);
  u8g2.setFontDirection(0);
  u8g2.setCursor(0, 15);
  u8g2.print("Humidity:"); 
  u8g2.setCursor(80, 15);
  u8g2.print(h);	// https://blog.zeruns.com
  u8g2.setCursor(120, 15);
  u8g2.print("%");
  u8g2.setCursor(0, 40);
  u8g2.print("Temperature:"); 
  u8g2.setCursor(0, 55); 
  u8g2.print(t);
  u8g2.setCursor(40, 55);
  u8g2.print("C");
  u8g2.sendBuffer(); 
  delay(1000);
}

Since the first line of the source code imports the DHT and U8g2 libraries, which are not built into Arduino IDE, you must first click SketchInclude LibraryManage Libraries to open the Library Manager, then search for and install the following three dependencies (Adafruit Unified Sensor, DHT sensor library, and U8g2):

Wiring Connections

NodeMcu DHT11
3V3 VCC
GND GND
D3 DATA
NodeMcu 0.96-inch OLED
3V3 VCC
GND GND
D1 SCL
D2 SDA

WiFi Connection

The ESP8266’s greatest feature is its ultra-low-cost Wi-Fi implementation. Here is a simple example of connecting to Wi-Fi:

#include <ESP8266WiFi.h>

const char* ssid = "blog.zeruns.com";		// WiFi name
const char* passwd = "blog.zeruns.com";	// WiFi password

void setup() {
  Serial.begin(115200);		// Set serial baud rate
  WiFi.begin(ssid,passwd);	// Connect to WiFi
  while (WiFi.status() != WL_CONNECTED)	// Check WiFi connection status
  {
    delay(500);
    Serial.print(".");		// Print "." via serial
  }
  Serial.println("");		// New line
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());	// Print obtained IP address
  Serial.println("https://blog.zeruns.com");
}

void loop() {

}

Purchase Links

NodeMCU Board (ESP8266): https://s.click.taobao.com/mWfgiTu

DHT11 Temperature and Humidity Sensor Module: https://s.click.taobao.com/gAduxTu

Dupont Wires: https://s.click.taobao.com/yxUfiTu

0.96-inch OLED Screen: https://s.click.taobao.com/6IFfiTu

Recommended Articles

2 Likes