esp wifi rust example

ESP WiFi with Rust: A Comprehensive Example

By Christopher Walker
AD

In the realm of modern development, the Esp-Wifi library in Rust has become a go-to choice for creating Wi-Fi-enabled applications. Rust, known for its memory safety and concurrency features, is an excellent match for building robust and secure IoT devices. Our focus today is exploring a practical example of using the Esp-Wifi library in a Rust project.

Before we dive into our example, let's briefly understand the Esp-Wifi library. It's a collection of Rust bindings for the espressif Non-OS SDK, enabling easier development of Wi-Fi applications on ESP8266 and ESP32 microcontrollers.

The Setup: Creating a New Rust Project

The first step in our journey is setting up a new Rust project. We'll use the `cargo-new`, a simple command-line tool, to create our project.

AD
Getting Started with Rust on ESP32 (2026): The Complete Beginner's Guide
Getting Started with Rust on ESP32 (2026): The Complete Beginner's Guide
Trying Rust on ESP32
Trying Rust on ESP32
ESP8266 NodeMCU Relay Module - Control AC Appliances (Web Server) | Random Nerd Tutorials
ESP8266 NodeMCU Relay Module - Control AC Appliances (Web Server) | Random Nerd Tutorials

Open your terminal and run `cargo new --bin esp-wifi-rust-example`. This will create a new binary project named `esp-wifi-rust-example`.

Installing Dependencies

Next, we need to install the Esp-Wifi library as a dependency. In your project directory, run `cargo add esp-wifi`. Cargo will automatically resolve and add the library to your `Cargo.toml` file.

🔒 Secure ESP-NOW Communication on ESP32! 🔐
🔒 Secure ESP-NOW Communication on ESP32! 🔐
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd Tutorials
Wi-PWN
Wi-PWN

For our example, we'll also need the `log` crate to handle logging. Add it with `cargo add log`.

Initializing Esp-Wifi: A Basic Example

Now that we have our project set up, let's start using Esp-Wifi. We'll begin with a simple example that initializes the Wi-Fi interface and connects to a network.

AD

First, replace the contents of `src/main.rs` with the following code:

ESP01/01S RELAY MODULE TUTORIAL
ESP01/01S RELAY MODULE TUTORIAL
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
Simulate ESP32 Projects in VS Code with Wokwi (Beginner Guide!)
a person holding an electronic device in front of a laptop computer with wires attached to it
a person holding an electronic device in front of a laptop computer with wires attached to it
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware Upload
ESP-MESH Getting Started using painlessMesh and ESP32/ESP8266
ESP-MESH Getting Started using painlessMesh and ESP32/ESP8266
```rust use esp_wifi::WifiController; use log::info; // Our Wi-Fi credentials const SSID: &str = "your-ssid"; const PASSWORD: &str = "your-password"; fn main() { // Initialize the Wi-Fi controller let mut wifi = WifiController::new().expect("Failed to initialize WiFi controller"); // Connect to the Wi-Fi network wifi.connect(SSID, PASSWORD).expect("Failed to connect to Wi-Fi"); info!("Connected to Wi-Fi: {:?}", wifi.sta_probably_connected()); } ```

Connecting to a Wi-Fi Network

In this code, we first import the necessary traits and set our SSID and PASSWORD constants. Then, we initialize the `WifiController` and connect to the Wi-Fi network using its `connect` method. The `expect` method is used to handle any errors that might occur during initialization or connection.

In the final line, we print a message using the `log` crate to indicate that the device has successfully connected to the Wi-Fi network. This message can be seen by running `cargo run` in your project directory and checking the output.

AD

Scanning for Wi-Fi Networks

As an additional example, let's explore how to scan for Wi-Fi networks using Esp-Wifi. Add the following code after the `wifi.connect` call:

How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
Better Debugging for Arduino IDE: SerialDebug Library | Random Nerd Tutorials
an electronic device is sitting on the back of a chair with wires attached to it
an electronic device is sitting on the back of a chair with wires attached to it
ESP32/ESP8266 WIFI Display Using MQTT Protocol
ESP32/ESP8266 WIFI Display Using MQTT Protocol
💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
an electronic device with four different parts labeled in red, blue and yellow on it
an electronic device with four different parts labeled in red, blue and yellow on it
ESP 01 Programming: Complete Setup, Code, and Debug
ESP 01 Programming: Complete Setup, Code, and Debug
Introduction to Embedded Systems with Rust: A Beginner's Guide Using ESP32
Introduction to Embedded Systems with Rust: A Beginner's Guide Using ESP32
ESP Soil Moisture Sensor
ESP Soil Moisture Sensor
MicroPython: ESP32/ESP8266 Access Point (AP) | Random Nerd Tutorials
MicroPython: ESP32/ESP8266 Access Point (AP) | Random Nerd Tutorials
ESP32Thing-Wifi-Remote
ESP32Thing-Wifi-Remote
ESP32 Universal IR Remote 📺✨
ESP32 Universal IR Remote 📺✨
```rust // Scan for available Wi-Fi networks let networks = wifi.scan().expect("Failed to scan for Wi-Fi networks"); info!("Available Wi-Fi networks:"); for network in networks { info!("{:?}", network); } ```

This code scans for available Wi-Fi networks and prints them along with their signal strengths. This can be helpful in building applications that allow users to select from a list of available networks.

In conclusion, the Esp-Wifi library provides a powerful and intuitive way to interact with Wi-Fi functionality in Rust. By following these examples, you can create a solid foundation for building your own Wi-Fi-enabled applications using Rust and the Esp-Wifi library. Happy coding!

AD
AD