Introduction
In this tutorial, we will walk you through the process of setting up an ESP32 microcontroller as a web server that connects to Wi-Fi using a static IP address. This is particularly useful for IoT projects where you want your device to have a fixed address on the network. We will be using the Arduino IDE for programming the ESP32.
Prerequisites
- ESP32 development board
- Arduino IDE installed on your computer
- USB cable to connect the ESP32 to your computer
- Basic knowledge of programming in Arduino IDE
- Wi-Fi network credentials (SSID and password)
Parts/Tools
- ESP32 Development Board
- USB Cable
- Computer with Arduino IDE installed
Steps
-
Install ESP32 Board in Arduino IDE
- Open the Arduino IDE.
- Go to File > Preferences.
- In the “Additional Board Manager URLs” field, add the following URL:
- Click OK.
- Go to Tools > Board > Board Manager.
- Search for “ESP32” and install the latest version.
https://dl.espressif.com/dl/package_esp32_index.json
-
Connect the ESP32 to Your Computer
- Use the USB cable to connect the ESP32 to your computer.
- In the Arduino IDE, select the appropriate board from Tools > Board > ESP32 Dev Module.
- Select the correct COM port from Tools > Port.
-
Write the Code
- Create a new sketch in the Arduino IDE.
- Copy and paste the following code:
- Replace
your_SSID
andyour_PASSWORD
with your actual Wi-Fi credentials.
#include <WiFi.h> const char* ssid = "your_SSID"; // replace with your SSID const char* password = "your_PASSWORD"; // replace with your password // Set static IP address IPAddress local_IP(192, 168, 1, 184); // Example IP address IPAddress gateway(192, 168, 1, 1); // Your router's IP address IPAddress subnet(255, 255, 255, 0); // Subnet mask void setup() { Serial.begin(115200); // Connect to Wi-Fi if (!WiFi.config(local_IP, gateway, subnet)) { Serial.println("STA Failed to configure"); } WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(1000); Serial.println("Connecting to WiFi..."); } Serial.println("Connected to WiFi"); Serial.println(WiFi.localIP()); } void loop() { // Your code for handling server requests will go here }
-
Upload the Code
- Click the upload button in the Arduino IDE (right arrow icon).
- Wait for the upload to be completed, and monitor the Serial Monitor for connection status.
-
Access the Web Server
- Once connected, note the IP address printed in the Serial Monitor.
- Open a web browser and enter the IP address to access your ESP32 web server.
Troubleshooting
- ESP32 not connecting to Wi-Fi: Double-check your SSID and password for typos.
- IP address conflict: Ensure that the static IP address you assigned is not already in use by another device on your network.
- Code upload issues: Confirm that the correct board and port are selected in the Arduino IDE.
- Check power supply: Ensure that your ESP32 is receiving adequate power through the USB connection.
Conclusion
Setting up your ESP32 as a web server with a static IP address is a straightforward process that can enhance your IoT projects. By following the steps outlined in this tutorial, you should be able to successfully connect your ESP32 to Wi-Fi and access it via a web browser. Experiment with adding more features to your server to make it even more functional!