Embedded & MCUEmbedded LinuxESPMicrocontrollersTutorials/DIY

Creating a Framework for ESP8266 NodeMCU Using C

The ESP8266 NodeMCU is a powerful Wi-Fi module widely used in IoT applications. To build a custom framework for ESP8266, you can choose from multiple development approaches based on your requirements. This guide covers various methods, including the ESP8266 SDKs (RTOS & Non-OS), Arduino Core, and PlatformIO.

1. Choosing the Right Approach

Before starting, consider the following development options:

A. ESP8266 SDKs (Official SDKs from Espressif)

These SDKs offer low-level control over the ESP8266, allowing for optimized and efficient firmware development.

  1. ESP8266 RTOS SDK
    • Based on FreeRTOS.
    • Best for multitasking and real-time applications.
    • Provides built-in Wi-Fi and peripheral support.
    • Uses esp-idf style programming with event-driven architecture.
  2. ESP8266 Non-OS SDK
    • Lightweight and event-driven (no multitasking).
    • Ideal for applications with lower complexity.
    • Provides direct access to ESP8266 hardware.

B. Arduino Core for ESP8266

  • Uses the Arduino IDE but provides a C++-based abstraction.
  • Simplifies development with high-level APIs.
  • Ideal for prototyping and quick projects.
  • Supports standard libraries like WiFi.h and ESP8266WebServer.h.

C. PlatformIO for ESP8266

  • A modern development environment supporting multiple frameworks.
  • Integrates with ESP8266 SDKs and Arduino Core.
  • Provides dependency management and build automation.
  • Works with VS Code, CLion, and other IDEs.

2. Setting Up Your Development Environment

A. Using ESP8266 RTOS SDK (Recommended for Advanced Users)

Installation Steps:

  1. Install ESP8266 RTOS SDK:
    git clone --recursive https://github.com/espressif/ESP8266_RTOS_SDK.git
    cd ESP8266_RTOS_SDK
    ./install.sh
    
  2. Set up the environment:
    . ./export.sh
    
  3. Create a new project:
    idf.py create-project my_esp_project
    
  4. Build and flash:
    idf.py build flash monitor
    

B. Using ESP8266 Non-OS SDK (Lightweight Approach)

Installation Steps:

  1. Download the SDK from Espressif’s GitHub.
  2. Set up a toolchain (GCC compiler for ESP8266).
  3. Write and compile code using Makefile.
  4. Flash using esptool.py.

C. Using Arduino Core for ESP8266 (Beginner-Friendly)

Installation Steps:

  1. Install Arduino IDE.
  2. Add ESP8266 Board Manager URL:
    https://arduino.esp8266.com/stable/package_esp8266com_index.json
    
  3. Install the ESP8266 board package from Arduino Board Manager.
  4. Select NodeMCU 1.0 (ESP-12E Module) and start coding!

D. Using PlatformIO for ESP8266 (Advanced Development)

Installation Steps:

  1. Install VS Code.
  2. Install the PlatformIO extension.
  3. Create a new ESP8266 project using PlatformIO UI.
  4. Select your framework (ESP8266 SDK or Arduino Core).
  5. Write your code and build it using PlatformIO’s CLI.

3. Structuring Your Custom Framework

A well-structured framework improves maintainability and scalability. Here’s an example structure:

/my_esp_framework/
|-- components/
|   |-- wifi_manager/
|   |   |-- wifi_manager.c
|   |   |-- wifi_manager.h
|   |-- mqtt_client/
|   |-- gpio_control/
|
|-- main/
|   |-- main.c
|-- sdkconfig
|-- CMakeLists.txt

Key Components:

  • wifi_manager/: Handles Wi-Fi connectivity.
  • mqtt_client/: Manages MQTT communications.
  • gpio_control/: Controls GPIO pins.
  • main.c: Entry point of the program.

4. Writing a Basic Wi-Fi Connection Module (C Code Example)

