ESP-IDF WiFi Networking

Mahavir Jain
3 min readApr 21, 2018

--

Introduction

In this article we will explore WiFi Networking Architecture in ESP-IDF (an Internet of Things development framework from Espressif for ESP32 chip).

Primary Components

  • TCPIP Stack (lwIP): Embedded networking stack, with POSIX/BSD socket interface to application
  • TCPIP Adapter Layer: Interfacing layer that provides wrappers on TCPIP stack APIs, for default handlers from Event Loop Task
  • Event Loop Task: Task responsible for event dispatching and co-ordination between WiFi driver, TCPIP stack and Application. This has default handlers registered for various system events
  • WiFi Driver: Acts as MAC layer in networking, with interface for sending/receiving data and WiFi events to higher layers

Application workflow

We are taking one of ESP-IDF example to demonstrate workflow of WiFi networking. Having said that, best way to represent would be using plantuml based sequence diagrams, there we go…

Part I — Initialization Phase

Initialization phase in WiFi Networking

Typical workflow involves initialization of TCPIP Adapter, Event Loop Task and WiFi driver. Some key points to note here are:

  • Application registers its event handler with Event Loop Task, refer here
  • WiFi driver also needs event handler to send events across to higher layer, that happens during call to esp_wifi_init()using default WiFi configuration from here
  • Default event handlers for various system events gets registered during Event Loop initialization, refer here
  • During last step in above sequence diagram, WiFi driver tx callback, i.e. esp_wifi_internal_tx() gets registered with TCPIP stack, refer here and here

Part II — Setup Phase

Setup phase in WiFi Networking
  • On successful WiFi association with access point, TCPIP stack input callback gets registered in WiFi driver, refer here
  • Once application receives SYSTEM_EVENT_STA_START, it basically starts WiFi connection process using esp_wifi_connect(), followed up by DHCP phase

Part III — Data Transfer

Data transfer phase in WiFi Networking

This is straight forward, once there is acknowledgement for IP connectivity, communication can start with TCPIP stack provided POSIX socket interface

Call Graph

If you are really interested (not encouraged :P), maybe following call-graph can get more details, basically stripped version that involves only components discussed in above application workflow use-case.

Generated using GCC -fdump-rtl-expand CFLAG and egypt call graph tool, also available at https://raw.githubusercontent.com/mahavirj/misc/master/callgraph.png

Call graph

Remarks

  • Architecture looks clean and elegant
  • Offers modularity as different components are not tightly coupled
  • Application interface is simple and neat

What Next

So far so good, but can we really do some complex things here?

Maybe yes, e.g. replacing the entire Networking Stack (marked in blue above), lets explore that in the context of different (and exciting?) project with ESP32…Stay tuned!

--

--

Mahavir Jain

Embedded Professional | Open Source Enthusiast | Technology Explorer.