The ESP32, known for its low power, high performance, and ease of use, is a popular microcontroller for various IoT and wireless projects. Its in-built Wi-Fi and Bluetooth capabilities make it an ideal choice for creating connected devices. Here, we'll explore some practical ESP32 WiFi examples that demonstrate its versatility.
Before diving into these examples, ensure you have the necessary tools set up: an ESP32 development board, a USB-to-serial converter, and the Arduino IDE with the ESP32 board definition installed.
Connecting to WiFi Networks
One of the primary use cases of the ESP32 is connecting to WiFi networks. This allows your project to communicate with other devices, send data to the cloud, or act as an access point.
AD
10 ESP32 Skills Every Maker Should Learnthe esp32 wi - fi is shown with fireworks in the backgroundHow to Power the ESP32 Correctly ⚡
The following example assumes you have already connected your ESP32 board to your computer and installed the required libraries (WiFi.h and WiFiClient.h).
Connecting as a Station
To connect the ESP32 to an existing WiFi network, use the following code:
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller NotesESP32/ESP8266 Thermostat Web Server - Control Output Based on Temperature | Random Nerd Tutorials🌐 ESP32 + IFTTT = Smart Home Magic! ✨
```cpp
#include
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {}
```
Replace your_SSID and your_PASSWORD with the network credentials. The code will connect the ESP32 to the specified network and print "Connected to WiFi" once connected.
Creating an Access Point
Alternatively, you can turn your ESP32 into an access point. This allows other devices to connect to your project directly. Here's an example:
This code creates an access point named ESP32_AP with the password your_password. The AP IP address will be printed once the access point is created.
Sending and Receiving Data
With the ESP32 connected to a WiFi network, you can now send and receive data. This could be useful for communicating with a server, another device, or exchanging data between your project and the cloud.
Sending Data to an HTTP Server
To send data to an HTTP server, you can use the WiFiClient library. Here's an example:
```cpp
#include
#include
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* host = "example.com";
const int port = 80;
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
connectToServer();
}
void loop() {}
void connectToServer() {
// Create WiFi client
WiFiClient client;
// Connect to host
if (!client.connect(host, port)) {
Serial.println("Connection to host failed");
return;
}
// Send data to host
String data = "Hello world from ESP32";
client.println(data);
client.println();
// Check response
while (client.connected()) {
if (client.available()) {
String line = client.readStringUntil('\r');
Serial.println(line);
}
}
// Close the connection
client.stop();
}
```
Replace example.com with the address of the HTTP server you want to connect to. This code sends the text "Hello world from ESP32" to the server and then prints any response received.
Receiving Data via MQTT
MQTT (Message Queue Telemetry Transport) is a popular protocol for low-bandwidth, high-latency networks. The ESP32 can act as both an MQTT client and server. Here's an example of receiving data as an MQTT client:
```cpp
#include
#include
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
const char* mqtt_server = "iot.eclipse.org";
const int mqtt_port = 1883;
const char* mqtt_subscribe_topic = "esp32/receive";
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
// Connect to MQTT server
client.setServer(mqtt_server, mqtt_port);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
if (client.connect("ESP32_Client")) {
client.subscribe(mqtt_subscribe_topic);
Serial.println("Connected to MQTT server");
} else {
delay(1000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
// Handle received message
String message;
for (int i = 0; i < length; i++) {
message += (char)payload[i];
}
Serial.println("Message received: " + message);
}
```
This code connects to the MQTT broker at iot.eclipse.org, subscribes to the topic esp32/receive, and prints any messages received.
The ESP32's versatility and low power consumption make it an ideal choice for IoT and wireless projects. Whether connecting to WiFi networks, sending and receiving data, or acting as an access point, it's clear that the ESP32 has a lot to offer. If you're new to the ESP32, experimenting with these examples is a great way to get started. Happy Making!
ESP32 Over-the-Air (OTA) Updates – Wireless Firmware UploadAnatomy esp32how to send sms from esp32 on the breadboard with an esp23esp 32 is a machine that can make you an engineerthe esp2 server is connected to an esp2 router and access point🌐 How to Set Static IP on ESP32 – Reliable Networking Made Easy!Complete Guide for Ultrasonic Sensor HC-SR04 with Arduino | Random Nerd TutorialsESP32 Access Point (AP) for Web Server | Random Nerd TutorialsESP32/ESP8266 WIFI Display Using MQTT Protocol💡 ESP32 Web Server – Control an LED from Your Browser!ESP32 Pinout Guide: GPIO, Buttons & Ports ExplainedESP32/ESP8266 WIFI Display Using MQTT Protocol⚡ ESP32 OTA Update: Upload Firmware Over Wi-Fi!