Skip to content

πŸ”‹ Smart modem power management with battery monitoring, automated charging, and FastAPI web interface

License

Notifications You must be signed in to change notification settings

bewithdhanu/modem-power

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

50 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ModemPower πŸ”‹ - Airtel Modem Control with Tuya Smart Plug

GitHub license

Overview

ModemPower is an intelligent modem management system that automatically controls your Airtel modem using a Tuya Smart Plug. It monitors battery levels, manages power consumption, and provides automated control through a web API and scheduled tasks.

Features

  • πŸ”‹ Battery Monitoring: Automatically checks modem battery levels via local network
  • ⚑ Smart Power Management: Automatically turns off modem when battery > 80%, turns on when < 20%
  • 🌐 FastAPI: Modern RESTful API with automatic Swagger UI documentation
  • ⏰ Smart Scheduling: Automated 4-hour charging cycles with 5-minute battery monitoring
  • πŸ”Œ Tuya Integration: Seamless control via Tuya Smart Plug
  • 🐳 Docker Support: Easy deployment with Docker Compose
  • πŸ“± Cross-Platform: Works on macOS, Linux, and Windows
  • πŸ›‘οΈ Internet Connectivity Check: Only operates when connected to modem network
  • πŸ“§ Email Notifications: Sends email alerts when modem is unreachable

Prerequisites

Before you get started, ensure you have the following:

  • Airtel Modem: Compatible with local network access (192.168.1.1)
  • Tuya Smart Plug: Connected to your network and configured
  • Tuya IoT Platform Account: For API access
  • Python 3.x: Installed on your system
  • macOS or Linux: For cron job support

Installation

Option 1: Docker (Recommended)

  1. Clone the repository:

    git clone https://github.com/bewithdhanu/modem-power.git
    cd ModemPower
  2. Configure environment:

    cp env.example .env
    # Edit .env with your Tuya credentials and modem IP
  3. Start with Docker:

    ./start.sh
    # Or manually: docker-compose up -d

Option 2: Manual Installation

  1. Clone the repository:

    git clone https://github.com/bewithdhanu/modem-power.git
    cd ModemPower
  2. Create virtual environment:

    python -m venv venv
    source venv/bin/activate  # On macOS/Linux
  3. Install dependencies:

    pip install -r requirements.txt

Configuration

  1. Copy environment file:

    cp env.py.example env.py
  2. Update .env with your credentials:

    ACCESS_ID=your_tuya_access_id
    ACCESS_KEY=your_tuya_access_key
    USERNAME=your_tuya_username
    PASSWORD=your_tuya_password
    DEVICE_ID=your_tuya_device_id
    ENDPOINT=https://openapi.tuyain.com
    MODEM_IP=192.168.1.1  # Your modem's IP address
    # Brevo email configuration:
    BREVO_API_KEY=your_brevo_api_key
    SENDER_EMAIL=your_email@example.com
    RECIPIENT_EMAIL=recipient@example.com
    TZ=Asia/Kolkata  # Indian timezone

Usage

Docker Usage (Recommended)

  1. Start services:

    docker-compose up -d
  2. Access the API:

    • Main API: http://localhost:8765
    • Swagger UI: http://localhost:8765/docs (Interactive API documentation)
    • ReDoc: http://localhost:8765/redoc (Alternative API documentation)
  3. View logs:

    docker-compose logs -f
  4. Stop services:

    docker-compose down

Manual Usage

  1. Start the Flask service:

    source venv/bin/activate
    python service.py
  2. Access the web interface:

    • Open your browser and go to http://127.0.0.1:8765

API Endpoints

Endpoint Method Description
/ GET Redirects to Swagger UI
/docs GET Swagger UI - Interactive API documentation
/redoc GET ReDoc - Alternative API documentation
/automate-modem GET Run modem automation (check battery, turn on/off) - Auto-scheduled every 5 minutes
/turn-on-charger GET Turn on charger (with modem reachability check) - Auto-scheduled every 4 hours
/turn-off-charger GET Turn off charger
/restart-modem GET Restart the modem
/scheduler-status GET Check scheduler status and next scheduled jobs

