DIYables Web Apps Library Reference
DIYables WebApps Library - API Reference
Overview
The DIYables WebApps library provides a comprehensive modular solution for creating WebSocket-based web applications on Arduino Uno R4 WiFi and DIYables STEM V4 IoT. It includes multiple pre-built web applications and a flexible framework for creating custom applications. Specially optimized for the DIYables STEM V4 IoT educational platform with enhanced IoT capabilities and built-in sensor integration.
The library uses a modular architecture where you only include the web applications you need, saving memory and improving performance.
Features
- Modular Architecture: Add only the web apps you need to optimize memory usage
- Memory Efficient: Each web app can be enabled/disabled independently
- 11+ Ready-to-Use Web Applications: Complete Arduino control without web programming knowledge
- Custom Web App Framework: Create your own applications using the base class system
- Real-time Communication: WebSocket-based for instant updates
- Responsive Design: Works on desktop, tablet, and mobile devices
- Easy Integration: Simple callback-based API
- Multiple App Support: Support adding multiple web apps simultaneously
- Platform Extensible: Currently implemented for Arduino Uno R4 WiFi, but can be extended for other hardware platforms. See DIYables_WebApps_ESP32
Core Classes
DIYablesWebAppServer
The main server class that manages web applications, HTTP requests, and WebSocket communication.
Constructor
Creates a web app server instance.
- httpPort: HTTP server port (default: 80)
- websocketPort: WebSocket server port (default: 81)
Methods
##### Setup and Connection
Initializes network connection (for Ethernet or pre-configured connections) and starts the web server.
- Returns: true if successful, false if failed
- Use case: For future Ethernet support or when WiFi credentials are pre-configured
Initializes WiFi connection and starts the web server.
- ssid: WiFi network name
- password: WiFi password
- Returns: true if successful, false if failed
- Use case: Standard WiFi connection with credentials
Handles HTTP requests and WebSocket communication. Must be called in the main loop.
Returns true if WiFi is connected.
Returns the Arduino's IP address as a string.
##### Application Management
Adds a web application to the server.
- app: Pointer to a web application instance
Removes a web application by its URL path.
- path: URL path of the application (e.g., "/chat")
Gets a web application by its URL path.
- path: URL path of the application
- Returns: Pointer to the app or nullptr if not found
Sets the 404 Not Found page (optional).
- page: 404 page instance
##### Specialized App Access
Gets the digital pins page instance if added.
- Returns: Pointer to the digital pins page or nullptr
Gets the slider page instance if added.
- Returns: Pointer to the slider page or nullptr
Gets the joystick page instance if added.
- Returns: Pointer to the joystick page or nullptr
Base Classes
DIYablesWebAppPageBase
Abstract base class that all web applications inherit from. Provides common functionality for HTTP handling, WebSocket communication, and page management.
Constructor
Creates a base page instance with the specified URL path.
- pagePath: URL path for the page (e.g., "/web-joystick")
Virtual Methods (Must be implemented by derived classes)
Handles HTTP requests for this page. Pure virtual method.
- client: Web client interface for sending response
Handles WebSocket messages for this page. Pure virtual method.
- ws: WebSocket connection interface
- message: Received message content
- length: Message length
Returns page identification string used in connection info display. Pure virtual method.
- Returns: Page info string (e.g., "đšī¸ Web Joystick: ")
Returns HTML for home page navigation button. Pure virtual method.
- Returns: HTML string for navigation card
Virtual Methods (Optional overrides)
Called when a new WebSocket connection is established.
- ws: New WebSocket connection
Called when a WebSocket connection is closed.
- ws: Closed WebSocket connection
Common Methods
Gets the URL path for this page.
- Returns: Page path string
Checks if the page is currently enabled.
- Returns: true if enabled, false if disabled
Enables or disables the page.
- enable: true to enable, false to disable
Utility Methods
Sends standard HTTP headers to client.
- client: Web client interface
- contentType: MIME type (default: "text/html")
Sends a message to specific WebSocket client.
- ws: Target WebSocket connection
- message: Message to send
Broadcasts a message to all connected WebSocket clients.
- message: Message to broadcast
Sends large HTML content using chunked transfer encoding.
- client: Web client interface
- html: HTML content to send
Usage Example
Web Application Classes
DIYablesHomePage
Central navigation hub providing links to all enabled applications.
Constructor
URL Path
- Path: / (root)
DIYablesWebChatPage
Interactive chat interface for two-way communication with Arduino.
Constructor
URL Path
- Path: /webchat
Methods
Sets callback for incoming chat messages.
Sends a message to the web chat interface.
DIYablesWebMonitorPage
Web-based serial monitor for real-time output and command input.
Constructor
URL Path
- Path: /webmonitor
Methods
Sets callback for incoming monitor messages.
Sends a message to the web monitor interface.
DIYablesWebDigitalPinsPage
Control and monitor digital pins 0-13 through web interface.
Constructor
URL Path
- Path: /webdigitalpins
Methods
Enables a pin for web control.
- pin: Pin number (0-13)
- mode: WEB_PIN_OUTPUT or WEB_PIN_INPUT
Sets callback for pin write operations (output pins).
Sets callback for pin read operations (input pins).
Sets callback for pin mode changes.
Updates pin state in real-time for web clients.
DIYablesWebSliderPage
Dual slider control for analog/PWM applications.
Constructor
URL Path
- Path: /webslider
Methods
Sets callback for slider value changes from web.
- Parameters: slider1 (0-255), slider2 (0-255)
Sets callback for web client requesting current values.
Sends slider values to web interface.
DIYablesWebJoystickPage
2D joystick control for robotics and positioning applications.
Constructor
- autoReturn: Whether joystick returns to center automatically
- sensitivity: Minimum movement percentage to trigger updates
URL Path
- Path: /webjoystick
Methods
Sets callback for joystick movement from web.
- Parameters: x (-100 to 100), y (-100 to 100)
Sets callback for web client requesting current position.
Sends joystick position to web interface.
Sets auto-return behavior.
Sets movement sensitivity (percentage).
DIYablesWebPlotterPage
Real-time data visualization with multiple data series support.
Constructor
URL Path
- Path: /webplotter
Methods
Sets the plot title.
Sets axis labels.
Enables or disables automatic Y-axis scaling.
Sets maximum number of data points to display.
Adds a data point to a series.
Clears all data from the plot.
DIYablesNotFoundPage
Optional 404 error page for better user experience.
Constructor
Basic Usage Example
Web Applications Overview
Home Page
- URL: http://[arduino-ip]/
- Purpose: Central navigation hub
- Features: Links to all enabled applications, connection status
WebChat Application
- URL: http://[arduino-ip]/webchat
- Purpose: Two-way communication interface
- Features: Real-time messaging, message history, WebSocket status
Web Monitor
- URL: http://[arduino-ip]/webmonitor
- Purpose: Serial monitor replacement
- Features: Real-time serial output, command input, dark theme
Web Digital Pins Control
- URL: http://[arduino-ip]/webdigitalpins
- Purpose: Control digital pins 0-13
- Features: Individual pin control, bulk operations, real-time status
Web Slider Control
- URL: http://[arduino-ip]/webslider
- Purpose: Dual analog/PWM control
- Features: Two independent sliders (0-255), preset values, real-time feedback
Web Joystick Control
- URL: http://[arduino-ip]/webjoystick
- Purpose: 2D position control for robots/vehicles
- Features: Touch/mouse control, coordinate display, sensitivity adjustment
Web Plotter
- URL: http://[arduino-ip]/webplotter
- Purpose: Real-time data visualization
- Features: Multiple data series, auto-scaling, configurable titles and axes
WebSocket Communication
All applications use WebSocket on port 81 for real-time communication:
- WebSocket URL: ws://[arduino-ip]:81
- Connection: Automatic reconnection on disconnect
- Protocol: Text-based message format
Message Formats
WebChat Messages
- From Web: Direct text message
- To Web: Direct text message
Web Monitor Messages
- From Web: Direct text message
- To Web: Direct text message
Web Digital Pin Messages
- From Web: JSON format: {"pin": 13, "state": 1}
- To Web: JSON format: {"pin": 13, "state": 1}
Web Slider Messages
- From Web: JSON format: {"slider1": 128, "slider2": 255}
- To Web: JSON format: {"slider1": 128, "slider2": 255}
Web Joystick Messages
- From Web: JSON format: {"x": 50, "y": -25}
- To Web: JSON format: {"x": 50, "y": -25}
Web Plotter Messages
- From Web: Not applicable (display only)
- To Web: JSON format: {"series": "temp", "x": 10.5, "y": 23.4}
Error Handling
The library includes automatic error handling for:
- WiFi connection failures
- WebSocket disconnections
- Invalid message formats
- Client connection limits
Memory Usage
Modular Architecture Benefits: Include only the web applications you need to optimize memory usage.
Approximate memory usage per component:
- DIYablesWebAppServer: ~8KB Flash, ~2KB RAM
- DIYablesHomePage: ~3KB Flash, ~1KB RAM
- DIYablesWebChatPage: ~6KB Flash, ~1.5KB RAM
- DIYablesWebMonitorPage: ~5KB Flash, ~1.5KB RAM
- DIYablesWebDigitalPinsPage: ~8KB Flash, ~2KB RAM
- DIYablesWebSliderPage: ~6KB Flash, ~1.5KB RAM
- DIYablesWebJoystickPage: ~7KB Flash, ~1.5KB RAM
- DIYablesWebPlotterPage: ~10KB Flash, ~2KB RAM
- WebSocket Buffer: ~1KB RAM per connection
Total if all apps enabled: ~53KB Flash, ~12KB RAM
Minimal setup (server + home + 1 app): ~17KB Flash, ~4.5KB RAM
Browser Compatibility
Supported browsers:
- Chrome 50+
- Firefox 45+
- Safari 10+
- Edge 79+
- Mobile browsers (iOS Safari, Chrome Mobile)
Security Notes
- No authentication implemented (suitable for local networks only)
- Use in trusted networks only
- Consider adding authentication for public deployments
Troubleshooting
Common Issues
- Cannot connect to WiFi
- Check SSID and password
- Verify network is 2.4GHz (not 5GHz)
- Check signal strength
- WebSocket connection fails
- Verify IP address is correct
- Check firewall settings
- Try different browser
- High memory usage
- Disable unused applications
- Limit concurrent connections
- Restart Arduino if memory fragmentation occurs
- Slow response
- Check WiFi signal strength
- Reduce WebSocket message frequency
- Use shorter callback functions
Example Projects
Example Applications
The DIYables WebApps library includes comprehensive examples designed for Arduino Uno R4 WiFi and DIYables STEM V4 IoT educational platform:
Available Examples
- Chat Example: Two-way communication interface
- WebMonitor Example: Serial monitor replacement with enhanced features
- WebDigitalPins Example: Control all digital pins with visual feedback
- WebSlider Example: Dual analog/PWM control with presets
- WebJoystick Example: 2D position control for robotics projects
- MultipleWebApps Example: All applications running simultaneously
Hardware Compatibility
- Recommended: DIYables STEM V4 IoT (enhanced features, built-in sensors)
- Compatible: Arduino Uno R4 WiFi
Tutorial Resources
Complete step-by-step tutorials available at:
- Newbiely.com/tutorials/arduino/arduino-uno-r4-wifi-web-apps
- DIYables STEM V4 IoT specific guides and sensor integration examples
See the examples/ folder for complete projects and docs/ folder for detailed setup instructions.
Platform Abstraction Interfaces
The DIYables WebApps library uses a platform abstraction layer with interfaces that enable support for multiple hardware platforms. These interfaces separate the core WebApp functionality from platform-specific implementations.
Core Interfaces
IWebClient
Interface for HTTP client connections.
IWebSocket
Interface for WebSocket connections with bidirectional communication support.
IWebServer
Interface for HTTP server functionality.
IWebSocketServer
Interface for WebSocket server with connection management.
INetworkProvider
Interface for network connectivity management.
IServerFactory
Factory interface for creating platform-specific implementations.
Platform Implementation Example
For Arduino Uno R4 WiFi, the interfaces are implemented using the UnoR4WiFi_WebServer library:
Adding New Platform Support
To add support for a new platform (e.g., ESP32):
- Implement all interfaces for the target platform
- Create a ServerFactory that instantiates your implementations
- Handle platform-specific networking and WebSocket protocols
- Test with existing WebApp classes (no changes needed)
Example usage with different platforms:
Benefits of Interface-Based Design
- Platform Independence: Core WebApp logic works on any platform
- Extensibility: Easy to add new hardware support
- Maintainability: Platform-specific code is isolated
- Testability: Interfaces can be mocked for unit testing
- Consistency: Same API across all supported platforms
Current Platform Support
- â Arduino Uno R4 WiFi: Fully implemented and tested
- đ ESP32: Available as separate extension - DIYables_WebApps_ESP32
- đ Future Platforms: Can be added using the same interface pattern