B4R Question Is it posible to store some global string in ESP8266?

ivan.tellez

Active Member
Licensed User
I want to make a Mqtt device, for that, they need to send unique topics, the MAC is a good option:

somename/MAC/somefunction
somename/MAC/somefunction


But as JoinStrings is said to be an expensive function, it should be more convenient to create the topics at the start and reuse them. I tried GlobalStore with no success.

In AppStart:

B4X:
        GlobalStore.Put(0, JoinStrings(Array As String("home/", MyMac, "/out")))
        'Also:
        Dim Topic As String = JoinStrings(Array As String("home/", MyMac, "/in"))
        GlobalStore.Put(1, Topic)
And then use it like:

B4X:
        mqtt.Subscribe(GlobalStore.Slot0, 0)
        mqtt.Publish(GlobalStore.Slot1, Serializator.ConvertArrayToBytes(Array(iBtnNum, iState)))
B4X:
Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
        Log("Topic: ", Topic)
        Log ("Slot1: ", GlobalStore.Slot1)
        'In the logs it prints the correct text, but this fails:

        If Topic = GlobalStore.Slot1 Then
                'Not true
        Else
                Log("Topic <> Slot1")
        End If

        'It prints: "Topic <> Slot1"
End Sub

And, in the other side, the Topic recived is something like this: "1073669004" instead of the text

Is the GlobalStore the correct way to do this?
 

bdunkleysmith

Active Member
Licensed User
I believe GlobalStore is the way to go, but you may need to use the ByteConverter library and change the if statement to:

B4X:
Sub Process_Globals
 
    Private bc As ByteConverter

End Sub

Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
        Log("Topic: ", Topic)
        Log ("Slot1: ", GlobalStore.Slot1)
        'In the logs it prints the correct text, but this fails:

        If Topic = bc.StringFromBytes(GlobalStore.Slot1) Then
                'Not true
        Else
                Log("Topic <> Slot1")
        End If

        'It prints: "Topic <> Slot1"
End Sub
 

ivan.tellez

Active Member
Licensed User
I believe GlobalStore is the way to go, but you may need to use the ByteConverter library and change the if statement to:

B4X:
Sub Process_Globals
 
    Private bc As ByteConverter

End Sub

Sub Mqtt_MessageArrived (Topic As String, Payload() As Byte)
        Log("Topic: ", Topic)
        Log ("Slot1: ", GlobalStore.Slot1)
        'In the logs it prints the correct text, but this fails:

        If Topic = bc.StringFromBytes(GlobalStore.Slot1) Then
                'Not true
        Else
                Log("Topic <> Slot1")
        End If

        'It prints: "Topic <> Slot1"
End Sub

Well, it works now with bc.StringFromBytes, thanks
 
Top