diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index d6014724..ebf5142d 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -129,12 +129,14 @@ void app::setup(uint32_t timeout) { char mqttUser[MQTT_USER_LEN]; char mqttPwd[MQTT_PWD_LEN]; char mqttTopic[MQTT_TOPIC_LEN]; - mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN); - mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN); - mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); - mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN); + char mqttDevName[DEVNAME_LEN]; + mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN); + mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN); + mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); + mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN); + mEep->read(ADDR_DEVNAME, mqttDevName, DEVNAME_LEN); //mEep->read(ADDR_MQTT_INTERVAL, &mMqttInterval); - mEep->read(ADDR_MQTT_PORT, &mqttPort); + mEep->read(ADDR_MQTT_PORT, &mqttPort); if(mqttAddr[0] > 0) { mMqttActive = true; @@ -147,13 +149,14 @@ void app::setup(uint32_t timeout) { if(0 == mqttPort) mqttPort = 1883; - mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttPort); + mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttDevName, mqttPort); mMqttTicker = 0; mSerialTicker = 0; if(mqttAddr[0] > 0) { char topic[30]; + mMqtt.sendMsg("device", mqttDevName); mMqtt.sendMsg("version", mVersion); for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { iv = mSys->getInverterByPos(i); @@ -296,6 +299,7 @@ void app::loop(void) { Inverter<> *iv = mSys->getInverterByPos(id); if(NULL != iv) { if(iv->isAvailable(mTimestamp)) { + DPRINTLN(DBG_INFO, "Inverter: " + String(id)); for(uint8_t i = 0; i < iv->listLen; i++) { if(0.0f != iv->getValue(i)) { snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); @@ -304,6 +308,7 @@ void app::loop(void) { } yield(); } + DPRINTLN(DBG_INFO, ""); } } } diff --git a/tools/esp8266/mqtt.h b/tools/esp8266/mqtt.h index 8af53d83..e8aecb10 100644 --- a/tools/esp8266/mqtt.h +++ b/tools/esp8266/mqtt.h @@ -16,25 +16,28 @@ class mqtt { mClient = new PubSubClient(mEspClient); mAddressSet = false; - memset(mAddr, 0, MQTT_ADDR_LEN); - memset(mUser, 0, MQTT_USER_LEN); - memset(mPwd, 0, MQTT_PWD_LEN); - memset(mTopic, 0, MQTT_TOPIC_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(mUser, MQTT_USER_LEN, "%s", user); - snprintf(mPwd, MQTT_PWD_LEN, "%s", pwd); - snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic); + 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_*/