Skip to content

pnstack/template-esp32-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ESP32 Template Project

A comprehensive ESP32 project template with WiFi configuration, web server, OTA updates, HTTP client, and serial logging. Built with Arduino framework and PlatformIO for easy development and deployment.

πŸš€ Features

  • βœ… WiFi Management: Auto-reconnect, configurable credentials via web interface
  • βœ… Web Server: Async web server with responsive HTML UI
  • βœ… OTA Updates: Over-the-air firmware updates for remote devices
  • βœ… HTTP Client: Send data to external APIs/servers
  • βœ… Serial Logging: Comprehensive logging with multiple log levels
  • βœ… SPIFFS: File system for storing configuration and web files
  • βœ… Clean Architecture: Well-organized code structure with separation of concerns
  • βœ… PlatformIO Support: Complete development environment setup

πŸ“ Project Structure

template-esp32-project/
β”œβ”€β”€ src/                        # Source files
β”‚   β”œβ”€β”€ main.cpp               # Main application entry point
β”‚   β”œβ”€β”€ wifi_manager.cpp       # WiFi connection and management
β”‚   β”œβ”€β”€ web_server.cpp         # Web server implementation
β”‚   β”œβ”€β”€ ota_manager.cpp        # OTA update handling
β”‚   β”œβ”€β”€ http_client.cpp        # HTTP client for API calls
β”‚   └── logger.cpp             # Serial logging implementation
β”œβ”€β”€ include/                    # Header files
β”‚   β”œβ”€β”€ config.h               # Configuration constants
β”‚   β”œβ”€β”€ wifi_manager.h         # WiFi manager interface
β”‚   β”œβ”€β”€ web_server.h           # Web server interface
β”‚   β”œβ”€β”€ ota_manager.h          # OTA manager interface
β”‚   β”œβ”€β”€ http_client.h          # HTTP client interface
β”‚   β”œβ”€β”€ logger.h               # Logger interface
β”‚   └── credentials.h.example  # Example credentials file
β”œβ”€β”€ data/                       # Web files (uploaded to SPIFFS)
β”‚   └── index.html             # Web configuration interface
β”œβ”€β”€ docs/                       # Documentation
β”‚   β”œβ”€β”€ SETUP.md               # Setup and installation guide
β”‚   β”œβ”€β”€ OTA_UPDATES.md         # OTA update instructions
β”‚   └── API.md                 # HTTP API documentation
β”œβ”€β”€ platformio.ini             # PlatformIO configuration
β”œβ”€β”€ .gitignore                 # Git ignore file
└── README.md                  # This file

πŸ› οΈ Quick Start

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/pnstack/template-esp32-project.git
    cd template-esp32-project
  2. Open in VS Code:

    code .
  3. Upload filesystem image (contains web interface):

    pio run --target uploadfs

    Or in VS Code: PlatformIO β†’ Upload Filesystem Image

  4. Build and upload firmware:

    pio run --target upload

    Or in VS Code: PlatformIO β†’ Upload

  5. Open serial monitor:

    pio device monitor

    Or in VS Code: PlatformIO β†’ Monitor

First-Time Configuration

  1. After uploading, the ESP32 will start and display its status in the serial monitor
  2. If no WiFi is configured, you can:
    • Option A: Connect to ESP32's default IP (usually 192.168.4.1) if it creates an AP
    • Option B: Connect it to your network and find its IP in the serial monitor
  3. Open a web browser and navigate to the ESP32's IP address
  4. Enter your WiFi credentials in the web interface
  5. Click "Save Configuration"
  6. The device will reconnect to your WiFi network

πŸ“– Documentation

Getting Started

Technical Documentation

Learning Resources

🎯 Usage Examples

Configure WiFi via Web Interface

  1. Access the device at http://<device-ip>/
  2. View current status (WiFi connection, IP address, uptime, etc.)
  3. Update WiFi credentials
  4. Monitor device status in real-time

