in Uncategorized

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!

Write a Comment

Comment