Announcement

Collapse
No announcement yet.

Bluetooth remote

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Bluetooth remote

    Bluetooth remote control from Aliexpress
    1. Orangepi zero
    2. Bluetooth reciever Orico
    3. Bluetooth remote button

    for placement in the shower, the button was waterproofing with auto sealant.



    files with updated code and service:


    thanks to
    Here show how to read Bluetooth Remote Shutter using Python on Raspberry Pi. My testing unit named "AB Shutter3", with two buttons marked...


    Code:
    from evdev import InputDevice, categorize, ecodes
    import requests
    import time
    
    SERVER = 'http://192.168.1.56:9000/jsonrpc.js'
    PLAYER = '2e:c5:a3:c9:da:f2'
    
    PLAY = {"method": "slim.request", "params":[PLAYER, ["pause"]]}
    VOLUP = {"method": "slim.request", "params":[ PLAYER , ["mixer", "volume", "+5" ]]}
    VOLDN = {"method": "slim.request", "params":[ PLAYER , ["mixer", "volume", "-5" ]]}
    NEXT = {"method": "slim.request", "params":[ PLAYER ,  ["playlist","index","+1"]]}
    PREV = {"method": "slim.request", "params":[ PLAYER ,  ["playlist","index","-1"]]}
    
    KEY_VOLUMEUP = 115
    KEY_VOLUMEDN = 114
    KEY_PLAYPAUSE = 164
    KEY_NEXT = 163
    KEY_PREV = 165
    
    def getkey():
        print('getkey started')
        remote = InputDevice('/dev/input/event2')
        print(remote)    
        for event in remote.read_loop():
            if event.type == ecodes.EV_KEY:
                if event.value == 1:        
                    print(categorize(event))
    
                    if event.code == KEY_VOLUMEUP:
                       print('volup Pressed')
                       r = requests.post(SERVER, json=VOLUP)
    
                    if event.code == KEY_VOLUMEDN:
                        print('voldn Pressed')
                        r = requests.post(SERVER, json=VOLDN)
                        
                    if event.code == KEY_PLAYPAUSE:
                        print('play Pressed')
                        r = requests.post(SERVER, json=PLAY)
    
                    if event.code == KEY_NEXT:
                        print('next Pressed')
                        r = requests.post(SERVER, json=NEXT)
    
                    if event.code == KEY_PREV:
                        print('prev Pressed')
                        r = requests.post(SERVER, json=PREV)
    
    def main():
        while True:
            print('start try')
            try:
                remote = InputDevice('/dev/input/event2')
                print(remote)
                print('start get key')
                getkey()
            except OSError as err:
                print("OS error: {0}".format(err))
                print('no bt connect')
            time.sleep(5)
    
    main()
    installation:
    Code:
    sudo apt-get update
    
    sudo apt-get install bluetooth
    bluetoothctl
    sudo apt install input-utils
    lsinput
    
    sudo apt-get install python3
    sudo apt install python3-pip
    sudo pip3 install evdev
    
    (sudo apt install python3-dev)
    (sudo apt-get install libzbar-dev libzbar0)
    Configuration file btremote.conf
    Code:
    {
    "volstep": "5", 
    "btdevice": "/dev/input/event2", 
    "player": "bb:bb:6c:b8:26:52", 
    "server": "http://192.168.1.56:9000/jsonrpc.js"
    }
    my setup:
    Behringer MS-40, usb dac, OrangePi Zero, LMS, Squeezelite, lan, livingroom
    JBL Playlist black, wifi 5, Chromecast, bedroom
    JBL Playlist white, wifi 5, Chromecast, veranda
    GGMM E2, wifi 2,4, DLNA, bathroom
    BBK BTA6000, DIY built-in OrangePi Zero, wifi 2,4, Squeeze lite, portable
    controls: SqueezeCtrl, iPeng on tablets/phones, tvbox remote G7V, Bluetooth media button.
    Attached Files
    Last edited by Errik128; 2022-06-04, 11:43.

    #2
    Nice and simple.

    If you put "code" tags around your code it will look better - plus, given that this is in Python then the tabs/spaces at the start of line are crucial for anyone trying to copy/paste.
    Might also add that it is python (could give an example file name) and how you start running it - for the people who are less confident in getting things up and running.
    Paul Webster
    Author of "Now Playing" plugins covering Radio France (FIP etc), PlanetRadio (Bauer - Kiss, Absolute, Scala, JazzFM etc), KCRW, ABC Australia and CBC/Radio-Canada
    and, via the extra "Radio Now Playing" plugin lots more - see https://forums.slimdevices.com/showt...Playing-plugin

    Comment


      #3
      Originally posted by Paul Webster View Post
      Nice and simple.

      If you put "code" tags around your code it will look better - plus, given that this is in Python then the tabs/spaces at the start of line are crucial for anyone trying to copy/paste.
      Might also add that it is python (could give an example file name) and how you start running it - for the people who are less confident in getting things up and running.
      Yes, I will fix it. so I will make a service to run the script and config file (server and player settings)

      you also need to install
      Code:
      [email protected]:~# sudo apt-get install bluetooth
      
      [email protected]:~# bluetoothctl
      [NEW] Controller 00:1A:7D:DA:71:13 DietPi [default]
      
      [bluetooth]# show
      Controller 00:1A:7D:DA:71:13
              Name: DietPi
              Alias: DietPi
              Class: 0x000000
              Powered: yes
              Discoverable: no
              Pairable: yes
              UUID: Generic Attribute Profile (00001801-0000-1000-8000-00805f9b34fb)
              UUID: A/V Remote Control        (0000110e-0000-1000-8000-00805f9b34fb)
              UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
              UUID: Generic Access Profile    (00001800-0000-1000-8000-00805f9b34fb)
              UUID: A/V Remote Control Target (0000110c-0000-1000-8000-00805f9b34fb)
              Modalias: usb:v1D6Bp0246d052B
              Discovering: no
      
      [bluetooth]# scan on
      
      [bluetooth]# connect 0C:FC:83:F7:17:E1
      Attempting to connect to 0C:FC:83:F7:17:E1
      [CHG] Device 0C:FC:83:F7:17:E1 Class: 0x002540
      [CHG] Device 0C:FC:83:F7:17:E1 Icon: input-keyboard
      [CHG] Device 0C:FC:83:F7:17:E1 Connected: yes
      [CHG] Device 0C:FC:83:F7:17:E1 Modalias: usb:v0000p0000d0000
      [CHG] Device 0C:FC:83:F7:17:E1 UUIDs: 00001124-0000-1000-8000-00805f9b34fb
      [CHG] Device 0C:FC:83:F7:17:E1 UUIDs: 00001200-0000-1000-8000-00805f9b34fb
      [CHG] Device 0C:FC:83:F7:17:E1 ServicesResolved: yes
      [CHG] Device 0C:FC:83:F7:17:E1 Paired: yes
      Connection successful
      
      [BT003]# info 0C:FC:83:F7:17:E1
      Device 0C:FC:83:F7:17:E1
              Name: BT003
              Alias: BT003
              Class: 0x002540
              Icon: input-keyboard
              Paired: yes
              Trusted: no
              Blocked: no
              Connected: yes
              LegacyPairing: no
              UUID: Human Interface Device... (00001124-0000-1000-8000-00805f9b34fb)
              UUID: PnP Information           (00001200-0000-1000-8000-00805f9b34fb)
              Modalias: usb:v0000p0000d0000
              RSSI: -74
      [BT003]# trust 0C:FC:83:F7:17:E1
      [CHG] Device 0C:FC:83:F7:17:E1 Trusted: yes
      Changing 0C:FC:83:F7:17:E1 trust succeeded
      
      [email protected]:~# sudo apt install input-utils
      [email protected]:~# lsinput
      
      /dev/input/event3
         bustype : BUS_BLUETOOTH
         vendor  : 0x0
         product : 0x0
         version : 0
         name    : "BT003"
         phys    : "00:1A:7D:DA:71:13"
         uniq    : "0C:FC:83:F7:17:E1"
      
      
      sudo apt-get update
      sudo apt-get install python3
      sudo apt install python3-pip
      sudo apt install python3-dev
      sudo pip3 install evdev
      
      sudo chmod ugo+x install.sh
      ./install.sh
      Click image for larger version

