Spanish Enviar datos con MQTT

tornado64

New Member
Licensed User
Longtime User
Hola
Tengo un broker de MQTT en una Rasberry con el "Mosquitto" instalado y funciona perfectamente, vamos con Arduino y con Python no tengo ningun problema.
La aplicacion que tengo en B4J y otra en B4A tambien me funcionan perfectamente recibiendo datos, pero en cambio no consigo enviar datos con ninguno de los dos sistemas, utilizo la libreria ofical, jMQTT.

He probado todos los ejemplos que he encontrado con el buscador pero no consigo adaptarlos para que me funcionen, alguien tiene un ejemplo simple que funcione?,
 

musaso

Active Member
Licensed User
Longtime User
Este codigo me funciona para leer temperatura y humedad de una sonda a traves de placa esp8266.
Este codigo es la parte de B4A

B4X:
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Private MQTT As MqttClient
   Private MQTTUser As String = "xxxxxxxx" 'Tu Usuario
   Private MQTTPassword As String = "xxxxxxxx" 'Tu pwd
   Private MQTTServerURI As String = "tcp://blablabla.cloudmqtt.com:10672"
   Private MQTTOpt As MqttConnectOptions
End Sub

Sub MQTT_Connect
   Dim ClientId As String = Rnd(0, 999999999) 'create a unique id
   MQTT.Initialize("MQTT", MQTTServerURI, ClientId)
   MQTTOpt.Initialize(MQTTUser, MQTTPassword)
   MQTT.Connect2(MQTTOpt)
End Sub

Sub MQTT_Connected (Success As Boolean)
   If Success = False Then
       Log(LastException)
       LabelConexion.Text = "Error de conexion"
       LabelConexion.Color = Colors.Red
       
   Else
       LabelConexion.Text = "Conectado"
       LabelConexion.Color = Colors.Green
       'MQTT.Subscribe("temperatura", 0)
       'MQTT.Subscribe("humedad", 0)
       'MQTT.Subscribe($"${MQTTUser}/temperatura/#"$, 0)
       MQTT.Subscribe("#", 0)
       
   End If
End Sub

Private Sub MQTT_MessageArrived (Topic As String, Payload() As Byte)
   If Topic.Contains("datos") Then
       Dim t,h As String
       Dim Txt As String = BytesToString(Payload,0,Payload.Length,"UTF-8")
       Dim myans() As String = SplitText(Txt) 'Recibo datos en dos lineas
       t = myans(0)
       h = myans(1)
       LabelTemperatura.Text = t & " ºC"
       LabelHumedad.Text =  h & " %"
   End If

Sub SplitText(Text As String) As String()
   Dim sText() As String
   sText = Regex.Split(CRLF, Text)
   Return sText
End Sub
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…