🎯 Pro Tip: Visit http://localhost:8765/docs to see the interactive Swagger UI where you can test all endpoints directly from your browser!

Troubleshooting

Common Issues

  1. Internet connectivity issues:

    • Ensure you're connected to the modem's WiFi network
    • Check if the modem is accessible at 192.168.1.1
  2. Tuya API errors:

    • Verify your credentials in env.py
    • Check if your Tuya subscription is active
    • Ensure device ID is correct

Manual Testing

Test individual components:

# Test internet connectivity
python -c "from modem import is_wifi_connected; print('Connected:', is_wifi_connected())"

# Test battery reading
python -c "from modem import getBatteryPercent; print('Battery:', getBatteryPercent())"

# Test device control
python -c "from device import getStatus; print('Device status:', getStatus())"

Docker Services

The Docker Compose setup includes two services:

  1. modem-power: Flask web service running on port 8765
  2. cron: Uses willfarrell/crontab image for simple scheduling:
    • Turn on modem every day at 1:00 PM
    • Check battery and auto-manage every 5 minutes
    • Standard crontab format for easy customization

Network Configuration

The containers use host networking to access your router:

  • Containers share the host's network interface
  • Can directly access your modem at 192.168.1.1 (or configured IP)
  • No port mapping needed for internal communication
  • Flask service accessible at http://localhost:8765

Simple Cron Scheduling

Edit the crontab file to customize schedules:

# Edit the crontab file
nano crontab

# Current default schedules:
# 0 13 * * * curl -s http://localhost:8765/turn-on-charger  # Daily 1PM
# */5 * * * * curl -s http://localhost:8765/automate-modem  # Every 5 minutes

Cron Examples:

  • 0 13 * * * - Every day at 1:00 PM
  • */5 * * * * - Every 5 minutes
  • 0 9 * * 1-5 - Weekdays at 9:00 AM
  • 0 14 * * 0 - Every Sunday at 2:00 PM

Brevo Email Service (Free & Easy)

When the modem is unreachable, the system will send you a beautiful HTML email notification via Brevo API.

Setup Brevo (2 minutes):

  1. Create free account: Go to brevo.com and sign up
  2. Get API key: Go to API Keys and create a new key
  3. Add to .env:
    BREVO_API_KEY=your_brevo_api_key
    SENDER_EMAIL=your_email@example.com
    SENDER_NAME=ModemPower
    RECIPIENT_EMAIL=recipient@example.com
    RECIPIENT_NAME=Your Name

Why Brevo is Perfect:

  • βœ… Free: 300 emails/day free forever
  • βœ… No SMTP: Uses simple REST API
  • βœ… No passwords: Just API key authentication
  • βœ… Beautiful emails: HTML formatted notifications
  • βœ… Reliable: Enterprise-grade email delivery
  • βœ… Easy setup: 2 minutes to configure
  • βœ… No app passwords: No complex authentication

File Structure

ModemPower/
β”œβ”€β”€ device.py              # Tuya device control
β”œβ”€β”€ modem.py               # Modem management
β”œβ”€β”€ service.py             # Flask web service
β”œβ”€β”€ crontab                # Simple cron schedule file
β”œβ”€β”€ env.py                 # Configuration (legacy)
β”œβ”€β”€ env.example            # Environment template
β”œβ”€β”€ docker-compose.yml     # Docker services configuration
β”œβ”€β”€ Dockerfile             # Container definition
β”œβ”€β”€ start.sh               # Startup script
└── requirements.txt       # Dependencies

Security Notes

  • Keep your .env file secure and never commit it to version control
  • The service runs on localhost (127.0.0.1:8765) by default
  • All API calls are made over your local network
  • Tuya credentials are stored in environment variables
  • Port 8765 is used to avoid conflicts with common services
  • Docker containers run in isolated network environment

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Test thoroughly
  5. Submit a pull request

Acknowledgments

License

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

Authors

Name Type
Dhanu K Author