gpt4 book ai didi

c - 使用 c 通过 mqtt 将 sqlite 数据库读取到 mqtt 代理

转载 作者:行者123 更新时间:2023-11-30 16:15:01 28 4
gpt4 key购买 nike

我想读取一个 sqlite3 db 文件,用于通过 mqtt 的数据连接将数据从嵌入式设备发送到 C 语言的 mqtt 代理。

我收到错误“连接失败,返回代码 5”

谁能帮我纠正这个代码

提前致谢

 #include <stdlib.h>
#include <string.h>
#include <MQTTClient.h>
#include <sqlite3.h>
#define ADDRESS "tcp:localhost:1883"
#define CLIENTID "......"
#define USERNAME "....."
#define PASSWORD "....."
#define TOPIC "..."
#define QOS 1
#define TIMEOUT 10000L

static int callback(void *NotUsed, int argc, char* argv[],char **azColName)
{
int i;
for(i = 0; i<argc; i++) {
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}

}
int main() {

int rc;
MQTTClient client;
MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
MQTTClient_message pubmsg = MQTTClient_message_initializer;
MQTTClient_deliveryToken token;

MQTTClient_create(&client, ADDRESS, CLIENTID,
MQTTCLIENT_PERSISTENCE_NONE, NULL);
conn_opts.username = USERNAME;
conn_opts.password = PASSWORD;
conn_opts.keepAliveInterval = 20;
conn_opts.cleansession = 1;

if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
{
printf("Failed to connect, return code %d\n", rc);
exit(EXIT_FAILURE);
}

pubmsg.qos = QOS;
pubmsg.retained = 0;

sqlite3 *db;
char *zErrMsg = 0;
char *sql;
const char* data = "Callback function called";

rc = sqlite3_open("db/dht.db", &db);
sql = "SELECT * from table";
rc = sqlite3_exec(db, sql, callback, (void*)data, &zErrMsg);

MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token);
printf("Waiting for up to %d seconds for publication of %s\n"
"on topic %s for client with ClientID: \n",
(int)(TIMEOUT/1000), TOPIC, CLIENTID);
rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
printf("Message with delivery token %d delivered\n", token);
sqlite3_close(db);
return 0 ;
}

最佳答案

来自泛美卫生组织 C doc

MQTTClient_connect()

int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)

This function attempts to connect a previously-created client (see MQTTClient_create()) to an MQTT server using the specified options. If you want to enable asynchronous message and status notifications, you must call MQTTClient_setCallbacks() prior to MQTTClient_connect().

Parameters

  • handle A valid client handle from a successful call to MQTTClient_create().
  • options A pointer to a valid MQTTClient_connectOptions structure.

Returns

MQTTCLIENT_SUCCESS if the client successfully connects to the server. An error code is returned if the client was unable to connect to the server. Error codes greater than 0 are returned by the MQTT protocol:

  • 1: Connection refused: Unacceptable protocol version
  • 2: Connection refused: Identifier rejected
  • 3: Connection refused: Server unavailable
  • 4: Connection refused: Bad user name or password
  • 5: Connection refused: Not authorized
  • 6-255: Reserved for future use

返回码 5 表示“未授权”,这意味着您的用户名/密码错误。

关于c - 使用 c 通过 mqtt 将 sqlite 数据库读取到 mqtt 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57305494/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com