How to Set Up an ESP32 Web Server with Static IP in Arduino IDE

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

  1. Install ESP32 Board in Arduino IDE

    1. Open the Arduino IDE.
    2. Go to File > Preferences.
    3. In the “Additional Board Manager URLs” field, add the following URL:
    4. https://dl.espressif.com/dl/package_esp32_index.json
    5. Click OK.
    6. Go to Tools > Board > Board Manager.
    7. Search for “ESP32” and install the latest version.
  2. Connect the ESP32 to Your Computer

    1. Use the USB cable to connect the ESP32 to your computer.
    2. In the Arduino IDE, select the appropriate board from Tools > Board > ESP32 Dev Module.
    3. Select the correct COM port from Tools > Port.
  3. Write the Code

    1. Create a new sketch in the Arduino IDE.
    2. Copy and paste the following code:
    3. 
      #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
      }
                  
    4. Replace your_SSID and your_PASSWORD with your actual Wi-Fi credentials.
  4. Upload the Code

    1. Click the upload button in the Arduino IDE (right arrow icon).
    2. Wait for the upload to be completed, and monitor the Serial Monitor for connection status.
  5. Access the Web Server

    1. Once connected, note the IP address printed in the Serial Monitor.
    2. 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!

Leave a Comment

Your email address will not be published. Required fields are marked *