Skip to content

scrozza2007/smartsense-home

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🏠 SmartSense Home

A modern, scalable IoT platform for monitoring and controlling ESP32-based devices through a beautiful web dashboard.

License: MIT PHP Version ESP32


πŸ“– Overview

SmartSense Home is a complete, production-ready IoT platform that enables users to:

  • πŸ“Š Monitor real-time sensor data from ESP32 devices
  • πŸŽ›οΈ Control IoT devices remotely via intuitive web interface
  • πŸ‘₯ Manage multiple devices from a single dashboard
  • πŸ” Secure multi-user authentication system
  • πŸ“‘ Communicate via REST API (no MQTT broker required)

Perfect for home automation, environmental monitoring, smart sensors, and IoT prototyping!


✨ Features

Backend

  • ⚑ Fast PHP 8+ REST API
  • πŸ”’ Secure authentication (bcrypt password hashing)
  • πŸ—„οΈ MySQL database with optimized schema
  • πŸ›‘οΈ SQL injection protection (prepared statements)
  • πŸ“ Input validation and sanitization
  • πŸ”‘ Token-based device authentication

Frontend

  • 🎨 Clean, modern UI/UX
  • πŸ“± Fully responsive design
  • ⚑ Real-time data updates
  • πŸ“Š Data visualization (tables + graphs)
  • πŸŽ›οΈ Device control interface
  • πŸŒ™ No frameworks - pure vanilla JavaScript

ESP32

  • πŸ“‘ WiFi connectivity
  • πŸ”„ Automatic reconnection
  • πŸ“€ Batch data transmission
  • πŸ“₯ Command polling & execution
  • πŸ’‘ Built-in LED control example
  • 🌑️ DHT22 sensor support (optional)

πŸš€ Quick Start

Prerequisites

  • XAMPP (Apache + PHP 8.0+ + MySQL)
  • Arduino IDE with ESP32 support
  • ESP32-C3 Super Mini (or compatible board)

Installation

  1. Clone the repository

    git clone https://github.com/scrozza2007/smartsense-home.git
    cd smartsense-home
  2. Setup database

    • Copy project to xampp/htdocs/
    • Open phpMyAdmin: http://localhost/phpmyadmin
    • Create database: smartsense_home
    • Import: database/schema.sql
  3. Configure backend

    • Verify settings in backend/config/database.php
    • Default: root user, no password (XAMPP default)
  4. Access web app

    • Open: http://localhost/smartsense-home/frontend/
    • Create account and login
    • Add your first device
  5. Setup ESP32

    • Install libraries: ArduinoJson
    • Configure WiFi in esp32/smartsense_device/smartsense_device.ino
    • Set server IP and device token
    • Upload to ESP32

πŸ“– Detailed guide: docs/INSTALLATION.md


πŸ“ Project Structure

smartsense-home/
β”œβ”€β”€ backend/                 # PHP backend
β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ auth/           # Authentication endpoints
β”‚   β”‚   β”œβ”€β”€ devices/        # Device management
β”‚   β”‚   └── device/         # ESP32 communication
β”‚   β”œβ”€β”€ config/             # Database configuration
β”‚   └── includes/           # Utility functions
β”œβ”€β”€ frontend/               # Web dashboard
β”‚   β”œβ”€β”€ assets/
β”‚   β”‚   β”œβ”€β”€ css/           # Stylesheets
β”‚   β”‚   └── js/            # JavaScript modules
β”‚   β”œβ”€β”€ index.html         # Login page
β”‚   β”œβ”€β”€ register.html      # Registration page
β”‚   └── dashboard.html     # Main dashboard
β”œβ”€β”€ esp32/                  # Arduino sketches
β”‚   └── smartsense_device/ # ESP32-C3 firmware
β”œβ”€β”€ database/               # SQL schema
β”‚   └── schema.sql
β”œβ”€β”€ docs/                   # Documentation
β”‚   β”œβ”€β”€ API.md             # API reference
β”‚   β”œβ”€β”€ ESP32_SETUP.md     # ESP32 guide
β”‚   └── INSTALLATION.md    # Setup instructions
β”œβ”€β”€ .gitignore
└── README.md

🎯 Usage

Web Dashboard

  1. Register/Login

    • Create account with username, email, password
    • Login to access dashboard
  2. Add Device

    • Click "Add Device"
    • Enter device name (e.g., "Living Room Sensor")
    • Save the device token! (needed for ESP32)
  3. View Data

    • Click on device card to view sensor data
    • Latest values shown at top
    • Historical data in table below
    • Auto-refreshes every 10 seconds
  4. Send Commands

    • Select command type (LED, RELAY, etc.)
    • Enter value (ON, OFF, TOGGLE)
    • Click "Send"
    • ESP32 executes command within seconds

ESP32 Device

Configuration:

const char* WIFI_SSID = "YourWiFiName";
const char* WIFI_PASSWORD = "YourPassword";
const char* API_BASE_URL = "http://192.168.1.100/smartsense-home/backend/api";
const char* DEVICE_TOKEN = "your_device_token_from_dashboard";

Features:

  • Sends sensor data every 30 seconds (configurable)
  • Polls for commands every 5 seconds (configurable)
  • Built-in LED control on GPIO8
  • Automatic WiFi reconnection
  • Detailed serial output for debugging

πŸ”Œ API Endpoints

