A robust Python application that drives an analog clock mechanism using a Raspberry Pi, RTC module, and NTP synchronization. Features a web interface for remote control and monitoring.
- Accurate Timekeeping: NTP synchronization with configurable servers
- Advanced Pulsing: Region-specific reverse logic and precise pulse control
- Hardware Control: GPIO-based clock mechanism control (forward, reverse, fast-forward)
- Web Interface: Flask-based dashboard for remote monitoring and control
- Production Ready: Proper logging, security hardening, error handling
- Fault Tolerance: Graceful error handling and recovery mechanisms
- Self-Contained: All files in one directory, no configuration files needed
- Raspberry Pi (3B+ or newer recommended)
- Modified ticking clock mechanism
- DS3231 RTC module
- Adafruit I2C Non-Volatile FRAM Breakout (optional)
- Dupont wires
- OS: Raspberry Pi OS (Bullseye or newer)
- Python: 3.11+
- Hardware: GPIO access, I2C support
- Network: Internet access for NTP synchronization
# Clone the repository
git clone https://github.com/yourusername/piclock.git
cd piclock
# Install dependencies
pip3 install -r requirements.txt
# Run the application
python3 piclock.py
## Service Installation
To run PiClock as a systemd service (recommended for production):
```bash
# Navigate to your PiClock directory
cd /path/to/your/piclock
# Install as a service (requires sudo)
sudo ./install_service.sh
# Start the service
sudo systemctl start piclock
# Check service status
sudo systemctl status piclock
# View logs
sudo journalctl -u piclock -fNote: The installation script automatically detects your current directory and user, so you can install PiClock from any location on your system.
# Start the service
sudo systemctl start piclock
# Stop the service
sudo systemctl stop piclock
# Restart the service
sudo systemctl restart piclock
# Enable auto-start on boot
sudo systemctl enable piclock
# Disable auto-start on boot
sudo systemctl disable piclock
# View real-time logs
sudo journalctl -u piclock -f
# View recent logs
sudo journalctl -u piclock --since "1 hour ago"# Remove the service
sudo ./uninstall_service.sh
## Configuration
The application uses sensible defaults and doesn't require a configuration file:
- **GPIO Pins**: 12 and 13 for clock control
- **NTP Server**: time.nist.gov
- **Sync Interval**: 5 minutes
- **Web Interface**: Port 5000 on all interfaces
To modify these settings, edit the values in `piclock.py`:
```python
# GPIO Configuration
self.tick_pin1 = 12
self.tick_pin2 = 13
# NTP Configuration
self.ntp_server = "time.nist.gov"
self.ntp_sync_interval = 300 # 5 minutes
# Flask Configuration
self.flask_host = "0.0.0.0"
self.flask_port = 5000
The application includes sophisticated pulsing logic for optimal clock control:
norm_tick_ms: Length of forward tick pulse (31ms default)norm_tick_on_us: Duty cycle of forward tick pulse (60μs out of 100μs)
fwd_tick_ms: Length of fast-forward tick pulse (32ms default)fwd_tick_on_us: Duty cycle of fast-forward tick pulse (60μs)fwd_count_mask: Speed control (1 = 4 ticks/sec default)fwd_speedup: Speed multiplier (4x default)
Region A (seconds 35-55):
rev_ticka_t1_ms: Short pulse length (10ms)rev_ticka_t2_ms: Delay before long pulse (7ms)rev_ticka_t3_ms: Long pulse length (28ms)rev_ticka_on_us: Duty cycle (90μs)
Region B (other seconds):
rev_tickb_t1_ms: Short pulse length (10ms)rev_tickb_t2_ms: Delay before long pulse (7ms)rev_tickb_t3_ms: Long pulse length (28ms)rev_tickb_on_us: Duty cycle (82μs)
diff_threshold_ss: Seconds tolerance (30s default)diff_threshold_mm: Minutes threshold (0 default)diff_threshold_hh: Hours threshold (6 default)
These parameters can be configured via the API or by editing the values in piclock.py.
Access the web interface at http://your-pi-ip:5000
- Dashboard: Real-time clock status and time synchronization
- Configuration: Set clock time and NTP settings
- Controls: Pause/resume clock operation
GET /api/current_time- Get current RTC timeGET /api/clock_time- Get current clock positionPOST /api/set_clock_time- Set clock timeGET /api/clock_status- Get clock operation statusPOST /api/pause_clock- Pause clockPOST /api/resume_clock- Resume clockGET /api/pulsing_config- Get pulsing configurationPOST /api/pulsing_config- Update pulsing configuration
# Run directly
python3 piclock.py
# Run in background
nohup python3 piclock.py > piclock.log 2>&1 &
# Run with screen
screen -S piclock
python3 piclock.py
# Press Ctrl+A, then D to detach-
Network Security:
- Use a reverse proxy (nginx) for HTTPS
- Configure firewall rules
- Limit access to trusted networks
-
Application Security:
- Run as dedicated user if needed
- Validate all input parameters
- Proper error handling
-
Hardware Security:
- Review and restrict GPIO permissions
- Monitor system logs
- Regular security updates
- Change default web interface port
- Configure firewall rules
- Set up HTTPS with reverse proxy
- Review and restrict GPIO permissions
- Monitor system logs
- Regular security updates
-
GPIO Permission Errors:
sudo usermod -a -G gpio $USER sudo chmod 666 /dev/gpiomem -
I2C Not Detected:
sudo raspi-config # Enable I2C sudo i2cdetect -y 1 -
Import Errors:
pip3 install -r requirements.txt
-
NTP Sync Issues:
- Check internet connectivity
- Verify NTP server availability
- Review firewall settings
For development/testing, run with verbose logging:
python3 piclock.py# View application logs
tail -f piclock.log
# Check system logs
dmesg | grep -i i2c
dmesg | grep -i gpiopiclock/
├── piclock.py # Main application
├── requirements.txt # Python dependencies
├── templates/ # HTML templates
│ ├── index.html
│ └── config.html
└── static/ # Static assets
├── style.css
└── scripts.js
# Install development dependencies
pip3 install -r requirements.txt
# Run tests (if available)
python3 -m pytest
# Run application
python3 piclock.py- Resource Management: Efficient memory usage
- Efficient Polling: Optimized API calls and caching
- Thread Safety: Proper synchronization mechanisms
- Error Handling: Graceful degradation and recovery
# Monitor resource usage
htop
iotop
nethogs
# Check application health
ps aux | grep piclock
netstat -tlnp | grep 5000- Clone the repository
- Install dependencies:
pip3 install -r requirements.txt - Run:
python3 piclock.py - Access web interface at
http://your-pi-ip:5000
- Copy files to target directory
- Install dependencies:
pip3 install -r requirements.txt - Set up process manager (systemd, supervisor, etc.)
- Configure firewall and security
- Start application
For production use, consider using a process manager:
# Using systemd (create service file)
sudo systemctl start piclock
# Using supervisor
supervisorctl start piclock
# Using screen
screen -S piclock -d -m python3 piclock.pyThis project is based on the work of victor-chew's espclock project. All credit for research goes to him.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
For issues and questions:
- Check the troubleshooting section
- Review application logs
- Open an issue on GitHub
- Check the original espclock documentation