Browse Source

fix #106 handpickled mDevName changes from #120

pull/121/head
stefan123t 3 years ago
parent
commit
f657ace328
  1. 17
      tools/esp8266/app.cpp
  2. 51
      tools/esp8266/mqtt.h

17
tools/esp8266/app.cpp

@ -129,12 +129,14 @@ void app::setup(uint32_t timeout) {
char mqttUser[MQTT_USER_LEN]; char mqttUser[MQTT_USER_LEN];
char mqttPwd[MQTT_PWD_LEN]; char mqttPwd[MQTT_PWD_LEN];
char mqttTopic[MQTT_TOPIC_LEN]; char mqttTopic[MQTT_TOPIC_LEN];
mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN); char mqttDevName[DEVNAME_LEN];
mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN); mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN);
mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN);
mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_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_INTERVAL, &mMqttInterval);
mEep->read(ADDR_MQTT_PORT, &mqttPort); mEep->read(ADDR_MQTT_PORT, &mqttPort);
if(mqttAddr[0] > 0) { if(mqttAddr[0] > 0) {
mMqttActive = true; mMqttActive = true;
@ -147,13 +149,14 @@ void app::setup(uint32_t timeout) {
if(0 == mqttPort) if(0 == mqttPort)
mqttPort = 1883; mqttPort = 1883;
mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttPort); mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttDevName, mqttPort);
mMqttTicker = 0; mMqttTicker = 0;
mSerialTicker = 0; mSerialTicker = 0;
if(mqttAddr[0] > 0) { if(mqttAddr[0] > 0) {
char topic[30]; char topic[30];
mMqtt.sendMsg("device", mqttDevName);
mMqtt.sendMsg("version", mVersion); mMqtt.sendMsg("version", mVersion);
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mSys->getInverterByPos(i); iv = mSys->getInverterByPos(i);
@ -296,6 +299,7 @@ void app::loop(void) {
Inverter<> *iv = mSys->getInverterByPos(id); Inverter<> *iv = mSys->getInverterByPos(id);
if(NULL != iv) { if(NULL != iv) {
if(iv->isAvailable(mTimestamp)) { if(iv->isAvailable(mTimestamp)) {
DPRINTLN(DBG_INFO, "Inverter: " + String(id));
for(uint8_t i = 0; i < iv->listLen; i++) { for(uint8_t i = 0; i < iv->listLen; i++) {
if(0.0f != iv->getValue(i)) { if(0.0f != iv->getValue(i)) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(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(); yield();
} }
DPRINTLN(DBG_INFO, "");
} }
} }
} }

51
tools/esp8266/mqtt.h

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

Loading…
Cancel
Save