|
@ -12,6 +12,8 @@ |
|
|
|
|
|
|
|
|
class mqtt { |
|
|
class mqtt { |
|
|
public: |
|
|
public: |
|
|
|
|
|
PubSubClient *mClient; |
|
|
|
|
|
|
|
|
mqtt() { |
|
|
mqtt() { |
|
|
mClient = new PubSubClient(mEspClient); |
|
|
mClient = new PubSubClient(mEspClient); |
|
|
mAddressSet = false; |
|
|
mAddressSet = false; |
|
@ -35,6 +37,10 @@ class mqtt { |
|
|
snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic); |
|
|
snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void setCallback(void (*func)(const char* topic, byte* payload, unsigned int length)){ |
|
|
|
|
|
mClient->setCallback(func); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void sendMsg(const char *topic, const char *msg) { |
|
|
void sendMsg(const char *topic, const char *msg) { |
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
|
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
|
|
|
char top[64]; |
|
|
char top[64]; |
|
@ -79,25 +85,25 @@ class mqtt { |
|
|
|
|
|
|
|
|
void loop() { |
|
|
void loop() { |
|
|
//DPRINT(F("m"));
|
|
|
//DPRINT(F("m"));
|
|
|
//if(!mClient->connected())
|
|
|
if(!mClient->connected()) |
|
|
// reconnect();
|
|
|
reconnect(); |
|
|
mClient->loop(); |
|
|
mClient->loop(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
|
void reconnect(void) { |
|
|
void reconnect(void) { |
|
|
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:reconnect"));
|
|
|
DPRINTLN(DBG_INFO, F("mqtt.h:reconnect")); |
|
|
if(!mClient->connected()) { |
|
|
if(!mClient->connected()) { |
|
|
if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) |
|
|
if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) |
|
|
mClient->connect(DEF_DEVICE_NAME, mUser, mPwd); |
|
|
mClient->connect(DEF_DEVICE_NAME, mUser, mPwd); |
|
|
else |
|
|
else |
|
|
mClient->connect(DEF_DEVICE_NAME); |
|
|
mClient->connect(DEF_DEVICE_NAME); |
|
|
} |
|
|
} |
|
|
|
|
|
mClient->subscribe("home/huette/powerset"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
WiFiClient mEspClient; |
|
|
WiFiClient mEspClient; |
|
|
PubSubClient *mClient; |
|
|
|
|
|
|
|
|
|
|
|
bool mAddressSet; |
|
|
bool mAddressSet; |
|
|
uint16_t mPort; |
|
|
uint16_t mPort; |
|
|
char mUser[MQTT_USER_LEN]; |
|
|
char mUser[MQTT_USER_LEN]; |
|
|