This project enables control of a relay module connected to an ESP32 using Bluetooth Low Energy (BLE) via a Flutter mobile application. The app communicates with the ESP32 to toggle the relay between ON and OFF states.
This repository contains the following key files:
esp32/esp32_relay_control.ino
: Arduino code to run on the ESP32.flutter_app/main.dart
: Flutter code for the app's UI and BLE logic.flutter_app/pubspec.yaml
: Configuration file listing dependencies for the Flutter project.
Note:
You need to create a new Flutter project and replace the default files with the provided code.
-
Hardware Configuration:
Connect your relay module to GPIO pin 5 of the ESP32. -
Upload Code:
- Install the ESP32 board in the Arduino IDE if you haven't already.
- Use the
esp32_relay_control.ino
code provided in this repository. - Ensure these libraries are available:
BLEDevice.h
BLEServer.h
- Upload the code to your ESP32.
-
Create a New Flutter Project:
flutter create smartish_lamp
-
Replace Files:
- Replace
lib/main.dart
with the providedmain.dart
. - Replace the
pubspec.yaml
file with the provided content.
- Replace
-
Install Dependencies:
flutter pub get
-
Grant Permissions: The app requires location permission for BLE scanning. The
permission_handler
package prompts for this when the app starts.
-
Build and Run: Make sure Bluetooth is enabled on your phone. Use the following command to run the app:
flutter run
-
Relay Control:
- The app automatically scans for ESP32 devices named "ESP32 BLE Relay Control".
- Tap the Button: Turns the relay ON momentarily.
- Hold the Button: Keeps the relay ON while pressed. The relay turns OFF upon release.
Ensure the following UUIDs are consistent in both the ESP32 and Flutter code:
- Service UUID:
12345678-1234-5678-1234-56789abcdef0
- Characteristic UUID:
abcd1234-5678-1234-5678-12345678abcd
Add these dependencies in your pubspec.yaml
:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.8
permission_handler: ^11.3.1
flutter_blue_plus: ^1.32.12
provider: ^6.1.2 # Optional, useful for future app extensions
This project demonstrates the use of Bluetooth Low Energy (BLE) to create a simple relay control system using an ESP32 and a Flutter mobile app. You can further expand this project by integrating state management using provider
or adding more BLE-controlled devices.