我正在尝试使用MQTT将数据发送到我的数据库(MySQL)。但我无法获得任何信息,或者只是不明白我是否需要先使用我的C代码连接到我的MQTT代理?或者,如果我只能输入我的数据库地址和端口号? 连接代码如下。
我正在使用HiveMQ作为经纪人。
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);
}
答案 0 :(得分:0)
DB和MQTT经纪人完全不知所措,按照接下来的步骤来获得你想要的东西:
要将数据发送到服务器,请执行以下操作: