B4R Question How to use a string from Eeprom in MQTT.Initialize2

janderkan

Well-Known Member
Licensed User
Longtime User
Hi
When using Mqtt.Initialize2 on Wemos mini everything works fine if the server string is defined in kode.
But when I read the string from eeprom I am not able to connect to the server.
This emulates the problem.

This works:
B4X:
Dim Str As String = "m21.cloudmqtt.com"
mqtt.Initialize2(Client.Stream, Str, MqttPort, "test", "Mqtt_MessageArrived", "Mqtt_Disconnected")
mqtt.connect

This does not work:
B4X:
Dim BC as ByteConverter
Dim Str As String = BC.StringFromBytes("m21.cloudmqtt.com".GetBytes)
mqtt.Initialize2(Client.Stream, Str, MqttPort, "test", "Mqtt_MessageArrived", "Mqtt_Disconnected")
mqtt.connect

Jan
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
MQTT library doesn't create a copy of the string. If the string is a literal string then it will work as it kept in memory.
However the dynamic string is created on the stack and therefore is later removed.

This code takes a dynamic string and copies it to a global variable:
B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private bc As ByteConverter
   Private GlobalString As String = "000000000000000000000000" 'must be long enough to hold the string
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Dim DynamicString As String = bc.StringFromBytes("m21.cloudmqtt.com".GetBytes)
   bc.ObjectCopy(DynamicString, GlobalString, DynamicString.Length + 1)
   'Now you can use GlobalString 
End Sub
 
Upvote 0

janderkan

Well-Known Member
Licensed User
Longtime User
I needed to have more than 1 global variable from eeprom,
but this code does not work:

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private bc As ByteConverter
   Private GlobalString1 As String = "000000000000000000000000" 'must be long enough to hold the string
   Private GlobalString2 As String = "000000000000000000000000" 'must be long enough to hold the string
   Dim tim As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Dim DynamicString1 As String = bc.StringFromBytes("m21.cloudmqtt.com".GetBytes)
   bc.ObjectCopy(DynamicString1, GlobalString1, DynamicString1.Length + 1)
   'Now you can use GlobalString1
   
   Dim DynamicString2 As String = bc.StringFromBytes("test".GetBytes)
   bc.ObjectCopy(DynamicString2, GlobalString2, DynamicString2.Length + 1)
   'Now you can use GlobalString2

   tim.Initialize("tim_Tick",1000)
   tim.Enabled=True
End Sub

Sub tim_Tick
    Log("1: ",GlobalString1)
    Log("2: ",GlobalString2)
End Sub


but this is working

B4X:
Sub Process_Globals
   Public Serial1 As Serial
   Private bc As ByteConverter
   Private GlobalString1 As String = "000000000000000000000000" 'must be long enough to hold the string
   Private GlobalString2 As String = "111111111111111111111111" 'must be long enough to hold the string
   Dim tim As Timer
End Sub

Private Sub AppStart
   Serial1.Initialize(115200)
   Dim DynamicString1 As String = bc.StringFromBytes("m21.cloudmqtt.com".GetBytes)
   bc.ObjectCopy(DynamicString1, GlobalString1, DynamicString1.Length + 1)
   'Now you can use GlobalString1
   
   Dim DynamicString2 As String = bc.StringFromBytes("test".GetBytes)
   bc.ObjectCopy(DynamicString2, GlobalString2, DynamicString2.Length + 1)
   'Now you can use GlobalString2

   tim.Initialize("tim_Tick",1000)
   tim.Enabled=True
End Sub

Sub tim_Tick
    Log("1: ",GlobalString1)
    Log("2: ",GlobalString2)
End Sub
 
Upvote 0
Top