Skip to content

Commit

Permalink
improved mqtt
Browse files Browse the repository at this point in the history
removed wrong "inverter type can't be detected!" messages
repaired NTP and static IP #459
MQTT status about availability and produce are retain messages now
  • Loading branch information
lumapu committed Nov 28, 2022
1 parent f4c3aea commit 0a5833e
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 87 deletions.
41 changes: 12 additions & 29 deletions src/config/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <LittleFS.h>
#include <ArduinoJson.h>
#include "../utils/dbg.h"
#include "../utils/helper.h"
#include "../defines.h"

/**
Expand Down Expand Up @@ -223,25 +224,6 @@ class settings {
return saveSettings();
}

String ip2Str(uint8_t ip[]) {
return String(ip[0]) + F(".")
+ String(ip[1]) + F(".")
+ String(ip[2]) + F(".")
+ String(ip[3]);
}

void ip2Arr(uint8_t ip[], const char *ipStr) {
char *tmp = new char[strlen(ipStr)];
strncpy(tmp, ipStr, strlen(ipStr));
char *p = strtok(tmp, ".");
uint8_t i = 0;
while(NULL != p) {
ip[i++] = atoi(p);
p = strtok(NULL, ".");
}
delete[] tmp;
}

private:
void loadDefaults(bool wifi = true) {
DPRINTLN(DBG_INFO, F("loadDefaults"));
Expand Down Expand Up @@ -288,25 +270,26 @@ class settings {

void jsonWifi(JsonObject obj, bool set = false) {
if(set) {
char buf[16];
obj[F("ssid")] = mCfg.sys.stationSsid;
obj[F("pwd")] = mCfg.sys.stationPwd;
obj[F("dev")] = mCfg.sys.deviceName;
obj[F("adm")] = mCfg.sys.adminPwd;
obj[F("ip")] = ip2Str(mCfg.sys.ip.ip);
obj[F("mask")] = ip2Str(mCfg.sys.ip.mask);
obj[F("dns1")] = ip2Str(mCfg.sys.ip.dns1);
obj[F("dns2")] = ip2Str(mCfg.sys.ip.dns2);
obj[F("gtwy")] = ip2Str(mCfg.sys.ip.gateway);
ah::ip2Char(mCfg.sys.ip.ip, buf); obj[F("ip")] = String(buf);
ah::ip2Char(mCfg.sys.ip.mask, buf); obj[F("mask")] = String(buf);
ah::ip2Char(mCfg.sys.ip.dns1, buf); obj[F("dns1")] = String(buf);
ah::ip2Char(mCfg.sys.ip.dns2, buf); obj[F("dns2")] = String(buf);
ah::ip2Char(mCfg.sys.ip.gateway, buf); obj[F("gtwy")] = String(buf);
} else {
snprintf(mCfg.sys.stationSsid, SSID_LEN, "%s", obj[F("ssid")].as<const char*>());
snprintf(mCfg.sys.stationPwd, PWD_LEN, "%s", obj[F("pwd")].as<const char*>());
snprintf(mCfg.sys.deviceName, DEVNAME_LEN, "%s", obj[F("dev")].as<const char*>());
snprintf(mCfg.sys.adminPwd, PWD_LEN, "%s", obj[F("adm")].as<const char*>());
ip2Arr(mCfg.sys.ip.ip, obj[F("ip")]);
ip2Arr(mCfg.sys.ip.mask, obj[F("mask")]);
ip2Arr(mCfg.sys.ip.dns1, obj[F("dns1")]);
ip2Arr(mCfg.sys.ip.dns2, obj[F("dns2")]);
ip2Arr(mCfg.sys.ip.gateway, obj[F("gtwy")]);
ah::ip2Arr(mCfg.sys.ip.ip, obj[F("ip")].as<const char*>());
ah::ip2Arr(mCfg.sys.ip.mask, obj[F("mask")].as<const char*>());
ah::ip2Arr(mCfg.sys.ip.dns1, obj[F("dns1")].as<const char*>());
ah::ip2Arr(mCfg.sys.ip.dns2, obj[F("dns2")].as<const char*>());
ah::ip2Arr(mCfg.sys.ip.gateway, obj[F("gtwy")].as<const char*>());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 43
#define VERSION_PATCH 44

//-------------------------------------
typedef struct {
Expand Down
2 changes: 1 addition & 1 deletion src/hm/hmSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class HmSystem {
break;
}
}
else
else if(p->config->serial.u64 != 0ULL)
DPRINTLN(DBG_ERROR, F("inverter type can't be detected!"));

p->init();
Expand Down
11 changes: 7 additions & 4 deletions src/publisher/pubMqtt.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ class PubMqtt {
}

void payloadEventListener(uint8_t cmd) {
mSendList.push(cmd);
if(mClient.connected()) // prevent overflow if MQTT broker is not reachable but set
mSendList.push(cmd);
}

void setSubscriptionCb(subscriptionCb cb) {
Expand Down Expand Up @@ -185,6 +186,7 @@ class PubMqtt {
void onWifiConnect(const WiFiEventStationModeGotIP& event) {
DPRINTLN(DBG_VERBOSE, F("MQTT connecting"));
mClient.connect();
mEnReconnect = true;
}

void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
Expand All @@ -197,6 +199,7 @@ class PubMqtt {
case SYSTEM_EVENT_STA_GOT_IP:
DPRINTLN(DBG_VERBOSE, F("MQTT connecting"));
mClient.connect();
mEnReconnect = true;
break;

case SYSTEM_EVENT_STA_DISCONNECTED:
Expand Down Expand Up @@ -360,15 +363,15 @@ class PubMqtt {
(status == MQTT_STATUS_AVAIL_NOT_PROD) ? "not " : "",
(status == MQTT_STATUS_NOT_AVAIL_NOT_PROD) ? "" : "producing"
);
publish(topic, val);
publish(topic, val, true);

snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/available", iv->config->name);
snprintf(val, 40, "%d", status);
publish(topic, val);
publish(topic, val, true);

snprintf(topic, 32 + MAX_NAME_LENGTH, "%s/last_success", iv->config->name);
snprintf(val, 40, "%i", iv->getLastTs(rec) * 1000);
publish(topic, val);
publish(topic, val, true);
}

// data
Expand Down
29 changes: 29 additions & 0 deletions src/utils/helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://github.com/lumpapu/ahoy
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

#include "helper.h"

namespace ah {
void ip2Arr(uint8_t ip[], const char *ipStr) {
memset(ip, 0, 4);
char *tmp = new char[strlen(ipStr)+1];
strncpy(tmp, ipStr, strlen(ipStr)+1);
char *p = strtok(tmp, ".");
uint8_t i = 0;
while(NULL != p) {
ip[i++] = atoi(p);
p = strtok(NULL, ".");
}
delete[] tmp;
}

// note: char *str needs to be at least 16 bytes long
void ip2Char(uint8_t ip[], char *str) {
if(0 == ip[0])
str[0] = '\0';
else
snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
}
19 changes: 19 additions & 0 deletions src/utils/helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://ahoydtu.de
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------

#ifndef __HELPER_H__
#define __HELPER_H__

#include <cstdint>
#include <cstring>
#include <stdio.h>
#include <stdlib.h>

namespace ah {
void ip2Arr(uint8_t ip[], const char *ipStr);
void ip2Char(uint8_t ip[], char *str);
}

#endif /*__HELPER_H__*/
4 changes: 2 additions & 2 deletions src/web/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ <h3>Support this project:</h3>
parseStat(obj["statistics"]);
parseIv(obj["inverter"]);
parseWarnInfo(obj["warnings"], obj["infos"]);
document.getElementById("refresh").innerHTML = obj["refresh_interval"];
document.getElementById("refresh").innerHTML = 10;
if(exeOnce) {
window.setInterval("getAjax('/api/index', parse)", obj["refresh_interval"] * 1000);
window.setInterval("getAjax('/api/index', parse)", 10000);
exeOnce = false;
}
}
Expand Down
45 changes: 18 additions & 27 deletions src/web/web.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "web.h"

