Display Your Public IP Address on an ESP32’s OLED Display

In this tutorial, we’ll show you how to display the public IP address of your ESP32 on an OLED display. This can be useful when you want to monitor your device’s connectivity status and ensure it’s connected to the internet with a valid IP address.

We’ll be using the following libraries and components:

  1. Adafruit SSD1306 library for OLED display
  2. Adafruit GFX library for graphics
  3. HTTPClient library for making HTTP requests
  4. ArduinoJson library for parsing JSON data
  5. An OLED display (128×64 resolution) compatible with SSD1306 driver

Let’s get started!

Step 1: Install the Required Libraries

First, you’ll need to install the following libraries in your Arduino IDE:

  1. Adafruit SSD1306: https://github.com/adafruit/Adafruit_SSD1306
  2. Adafruit GFX: https://github.com/adafruit/Adafruit-GFX-Library
  3. ArduinoJson: https://arduinojson.org/ (you can also install this via the Arduino Library Manager)

Step 2: Set Up the Hardware

For this tutorial, we’ll be using an OLED display with a resolution of 128×64 pixels, driven by the SSD1306 controller. Connect the OLED display to your ESP32 according to the following wiring diagram:

  • OLED SDA -> ESP32 GPIO4 (SDA)
  • OLED SCL -> ESP32 GPIO15 (SCL)
  • OLED RST -> ESP32 GPIO16 (RST)
  • OLED VCC -> 3.3V
  • OLED GND -> GND

Step 3: Configure Your Wi-Fi Credentials

Replace "your_SSID" and "your_PASSWORD" in the provided code with your actual Wi-Fi network name and password.

Step 4: Upload the Sketch

Upload the following Arduino sketch to your ESP32:

#include <Wire.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

// Replace with your WiFi credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";

// OLED display pins
#define OLED_SDA 4
#define OLED_SCL 15
#define OLED_RST 16

Adafruit_SSD1306 display(128, 64, &Wire, OLED_RST);

String getPublicIP() {
  HTTPClient http;
  String payload;

  http.begin("http://api.ipify.org?format=json");
  int httpCode = http.GET();
  
  if (httpCode > 0) {
    payload = http.getString();
  } else {
    payload = "";
  }
  http.end();
  
  return payload;
}

void setup() {
  // Set up the OLED display
  pinMode(OLED_RST, OUTPUT);
  digitalWrite(OLED_RST, LOW);
  delay(20);
  digitalWrite(OLED_RST, HIGH);
  Wire.begin(OLED_SDA, OLED_SCL);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  display.println("Connecting to WiFi...");
  display.display();

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    display.print(".");
    display.display();
  }

  // Get public IP address
  String publicIPJson = getPublicIP();

  // Show the public IP address on the OLED display
  display.clearDisplay();
  display.setCursor(0, 0);
  display.println("Connected!");
  display.print("Public IP: ");

  if (publicIPJson != "") {
    StaticJsonDocument<128> doc;
    deserializeJson(doc, publicIPJson);
    const char* ip = doc["ip"];
    display.println(ip);
  } else {
    display.println("Error");
  }

  display.display();
}

void loop() {
  // Nothing to do here
}

This code connects your ESP32 to the specified Wi-Fi network and retrieves its public IP address from the ipify API. The public IP address is then displayed on the OLED screen.

Step 5: Verify the Results

After uploading the sketch to your ESP32, open the Serial Monitor in the Arduino IDE. You should see your device connecting to the Wi-Fi network and then displaying its public IP address on the OLED display.

Conclusion:

In this tutorial, we showed you how to display the public IP address of your ESP32 on an OLED display. This can be a helpful addition to your IoT projects, giving you a simple way to monitor the connectivity status of your devices.

You can further enhance this project by adding more features, such as periodically checking for a new IP address, displaying additional network information, or even connecting to a remote server to report its status. The possibilities are endless!

How to setup Nginx Reverse Proxy

Setting up a reverse proxy using Nginx involves the following steps:

  1. Install Nginx: First, you need to install Nginx on your server. You can use your package manager to install Nginx on your system.
  2. Configure Nginx: Next, you need to configure Nginx to act as a reverse proxy. To do this, you need to create a new configuration file in the /etc/nginx/conf.d/ directory.
  3. Set up the upstream server: In the configuration file, you need to specify the upstream server that Nginx should proxy requests to. You can specify the IP address or hostname of the upstream server.
  4. Define the server block: Next, you need to define the server block for the reverse proxy. This block specifies the listen directive that Nginx should use to listen for incoming connections.
  5. Configure the location block: In the location block, you need to specify the path that Nginx should proxy requests to the upstream server. You can use regular expressions to match different paths.

Here is a sample configuration file that you can use as a starting point:

upstream backend {
    server backend.example.com;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

In this example, Nginx is configured to listen on port 80 for requests to the example.com domain. All requests to this domain are proxied to the backend.example.com server.

The proxy_pass directive specifies the upstream server that Nginx should proxy requests to. The proxy_set_header directives set the Host and X-Real-IP headers for the proxied requests.

Once you have saved your configuration file, you can test it by running sudo nginx -t to check for syntax errors, and then sudo systemctl reload nginx to apply the changes.