A simple IoT project that demonstrates MQTT communication between a web interface and a simulated IoT device. Control a virtual light bulb from your browser and see real-time updates in a Python GUI.
This project consists of two main components:
- Web Interface (index.html): A browser-based UI with ON/OFF buttons to control a virtual light
- Simulated IoT Device (light_simulation.py): A Python script that simulates an IoT device (like an ESP8266)
Both components communicate via MQTT (Message Queuing Telemetry Transport), a lightweight messaging protocol designed for IoT applications.
- Modern, responsive web interface with dark mode support
- Real-time communication between web browser and Python script
- Visual feedback with animated light bulb
- Connection status indicators
- Detailed error handling and reporting
- Python GUI for visualizing light status
Before running this project, you need:
-
Python 3.6+ with the following packages:
paho-mqtt(MQTT client for Python)pillow(Python Imaging Library - PIL fork)tkinter(Usually comes with Python, but may need separate installation on some systems)
-
Web Browser (Chrome, Firefox, Edge, etc.)
-
HiveMQ Account (Optional - already configured with test account)
- The project is pre-configured with test credentials
- For your own setup, create an account at HiveMQ Cloud
-
Clone the repository:
git clone https://github.com/yourusername/mqtt-light-control.git cd mqtt-light-control -
Install Python dependencies:
pip install paho-mqtt pillow -
Install tkinter (if not already installed):
- Windows: Typically included with Python installation
- macOS:
brew install python-tk - Ubuntu/Debian:
sudo apt-get install python3-tk - Fedora:
sudo dnf install python3-tkinter
- Open a terminal/command prompt
- Navigate to the project directory
- Run the Python script:
python light_simulation.py - A GUI window will open showing the light status and connection information
-
Open the
index.htmlfile in your web browser:- Double-click the file, or
- Drag and drop it into your browser, or
- Use a local server if you have one
-
You should see the light control interface with:
- A light bulb visualization
- ON and OFF buttons
- Connection status indicator
-
Click the "Turn ON" button in the web interface
- The light bulb in the browser will turn on
- The Python GUI will show: "💡 Light is TURNED ON"
-
Click the "Turn OFF" button in the web interface
- The light bulb in the browser will turn off
- The Python GUI will show: "💡 Light is TURNED OFF"
The web interface uses:
- MQTT.js to connect to the HiveMQ broker via WebSockets
- Bootstrap and custom CSS for styling
- JavaScript for interactivity and MQTT communication
When you click a button:
- The browser publishes an "ON" or "OFF" message to the topic
/charles/light_control - The interface updates to show the current state
The Python script:
- Connects to the same HiveMQ broker using the paho-mqtt library
- Subscribes to the
/charles/light_controltopic - Displays the light status in a GUI window
- Updates the GUI in real-time when it receives "ON" or "OFF" commands
If you see "Authorization failed" or connection errors:
-
Check Internet Connection:
- Ensure you have a stable internet connection
-
Firewall Settings:
- Make sure your firewall allows WebSocket connections on port 8884
-
Browser Console:
- Open your browser's developer console (F12) to see detailed error messages
If the Python script fails to connect:
-
Check Dependencies:
- Ensure all dependencies are installed:
pip install paho-mqtt pillow - Verify tkinter is installed by running
python -m tkinterin your terminal
- Ensure all dependencies are installed:
-
TLS/SSL Issues:
- Make sure your Python environment supports TLS connections
-
GUI Issues:
- If the GUI doesn't display properly, try using the "Reconnect" button
- Check the terminal for any error messages
To use your own MQTT broker:
-
Edit
light_simulation.py:- Update the
BROKER,PORT,USERNAME, andPASSWORDconstants
- Update the
-
Edit
index.html:- Find the MQTT connection section and update the connection URL and credentials
To use a different topic:
-
Edit
light_simulation.py:- Update the
TOPICconstant
- Update the
-
Edit
index.html:- Update the
topicvariable - Update the topic check in the message handler
- Update the
Feel free to clone and use this project.
- HiveMQ for providing a free MQTT broker
- MQTT.js for the browser-based MQTT client
- Paho for the Python MQTT client
- Tkinter and Pillow for the Python GUI