Send Data to External Server

#include "http_client.h"

HTTPClientManager httpClient;

void sendSensorData() {
    float temperature = 25.5;
    float humidity = 60.0;
    
    const char* endpoint = "http://your-server.com/api/data";
    bool success = httpClient.sendSensorData(endpoint, temperature, humidity);
    
    if (success) {
        Logger::info("Data sent successfully");
    }
}

Update Firmware via OTA

# Configure upload port in platformio.ini
upload_protocol = espota
upload_port = 192.168.1.100

# Upload via OTA
pio run --target upload

Access Device Status via API

curl http://192.168.1.100/api/status

Response:

{
  "device_name": "ESP32-Device",
  "uptime": 123456,
  "wifi_connected": true,
  "ssid": "MyWiFiNetwork",
  "ip_address": "192.168.1.100",
  "signal_strength": -45
}

πŸ”§ Configuration

WiFi Settings

Edit include/config.h:

#define WIFI_RECONNECT_INTERVAL 5000  // ms between reconnect attempts
#define WIFI_MAX_RETRY 20             // max reconnection attempts

Web Server Port

#define WEBSERVER_PORT 80

OTA Settings

#define OTA_HOSTNAME "esp32-device"
#define OTA_PASSWORD "admin"

Serial Logging

#define SERIAL_BAUD_RATE 115200

// In your code:
Logger::setLogLevel(LOG_DEBUG);  // LOG_ERROR, LOG_WARN, LOG_INFO, LOG_DEBUG

πŸ§ͺ Development

PlatformIO Commands

# Build project
pio run

# Upload firmware via USB
pio run --target upload

# Upload filesystem (web files)
pio run --target uploadfs

# Open serial monitor
pio device monitor

# Clean build files
pio run --target clean

# Update libraries
pio lib update

Adding New Features

  1. Create new .cpp and .h files in src/ and include/
  2. Add your functionality following the existing code structure
  3. Use the Logger for debugging: Logger::info("Your message")
  4. Test thoroughly via serial monitor

Modifying Web Interface

  1. Edit data/index.html
  2. Upload filesystem: pio run --target uploadfs
  3. Refresh your browser

πŸ“¦ Dependencies

The following libraries are automatically installed by PlatformIO:

  • ESP32 Arduino Framework: Core ESP32 support
  • ESP AsyncWebServer: Asynchronous web server
  • AsyncTCP: Async TCP library for ESP32
  • ArduinoJson: JSON parsing and generation

πŸ› Troubleshooting

Cannot connect to serial port

# Linux: Add user to dialout group
sudo usermod -a -G dialout $USER
# Log out and log back in

Upload fails

  • Hold the BOOT button during upload
  • Try a different USB cable
  • Reduce upload speed in platformio.ini

WiFi connection issues

  • Verify credentials are correct
  • Ensure network is 2.4GHz (ESP32 doesn't support 5GHz)
  • Check router settings (MAC filtering, etc.)
  • View detailed logs in serial monitor

Web interface not loading

  • Upload filesystem first: pio run --target uploadfs
  • Verify data/index.html exists
  • Check SPIFFS mount status in serial monitor

πŸ”’ Security Notes

  1. Change default OTA password before deployment
  2. Never commit credentials to version control (use credentials.h, which is in .gitignore)
  3. Use strong WiFi passwords
  4. Consider adding API authentication for production
  5. Keep firmware updated to patch security vulnerabilities

πŸ“ License

This project is provided as a template. Feel free to use, modify, and distribute as needed.

🀝 Contributing

Contributions are welcome! Please feel free to submit issues and pull requests.

πŸ“§ Support

For questions and support:

  • Check the documentation
  • Review serial monitor logs for errors
  • Create an issue on GitHub

πŸ™ Acknowledgments

Built with:

πŸŽ“ Learning Resources


Made with ❀️ for the ESP32 community

About

template-esp32-project

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Contributors 2

  •  
  •