MQTT xHarbour

MQTT xHarbour

Postby lzanardo » Mon May 20, 2024 9:29 pm

Hello goodnight.

Is there any way to connect MQTT
because I already tried:
* hbamqp
* paho-mqtt3c.dll


exempl:

conn_opts := "{'M', 'Q', 'T', 'C'}, 8, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL,"
conn_opts += "0, NULL, 0, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL"

DllCall( hHandle, DLL_OSAPI, "MQTTClient_connect", @hHandle, Conn_Opts )

No connect


Help?


Att.


lZanardo - Brazil
lzanardo
 
Posts: 9
Joined: Wed Nov 08, 2023 2:58 pm

Re: MQTT xHarbour

Postby Antonio Linares » Tue May 21, 2024 3:44 am

Dear Izanardo,

This is an example of use:
Code: Select all  Expand view  RUN
#include "MQTTClient.h"

int main() {
    MQTTClient client;
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
    conn_opts.keepAliveInterval = 20; // tiempo de keep alive en segundos
    conn_opts.cleansession = 1; // limpiar sesión al conectar

    int rc = MQTTClient_connect(client, "tcp://localhost:1883", &conn_opts);
    if (rc!= MQTTCLIENT_SUCCESS) {
        printf("Error al conectar: %d\n", rc);
        return 1;
    }

    if (MQTTClient_isConnected(client)) {
        printf("Conectado correctamente al broker MQTT\n");
    } else {
        printf("Error al conectar al broker MQTT\n");
        return 1;
    }

    // Realiza operaciones MQTT aquí (publicar, suscribir, recibir mensajes, etc.)

    // Cerrar la conexión cuando termines
    MQTTClient_disconnect(client);

    return 0;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: MQTT xHarbour

Postby Antonio Linares » Tue May 21, 2024 3:48 am

Code: Select all  Expand view  RUN
#pragma BEGINDUMP

#include "hbapi.h"
#include "MQTTClient.h"

HB_FUNC( MQTTCONNECT )
{
   MQTTClient client;
   MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
   int rc;

   conn_opts.keepAliveInterval = 20; // tiempo de keep alive en segundos
   conn_opts.cleansession = 1; // limpiar sesión al conectar

   rc = MQTTClient_connect(client, "tcp://localhost:1883", &conn_opts);
   if (rc!= MQTTCLIENT_SUCCESS)
      hb_retc( "Error al conectar: " + hb_ntos( rc ) );
   else
   {
      if (MQTTClient_isConnected(client))
         hb_retc( "Conectado correctamente al broker MQTT" );
      else
         hb_retc( "Error al conectar al broker MQTT" );
   }
}

#pragma ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42113
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: MQTT xHarbour

Postby albeiroval » Tue May 21, 2024 9:32 pm

lzanardo wrote:Hello goodnight.

Is there any way to connect MQTT
because I already tried:
* hbamqp
* paho-mqtt3c.dll


exempl:

conn_opts := "{'M', 'Q', 'T', 'C'}, 8, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL,"
conn_opts += "0, NULL, 0, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL"

DllCall( hHandle, DLL_OSAPI, "MQTTClient_connect", @hHandle, Conn_Opts )

No connect


Help?


Att.


lZanardo - Brazil


Hola IZanardo, donde encuentro los DLL para probar el MQTT
Saludos,
Regards,

Albeiro Valencia
www.avcsistemas.com
User avatar
albeiroval
 
Posts: 377
Joined: Tue Oct 16, 2007 5:51 pm
Location: Barquisimeto - Venezuela

Re: MQTT xHarbour

Postby lzanardo » Tue May 21, 2024 9:55 pm

lzanardo
 
Posts: 9
Joined: Wed Nov 08, 2023 2:58 pm

Re: MQTT xHarbour

Postby albeiroval » Thu May 23, 2024 2:44 am

lZanardo,

Gracias por compatirlo.
Saludos,
Regards,

Albeiro Valencia
www.avcsistemas.com
User avatar
albeiroval
 
Posts: 377
Joined: Tue Oct 16, 2007 5:51 pm
Location: Barquisimeto - Venezuela

Re: MQTT xHarbour

Postby lzanardo » Thu May 23, 2024 12:54 pm

#Include "WPRAMQTT.CH"

#DEFINE MQTTLIB "paho-mqtt3c.dll"
#DEFINE DLL_OSAPI 0x0020 // __stdcall
#DEFINE NULL Nil

Static nHandle
Static ConnLost , MsgArrvd, Delivered

Function WPRA101_MQTT()
Local nResult

if !file( MQTTLIB )
Vt_Aviso("Arquivo " + MQTTLIB + " não localizado...",.T.)
Return( Nil )
Endif

nHandle := 0
cAddress := "tcp:localhost:1883"
cClientID := "teste"

nResult := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_create", @nHandle, cAddress , cClientID, MQTTCLIENT_PERSISTENCE_NONE )
if nResult != MQTTCLIENT_SUCCESS
? "MQTTClient_create:", nResult
Vt_Aviso("Erro ao criar objeto acesso",.T.) // <- Function View
Return( Nil )
Endif

ConnLost := HB_String2Pointer("fConnLost()")
MsgArrvd := Nil
Delivered:= 0

nResult := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_setCallbacks", nHandle, @ConnLost , @MsgArrvd , @Delivered )
if nResult != MQTTCLIENT_SUCCESS
? "MQTTClient_setCallbacks", nResult
Vt_Aviso("Erro ao criar modo multithread. " + lZA_Zero( nResult, 10, .F. ) ,.T.)
quit
Return( Nil )
Endif

cTopicLen := "cTopicLen"
nResult := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_receive", nHandle, cClientID, 1 , 3, 10000) //, @cBuffer,,100) //, , conn_opts, 0 )
? nResult, cClientID, ConnLost



conn_opts := "'M', 'Q', 'T', 'C'" , 0, 20, 1, 1, NULL, NULL, NULL, 30, 0, NULL,"
conn_opts += "0, NULL, 0, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL"

conn_opts := HB_String2Pointer( conn_opts )

nResult := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_connect", @nHandle , conn_opts , 0)

? "nResult", nResult

Return( Nil )


Function fConnLost()
? "#### fConnLost() ####"

Return( 0 )


Function fMsgArrvd() ; Return( 1 )



Estou neste ponto poren não consigo receber dados mosquitto.exe

Skype: lfzanardo

att.
LZanardo
lzanardo
 
Posts: 9
Joined: Wed Nov 08, 2023 2:58 pm

Re: MQTT xHarbour

Postby karinha » Thu May 23, 2024 1:49 pm

Intenta asi, no tengo completo, sin eso no és posible probar.

Code: Select all  Expand view  RUN

#include "FiveWin.ch"
#Include "WPRAMQTT.CH"  // no tengo, donde tien?

// #DEFINE MQTTLIB           "paho-mqtt3c.dll"
#DEFINE DLL_OSAPI         0x0020      // __stdcall
#DEFINE NULL              Nil

STATIC nHandle
STATIC ConnLost, MsgArrvd, Delivered
STATIC cDll := "C:\MQTT\MQTT.DLL" // Renombre

FUNCTION Main()

   LOCAL nResult, hDll

   IF FILE( "C:\MQTT\MQTT.DLL" )

      hDll := LoadLibrary( cDll )

   ELSE

      ? "Que pasa con el MQTT.DLL?"

      RETURN NIL

   ENDIF

   /*
   IF !File( MQTTLIB )
      Vt_Aviso( "Arquivo " + MQTTLIB + " não localizado...", .T. )
      Return( NIL )
   ENDIF
   */


   nHandle := 0
   cAddress  := "tcp:localhost:1883"
   cClientID := "teste"

   nResult  := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_create", @nHandle, cAddress, cClientID, MQTTCLIENT_PERSISTENCE_NONE )

   IF nResult != MQTTCLIENT_SUCCESS

      ? "MQTTClient_create:", nResult

      //Vt_Aviso( "Erro ao criar objeto acesso", .T. ) // <- Function View

      MsgInfo( "Erro ao criar objeto acesso", "Erro ao criar objeto acesso" )

      FreeLibrary( hDll )

      RETURN NIL
     
   ENDIF

   ConnLost  := HB_String2Pointer( "fConnLost()" )
   MsgArrvd  := Nil
   Delivered := 0

   nResult := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_setCallbacks", nHandle, @ConnLost, @MsgArrvd, @Delivered )

   IF nResult != MQTTCLIENT_SUCCESS

      ? "MQTTClient_setCallbacks", nResult

      // Vt_Aviso( "Erro ao criar modo multithread. " + lZA_Zero( nResult, 10, .F. ), .T. )

      MsgInfo( "Erro ao criar modo multithread", "Erro ao criar modo multithread" )

      FreeLibrary( hDll )

      QUIT

      RETURN NIL

   ENDIF

   cTopicLen := "cTopicLen"

   nResult   := DllCall( MQTTLIB, DLL_OSAPI, "MQTTClient_receive", nHandle, cClientID, 1,  3, 10000 ) // , @cBuffer,,100) //,  , conn_opts, 0 )

   ?  nResult, cClientID, ConnLost

   conn_opts := "'M', 'Q', 'T', 'C'", 0, 20, 1, 1, NULL, NULL, NULL, 30, 0, NULL, "

   conn_opts += "
0, NULL, 0, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL"

   conn_opts :=  HB_String2Pointer( conn_opts )

   nResult := DllCall( MQTTLIB, DLL_OSAPI, "
MQTTClient_connect", @nHandle, conn_opts, 0 )

   ? "
nResult", nResult

RETURN NIL

FUNCTION fConnLost()

   ? "
#### fConnLost() ####"

RETURN( 0 )


FUNCTION fMsgArrvd() ; Return( 1 )

// FIN / END


Regards, saludos.
João Santos - São Paulo - Brasil - Phone: +55(11)95150-7341
User avatar
karinha
 
Posts: 7831
Joined: Tue Dec 20, 2005 7:36 pm
Location: São Paulo - Brasil


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 50 guests