How to make a night light with Bluetooth control and three colors?
How to make a night light with Bluetooth control and three colors?

How to make a night light with Bluetooth control and three colors?

Today, devices controlled by Bluetooth have proliferated. Electronic devices with features that make our lives easier are becoming more preferred thanks to their bluetooth features.
So, today we will make a night lamp that we can use in our daily life. This night light will be tricolor and will be controlled by the HC-05 bluetooth module. This night light, which consists of very economical parts, is also very easy to make. The physical design of the night light can be viewed in the video below.

The project consists of three parts:
1. Microcontroller part
2. Android program
3. Chandelier design


We provide lighting with two or three LEDs that we connect to the microcontroller.
We made the Android program using the Flutter-Dart programming language.
The chandelier part consists of paper cups, straws and glossy gelatin paper.

 

Below are the Flutter-Dart codes we wrote for the Android phone:

import 'dart:async';

//import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';

void main() => runApp(const MyApp());

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  List<BluetoothDevice> _devices = [];
  late BluetoothConnection connection;
  String adr="00:21:07:00:50:69"; // my bluetooth device MAC Adres

  @override
  void initState() {
    super.initState();
    _loadDevices();
  }

  Future<void> _loadDevices() async {
    List<BluetoothDevice> devices = await FlutterBluetoothSerial.instance.getBondedDevices();

    setState(() {   _devices = devices;    });
  }
//----------------------------
  Future<void> sendData(String data)  async {

    data = data.trim();
    try {
      List<int> list = data.codeUnits;
      Uint8List bytes = Uint8List.fromList(list);
      connection.output.add(bytes);
      await connection.output.allSent;
      if (kDebugMode) {
        // print('Data sent successfully');
      }
    } catch (e) {
      //print(e.toString());
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text("Bluetooth Night LAMP Control"),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text("MAC Adress: 00:21:07:00:50:69"),

                ElevatedButton(child:Text("Connect"),onPressed: () {
                  connect(adr);   },),
                SizedBox(height: 30.0,),
                ElevatedButton(child:Text(" YELLOW_ON "),onPressed: () {
                  sendData("yellow_on");  },),
                SizedBox(height: 10.0,),
                ElevatedButton(child:Text("YELLOW_OFF"),onPressed: () {
                  sendData("yellow_off"); },),
                SizedBox(height: 10.0,),

                ElevatedButton(child:Text(" RED_ON "),onPressed: () {
                  sendData("red_on");  },),
                SizedBox(height: 10.0,),
                ElevatedButton(child:Text("RED_OFF"),onPressed: () {
                  sendData("red_off"); },),
                SizedBox(height: 10.0,),

                ElevatedButton(child:Text(" BLUE_ON "),onPressed: () {
                  sendData("blue_on");  },),
                SizedBox(height: 10.0,),
                ElevatedButton(child:Text("BLUE_OFF"),onPressed: () {
                  sendData("blue_off"); },),
                SizedBox(height: 10.0,),

              ],
            ),
          ),


        )

    );
  }

  Future connect(String address) async {
    try {
      connection = await BluetoothConnection.toAddress(address);
      sendData('111');
      //durum="Connected to the device";
      connection.input!.listen((Uint8List data) {
        //Data entry point
        // durum=ascii.decode(data);
      });

    } catch (exception) {
      // durum="Cannot connect, exception occured";
    }
  }

// --------------**************data gonder
//Future send(Uint8List data) async {
//connection.output.add(data);
//await connection.output.allSent;
}
//------------*********** data gonder end

 

Below are the microcontroller codes:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

// YELLOW-->pin 7   RED--->pin 6     BLUE--->pin 5    ====> digital pins
String rec_data="off";

void setup() {
   pinMode(7, OUTPUT);
   pinMode(6, OUTPUT);
   pinMode(5, OUTPUT);
   
  Serial.begin(9600);
  mySerial.begin(9600);   // BlueTooth Data baud,set the data rate for the SoftwareSerial port
  
  digitalWrite(7,LOW);
  digitalWrite(6, LOW);
  digitalWrite(5, LOW);
}

void loop() { // run over and over
  if (mySerial.available()) {
    
    rec_data=mySerial.readString();
   
    if(rec_data=="yellow_on"){digitalWrite(7, HIGH);}
    if(rec_data=="yellow_off"){digitalWrite(7, LOW);}

    if(rec_data=="red_on"){digitalWrite(6, HIGH);}
    if(rec_data=="red_off"){digitalWrite(6, LOW);}

    if(rec_data=="blue_on"){digitalWrite(5, HIGH);}
    if(rec_data=="blue_off"){digitalWrite(5, LOW);}

    
    
    Serial.println(rec_data);
    delay(100);
  }
  
}