Skip to content

About

Raspberry Pi 4 Embedded Linux IoT project for LED control using C, GPIO, MQTT, Flask and a web interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

LED Control Using IoT

A Raspberry Pi 4 based Embedded Linux IoT project for controlling an LED through a physical push button and a mobile-friendly web application.

The project contains two source-code components:

  • LED_Control_MQTT — Embedded Linux C application running on Raspberry Pi 4. It controls GPIO17, monitors GPIO27, and communicates using MQTT.
  • LED_Control_Web — Python Flask web application with HTML, Bootstrap 5, JavaScript, and Paho MQTT for user-friendly LED control.

MQTT is the communication layer between the hardware-side C application and the web application.

Project Structure

LED_Control_Using_IoT/
├── README.md
├── .gitignore
│
├── LED_Control_MQTT/
│   ├── led_control_mqtt.c
│   └── README.md
│
└── LED_Control_Web/
    ├── app.py
    ├── mqtt_client.py
    ├── requirements.txt
    ├── templates/
    │   └── index.html
    ├── static/
    │   ├── css/
    │   │   └── style.css
    │   └── js/
    │       └── app.js
    └── README.md

System Architecture

                         Local Network
                              │
                     ┌────────▼────────┐
                     │ Phone / Laptop  │
                     │                 │
                     │ HTML            │
                     │ Bootstrap 5     │
                     │ JavaScript      │
                     └────────┬────────┘
                              │ HTTP
                              ▼
                     ┌─────────────────┐
                     │   Flask Web App │
                     │                 │
                     │ Python          │
                     │ Paho MQTT       │
                     └────────┬────────┘
                              │ MQTT
                              ▼
                     ┌─────────────────┐
                     │    Mosquitto    │
                     │   MQTT Broker   │
                     └────────┬────────┘
                              │ MQTT
                              ▼
                     ┌─────────────────┐
                     │ Raspberry Pi 4  │
                     │                 │
                     │ C Application   │
                     │ libmosquitto    │
                     │ libgpiod        │
                     └───────┬─────────┘
                             │
                       ┌─────┴─────┐
                       │           │
                    GPIO17       GPIO27
                       │           │
                       ▼           ▼
                      LED       Push Button

Hardware

Component Raspberry Pi GPIO
LED GPIO17
Push Button GPIO27

The push button uses an input with an internal pull-up. The LED is controlled as a GPIO output.

Software Stack

Embedded Linux Side

  • Raspberry Pi 4
  • Debian Linux
  • C
  • AArch64 GNU cross compiler
  • libgpiod
  • libmosquitto
  • Mosquitto MQTT broker

Web Application Side

  • Python 3
  • Flask
  • Paho MQTT
  • HTML5
  • Bootstrap 5
  • JavaScript

MQTT Topics

LED Command

rpi4/led/set

Messages:

ON
OFF

The web application publishes commands to this topic.

LED Status

rpi4/led/status

Messages:

ON
OFF

The C application publishes the actual LED state to this topic.

Web-to-Hardware Flow

Web Button
    ↓
Flask
    ↓
Paho MQTT
    ↓
rpi4/led/set
    ↓
Mosquitto
    ↓
C Application
    ↓
libgpiod
    ↓
GPIO17
    ↓
LED

Hardware-to-Web Flow

Physical Push Button
        ↓
      GPIO27
        ↓
   C Application
        ↓
rpi4/led/status
        ↓
    Mosquitto
        ↓
 Python MQTT Client
        ↓
      Flask
        ↓
    Web Interface

This provides two-way IoT communication: the web application controls the LED, while the hardware-side application publishes the actual LED state back to the web application.

Local Network Operation

The current implementation can operate without Internet access when all components are reachable on the same local network.

Internet access is only required if the system is later extended to use a cloud service or remote MQTT broker.

Running the Embedded Linux Application

Cross-compile led_control_mqtt.c for AArch64 Linux and copy the resulting executable to the Raspberry Pi.

Then:

chmod +x LED_Control_MQTT
./LED_Control_MQTT

If GPIO permissions require elevated privileges:

sudo ./LED_Control_MQTT

Verify Mosquitto:

sudo systemctl status mosquitto

Running the Web Application

From LED_Control_Web:

python -m venv .venv

Windows:

.venv\Scripts\activate

Linux:

source .venv/bin/activate

Install dependencies:

pip install -r requirements.txt

Start Flask:

python app.py

For local testing:

http://127.0.0.1:5000

To access the application from a phone on the same LAN:

http://<SERVER-IP>:5000

Replace <SERVER-IP> with the IP address of the computer running Flask.

Testing

Web Control

  1. Start Mosquitto on the Raspberry Pi.
  2. Start the C application.
  3. Start the Flask web application.
  4. Open the web interface.
  5. Press ON and verify that the LED turns ON.
  6. Press OFF and verify that the LED turns OFF.

Physical Button

  1. Keep the C application running.
  2. Press the physical push button.
  3. Verify the LED state changes.
  4. Verify the web interface reflects the LED status.

Security

Do not commit passwords, private credentials, MQTT authentication data, or other secrets.

Do not put machine-specific private IP addresses into public documentation. Use placeholders such as <RASPBERRY-PI-IP> or <SERVER-IP>.

Future Improvements

  • Cloud MQTT deployment
  • Remote Internet-based control
  • MQTT authentication
  • TLS/SSL
  • HTTPS
  • User authentication
  • Multiple GPIO devices
  • Sensor integration
  • IoT dashboard
  • systemd service deployment
  • Docker deployment

Project Goal

This project demonstrates an end-to-end Embedded Linux IoT workflow:

Embedded Linux
      +
C
      +
GPIO
      +
MQTT
      +
Python
      +
Flask
      +
Web UI
      =
IoT LED Control System

About

Raspberry Pi 4 Embedded Linux IoT project for LED control using C, GPIO, MQTT, Flask and a web interface.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages