A Django web application that controls an ESP32-based relay system through AWS IoT Core MQTT messaging. This system provides a secure and reliable way to remotely control relay operations through a web interface.
Web Interface (Django) <-> AWS IoT Core <-> ESP32 Relay
[MQTT Publish] [MQTT Subscribe]
- Secure relay control through AWS IoT Core
- Real-time MQTT messaging
- Web-based control interface
- SSL/TLS security with certificate-based authentication
- Error handling and status feedback
- Secure certificate management
- Python 3.8+
- Django 4.2+
- AWS IoT Core account
- ESP32 device with relay module
- AWS IoT certificates
- paho-mqtt library
- Clone the repository
git clone [your-repository-url]
cd esp32-relay-control
- Create and activate virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
- Install dependencies
pip install -r requirements.txt
- Set up environment variables
cp .env.example .env
Edit .env
with your AWS IoT Core settings:
AWS_IOT_ENDPOINT=your-endpoint.iot.region.amazonaws.com
AWS_IOT_PORT=Your Port
AWS_IOT_TOPIC= Your Topic
AWS_IOT_CLIENT_ID= Your Client ID
DJANGO_SECRET_KEY=your-secret-key
-
Create an AWS IoT Core Thing
- Go to AWS IoT Core Console
- Create a Thing for your ESP32
- Create and download certificates
-
Certificate Setup
- Create a
certificates
directory in project root - Add your AWS IoT certificates:
- Root CA certificate (
AWS_ROOT_CA_FILENAME
) - Device certificate (
AWS_DEVICE_CERT_FILENAME
) - Private key (
AWS_PRIVATE_KEY_FILENAME
)
- Root CA certificate (
- Update certificate filenames in
.env
- Create a
-
Configure IoT Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iot:Connect",
"iot:Publish",
"iot:Subscribe",
"iot:Receive"
],
"Resource": [
"arn:aws:iot:your-region:your-account:client/${iot:Connection.Thing.ThingName}",
"arn:aws:iot:your-region:your-account:topic/relaycontrol"
]
}
]
}
- Apply migrations
python manage.py migrate
- Start the development server
python manage.py runserver
- Access the control panel at
http://localhost:8000
- Keep certificates secure and never commit them to version control
- Use proper file permissions for certificate files
- Regularly rotate certificates
- Monitor AWS IoT Core logs for unauthorized access attempts
- Use HTTPS in production
- Implement user authentication for the web interface
The system uses the following MQTT topic and message format
esp32-relay-control/
├── certificates/ # AWS IoT certificates (not in version control)
├── relay_control/ # Django app
│ ├── views.py # Main control logic
│ └── urls.py # URL routing
├── templates/ # HTML templates
├── manage.py # Django management script
└── requirements.txt # Python dependencies
-
RelayController Class
- Handles MQTT communication
- Manages SSL/TLS certificates
- Processes relay commands
-
Web Interface
- Simple control panel
- Status feedback
- Error handling
MIT
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
- AWS IoT Core documentation
- Django documentation
- Paho MQTT library