B4R Question mqtt Client Id encoding

fbritop

Active Member
Licensed User
Longtime User
I'm using a simple MQTT init. I want to send as the clientId the Mac Address of an ESP32.


B4X:
mqtt.Initialize2(wifiSocket.Stream, "api.***.com", 82, bc.HexFromBytes(MacAddress), "mqtt_MessageArrived", "mqtt_Disconnected")"]

Sub MacAddress() As Byte()
    RunNative("getMac", Null)
    Return MacArray
End Sub

#if C
  void getMac(B4R::Object* u) {
  WiFi.macAddress((Byte*)b4r_main::_macarray->data);
  }
#end if

I cannot find the way that the clientId is sent as plain text. On the other end, I have mosca mqtt broker, that when the client connects it just gives me a strange encoding for the client Id

1645450811423.png


Is this a problem with the client or the broker?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It happens because bc.HexFromBytes creates the string on the stack.

1645542788670.png


The fact that we need a string here makes it more complicated.
This should work (I haven't tested it with MQTT):
B4X:
Sub Process_Globals
    Public Serial1 As Serial
    Private MacArray(6) As Byte
    Private MacHexBytes(13) As Byte
    Private MacString As String
    
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    RunNative("getMac", Null)
    Dim bc As ByteConverter
    Dim b() As Byte = bc.HexFromBytes(MacArray)
    bc.ArrayCopy2(b, 0, MacHexBytes, 0, 12) 'byte 13 = 0
    RunNative("UpdateStringDataPointer", Null)
    Log(MacString)
End Sub


#if C
 void getMac(B4R::Object* u) {
      WiFi.macAddress((Byte*)b4r_main::_macarray->data);
    ((Byte*)b4r_main::_macarray->data)[6] = 0;
    
}
void UpdateStringDataPointer(B4R::Object* u) {
    b4r_main::_macstring->data = (const char*)b4r_main::_machexbytes->data;
}
#end if
 
Upvote 0
Top