A simple HTTP service for Raspberry Pi that allows you to rotate modem connections via REST API calls.
- HTTP API: Simple REST endpoints to control modem connections
- Connection Status: Real-time monitoring of connection state
- Automatic Rotation: Disconnect and reconnect modem with configurable delays
- Multiple Methods: Supports NetworkManager, direct interface control, and dial-up processes
- Logging: Comprehensive logging to file and console
- Systemd Integration: Runs as a system service with auto-restart
-
Transfer files to your Raspberry Pi:
scp -r * pi@your-pi-ip:/home/pi/rotator/ -
Run setup script on Pi:
cd /home/pi/rotator chmod +x setup.sh ./setup.sh -
Test the service:
# Check connection status curl http://localhost:8080/status # Rotate connection curl -X POST http://localhost:8080/rotate
Service information and available endpoints
Returns current connection status:
{
"interface": "wwan0",
"interface_up": true,
"ip_address": "192.168.1.100",
"internet_connected": true,
"last_rotation": "2024-01-01T12:00:00",
"rotation_count": 5
}Simple health check endpoint
Rotates the modem connection (disconnect + reconnect):
{
"success": true,
"message": "Connection rotated successfully",
"initial_status": {...},
"final_status": {...},
"rotation_time": "2024-01-01T12:05:00",
"total_rotations": 6
}Edit config.json to customize settings:
{
"port": 8080,
"interface": "wwan0",
"disconnect_delay": 5,
"reconnect_timeout": 30,
"log_level": "INFO"
}- port: HTTP server port
- interface: Network interface name (common:
wwan0,ppp0,usb0) - disconnect_delay: Seconds to wait between disconnect and reconnect
- reconnect_timeout: Maximum seconds to wait for reconnection
- log_level: Logging level (DEBUG, INFO, WARNING, ERROR)
To find your modem's interface name:
# List all network interfaces
ip link show
# Check for cellular/modem interfaces
ip link show | grep -E "(wwan|ppp|usb)"
# Check NetworkManager devices
nmcli device statusCommon interface names:
wwan0- USB cellular modemsppp0- Dial-up connectionsusb0- USB tetheringeth1- Some USB ethernet adapters
# View service status
sudo systemctl status rotator.service
# Start/stop/restart
sudo systemctl start rotator.service
sudo systemctl stop rotator.service
sudo systemctl restart rotator.service
# View logs
sudo journalctl -u rotator.service -f
# Disable auto-start
sudo systemctl disable rotator.serviceTo access from other devices on your network:
-
Find Pi's IP address:
ip addr show | grep inet -
Test from remote device:
curl http://PI_IP_ADDRESS:8080/status curl -X POST http://PI_IP_ADDRESS:8080/rotate
-
Open firewall if needed:
sudo ufw allow 8080
The service needs sudo access for network commands. If you get permission errors:
# Check sudoers configuration
sudo visudo
# Should include something like:
pi ALL=(ALL) NOPASSWD: /sbin/ip, /usr/bin/nmcli, /sbin/dhclient, /usr/bin/killallIf the service can't find your modem interface:
- Check available interfaces:
ip link show - Update
interfaceinconfig.json - Restart service:
sudo systemctl restart rotator.service
If rotation fails:
- Check logs:
sudo journalctl -u rotator.service -n 50 - Test manually:
sudo nmcli device disconnect wwan0 - Verify modem is detected:
lsusb | grep -i modem
Check the logs for errors:
sudo journalctl -u rotator.service -n 20Common issues:
- Python3 not installed:
sudo apt install python3 - Port already in use: Change port in
config.json - File permissions:
chmod +x /home/pi/rotator/rotator.py
#!/bin/bash
echo "Rotating connection..."
curl -X POST http://localhost:8080/rotate
echo "Waiting for new IP..."
sleep 10
curl http://localhost:8080/status | grep ip_addressimport requests
import time
# Rotate connection
response = requests.post('http://PI_IP:8080/rotate')
if response.json()['success']:
print("Connection rotated successfully")
# Wait and check new status
time.sleep(5)
status = requests.get('http://PI_IP:8080/status').json()
print(f"New IP: {status['ip_address']}")# Add to crontab (crontab -e)
# Rotate every hour
0 * * * * curl -X POST http://localhost:8080/rotate >/dev/null 2>&1MIT License - feel free to modify and distribute.