I'm trying to send data to my db (MySQL) with MQTT. But I cant get any information or just don't understand if I need to connect to my MQTT broker with my C-code first? Or if I just can put in my DB address and portnumber? Connection code down below.
I'm using HiveMQ as broker.
while(1){
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
int rc = 0;
char buf[200];
MQTTString topicString = MQTTString_initializer;
char* payload = "Hello World!";
int payloadlen = strlen(payload);
int buflen = sizeof(buf);
data.clientID.cstring = "me";
data.keepAliveInterval = 20;
data.cleansession = 1;
int len = MQTTSerialize_connect(buf, buflen, &data); /* 1 */
topicString.cstring = "cc3200-ben";
len += MQTTSerialize_publish(buf + len, buflen - len, 0, 0, 0, 0, topicString, payload, payloadlen); /* 2 */
len += MQTTSerialize_disconnect(buf + len, buflen - len); /* 3 */
// creating a TCP socket
int mysock = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0);
if( mysock < 0 )
{
ERR_PRINT(mysock);
LOOP_FOREVER();
}
SlSockAddrIn_t addr;
//filling the TCP server socket address
addr.sin_family = SL_AF_INET;
addr.sin_port = sl_Htons(1883);
//addr.sin_port = sl_Htons(3306);
addr.sin_addr.s_addr = sl_Htonl(0xC6291EF1);
// connecting to TCP server
rc = sl_Connect(mysock, (SlSockAddr_t *) &addr, sizeof(addr));
if(rc<0){
ERR_PRINT(rc);
LOOP_FOREVER();
}
// sending packet
rc = sl_Send(mysock, buf, len, NULL);
if(rc<0){
ERR_PRINT(rc);
LOOP_FOREVER();
}
//closing the socket
rc = sl_Close(mysock);
if(rc<0){
ERR_PRINT(rc);
LOOP_FOREVER();
}
// rc = Socket_new("0.0.0.0", 1883, &mysock);
// rc = write(mysock, buf, len);
// rc = close(mysock);
osi_Sleep(200);
}