#include "../utils/ahoyTimer.h"
#include "../utils/helper.h"

#include "html/h/index_html.h"
#include "html/h/login_html.h"
Expand Down Expand Up @@ -343,28 +344,16 @@ void web::showSave(AsyncWebServerRequest *request) {


// static ip
if(request->arg("ipAddr") != "") {
request->arg("ipAddr").toCharArray(buf, SSID_LEN);
ip2Arr(mConfig->sys.ip.ip, buf);
if(request->arg("ipMask") != "") {
request->arg("ipMask").toCharArray(buf, SSID_LEN);
ip2Arr(mConfig->sys.ip.mask, buf);
}
if(request->arg("ipDns1") != "") {
request->arg("ipDns1").toCharArray(buf, SSID_LEN);
ip2Arr(mConfig->sys.ip.dns1, buf);
}
if(request->arg("ipDns2") != "") {
request->arg("ipDns2").toCharArray(buf, SSID_LEN);
ip2Arr(mConfig->sys.ip.dns2, buf);
}
if(request->arg("ipGateway") != "") {
request->arg("ipGateway").toCharArray(buf, SSID_LEN);
ip2Arr(mConfig->sys.ip.gateway, buf);
}
}
else
memset(&mConfig->sys.ip.ip, 0, 4);
request->arg("ipAddr").toCharArray(buf, 20);
ah::ip2Arr(mConfig->sys.ip.ip, buf);
request->arg("ipMask").toCharArray(buf, 20);
ah::ip2Arr(mConfig->sys.ip.mask, buf);
request->arg("ipDns1").toCharArray(buf, 20);
ah::ip2Arr(mConfig->sys.ip.dns1, buf);
request->arg("ipDns2").toCharArray(buf, 20);
ah::ip2Arr(mConfig->sys.ip.dns2, buf);
request->arg("ipGateway").toCharArray(buf, 20);
ah::ip2Arr(mConfig->sys.ip.gateway, buf);


// inverter
Expand Down Expand Up @@ -437,12 +426,14 @@ void web::showSave(AsyncWebServerRequest *request) {
String addr = request->arg("mqttAddr");
addr.trim();
addr.toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN);
request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN);
if(request->arg("mqttPwd") != "{PWD}")
request->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN);
request->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN);
mConfig->mqtt.port = request->arg("mqttPort").toInt();
}
else
mConfig->mqtt.broker[0] = '\0';
request->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN);
if(request->arg("mqttPwd") != "{PWD}")
request->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN);
request->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN);
mConfig->mqtt.port = request->arg("mqttPort").toInt();

// serial console
if(request->arg("serIntvl") != "") {
Expand Down
9 changes: 0 additions & 9 deletions src/web/web.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,6 @@ class web {
void onSerial(AsyncWebServerRequest *request);
void onSystem(AsyncWebServerRequest *request);

void ip2Arr(uint8_t ip[], char *ipStr) {
char *p = strtok(ipStr, ".");
uint8_t i = 0;
while(NULL != p) {
ip[i++] = atoi(p);
p = strtok(NULL, ".");
}
}

#ifdef ENABLE_JSON_EP
void showJson(void);
#endif
Expand Down
15 changes: 7 additions & 8 deletions src/web/webApi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,12 @@ void webApi::getSerial(JsonObject obj) {

//-----------------------------------------------------------------------------
void webApi::getStaticIp(JsonObject obj) {
if(mConfig->sys.ip.ip[0] != 0) {
obj[F("ip")] = ip2String(mConfig->sys.ip.ip);
obj[F("mask")] = ip2String(mConfig->sys.ip.mask);
obj[F("dns1")] = ip2String(mConfig->sys.ip.dns1);
obj[F("dns2")] = ip2String(mConfig->sys.ip.dns2);
obj[F("gateway")] = ip2String(mConfig->sys.ip.gateway);
}
char buf[16];
ah::ip2Char(mConfig->sys.ip.ip, buf); obj[F("ip")] = String(buf);
ah::ip2Char(mConfig->sys.ip.mask, buf); obj[F("mask")] = String(buf);
ah::ip2Char(mConfig->sys.ip.dns1, buf); obj[F("dns1")] = String(buf);
ah::ip2Char(mConfig->sys.ip.dns2, buf); obj[F("dns2")] = String(buf);
ah::ip2Char(mConfig->sys.ip.gateway, buf); obj[F("gateway")] = String(buf);
}


Expand Down Expand Up @@ -397,7 +396,7 @@ void webApi::getIndex(JsonObject obj) {
JsonArray warn = obj.createNestedArray(F("warnings"));
if(!mApp->mSys->Radio.isChipConnected())
warn.add(F("your NRF24 module can't be reached, check the wiring and pinout"));
if(!mApp->mqttIsConnected())
if((!mApp->mqttIsConnected()) && (String(mConfig->mqtt.broker).length() > 0))
warn.add(F("MQTT is not connected"));

JsonArray info = obj.createNestedArray(F("infos"));
Expand Down
6 changes: 0 additions & 6 deletions src/web/webApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ class webApi {
return (int)(value * 1000 + 0.5) / 1000.0;
}

String ip2String(uint8_t ip[]) {
char str[16];
snprintf(str, 16, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
return String(str);
}

AsyncWebServer *mSrv;
app *mApp;

Expand Down

0 comments on commit 0a5833e

Please sign in to comment.