B4R Question Sending Numbers as MQTT data


Well-Known Member
Licensed User
Longtime User
I have started playing with B4R after a rather long break.
I want to publish a number to an MQTT broker. In this case it is a UInt.

MQTT.publish expects an array of bytes. So, I was wondering what is the most correct / efficient way to do this?
Not even sure if any of these would work. At the moment I'm just typing things until I get no compiler errors 🤣🤣

MQTT.Publish("home/settings/pv-data-refresh", DATA_REFRESH_RATE.As(String))


Dim PubData As String = DATA_REFRESH_RATE
MQTT.Publish("home/settings/pv-data-refresh", PubData)

'something else

Like I say, in each case DATA_REFERSH_RATE is a UInt declared in Process_Globals with
Dim DATA_REFRESH_RATE As UInt = 1000 ' initial setting

I will also need the data to be converted back to UInt with a MQTT_MessageArrived sub

Just thinking out loud as I type. Would it be better to just declare the value as a String and only cast it when I update the timer interval. Realistically, I think this is the only time it actually 'needs' to be an UInt

Just to add a little context to what I am trtying to achieve. I am using ESP8266 to publish data from my solar PV inverter to MQTT broker that I run on rPi. I would like to see and be able to adjust the frequency of rate at which the data is published. The setting will be stored in EEPROM (also first time playing with read/write to EEPROM) it will be published to MQTT and if I then publish to the topic to update from another device, the ESP8266 will update the Timer tick interval and write the new value to EEPROM to save it for restarts.
Hope that makes sense.

Any help appreciated.


B4X founder
Staff member
Licensed User
Longtime User
Using strings is the wrong solution.

I want to publish a number to an MQTT broker.
The broker is just a required tool. You publish messages to other clients.

B4RSerializator is a good option.
Other good options: ByteConverter or RandomAccessFile

All of them are available in rRandomAccessFile library.
Upvote 0