在函数中创建新对象 - C++
Create a New Object in a Function - C++
我正在为 MQTT 连接使用一个库,我想创建一个函数来创建对象而不是在预处理器中设置它。原因是因为我需要在程序启动后抓取它连接到哪个服务器运行.
这是我的 mqttConnection 函数:
void mqttConnection(bool checkConnection) {
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
PubSubClient client(mqttServer, 1883, callback, wifiClient); // why would it care if this was here?
}
// Connecting to MQTT broker, loop until connected
while (!client.connected()){
if(!client.connect("device-name")) {
Serial.println("MQTT Broker Connection Failed. Trying Again...");
}
else {
Serial.println("MQTT Broker Connection Success!");
if(client.subscribe(mqttTopic)) {
Serial.println("Subscription Successful! Woot!");
}
}
}
}
这是Class:
#ifndef PubSubClient_h
#define PubSubClient_h
#include <Arduino.h>
#include "Client.h"
// MQTT_MAX_PACKET_SIZE : Maximum packet size
#define MQTT_MAX_PACKET_SIZE 128
// MQTT_KEEPALIVE : keepAlive interval in Seconds
#define MQTT_KEEPALIVE 15
#define MQTTPROTOCOLVERSION 3
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
#define MQTTPUBLISH 3 << 4 // Publish message
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
#define MQTTPINGREQ 12 << 4 // PING Request
#define MQTTPINGRESP 13 << 4 // PING Response
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
#define MQTTReserved 15 << 4 // Reserved
#define MQTTQOS0 (0 << 1)
#define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1)
class PubSubClient {
private:
Client* _client;
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
uint16_t nextMsgId;
unsigned long lastOutActivity;
unsigned long lastInActivity;
bool pingOutstanding;
void (*callback)(char*,uint8_t*,unsigned int);
uint16_t readPacket();
uint8_t readByte();
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
uint8_t *ip;
char* domain;
uint16_t port;
public:
PubSubClient(Client& client);
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
boolean connect(char *);
boolean connect(char *, char *, char *);
boolean connect(char *, char *, uint8_t, uint8_t, char *);
boolean connect(char *, char *, char *, char *, uint8_t, uint8_t, char*);
void disconnect();
boolean publish(char *, char *);
boolean publish(char *, uint8_t *, unsigned int);
boolean publish(char *, uint8_t *, unsigned int, boolean);
boolean publish_P(char *, uint8_t *, unsigned int, boolean);
boolean subscribe(char *);
boolean loop();
boolean connected();
};
#endif
我得到的错误是,
'client' was not declared in this scope
为什么它在函数中很重要?
PubSubClient client(mqttServer, 1883, callback, wifiClient);
在 if 语句范围内。由于 C++ 中的范围规则,只能在 if 语句的右大括号之前访问变量。
你可以改变
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
PubSubClient client(mqttServer, 1883, callback, wifiClient); // why would it care if this was here?
}
至
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
}
PubSubClient client(mqttServer, 1883, callback, wifiClient);
client
的范围仅限于 if
语句。您需要将其声明移到该范围之外,如下所示:
PubSubClient client(/*Some default arguments*/);
if (!checkConnection) {
client = client(mqttServer, 1883, callback, wifiClient);
}
while (!client.connected()) {
/*...*/
}
除了有关 client
范围的答案外,如果您使用最新版本的 PubSubClient
库,现在有用于在客户端对象被创建后设置服务器详细信息的函数创建:
PubSubClient client(wifiClient);
// some time later
client.setServer(mqttServer,1883);
这使得在运行时更改服务器详细信息变得更加容易。
完整的 api 文档可在此处获得:http://pubsubclient.knolleary.net/
我可以连接到mQTT服务器,也可以从客户端订阅。但是如何从客户端获取订阅数据
它显示 1
str=client.subscribe(testq);
if(str>0)
{
Serial.println(str);
}
表示订阅成功
但是如何打印订阅的数据
请帮助我
我正在为 MQTT 连接使用一个库,我想创建一个函数来创建对象而不是在预处理器中设置它。原因是因为我需要在程序启动后抓取它连接到哪个服务器运行.
这是我的 mqttConnection 函数:
void mqttConnection(bool checkConnection) {
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
PubSubClient client(mqttServer, 1883, callback, wifiClient); // why would it care if this was here?
}
// Connecting to MQTT broker, loop until connected
while (!client.connected()){
if(!client.connect("device-name")) {
Serial.println("MQTT Broker Connection Failed. Trying Again...");
}
else {
Serial.println("MQTT Broker Connection Success!");
if(client.subscribe(mqttTopic)) {
Serial.println("Subscription Successful! Woot!");
}
}
}
}
这是Class:
#ifndef PubSubClient_h
#define PubSubClient_h
#include <Arduino.h>
#include "Client.h"
// MQTT_MAX_PACKET_SIZE : Maximum packet size
#define MQTT_MAX_PACKET_SIZE 128
// MQTT_KEEPALIVE : keepAlive interval in Seconds
#define MQTT_KEEPALIVE 15
#define MQTTPROTOCOLVERSION 3
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
#define MQTTPUBLISH 3 << 4 // Publish message
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
#define MQTTPINGREQ 12 << 4 // PING Request
#define MQTTPINGRESP 13 << 4 // PING Response
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
#define MQTTReserved 15 << 4 // Reserved
#define MQTTQOS0 (0 << 1)
#define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1)
class PubSubClient {
private:
Client* _client;
uint8_t buffer[MQTT_MAX_PACKET_SIZE];
uint16_t nextMsgId;
unsigned long lastOutActivity;
unsigned long lastInActivity;
bool pingOutstanding;
void (*callback)(char*,uint8_t*,unsigned int);
uint16_t readPacket();
uint8_t readByte();
boolean write(uint8_t header, uint8_t* buf, uint16_t length);
uint16_t writeString(char* string, uint8_t* buf, uint16_t pos);
uint8_t *ip;
char* domain;
uint16_t port;
public:
PubSubClient(Client& client);
PubSubClient(uint8_t *, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
PubSubClient(char*, uint16_t, void(*)(char*,uint8_t*,unsigned int),Client& client);
boolean connect(char *);
boolean connect(char *, char *, char *);
boolean connect(char *, char *, uint8_t, uint8_t, char *);
boolean connect(char *, char *, char *, char *, uint8_t, uint8_t, char*);
void disconnect();
boolean publish(char *, char *);
boolean publish(char *, uint8_t *, unsigned int);
boolean publish(char *, uint8_t *, unsigned int, boolean);
boolean publish_P(char *, uint8_t *, unsigned int, boolean);
boolean subscribe(char *);
boolean loop();
boolean connected();
};
#endif
我得到的错误是,
'client' was not declared in this scope
为什么它在函数中很重要?
PubSubClient client(mqttServer, 1883, callback, wifiClient);
在 if 语句范围内。由于 C++ 中的范围规则,只能在 if 语句的右大括号之前访问变量。
你可以改变
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
PubSubClient client(mqttServer, 1883, callback, wifiClient); // why would it care if this was here?
}
至
if (!checkConnection) {
strcpy(mqttServer, mqttUrl);
}
PubSubClient client(mqttServer, 1883, callback, wifiClient);
client
的范围仅限于 if
语句。您需要将其声明移到该范围之外,如下所示:
PubSubClient client(/*Some default arguments*/);
if (!checkConnection) {
client = client(mqttServer, 1883, callback, wifiClient);
}
while (!client.connected()) {
/*...*/
}
除了有关 client
范围的答案外,如果您使用最新版本的 PubSubClient
库,现在有用于在客户端对象被创建后设置服务器详细信息的函数创建:
PubSubClient client(wifiClient);
// some time later
client.setServer(mqttServer,1883);
这使得在运行时更改服务器详细信息变得更加容易。
完整的 api 文档可在此处获得:http://pubsubclient.knolleary.net/
我可以连接到mQTT服务器,也可以从客户端订阅。但是如何从客户端获取订阅数据
它显示 1
str=client.subscribe(testq);
if(str>0)
{
Serial.println(str);
}
表示订阅成功
但是如何打印订阅的数据
请帮助我