Authentication

  • POST /api/auth/register - Create account
  • POST /api/auth/login - Login
  • POST /api/auth/logout - Logout

Device Management

  • GET /api/devices/list - List user devices
  • POST /api/devices/create - Add new device
  • GET /api/devices/data - Get device data
  • POST /api/devices/command - Send command

ESP32 Communication

  • POST /api/device/send_data - Submit sensor data
  • GET /api/device/get_commands - Poll for commands
  • POST /api/device/update_command - Update command status

πŸ“– Full API documentation: docs/API.md


πŸ›  Technology Stack

Component Technology
Backend PHP 8+, PDO, MySQL
Frontend HTML5, CSS3, JavaScript (ES6+)
Database MySQL 5.7+ / MariaDB 10.3+
Hardware ESP32-C3 Super Mini
Firmware Arduino (C++), ArduinoJson
Server Apache 2.4+ (XAMPP)

πŸ“Š Database Schema

users
β”œβ”€β”€ id
β”œβ”€β”€ username
β”œβ”€β”€ email
β”œβ”€β”€ password_hash
└── created_at

devices
β”œβ”€β”€ id
β”œβ”€β”€ user_id (FK β†’ users)
β”œβ”€β”€ device_name
β”œβ”€β”€ device_token (unique)
β”œβ”€β”€ is_online
└── last_seen

sensor_data
β”œβ”€β”€ id
β”œβ”€β”€ device_id (FK β†’ devices)
β”œβ”€β”€ sensor_type
β”œβ”€β”€ sensor_value
β”œβ”€β”€ unit
└── timestamp

commands
β”œβ”€β”€ id
β”œβ”€β”€ device_id (FK β†’ devices)
β”œβ”€β”€ command_type
β”œβ”€β”€ command_value
β”œβ”€β”€ status (pending/executed/failed)
└── created_at

πŸ” Security Features

βœ… Password Security

  • Bcrypt hashing (cost factor 10)
  • Salted hashes

βœ… Database Security

  • Prepared statements (SQL injection protection)
  • Input validation and sanitization
  • Foreign key constraints

βœ… API Security

  • Session-based authentication
  • Device token authentication
  • Access control (users only see their devices)

βœ… Best Practices

  • HTTPS ready (configure SSL certificate)
  • CORS headers configured
  • XSS protection via htmlspecialchars()

🎨 Screenshots

Login Page

Clean authentication interface with registration option.

Dashboard

Dashboard Preview

Device cards, real-time data, command controls

Device Data View

Data View

Latest values, historical data table, command interface


πŸ§ͺ Testing

Manual API Testing

# Register user
curl -X POST http://localhost/smartsense-home/backend/api/auth/register.php \
  -H "Content-Type: application/json" \
  -d '{"username":"test","email":"test@example.com","password":"test123"}'

# Login
curl -X POST http://localhost/smartsense-home/backend/api/auth/login.php \
  -H "Content-Type: application/json" \
  -d '{"username":"test","password":"test123"}' \
  -c cookies.txt

# List devices
curl -X GET http://localhost/smartsense-home/backend/api/devices/list.php \
  -b cookies.txt

ESP32 Testing

  1. Upload code to ESP32
  2. Open Serial Monitor (115200 baud)
  3. Verify:
    • WiFi connection βœ“
    • Data sending (HTTP 201) βœ“
    • Command polling βœ“
    • LED control βœ“

πŸ“š Documentation


πŸ”§ Customization

Add Custom Sensor

// In ESP32 code
JsonObject custom = readings.createNestedObject();
custom["sensor_type"] = "soil_moisture";
custom["sensor_value"] = String(analogRead(A0));
custom["unit"] = "%";

Add New Command Type

// In executeCommand()
} else if (type == "RELAY") {
    digitalWrite(RELAY_PIN, value == "ON" ? HIGH : LOW);
    Serial.println("βœ“ Relay controlled");
}

Change Update Intervals

const unsigned long DATA_SEND_INTERVAL = 60000;  // 1 minute
const unsigned long COMMAND_CHECK_INTERVAL = 10000;  // 10 seconds

πŸ› Troubleshooting

Issue Solution
Database connection failed Check MySQL is running, verify credentials
API returns 404 Ensure files in correct htdocs location
ESP32 won't connect Verify WiFi credentials, use 2.4GHz network
Device token invalid Copy token exactly from dashboard
Data not appearing Check Serial Monitor for HTTP response codes

πŸ“– More help: docs/INSTALLATION.md#troubleshooting


πŸ›£οΈ Roadmap

  • Data export (CSV, JSON)
  • Email/SMS alerts on sensor thresholds
  • Device groups and automation rules
  • Mobile app (React Native)
  • MQTT support (optional alternative)
  • WebSocket for real-time updates
  • Charts and data visualization
  • Device firmware OTA updates

🀝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the project
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ‘¨β€πŸ’» Author

scrozza2007


πŸ™ Acknowledgments

  • Inspired by platforms like Blynk and ThingsBoard
  • ESP32 community for amazing support
  • ArduinoJson library by Benoit Blanchon
  • All contributors and testers

⭐ Star History

If you find this project useful, please consider giving it a star! ⭐


πŸ“ž Support


Made with ❀️ for the IoT community

Report Bug Β· Request Feature Β· Documentation

About

Modern IoT platform for monitoring and controlling ESP32 devices via web dashboard

Topics

Resources

License

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors