B4J Library Broadlink home automation [Class] [B4X]

Discussion in 'B4J Libraries & Classes' started by walt61, Jul 26, 2018.

Tags:
  1. walt61

    walt61 Active Member Licensed User

    Hi all,

    These are two classes (the attached is a B4J project but I believe the classes will work for B4X, correct me if I'm wrong in which case I'll update the post) that let you control IR and RF devices with Broadlink RM Pro and Mini 3 ('Black Bean') controllers - see http://www.ibroadlink.com/rm/. The B4J program itself demonstrates (I hope) how to do that.

    Preparations:
    1. The Broadlink e-Control app must have been installed on your Android device and your Broadlink devices, remote controls, and their buttons must have been setup in that app.
    2. In the e-Control app, tap the hamburger menu at the top left, then tap 'Share', and then tap 'Share to other phones in WLAN'.
    3. Connect the Android device to your PC and copy files jsonButton, jsonIrCode, and jsonSubIr from directory /[Internal|External] Storage/broadlink/newremote/SharedData/

    Next step: reading the e-Control data:
    Class BroadlinkJson (inspired by 'getBroadlinkSharedData.py' from https://github.com/NightRang3r/Broadlink-e-control-db-dump) is used to interpret the three aforementioned files and get their contents ready for use with the BroadlinkDevice class. The data can be saved to a file which can be reused later on:

    Code:
    Sub ButtonGetJsonFiles_Click

        
    Dim jsonButtonFilePath As String = GetOnefile(True"Select the 'jsonButton' file you copied from your Android device")
        
    If jsonButtonFilePath = "" Then Return

        
    Dim jsonIrCodeFilePath As String = GetOnefile(True"Select the 'jsonIrCode' file you copied from your Android device")
        
    If jsonIrCodeFilePath = "" Then Return

        
    Dim jsonSubIrFilePath As String = GetOnefile(True"Select the 'jsonSubIr' file you copied from your Android device")
        
    If jsonSubIrFilePath = "" Then Return

        
    Dim outputFilePath As String
        
    Dim outputFilePath As String = GetOnefile(False"Select the output file to which to save your data (optional)")

        
    Dim BLjson As BroadlinkJson
        
    Dim s As String = BLjson.Initialize(jsonButtonFilePath, jsonIrCodeFilePath, jsonSubIrFilePath, outputFilePath)
        
    If s = "" Then
            
    If outputFilePath = "" Then
                fx.Msgbox(MainForm, 
    "The JSON data have been read""Ready")
            
    Else
                fx.Msgbox(MainForm, 
    "The output file was created""Ready")
            
    End If
            myRemoteControlsButtons = BLjson.allRemoteControlsButtons
            gotJson = 
    True
            PopulateComboBoxHexData
        
    Else
            fx.Msgbox(MainForm, s, 
    "An error occurred")
        
    End If

    End Sub

    Sub ButtonReadSavedJson_Click

        
    Dim fname As String = GetOnefile(True"Select the file with the saved JSON data")
        
    If fname = "" Then Return

        
    Try
            
    Dim raf As RandomAccessFile
            raf.Initialize(fname, 
    ""False)
            myRemoteControlsButtons = raf.ReadB4XObject(
    0)
            raf.Close
            gotJson = 
    True
            PopulateComboBoxHexData
    Catch
            fx.Msgbox(MainForm, 
    LastException"An error occurred")
    End Try

    End Sub

    Sub GetOnefile(forInput As Boolean, whichFile As StringAs String

        
    Private fc As FileChooser
        fc.Initialize
        fc.Title = whichFile
        
    If forInput Then
            
    Return fc.ShowOpen(MainForm)
        
    Else
            
    Return fc.ShowSave(MainForm)
        
    End If

    End Sub

    Finally: using the e-Control data:
    This is where class BroadlinkDevice comes into play. It is a partial port from Python (which I don't speak, but I can read and Google can be your friend, and with some blood, sweat, and tears I got there) to B4J from https://github.com/mjg59/python-broadlink

    The following methods must be called (all methods contain documentation, check the class source):
    1. Initialize
    2. Auth: establishes the communication, must be called after Initialize; after Auth, it is recommended to insert a Sleep call to give the authorization process time to complete
    3. RM_send_data: can be called multiple times; it sends data to the device (an 'rm' type device like the Pro or Mini) and returns a result code; the data come from class BroadlinkJson and are present in the 'Data' key in the 'allRemoteControlsButtons' map's elements in that class

    Code:
    Dim d As BroadlinkDevice
        d.Initialize(TextFieldIP.Text, TextFieldMAC.Text, 
    80, devType, 3)
        
    Wait For (d.Auth) Complete (authOK As Int)
        
    If authOK = 0 Then
            Sleep(
    3000' Give the authorization process time to complete, use the same value as 'timeout' (but in milliseconds here)
            fx.Msgbox(MainForm, d.RM_send_data(bc.HexToBytes(hexData)), "Result")
        
    Else
            fx.Msgbox(MainForm, 
    "Auth failed: " & authOK, "Error")
        
    End If
        d.Close

    What else?
    Multiple Broadlink devices (e.g. a Pro and a Mini, or other combinations) can be used by instantiating a BroadlinkDevice for each of them. Each device will of course have its own IP and MAC address, both of which need to be passed to the Initialize method.

    Dependencies:
    The following non-core libraries are required:
    - ByteConverter by agraham: https://www.b4x.com/android/forum/threads/byteconverter-library.6787/
    - Encryption by agraham: https://www.b4x.com/android/forum/threads/base64-and-encryption-library.6839/
    - ... and of course generally speaking the B4X forums without which I wouldn't have been able to do this - at all :)

    Changes:

    2018-07-28
    - Bug fixes
    - Added 'Discover' method to discover the available Broadlink devices on the network
    - Attached project was updated

    2018-07-31
    - Bug fixes
    - Added important comment about B4J non-UI apps to the start of the BroadlinkDevice module
    - Attached project was updated

    Have fun!
     

    Attached Files:

    Last edited: Jul 31, 2018
    dar2o3, Dadaista, Erel and 1 other person like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice