B4R Question Send Mqtt data Receive app

Cesar_Morisco

Active Member
Hey guys
As I said Getting Started Using MQTT
I need to send the temperature of the esp8266 to my app It's giving an error
here is the snippet of my code.
Thank you already
B4R:
Sub mqtt_publish(Topic As String,msg As String)
    If connected=True Then
        mqtt.Publish(Topic,msg.GetBytes)
    Else
        Log("Publishg, ERROR ! MQTT Not Connected")
    End If
End Sub

Sub Temp
    Dim TempESP32 As String = 23 'Graus teste
    mqtt_publish("temperature/",TempESP32)
End Sub
B4A:
Sub mqtt_MessageArrived(Topic As String, payload()As Byte)
    Dim obj As Object = serializator.ConvertBytesToObject(payload)
    EditText1.Text=obj
    End Sub
 

Cesar_Morisco

Active Member
Hello XorAndOr
Thanks for your response
My app controls 8 relays and I have to read the temperature too
For future temperature comparison Examples > 35 turn on fan Etc
About the temperature everything is ok now I get it without the true and false everything ok thank you
decoration

B4A:
Temp = BytesToString(payload,0,payload.Length,"UTF-8")
    If Topic = "Temp_Ds" Then
        EditText1.Text = Temp & " °C"
    End If
 
Upvote 0

XorAndOr

Active Member
Licensed User
Longtime User
For future temperature comparison Examples > 35 turn on fan Etc
it should be so...
B4A:
Temp = BytesToString(payload,0,payload.Length,"UTF-8")
If Topic = "Temp_Ds" Then
    EditText1.Text = Temp & " °C"
    If Temp > 35 Then
    MQTT.Publish("Temp_DS18B20", "Fan_ON".GetBytes("UTF8"))'Send to esp board
    End If
End If
B4R:
MQTT.Subscribe("Temp_DS18B20", 1)'From B4A

Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
Select Case Topic
    Case "Temp_DS18B20"
        PinESPxx.DigitalWrite(False)' ON Relay
End Select
End Sub

all this could do even only the esp32
B4R:
Private Sub ReadTemparature (u As Byte)
'..................
'..................
'..................
    Dim celsius As Double = raw/16
    Log("Temp_Ds = ", NumberFormat(celsius,0,1),"°C")
    mqtt.Publish("Temp_Ds" , NumberFormat(celsius,0,1))
  
    Dim Tmp As String    
    Tmp = NumberFormat(celsius, 0, 1)
    If Tmp > 35 Then
        PinESPxx.DigitalWrite(True)' FAN ON     
    Else 
        PinESPxx.DigitalWrite(False)'Fan OFF
    End If
  
End Sub
 
Last edited:
Upvote 0

Similar Threads

Top