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.
- π 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
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
-
Clone the repository:
git clone https://github.com/bewithdhanu/modem-power.git cd ModemPower -
Configure environment:
cp env.example .env # Edit .env with your Tuya credentials and modem IP -
Start with Docker:
./start.sh # Or manually: docker-compose up -d
-
Clone the repository:
git clone https://github.com/bewithdhanu/modem-power.git cd ModemPower -
Create virtual environment:
python -m venv venv source venv/bin/activate # On macOS/Linux
-
Install dependencies:
pip install -r requirements.txt
-
Copy environment file:
cp env.py.example env.py
-
Update
.envwith 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
-
Start services:
docker-compose up -d
-
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)
- Main API:
-
View logs:
docker-compose logs -f
-
Stop services:
docker-compose down
-
Start the Flask service:
source venv/bin/activate python service.py -
Access the web interface:
- Open your browser and go to
http://127.0.0.1:8765
- Open your browser and go to
| 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/docsto see the interactive Swagger UI where you can test all endpoints directly from your browser!
-
Internet connectivity issues:
- Ensure you're connected to the modem's WiFi network
- Check if the modem is accessible at
192.168.1.1
-
Tuya API errors:
- Verify your credentials in
env.py - Check if your Tuya subscription is active
- Ensure device ID is correct
- Verify your credentials in
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())"The Docker Compose setup includes two services:
- modem-power: Flask web service running on port 8765
- cron: Uses
willfarrell/crontabimage 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
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
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 minutesCron Examples:
0 13 * * *- Every day at 1:00 PM*/5 * * * *- Every 5 minutes0 9 * * 1-5- Weekdays at 9:00 AM0 14 * * 0- Every Sunday at 2:00 PM
When the modem is unreachable, the system will send you a beautiful HTML email notification via Brevo API.
- Create free account: Go to brevo.com and sign up
- Get API key: Go to API Keys and create a new key
- 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
- β 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
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
- Keep your
.envfile 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
- Fork the repository
- Create a feature branch
- Make your changes
- Test thoroughly
- Submit a pull request
- Tuya Official Python SDK
- Flask web framework
- Airtel modem API
This project is licensed under the MIT License - see the LICENSE file for details.
| Name | Type |
|---|---|
| Dhanu K | Author |