I want to connect a single device like raspberry pi to thingsboard using MQTT. What I did so far: I set a virtual machine on a computer. This computer is conected with ethernet cable to the device. I set the newtwork, all the pings where working: ping device-> VM = works on both ways ping device-> computer = works on both ways
So the networking part is covered, I started to install thingsboard server. On the virtual machine (which has ubuntu btw), I crosscompiled the lib embedded paho to my device and set host to VM's ip(port =1883) and as username the token given by thingsboard. When I try to connect, I always get error (rc=-1) and I dont know why. I tried disabling all the firewalls (from windows and ubuntu) but I still could not connect, so I thought that may be the network was not set properly. SO i decided to compile the program straight on the VM and run it from there using as IP localhost or 127.0.0.1 and I got the same result....I still dont know why.
On the conf file of TB, the bind ip for mqtt is 0.0.0.0 and port 1883. And the code of the program I use to test connection is the same as the sample, but changing the ip. Here it is:
#define MQTTCLIENT_QOS2 1
#include <memory.h>
#include "MQTTClient.h"
#define DEFAULT_STACK_SIZE -1
#include "linux.cpp"
int arrivedcount = 0;
void messageArrived(MQTT::MessageData& md)
{
MQTT::Message &message = md.message;
printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
++arrivedcount, message.qos, message.retained, message.dup, message.id);
printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
}
int main(int argc, char* argv[])
{
IPStack ipstack = IPStack();
float version = 0.3;
const char* topic = "v1/devices/me/telemetry"
printf("Version is %f\n", version);
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
const char* hostname = "localhost"; // also tried with 127.0.0.1
int port = 1883;
printf("Connecting to %s:%d\n", hostname, port);
int rc = ipstack.connect(hostname, port);
if (rc != 0)
printf("rc from TCP connect is %d\n", rc); //this conections is done succesfully i think.
printf("MQTT connecting\n");
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.username.cstring = (char*) "TOKEN";
rc = client.connect(data);
if (rc != 0)
printf("rc from MQTT connect is %d\n", rc); //doesnt go further than here, i get rc = -1
printf("MQTT connected\n");
rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
if (rc != 0)
printf("rc from MQTT subscribe is %d\n", rc);
.
.
.
.
.
return 0;
}
Any clue why this is not working? Im struggling for days with this issue already and im getting nuts.
Thanks
EDIT: The problem i dont think is on the code, because thingsboard return 0, 4 or 5 as a status of conection. It needs to be something else.
SOLUTION: If you came here having the same issue as me, I used the sample on paho called "Hello", I changed to "qos0pub" sample and it worked.