top of page
Search

Home automation Using Raspberry pi 4 and Bluetooth controller

After looking into lot of projects on internet I came up with an idea of controlling Home appliances using Bluetooth Controller.


So basically this project has 2 main parts:


1)Hardware:Raspberry Pi 4 (or below), any cheap bluetooth controller i am using vr controller few leds and jumper wires.


2)Program: program is written in python.



before proceeding further we have to update the libraries of python so type the following commands



After connecting the bluetooth Controller to Raspberry Pi (connect by clicking on Bluetooth button on right corner of the screen) We have to identify the "event Number" that can be found by typing following comands on Termial


As it is showing me two events to identify which is my controller we have to write another comand i.e,


In the above image when i typed " cat /dev/input/event0 " (Then hit Enter obviously) on the terminal then pressed button on Controller nothing happend , after that i typed " cat /dev/input/event1 " (Hit enter) then pressed button on the controller then some characters appeared on the screen.


NOTE:to Exit controller input mode press Ctrl+z

Here controller Event is found i,e "event1"


In this part first we have to Read the the values of bluetooth controller before we use it.



the above program is helpful in finding the values of bluetooth controller when you run the program after updating all the libraries and press the button on the controller the respective Number will be printend on the screen as shown.


as you can see when i pressed Key_w ---> respective number 17 is printed(in yellow)

Now find all the code numebrs for all the buttons of the controller.



After finding the numbers use then in a program to control any thing.

--------------------------------------------------------------------------------------------------------------------

I wrote a simple program to control 4 leds using Bluetooth controller

----------------------------------------------------------------------->>

#import evdev

from evdev import InputDevice, categorize, ecodes

import RPi.GPIO as GPIO

import time


GPIO.setmode(GPIO.BOARD)

GPIO.setup(3, GPIO.OUT)

GPIO.setup(5, GPIO.OUT)

GPIO.setup(7, GPIO.OUT)

GPIO.setup(8, GPIO.OUT)

# creates object 'gamepad' to store the data

# you can call it whatever you like

gamepad = InputDevice('/dev/input/event1')


# button code variables (change to suit your device)

aBtn = 22

bBtn = 35

cBtn = 21

dBtn = 36


up = 17

down = 45

left = 30

right = 32


# start = 24

# select = 49


# lTrig = 37

# rTrig = 50


# prints out device info at start

print(gamepad)


# loop and filter by event code and print the mapped label

for event in gamepad.read_loop():

if event.type == ecodes.EV_KEY:

if event.value == 1:

if event.code == cBtn:

print("C")

GPIO.output(3,True)

GPIO.output(5,False)

GPIO.output(7,True)

GPIO.output(8,False)

elif event.code == bBtn:

print("B")

GPIO.output(3,False)

GPIO.output(5,True)

GPIO.output(7,True)

GPIO.output(8,False)


elif event.code == aBtn:

print("A")

GPIO.output(3,True)

GPIO.output(5,False)

GPIO.output(7,False)

GPIO.output(8,True)

elif event.code == dBtn:

print("D")

GPIO.output(3,False)

GPIO.output(5,True)

GPIO.output(7,False)

GPIO.output(8,True)


elif event.code == up:

print("up")

elif event.code == down:

print("down")

elif event.code == left:

print("left")

elif event.code == right:

print("right")

if event.value == 0:

GPIO.output(3,False)

GPIO.output(5,False)

GPIO.output(7,False)

GPIO.output(8,False)





# elif event.code == start:

# print("start")

# elif event.code == select:

# print("select")


# elif event.code == lTrig:

# print("left bumper")

# elif event.code == rTrig:

# print("right bumper")

---------------------------------------------------------------------------------------------------------------------------

Using above code You can control any home appliances by connecting Relay Modules.

60 views0 comments

Recent Posts

See All

コメント


Post: Blog2_Post
bottom of page