A Python bot that uses Discord to control GPIO pins on a Raspberry Pi Zero W. The bot only responds to messages from a specific Discord channel.
- Control GPIO pins remotely via Discord
- Only responds to messages from a specified Discord channel
- Command-based interface to control devices
- Timed operations (e.g., turn on a device for 1 hour)
- Automatic scheduling: Set devices to turn on/off at specific times
- Emulation mode for testing on non-RPi devices
- Configurable device-to-pin mapping via .env file
- Reasonable unit test coverage
- Python 3.11+
- Raspberry Pi Zero W (or any Raspberry Pi)
- Discord bot token
- Discord server with a channel for the bot
- Clone this repository:
- Install the required packages:
- Create a .env file with your configuration:
This bot uses Discord's bot API to communicate. Follow these steps to set up your Discord bot:
- Create a Discord Application at the Discord Developer Portal
- Create a bot user in your application
- Copy the bot token and add it to your .env file as DISCORD_BOT_TOKEN
- Invite the bot to your Discord server with appropriate permissions:
- Send Messages
- Read Message History
- Use Slash Commands
- Get your Discord channel ID (enable Developer Mode in Discord, right-click channel, Copy ID)
- Update your .env file with the channel ID
Send these commands from the Discord channel to control your devices:
- status - Show the status of all devices
- on <device> - Turn on a specific device
- off <device> - Turn off a specific device
- on <device> <seconds> - Turn on a device for a specified time
- off <device> <seconds> - Turn off a device for a specified time
- on all - Turn on all devices
- off all - Turn off all devices
- schedules - Show all configured schedules and next runs
- schedule <device> <on|off> <HH:MM> - Add a new schedule
- unschedule <device> <on|off> <HH:MM> - Remove a schedule
- Send any unrecognized command to get help
For development and testing on non-RPi devices, set OPERATION_MODE=emulation in your .env file. In this mode, GPIO operations will be simulated and printed to the console.
The project includes comprehensive unit tests. To run the tests:
The test suite covers:
- GPIO interface and hardware abstraction
- Device control logic and timing
- Schedule configuration and management
- Discord bot message handling
- Command parsing and validation
- Error handling and edge cases
Tests use mock objects and dependency injection to ensure they can run without hardware dependencies or external services.
WaterBot includes comprehensive CI/CD pipelines for automated testing and deployment:
- Automated testing on every commit and merge request
- Multi-Python version testing (3.8-3.11)
- Code quality checks (linting, formatting, type checking)
- Security vulnerability scanning
- Docker image building and testing
- Similar comprehensive pipeline for GitHub repositories
- Automatic PyPI publishing on releases
- Codecov integration for coverage reporting
See CI-CD.md for detailed pipeline documentation.
To run the bot as a systemd service on your Raspberry Pi, follow these comprehensive steps:
- Ensure you have a dedicated user for the service (recommended for security):
- Add the service user to the gpio group (for GPIO access):
- Install the bot in a system location (recommended):
- Install Python dependencies:
- Setup configuration:
- Configure Discord bot credentials for the service user:
- Create the systemd service file:
- Add the service configuration:
Note: If using the pi user instead of a dedicated user, change User=pi and Group=pi in the service file, and adjust paths accordingly (e.g., /home/pi/waterbot).
- Reload systemd and enable the service:
- Start the service:
- Check service status:
- View service logs:
Common issues and solutions:
-
Permission denied errors:
- Ensure the service user is in the gpio group
- Check file ownership and permissions for the bot directory
- Verify the .env file is accessible to the service user
-
Discord bot not working:
- Ensure the Discord bot token is valid and properly configured
- Check that the bot has permissions in the Discord channel
- Verify the Discord channel ID is correct
-
Module import errors:
- Ensure all dependencies are installed in the correct Python environment
- Check that the PYTHONPATH includes the waterbot directory
- Verify the virtual environment path (if used) is correct
-
Service won't start:
- Check the service logs: sudo journalctl -u waterbot.service
- Verify all file paths in the service configuration
- Test the bot manually first: python3 -m waterbot.bot
-
GPIO access issues:
- Ensure the service user is in the gpio group: groups waterbot-service
- Check that GPIO pins are not being used by other processes
- Verify the device-to-pin mapping in your .env file
.png)



