Control a led lamp using my phone with Bluetooth on rasp4

July 29, 2024, 17:02

wille____

How can I control a led lamp connected to raspberry pi 4b using the Bluetooth on my phone?

wille____

Would HC-05 module work for this? Cause I have tried the raspberry pi's Bluetooth and it dident work to send a signal to it

thunder07337

The Pi 4 itself has Bluetooth. Did you install the OS with GUI or headless?

wille____

i tried using the bluetooth on the rasp4 and i couldent make the code work as my phone couldent pick up the rasp4 bluetooth

thunder07337

How did you try it?

wille____

i tried it at first on my laptop where i have ubuntu installed so very simillar experience but when running the bluetooth code it picked up everything but my phone

thunder07337

OK, back to the first question. Have you installed the OS with a graphical user interface or without?

wille____

GUI

thunder07337

Do you also have Ubuntu installed on the Pi? Can you connect to the cell phone on the laptop? How around, cell phone to laptop or laptop to cell phone? Or can you do both?

wille____

Yes, yes, Wdm

thunder07337

So you turn on the Bluetooth on your cell phone and Pi. Is the Pi displayed on the cell phone? Is the cell phone displayed on the Pi? Which devices are found?

wille____

Like I can see the pi and everything when I run the code tho it finds every other device than my phone

wille____

Even tho they are connected

thunder07337

Which device are you running the code on? And how is the LED connected?

wille____

Rn I have not connected the led as I just want a result from the code at first by it self, I am running the code rn from rasp4, the phone is a iPhone 13 mini (sadly)

thunder07337

OK and what exactly is supposed to happen so that you can see if it works?

wille____

print(1)

wille____

<:babyblob:1053364680200044564>

thunder07337

On Pi Konsole? It's best if you show the code. Did you follow any instructions?

wille____

Nvm got it to work thanks for the help

oops.se

Could you share the solution?

wille____

Tbh I have no idea what I did to make it work

wille____

πŸ˜‚

wille____

'''py import bluetooth def receive_messages(): # Create a Bluetooth socket and bind it to any address and a port server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM) port = bluetooth.PORT_ANY server_sock.bind(("", port)) server_sock.listen(1) # Get the port that the server socket is using port = server_sock.getsockname()[1] print(f"Waiting for connection on RFCOMM channel {port}") # Advertise this service bluetooth.advertise_service(server_sock, "SampleServer", service_id="00001101-0000-1000-8000-00805F9B34FB", service_classes=["00001101-0000-1000-8000-00805F9B34FB", bluetooth.SERIAL_PORT_CLASS], profiles=[bluetooth.SERIAL_PORT_PROFILE]) # Accept a connection client_sock, client_info = server_sock.accept() print(f"Accepted connection from {client_info}") try: while True: # Receive data from the client data = client_sock.recv(1024) if data: print(f"Received: {data}") if data.strip() == b'1': print("Hello World") except OSError: pass # Close the connections print("Disconnected") client_sock.close() server_sock.close() if __name__ == "__main__": receive_messages() '''

oops.se

python
import bluetooth

def receivemessages():
    # Create a Bluetooth socket and bind it to any address and a port
    serversock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    port = bluetooth.PORTANY
    serversock.bind(("", port))
    server_sock.listen(1)

    # Get the port that the server socket is using
    port = server_sock.getsockname()[1]
    print(f"Waiting for connection on RFCOMM channel {port}")

    # Advertise this service
    bluetooth.advertise_service(server_sock, "SampleServer",
                                service_id="00001101-0000-1000-8000-00805F9B34FB",
                                service_classes=["00001101-0000-1000-8000-00805F9B34FB", bluetooth.SERIAL_PORT_CLASS],
                                profiles=[bluetooth.SERIAL_PORT_PROFILE])

    # Accept a connection
    client_sock, client_info = server_sock.accept()
    print(f"Accepted connection from {client_info}")

    try:
        while True:
            # Receive data from the client
            data = client_sock.recv(1024)
            if data:
                print(f"Received: {data}")

                if data.strip() == b'1':
                    print("Hello World")
    except OSError:
        pass

    # Close the connections
    print("Disconnected")
    client_sock.close()
    server_sock.close()

if __name == "__main":
    receive_messages()

oops.se

Use backtick `

wille____

πŸ‘

tolek3.0

Wait so this opens a Bluetooth connection? I'm trying to learn new things by learning off source codes πŸ‘

wille____

Look up ESP32 microcontroller if u want an even better microcontroller for dealing with Bluetooth, myself will probably jump to that instead

tolek3.0

Ah okay

wille____

It has ESP-now that u can connect 2 esp devices with a range around 200 meters might be worth looking into for ya

tolek3.0

So it opens a Bluetooth connection... do you mind if I'll use your source code?

wille____

Use it

tolek3.0

Okay , thanks !

riothedev

<@421332284461809675> if i may ask, why use a pi 4 for such a simple task?

timinytim

This. The best option is a microcontroller, like an ESP32-c3 Dev board. You're running a machine, not an OS.

wille____

Look above and u can see that i mention a ESP-32

wille____

I am switching to an ESP32 like next month, I used a rasp4 cause that was what I had laying around

tolek3.0

Wait wouldn't it be possible to open thousands of Bluetooth networks?

wille____

No idea tbh

wille____

In theory probably, but u will reach a point were the device cannot handle more

tolek3.0

Well when I'm back from vacation, ima try out