|
|
@ -16,25 +16,28 @@ class mqtt { |
|
|
|
mClient = new PubSubClient(mEspClient); |
|
|
|
mAddressSet = false; |
|
|
|
|
|
|
|
memset(mAddr, 0, MQTT_ADDR_LEN); |
|
|
|
memset(mBroker, 0, MQTT_ADDR_LEN); |
|
|
|
memset(mUser, 0, MQTT_USER_LEN); |
|
|
|
memset(mPwd, 0, MQTT_PWD_LEN); |
|
|
|
memset(mTopic, 0, MQTT_TOPIC_LEN); |
|
|
|
memset(mDevName, 0, DEVNAME_LEN); |
|
|
|
} |
|
|
|
|
|
|
|
~mqtt() { } |
|
|
|
|
|
|
|
void setup(const char *addr, const char *topic, const char *user, const char *pwd, uint16_t port) { |
|
|
|
void setup(const char *broker, const char *topic, const char *user, const char *pwd, const char *devname, uint16_t port) { |
|
|
|
DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup")); |
|
|
|
mAddressSet = true; |
|
|
|
mClient->setServer(addr, port); |
|
|
|
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE); |
|
|
|
|
|
|
|
mPort = port; |
|
|
|
snprintf(mAddr, MQTT_ADDR_LEN, "%s", addr); |
|
|
|
snprintf(mBroker, MQTT_ADDR_LEN, "%s", broker); |
|
|
|
snprintf(mUser, MQTT_USER_LEN, "%s", user); |
|
|
|
snprintf(mPwd, MQTT_PWD_LEN, "%s", pwd); |
|
|
|
snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic); |
|
|
|
snprintf(mDevName, DEVNAME_LEN, "%s", devname); |
|
|
|
|
|
|
|
mClient->setServer(mBroker, mPort); |
|
|
|
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE); |
|
|
|
} |
|
|
|
|
|
|
|
void sendMsg(const char *topic, const char *msg) { |
|
|
@ -60,9 +63,9 @@ class mqtt { |
|
|
|
return mClient->connected(); |
|
|
|
} |
|
|
|
|
|
|
|
char *getAddr(void) { |
|
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getAddr"));
|
|
|
|
return mAddr; |
|
|
|
char *getBroker(void) { |
|
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getBroker"));
|
|
|
|
return mBroker; |
|
|
|
} |
|
|
|
|
|
|
|
char *getUser(void) { |
|
|
@ -80,6 +83,11 @@ class mqtt { |
|
|
|
return mTopic; |
|
|
|
} |
|
|
|
|
|
|
|
char *getDevName(void) { |
|
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getDevName"));
|
|
|
|
return mDevName; |
|
|
|
} |
|
|
|
|
|
|
|
uint16_t getPort(void) { |
|
|
|
return mPort; |
|
|
|
} |
|
|
@ -93,15 +101,19 @@ class mqtt { |
|
|
|
|
|
|
|
private: |
|
|
|
void reconnect(void) { |
|
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:reconnect"));
|
|
|
|
DPRINTLN(DBG_DEBUG, F("mqtt.h:reconnect")); |
|
|
|
DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) ); |
|
|
|
DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) ); |
|
|
|
if(!mClient->connected()) { |
|
|
|
if(strlen(mAddr) > 0) { |
|
|
|
if(strlen(mDevName) > 0) { |
|
|
|
if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) |
|
|
|
mClient->connect(mAddr, mUser, mPwd); |
|
|
|
mClient->connect(mDevName, mUser, mPwd); |
|
|
|
else |
|
|
|
mClient->connect(mAddr); |
|
|
|
mClient->connect(mDevName); |
|
|
|
} |
|
|
|
} |
|
|
|
DPRINTLN(DBG_DEBUG, F("MQTT mClient->_state ") + String(mClient->state()) ); |
|
|
|
DPRINTLN(DBG_DEBUG, F("WIFI mEspClient.status ") + String(mEspClient.status()) ); |
|
|
|
} |
|
|
|
|
|
|
|
WiFiClient mEspClient; |
|
|
@ -109,10 +121,11 @@ class mqtt { |
|
|
|
|
|
|
|
bool mAddressSet; |
|
|
|
uint16_t mPort; |
|
|
|
char mAddr[MQTT_ADDR_LEN]; |
|
|
|
char mBroker[MQTT_ADDR_LEN]; |
|
|
|
char mUser[MQTT_USER_LEN]; |
|
|
|
char mPwd[MQTT_PWD_LEN]; |
|
|
|
char mTopic[MQTT_TOPIC_LEN]; |
|
|
|
char mDevName[DEVNAME_LEN]; |
|
|
|
}; |
|
|
|
|
|
|
|
#endif /*__MQTT_H_*/ |
|
|
|