Design of an Intelligent Fan Control System Based on 51 Microcontroller and ESP8266

In the previous article, we used the ESP8266 (“Sound and Light Guardian”: 51 Microcontroller for Energy-Saving Lighting “Guardian”), today we will first introduce the ESP8266 separately, and then implement the design to control the fan wirelessly (ESP8266).

As a pioneer in the Internet of Things (IoT) field, the ESP8266 wireless module has become one of the top choices for many smart devices due to its stable and reliable performance and powerful features. Whether it is smart home, smart wearables, smart agriculture, or smart industry, the ESP8266 can provide strong support, enabling your devices to achieve more possibilities. The ESP8266 wireless module uses a high-performance Tensilica L106 32-bit microcontroller, integrates a TCP/IP protocol stack, supports 802.11 b/g/n Wi-Fi standards, and has strong data processing capabilities and stable wireless connection performance. Additionally, the ESP8266 supports various communication interfaces, including UART, SPI, and I2C, making it easy to connect with various sensors, actuators, and other external devices. Besides its powerful hardware performance, the ESP8266 also has rich software resources. The development environment based on the ESP8266 is simple and easy to use, supporting various development tools such as Arduino IDE and MicroPython, allowing developers to quickly get started and implement various functions. Below is a simple ESP8266 code example for connecting to a Wi-Fi network:

#include <ESP8266WiFi.h>
const char* ssid = "YourNetworkName";
const char* password = "YourNetworkPassword";

void setup() {
  Serial.begin(115200);
  delay(10);
  // Connect to Wi-Fi 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());
}

void loop() {
  // Your code here
}

Through the above code, you can easily connect to a Wi-Fi network and obtain the local IP address, providing basic support for subsequent smart device development. In summary, the ESP8266 wireless module, with its powerful performance and rich resources, offers unlimited possibilities for smart device development. Whether you are a beginner or a professional developer, the ESP8266 can meet your needs, making your smart life more convenient and intelligent. Let us explore the ESP8266 together and enjoy the fun of smart living!

1. System Overview This article introduces an intelligent fan control system based on the 51 microcontroller (STC89C51) and the ESP8266 wireless module. The system connects to a mobile app via Wi-Fi, enabling remote control of the fan’s on/off switch, speed adjustment, and timing functions, while also integrating temperature and humidity monitoring. Users can view environmental data in real-time and control the fan through an Android application.

2. Hardware Design
1. 51 Microcontroller Minimum System
• Core Chip: STC89C51RC (40-pin DIP package)
• Crystal Oscillator: 11.0592MHz (ensures accurate baud rate for serial communication)
• Reset Circuit: 10K resistor + 10uF electrolytic capacitor
• Power Supply: 5V DC
2. ESP8266 Module
• Model: ESP-01S (8-pin module)
• Interface: Connected to the 51 microcontroller’s P3.0 (RXD) and P3.1 (TXD) via MAX232 level conversion chip
• Power Supply: 3.3V (converted from 5V power supply using AMS1117 voltage regulator)
3. Fan Control Circuit
• Driver Chip: L293D (dual H-bridge motor driver)
• Interface: P1.0-P1.2 controls the three speed levels of the fan (low, medium, high)
• Power Supply: 12V DC motor power supply (isolated from the microcontroller’s 5V power supply)
4. Sensor Module
• Temperature and Humidity Sensor: DHT11 (connected to P2.0 pin)
• Display: 0.96-inch OLED SSD1306 (I2C interface, connected to P2.1 (SCL) and P2.2 (SDA))
5. Android Application
• Function: Real-time display of temperature and humidity, control of fan on/off/speed, set timing for on/off
• Communication Protocol: MQTT protocol (using EMQX server)

3. Software Design
1. Main Program Flow

#include <reg51.h>
#include <intrins.h>
#include <dht11.h>
#include <oled_i2c.h>
#include <esp8266.h>
sbit Fan_Low = P1^0;
sbit Fan_Mid = P1^1;
sbit Fan_High = P1^2;
sbit DHT_Data = P2^0;
char temp, humidity;
char mqtt_state[10]; // Store MQTT received status data
void main() {
    init_dht11();
    oled_init();
    esp8266_init();
    while(1) {
        // Read temperature and humidity
        read_dht11();
        temp = DHT_Temp;
        humidity = DHT_Humidity;
        oled_display(temp, humidity);
        // Process MQTT messages
        if(esp8266_mqtt_available()) {
            esp8266_mqtt_read(mqtt_state);
            parse_command(mqtt_state);
        }
        // Timing function processing
        check_timer();
    }
}
void parse_command(char *state) {
    if(strstr(state, "ON")) {
        Fan_Low = 1;
    } else if(strstr(state, "OFF")) {
        Fan_Low = 0;
        Fan_Mid = 0;
        Fan_High = 0;
    } else if(strstr(state, "MID")) {
        Fan_Mid = 1;
    } else if(strstr(state, "HIGH")) {
        Fan_High = 1;
    }
}

2. ESP8266 Driver Program

void esp8266_init() {
    // Initialize Serial Port 1 (baud rate 115200)
    SCON = 0x50; // 8-bit data, variable baud rate
    TMOD |= 0x20; // Timer 1 mode 2 (8-bit auto-reload)
    TH1 = 0xFD; // Baud rate 115200
    TR1 = 1;
    ES = 1; // Enable serial interrupt
    EA = 1; // Send AT commands to configure ESP8266
    send_cmd("AT+RST");
    delay_ms(1000);
    send_cmd("AT+CWMODE=1"); // Station mode
    send_cmd("AT+CWJAP=\"YourWiFi\",\"Password\"");
    send_cmd("AT+CIPMUX=1"); // Multi-connection mode
    send_cmd("AT+CIPSTART=4,\"TCP\",\"broker.emqx.io\",1883");
    send_cmd("AT+CIPMODE=1"); // Transparent mode
    send_cmd("AT+CIPSEND");
}
void send_mqtt(char *topic, char *msg) {
    char mqtt_str[50];
    sprintf(mqtt_str, "PUBLISH %s %s", topic, msg);
    send_str(mqtt_str);
}
void send_str(char *str) {
    while(*str) {
        SBUF = *str++;
        while(!TI); // Wait for send to complete
        TI = 0;
    }
}

3. DHT11 Driver Program

#include <dht11.h>
void init_dht11() {
    DHT_Data = 1;
    delay_us(1);
    DHT_Data = 0;
    delay_ms(18);
    DHT_Data = 1;
    delay_us(40);
}
void read_dht11() {
    char data[5] = {0};
    char i, j;
    for(i=0; i<5; i++) {
        // Wait for low level to start
        while(DHT_Data);
        // Wait for high level
        while(DHT_Data);
        // Read 40 bits of data
        for(j=0; j<8; j++) {
            data[i] <<= 1;
            while(DHT_Data); // Wait for data bit
            delay_us(30);
            if(DHT_Data) {
                data[i] |= 0x01;
                while(DHT_Data); // Wait for end
            }
        }
    }
    // Parse data
    DHT_Temp = data[2];
    DHT_Humidity = data[0];
}

4. System Testing
1. Hardware Connection Test: Use an oscilloscope to detect the serial communication waveform of the ESP8266 to ensure the baud rate is correct.
2. MQTT Communication Test: Use an MQTT client tool (such as MQTT.fx) to send control commands and observe the fan’s state changes.
3. Temperature and Humidity Test: Compare the DHT11 measurement values with those from a calibrated thermometer, with an error within ±2°C and ±5% RH.

The above is the finished product of the ESP8266 wireless design. If you need it, please contact me privately for the backend. Thank you.

Leave a Comment