Here’s a simple wifi_manager.c to connect ESP8266 to Wi-Fi:

#include "wifi_manager.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"

static esp_err_t event_handler(void *ctx, system_event_t *event) {
    switch(event->event_id) {
        case SYSTEM_EVENT_STA_START:
            esp_wifi_connect();
            break;
        case SYSTEM_EVENT_STA_GOT_IP:
            printf("WiFi connected\n");
            break;
        case SYSTEM_EVENT_STA_DISCONNECTED:
            esp_wifi_connect();
            break;
        default:
            break;
    }
    return ESP_OK;
}

void wifi_manager_init() {
    tcpip_adapter_init();
    esp_event_loop_init(event_handler, NULL);
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    esp_wifi_init(&cfg);
    esp_wifi_set_mode(WIFI_MODE_STA);
    esp_wifi_start();
}

Using the Wi-Fi Module in main.c

#include "wifi_manager.h"

void app_main() {
    wifi_manager_init();
}

5. Flashing and Debugging Your Firmware

  • Use idf.py flash monitor for RTOS SDK.
  • Use esptool.py for Non-OS SDK.
  • Use Arduino Serial Monitor for Arduino Core.
  • Use PlatformIO Serial Monitor for PlatformIO projects.

Conclusion

You now have a comprehensive guide to creating a framework for ESP8266 NodeMCU using C. Choose the best approach based on your needs:

  • ESP8266 RTOS SDK: Best for advanced multitasking.
  • ESP8266 Non-OS SDK: Lightweight, event-driven.
  • Arduino Core: Easy and fast development.
  • PlatformIO: Modern and flexible development.

=====================You can use this full code for your reference=======================

/* ESP8266 Framework Code */

// Directory Structure:
// /my_esp_framework/
// |-- components/
// | |-- wifi_manager/
// | | |-- wifi_manager.c
// | | |-- wifi_manager.h
// | |-- mqtt_client/
// | | |-- mqtt_client.c
// | | |-- mqtt_client.h
// | |-- gpio_control/
// | | |-- gpio_control.c
// | | |-- gpio_control.h
// |
// |-- main/
// | |-- main.c
// |-- sdkconfig
// |-- CMakeLists.txt

// wifi_manager.h
#ifndef WIFI_MANAGER_H
#define WIFI_MANAGER_H

void wifi_manager_init();

#endif // WIFI_MANAGER_H

// wifi_manager.c
#include "wifi_manager.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"

static esp_err_t event_handler(void *ctx, system_event_t *event) {
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
ESP_LOGI("WIFI", "Connected");
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
esp_wifi_connect();
break;
default:
break;
}
return ESP_OK;
}

void wifi_manager_init() {
tcpip_adapter_init();
esp_event_loop_init(event_handler, NULL);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_mode(WIFI_MODE_STA);
esp_wifi_start();
}

// mqtt_client.h
#ifndef MQTT_CLIENT_H
#define MQTT_CLIENT_H

void mqtt_client_init();

#endif // MQTT_CLIENT_H

// mqtt_client.c
#include "mqtt_client.h"
#include "mqtt_client.h"
#include "esp_log.h"

void mqtt_client_init() {
ESP_LOGI("MQTT", "Initializing MQTT client...");
// MQTT setup logic here
}

// gpio_control.h
#ifndef GPIO_CONTROL_H
#define GPIO_CONTROL_H

void gpio_init();
void gpio_toggle(int pin);

#endif // GPIO_CONTROL_H

// gpio_control.c
#include "gpio_control.h"
#include "driver/gpio.h"

void gpio_init() {
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
}

void gpio_toggle(int pin) {
int level = gpio_get_level(pin);
gpio_set_level(pin, !level);
}

// main.c
#include "wifi_manager.h"
#include "mqtt_client.h"
#include "gpio_control.h"

void app_main() {
wifi_manager_init();
mqtt_client_init();
gpio_init();
}

//Note: read comments to find each file code

==========================================================================

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *