I need to use mqtt protocol in gambas to get jobs done. I used mosquitto api and mosquitto-dev library then created something like that: `
#include <stdio.h>
#include <mosquitto.h>
int connectt(char *mqname,bool mqbool){
printf("something happens...1");
int rc;
struct mosquitto * mosq;
mosquitto_lib_init();
mosq=mosquitto_new(mqname,mqbool,NULL);
mosquitto_connect(mosq,"localhost",1883,60);
if(rc!=0){
printf("i cant connect to broker");
mosquitto_destroy(mosq);
return -1;
}else if(rc==0){
printf("Connected to broker yeey");
return 0;
}
mosquitto_publish(mosq,NULL,"targe/test",6,"Yeey",0,false);
mosquitto_disconnect(mosq);
printf("something happening...2");
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
printf("something happening...3");
return 0;
int main(){
connectt("tester",true);
}
and i created shared object file with this way:
1gcc -c -g mosquit.c -lmosquitto
gcc -shared -o libmosquit.so mosquit.o
gcc -Llib/ -Wall -o targele mosquit.c -lmosquitto
so i moved libmosquit.so file to /lib/x86_64-linux-gnu/ directory
Untill here everything is fine, when i run targele it send "Yeeyt" payload but when i try it in gambas with these lines
`
Library "libmosquit"
Extern connectt(mqname As String, mqbool As Boolean) As Integer
Public Sub Main()
connectt("tester", True)
End
` it says connection estabilished but do not send payload.
I tried directy importing library from mosquitto api but i couldn't figure out gambas structures and i don't need all of those functions
Your code returns if rc != 0
or rc == 0
(i.e. in all cases) meaning the call to mosquitto_publish
is never reached. In your case rc
is never set, you define it (int rc
), but do not assign anything to it i.e. rc = mosquitto_connect(mosq, "test.mosquitto.org", 1883, 60);
so effectively your code is:
int rc
if (rc != 0) {
printf("i cant connect to broker");
mosquitto_destroy(mosq);
return -1;
} else if (rc == 0) {
printf("Connected to broker yeey");
return 0;
}
// Any code below here is unreachable
When you have fixed that you may run into another issue; you might find this example useful. mosquitto_connect
"makes the socket connection only, it does not complete the MQTTCONNECT
/CONNACK
flow, you should use mosquitto_loop_start()
or mosquitto_loop_forever()
for processing net traffic".