Name:	Screenshot_2022-05-18-13-54-51-969.jpg
Views:	1
Size:	85.8 KB
ID:	1575100
      Last edited by Errik128; 2022-05-19, 23:27.

      Comment


        #4
        optimized code



        Code:
        from evdev import InputDevice, categorize, ecodes
        import time, sys, json, requests
        
        try:
            print('try open btremote.conf')
            with open('btremote.conf') as json_file:
                settings = json.load(json_file)
            print('file open ok')
        except:
            print('no file btremote.conf')
            sys.exit() 
        
        print('---settings---')
        print(settings)
        print('-----')
        
        SERVER = settings['server']
        PLAYER = settings['player']
        BTDEV = settings['btdevice']
        
        print('SERVER ', SERVER)
        print('PLAYER ', PLAYER)
        print('BTDEV ', BTDEV)
        
        ACTIONS = {
        "115":["mixer", "volume", "+5" ], 
        "114":["mixer", "volume", "-5" ],
        "164":["pause"],
        "163":["playlist","index","+1"], 
        "165":["playlist","index","-1"] 
        }
        print('ACTIONS')
        for key,value in ACTIONS.items(): 
            print(key, ':', value)
        
        def send(ACTION):
            print('try send: ', ACTION)
            try:
                r = requests.post(SERVER, json={
                "method": "slim.request", 
                "params":[ PLAYER ,  ACTIONS[str(ACTION)]]
                }, timeout=5)
            except:
                print('no server')
        
        def getkey():
            print('getkey started')
            remote = InputDevice(BTDEV)
            print(remote)    
            for event in remote.read_loop():
                if event.type == ecodes.EV_KEY:
                    if event.value == 1:   
                        print('key pressed ', event.code)     
                        print(categorize(event))
                        print('action: ', ACTIONS[str(event.code)])
                        send(event.code)
                  
        def main():
            while True:
                print('try bt device: ', BTDEV)
                try:
                    remote = InputDevice(BTDEV)
                    print('bt device ok: ', remote)
                    print('start get key')
                    getkey()
                except OSError as err:
                    print("OS error: {0}".format(err))
                    print('no bt device')
                time.sleep(5)
        
        main()
        Last edited by Errik128; 2022-05-22, 23:33.

        Comment

        Working...
        X