B4R Library RDC based on MQTT

Discussion in 'B4R Libraries' started by Erel, Oct 27, 2016.

  1. Erel

    Erel Administrator Staff Member Licensed User

    This is an extension to jRDC2. It adds support for MQTT and allows accessing the remote database with an Arduino or ESP8266 board.

    It requires B4R v1.5+ and it uses B4RSerializator to convert the messages to bytes.

    The RDC server listens to both MQTT messages and the standard http requests.

    Client side

    Code:
    Sub Timer1_Tick
       
    'Structure is: ClientId, Command, Tag, zero or more parameters
       '"null" strings will be converted to Null references.
       SendInsert(Array(ClientId, "insert_animal""tag""abc" , "null"))
       
       
    'Structure is: ClientId, Command, Tag, zero or more parameters
       SendSelect(Array(ClientId, "select_animal""tag"539))
    End Sub

    Sub SendInsert (Message() As Object)
       mqtt.Publish(
    "insert", ser.ConvertArrayToBytes(Message))
    End Sub

    Sub SendSelect(Message() As Object)
       mqtt.Publish(
    "select", ser.ConvertArrayToBytes(Message))
    End Sub


    Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
       
    If Topic = ClientId Then
         
    Dim be(10As Object
         
    Dim data() As Object = ser.ConvertBytesToArray(Payload, be)
         
    If data.Length = 0 Then Return
         
    Dim method As String = data(0'ignore
         Dim tag As Object = data(1)
         
    If method = "insert" Then
           
    Dim success As Boolean = data(2)
           
    Log(method, " - tag: ", tag, ", success: ", success)
         
    Else if method = "select" Then
           
    Dim numberOfRows As Int = data(2)
           
    Log(method, " - tag: ", tag, ", number of rows: ", numberOfRows)
           
    If numberOfRows = 1 Then
             
    For i = 3 To data.Length - 1
               
    Log(data(i))
             
    Next
           
    End If
         
    End If
       
    End If
    End Sub
    To send a command you need to prepare an array of objects with the following values:
    ClientId, Command, Tag, zero or more parameters

    You should then call SendInsert or SendSelect.

    The MessageArrived event will be raised with the server response.
    The above code shows how the data should be extracted from the response. Note that select commands will only return the first row.

    Server side

    The changes needed to jRDC2 project are:
    1. Add the MqttGate and B4RSerializator classes (available in the attached project).
    2. Run a broker. I recommend running an external broker such as mosquitto. It better handles reconnections compared to the MqttBroker library.
    3. Initialize an MqttGate object in the main module with the broker port.
     

    Attached Files:

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