Home / References / ESP32 Library / WiFi API / AP
Description
The softAPBroadcastIP()
retrieves the broadcast IP address of the ESP32 when it is operating in Access Point (AP) mode. A broadcast IP address is a special type of IP address that allows a message or packet to be sent to all devices within the same local network. For example, if your network’s IP range is 192.168.4.x, the broadcast IP might be 192.168.4.255, which sends data to every device in that subnet.
Syntax and Usage
The softAPBroadcastIP()
method is used as follows:
IPAddress broadcastIP = WiFi.softAPBroadcastIP();
Argument(s)
This method does not require any arguments.
Return Value
The method returns an IPAddress
object representing the broadcast IP address of the ESP32 in AP mode.
Example Codes
Example 1: Retrieve and print the broadcast IP address in AP mode
This code demonstrates how to use the ESP32 AP Class’s softAPBroadcastIP()
method to retrieve the broadcast IP address of the ESP32’s Soft Access Point (AP). The ESP32 is configured as a WiFi access point with the SSID “AvantMaker-ESP32-AP” and password “12345678”. The softAPBroadcastIP()
method fetches the broadcast IP address, which is used for sending data to all devices connected to the ESP32’s network, and prints it to the Serial Monitor.
To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. After a brief delay, the broadcast IP address of the ESP32’s AP will be displayed. This can be useful for understanding the network configuration or debugging purposes. The broadcast IP address is particularly helpful when you need to send information to all devices on the network simultaneously, such as in multicast scenarios. Ensure the ESP32 is set up as a SoftAP before running the code.
/*
* Author: Avant Maker
* Date: February 14, 2025
* Version: 1.0
* Description: This code demostrates how to use softAPBroadcastIP
* to retrieve the broadcast IP address.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers,learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
void setup() {
Serial.begin(115200);
WiFi.softAP("AvantMaker-ESP32-AP", "12345678");
IPAddress broadcastIP = WiFi.softAPBroadcastIP();
// wait a bit
delay(1000);
Serial.print("Broadcast IP Address: ");
Serial.println(broadcastIP);
}
void loop() {
// Your code here
}
Example 2: ESP32 broadcasts UDP message to all connected stations
Example 2-1 ESP32 WiFi AP CODE
This code demonstrates how to use the ESP32’s WiFi AP Class and the WiFiUDP
library to broadcast a message to all devices connected to the ESP32’s Soft Access Point (AP). The ESP32 is configured as a WiFi access point with the SSID “AvantMaker-ESP32-AP” and password “12345678”. The softAPBroadcastIP()
method retrieves the broadcast IP address, which is used to send data to all devices on the local network. The code initializes a UDP connection on port 1234 and sends the message “Hello from AvantMaker-ESP32-AP!” to the broadcast IP address, ensuring that all connected clients receive it.
To use this code, upload it to your ESP32 and open the Serial Monitor at 115200 baud. After the ESP32 starts in AP mode, it retrieves the broadcast IP address and sends the UDP broadcast message. The Serial Monitor will display the broadcast IP address and confirm that the message was sent. Devices connected to the ESP32’s network can listen on port 1234 to receive the broadcast message. This example is useful for beginners to understand how broadcasting works in networking and how to implement it using UDP on the ESP32. Ensure the ESP32 is set up as a SoftAP before running the code.
If you want to check the broadcast messages sent by the ESP32 AP, please refer to the ESP32 WiFi Station code provided below this AP example code. This client code shows how an ESP32 can connect to the AP’s network and listen on port 1234 to receive and display these broadcast messages.
/*
* Author: Avant Maker
* Date: February 14, 2025
* Version: 1.0
* Description: This code sets up the ESP32 as an access point
* and sends a UDP broadcast message "Hello from AvantMaker-ESP32-AP!" to
* all connected devices.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers,learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WiFiUdp.h>
WiFiUDP udp;
const char *ssid = "AvantMaker-ESP32-AP";
const char *password = "12345678";
const int udpPort = 1234;
IPAddress broadcastIP;
void setup() {
Serial.begin(115200);
WiFi.softAP(ssid, password);
broadcastIP = WiFi.softAPBroadcastIP();
// wait a bit
delay(500);
Serial.print("Broadcast IP Address: ");
Serial.println(broadcastIP);
}
void loop() {
udp.begin(udpPort);
udp.beginPacket(broadcastIP, udpPort);
udp.print("Hello from AvantMaker-ESP32-AP!");
udp.endPacket();
Serial.println("Broadcast message sent.");
delay(5000);
}
Example 2-2 ESP32 WiFi Station Code
This code demonstrates how to create an ESP32 client that connects to the Soft Access Point (AP) of another ESP32 and listens for broadcast messages. The client joins the AP network using the SSID “AvantMaker-ESP32-AP” and password “12345678”. It uses the WiFiUDP
library to listen on port 1234, where the AP broadcasts messages. When a broadcast message is received, it is printed to the Serial Monitor. This example helps beginners understand how to receive broadcast messages in a local network and verify communication between devices.
/*
* Author: Avant Maker
* Date: February 14, 2025
* Version: 1.0
* Description: This code demonstrates how to create an ESP32 client
* that connects to the Soft Access Point (AP) of another ESP32 and
* listens for broadcast messages.
*
* License: MIT
*
* Code Source: This example code is sourced from the Comprehensive
* Guide to the ESP32 Arduino Core Library, accessible on
* AvantMaker.com. For additional code examples and in-depth
* documentation related to the ESP32 Arduino Core Library,
* please visit:
*
* https://avantmaker.com/references/esp32-arduino-core-index/
*
* AvantMaker.com, your premier destination for all things
* DIY, AI, IoT, Smart Home, and STEM projects. We are dedicated
* to empowering makers,learners, and enthusiasts with the resources
* they need to bring their innovative ideas to life.
*/
#include <WiFi.h>
#include <WiFiUdp.h>
// WiFi credentials of the ESP32 AP
const char *ssid = "AvantMaker-ESP32-AP";
const char *password = "12345678";
// UDP settings
const int udpPort = 1234;
WiFiUDP udp;
void setup() {
Serial.begin(115200);
// Connect to the ESP32 AP
Serial.println("Connecting to AP...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nConnected to AP!");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
// Start listening on UDP port
udp.begin(udpPort);
Serial.print("Listening for UDP packets on port: ");
Serial.println(udpPort);
}
void loop() {
// Check if a UDP packet is available
int packetSize = udp.parsePacket();
if (packetSize > 0) {
// Read the incoming packet
char buffer[255];
int len = udp.read(buffer, sizeof(buffer) - 1);
if (len > 0) {
buffer[len] = '\0'; // Null-terminate the string
Serial.print("Received broadcast message: ");
Serial.println(buffer);
}
}
delay(100); // Small delay to avoid busy-waiting
}
AvantMaker Tutorial Suggestion
If you want to learn more about the ESP32’s WiFi capabilities, we’ve created a beginner-friendly guide that explains them in detail. Just click the link below, and you’ll be teleported to that page. It will clear up any confusion you may have while connecting your ESP32 to WiFi or setting it up as a WiFi access point (AP).
ESP32 Library Index
- ESP32 WiFiClient Library
- ESP32 HTTPClient Library
- ESP32 WiFiClientSecure Library
- ESP32 WebServer Library
- ESP32 WiFi Library
- Which ESP32 Boards are Recommended for Learners
- How to Copy Codes from AvantMaker.com
- What is SPIFFS and how to upload files to it?
- What is LIttleFS and how to upload files to it?
Ready to experiment and explore more about ESP32? Visit our website’s All About ESP32 Resources Hub, packed with tutorials, guides, and tools to inspire your maker journey. Experiment, explore, and elevate your skills with everything you need to master this powerful microcontroller platform!