Arduino Nano ESP32 - Web Apps Web Digital Pins

Overview

This tutorial covers the DIYablesWebDigitalPinsPage class from the DIYables ESP32 WebApps Library. The page presents each registered pin as a button in the browser. Output pins can be toggled; input pins display their current state. Pin configuration is done entirely in the sketch — the browser reflects whatever mode the sketch defines.

Arduino Nano ESP32 Web Digital Pins

What This Tutorial Covers

  • Enabling individual pins as WEB_PIN_OUTPUT or WEB_PIN_INPUT
  • Reading output state changes from the browser via a callback
  • Supplying input pin values to the browser via a read callback
  • Pushing state updates to connected browsers from the sketch

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Recommended: Screw Terminal Expansion Board for Arduino Nano
1×Recommended: Breakout Expansion Board for Arduino Nano
1×Recommended: Power Splitter for Arduino Nano ESP32

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Steps

Follow these instructions step by step:

  • If this is your first time using the Arduino Nano ESP32, refer to the tutorial on setting up the Arduino Nano ESP32 development environment.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate board (e.g. Arduino Nano ESP32) and COM port.
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search "DIYables ESP32 WebApps", then find the DIYables ESP32 WebApps Library by DIYables
  • Click Install button to install the library.
  • Search for DIYables ESP32 WebApps created by DIYables and click the Install button.
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Library Manager
Type:
All
Topic:
All
DIYables ESP32 WebApps by DIYables
A comprehensive library designed for ESP32 that provides multiple professional web applications including Web Monitor, Chat, Digital Pin Control, Sliders, Joystick, Analog Gauge, Rotator Control, and Temperature Display via WebSocket communication. Features modular architecture for memory efficiency, automatic config handling, and perfect for IoT projects, robotics, sensor monitoring, servo/stepper control, temperature monitoring, and remote ESP32 control. More info
1.0.1
INSTALL
Newbiely.ino
···
1 void setup() {
Output
Serial Monitor
Ln 1, Col 1
Arduino Nano ESP32 on COM15
1
  • You will be asked for installing some other library dependencies
  • Click Install All button to install all library dependencies.
  • On Arduino IDE, Go to File Examples DIYables ESP32 WebApps WebDigitalPins example, or copy the above code and paste it to the editor of Arduino IDE
/* * DIYables WebApp Library - Web Digital Pins Example * * This example demonstrates the Web Digital Pins feature with automatic command handling: * - Control output pins 0-13 via web interface * - Monitor input pins 0-13 in real-time * - Individual pin ON/OFF control for outputs * - Real-time pin status feedback for inputs * - Bulk operations (All ON, All OFF, Toggle All) for outputs * * Hardware: ESP32 Boards * * Setup: * 1. Update WiFi credentials below * 2. Upload the sketch to your Arduino * 3. Open Serial Monitor to see the IP address * 4. Navigate to http://[IP_ADDRESS]/webdigitalpins */ #include <DIYables_ESP32_Platform.h> #include <DIYablesWebApps.h> // WiFi credentials - UPDATE THESE WITH YOUR NETWORK const char WIFI_SSID[] = "YOUR_WIFI_SSID"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD"; // Create WebApp server and page instances using platform abstraction ESP32ServerFactory serverFactory; DIYablesWebAppServer webAppsServer(serverFactory, 80, 81); DIYablesHomePage homePage; DIYablesWebDigitalPinsPage webDigitalPinsPage; // Array to store the state of each digital pin (0-13). Index corresponds to pin number. int pinStates[16] = { 0 }; // Initialize all states to LOW (0) void setup() { Serial.begin(9600); delay(1000); Serial.println("DIYables ESP32 WebApp - Web Digital Pins Example"); // Add home and digital pins pages webAppsServer.addApp(&homePage); webAppsServer.addApp(&webDigitalPinsPage); // Optional: Add 404 page for better user experience webAppsServer.setNotFoundPage(DIYablesNotFoundPage()); // Pin Configuration Examples: // Method 1: Enable specific pins individually for output control webDigitalPinsPage.enablePin(0, WEB_PIN_OUTPUT); // Enable pin 0 (TX - use with caution) webDigitalPinsPage.enablePin(1, WEB_PIN_OUTPUT); // Enable pin 1 (RX - use with caution) webDigitalPinsPage.enablePin(2, WEB_PIN_OUTPUT); webDigitalPinsPage.enablePin(3, WEB_PIN_OUTPUT); webDigitalPinsPage.enablePin(4, WEB_PIN_OUTPUT); //webDigitalPinsPage.enablePin(5, WEB_PIN_OUTPUT); // Comment/uncomment to disable/enable //webDigitalPinsPage.enablePin(6, WEB_PIN_OUTPUT); // Comment/uncomment to disable/enable webDigitalPinsPage.enablePin(13, WEB_PIN_OUTPUT); // Enable pin 13 (built-in LED) // Method 2: Enable pins for input monitoring webDigitalPinsPage.enablePin(8, WEB_PIN_INPUT); webDigitalPinsPage.enablePin(9, WEB_PIN_INPUT); //webDigitalPinsPage.enablePin(10, WEB_PIN_INPUT); // Comment/uncomment to disable/enable //webDigitalPinsPage.enablePin(11, WEB_PIN_INPUT); // Comment/uncomment to disable/enable // Method 3: Enable all pins at once (uncomment to use) // for (int pin = 0; pin <= 13; pin++) { // webDigitalPinsPage.enablePin(pin, WEB_PIN_OUTPUT); // or WEB_PIN_INPUT as needed // } // Method 4: Enable pins in a range using for loop (uncomment to use) // for (int pin = 7; pin <= 11; pin++) { // webDigitalPinsPage.enablePin(pin, WEB_PIN_OUTPUT); // or WEB_PIN_INPUT as needed // } // Initialize enabled pins int outputPins[] = { 0, 1, 2, 3, 4, 13 }; // Note: Pins 0,1 are TX/RX - use with caution for (int i = 0; i < 6; i++) { int pin = outputPins[i]; pinMode(pin, OUTPUT); digitalWrite(pin, LOW); pinStates[pin] = LOW; } int inputPins[] = { 8, 9 }; for (int i = 0; i < 2; i++) { int pin = inputPins[i]; pinMode(pin, INPUT); // Use INPUT_PULLUP if needed pinStates[pin] = digitalRead(pin); } // Start the WebApp server if (!webAppsServer.begin(WIFI_SSID, WIFI_PASSWORD)) { while (1) { Serial.println("Failed to start WebApp server!"); delay(1000); } } // Return the current state to display on Web App webDigitalPinsPage.onPinRead([](int pin) { return pinStates[pin]; // Return the current state of the pin // You can implement custom read logic here if needed }); // Handle the control request from Web App (for output pins) webDigitalPinsPage.onPinWrite([](int pin, int state) { digitalWrite(pin, state); pinStates[pin] = state; // You can implement custom control logic here if needed }); // Handle pin mode change requests (optional) webDigitalPinsPage.onPinModeChange([](int pin, int mode) { if (mode == WEB_PIN_OUTPUT) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); pinStates[pin] = LOW; } else if (mode == WEB_PIN_INPUT) { pinMode(pin, INPUT); // or INPUT_PULLUP as needed pinStates[pin] = digitalRead(pin); } // You can implement custom mode change logic here if needed }); } void loop() { // Handle WebApp server communications webAppsServer.loop(); // Monitor input pins for real-time updates static unsigned long lastInputCheck = 0; if (millis() - lastInputCheck > 100) { // Check every 100ms lastInputCheck = millis(); // Check input pins for changes and send real-time updates int inputPins[] = { 8, 9 }; for (int i = 0; i < 2; i++) { int pin = inputPins[i]; int currentState = digitalRead(pin); if (currentState != pinStates[pin]) { pinStates[pin] = currentState; // Send real-time update to web clients webDigitalPinsPage.updatePinState(pin, currentState); } } } // Add your main application code here delay(10); }
  • Update the WiFi credentials in the sketch:
const char WIFI_SSID[] = "YOUR_WIFI_NETWORK"; const char WIFI_PASSWORD[] = "YOUR_WIFI_PASSWORD";
  • Click Upload button on Arduino IDE to upload code to Arduino Nano ESP32
  • Open the Serial Monitor
  • The Serial Monitor output should resemble the following:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Nano ESP32
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Nano ESP32' on 'COM15')
New Line
9600 baud
DIYables WebApp - Web Digital Pins Example INFO: Added app / INFO: Added app /web-digital-pins DEBUG: Enabling pin 0 with mode OUTPUT DEBUG: Enabling pin 1 with mode OUTPUT DEBUG: Enabling pin 2 with mode OUTPUT DEBUG: Enabling pin 3 with mode OUTPUT DEBUG: Enabling pin 4 with mode OUTPUT DEBUG: Enabling pin 13 with mode OUTPUT DEBUG: Enabling pin 8 with mode INPUT DEBUG: Enabling pin 9 with mode INPUT DIYables WebApp Library Platform: Arduino Nano ESP32 Network connected! IP address: 192.168.0.2 HTTP server started on port 80 Configuring WebSocket server callbacks... WebSocket server started on port 81 WebSocket URL: ws://192.168.0.2:81 WebSocket server started on port 81 ========================================== DIYables WebApp Ready! ========================================== Web Interface: http://192.168.0.2 WebSocket: ws://192.168.0.2:81 Available Applications: Home Page: http://192.168.0.2/ Digital Pins: http://192.168.0.2/web-digital-pins ==========================================
Ln 11, Col 1
Arduino Nano ESP32 on COM15
2
  • If nothing appears, press the reset button on the board.
  • Enter the IP address from the Serial Monitor into a browser on the same network.
  • Example: http://192.168.0.2
  • The home page displays a card for the digital pins application:
Arduino Nano ESP32 DIYables WebApp Home page with Web Digital Pins app
  • Select the Digital Pins card to open the pin control page:
Arduino Nano ESP32 DIYables WebApp Web Digital Pins app
  • The page is also accessible directly at http://192.168.0.2/web-digital-pins.
  • Toggle output pins by clicking the corresponding buttons. Pin 13 (built-in LED) should respond immediately.

Pin Configuration Methods

Pins must be registered before webAppsServer.begin(). Three patterns are supported:

Enabling Individual Pins

webDigitalPinsPage.enablePin(2, WEB_PIN_OUTPUT); // Pin 2 as output webDigitalPinsPage.enablePin(3, WEB_PIN_OUTPUT); // Pin 3 as output webDigitalPinsPage.enablePin(4, WEB_PIN_INPUT); // Pin 4 as input

Enabling a Range of Output Pins

// Registers pins 2 through 13 as outputs webDigitalPinsPage.enableOutputPins(2, 13);

Enabling All Pins

// Registers all pins 0-13 (use with care for pins 0 and 1 — UART) webDigitalPinsPage.enableAllPins();

Callbacks

Write Callback (Output Pins)

Called when the browser toggles an output pin:

webDigitalPinsPage.onPinWrite([](int pin, int state) { digitalWrite(pin, state); Serial.println("Pin " + String(pin) + " set to " + String(state)); });

Read Callback (Input Pins)

Called when the browser requests an input pin's current state:

webDigitalPinsPage.onPinRead([](int pin) -> int { return digitalRead(pin); });

Pushing State from the Sketch

When an input changes state outside of a browser request, push the update manually:

void loop() { webAppsServer.loop(); int currentState = digitalRead(buttonPin); if (currentState != lastButtonState) { lastButtonState = currentState; webDigitalPinsPage.updatePinState(buttonPin, currentState); } }

Web Interface Controls

  • Pin button: Click to toggle an output pin HIGH or LOW. Green indicates HIGH; red indicates LOW.
  • All ON: Sets all registered output pins to HIGH.
  • All OFF: Sets all registered output pins to LOW.
  • Toggle All: Inverts the current state of all output pins.

Input pins display their current state without a toggle button — the browser refreshes them periodically via WebSocket.

Troubleshooting

Pin state does not change when clicked

  • Verify onPinWrite callback is registered before webAppsServer.begin()
  • Confirm the pin is registered with WEB_PIN_OUTPUT
  • Check the Serial Monitor for incoming WebSocket messages

Input pin shows wrong state

  • Confirm onPinRead callback is registered
  • Add a pull-up or pull-down resistor to avoid floating inputs
  • Check digitalRead() returns the expected value in the callback

Page not accessible

  • Check the IP address printed in the Serial Monitor
  • Make sure port 80 is open on the network
  • Both the board and browser device must be on the same 2.4 GHz WiFi network

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!