Skip to content

Commit

Permalink
20240529
Browse files Browse the repository at this point in the history
- Add support **USB mode for ALL boards**
- Move device mode selection to Zigbee page
- Increase Zigbee firmware flashing speed (115200 vs 460800)
- Improve Zigbee FW detection mechanism, fix #47
- Remove configSys.keepWeb - network is always active
- Remove vars.hwUartSelIs - no need to check
- Add DNS check logs to web console, could help with #11, #24, #42, #43
- Add cod.m CZC-v1.0 config to boards list (but cause it's the same as UZG-01 ones, it has no sense right now)
- Some code cleanup, and of course a lot of new garbage :)
  • Loading branch information
xyzroe committed May 29, 2024
1 parent ff6426d commit a566040
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 103 deletions.
28 changes: 17 additions & 11 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ extern struct NetworkConfigStruct networkCfg;
extern struct VpnConfigStruct vpnCfg;
extern struct MqttConfigStruct mqttCfg;

String tag = "NVS";

void getNvsStats(int *total, int *used)
{
nvs_stats_t nvsStats;
Expand Down Expand Up @@ -262,7 +260,7 @@ void saveSystemConfig(const SystemConfigStruct &config)
{
preferences.begin(systemConfigKey, false);

preferences.putBool(keepWebKey, config.keepWeb);
//preferences.putBool(keepWebKey, config.keepWeb);
preferences.putBool(disableWebKey, config.disableWeb);
preferences.putBool(webAuthKey, config.webAuth);
preferences.putString(webUserKey, config.webUser);
Expand Down Expand Up @@ -292,7 +290,7 @@ void loadSystemConfig(SystemConfigStruct &config)
{
preferences.begin(systemConfigKey, true);

config.keepWeb = preferences.getBool(keepWebKey, true);
//config.keepWeb = preferences.getBool(keepWebKey, true);
config.disableWeb = preferences.getBool(disableWebKey, false);
config.webAuth = preferences.getBool(webAuthKey, false);
strlcpy(config.webUser, preferences.getString(webUserKey, "").c_str(), sizeof(config.webUser));
Expand Down Expand Up @@ -348,7 +346,7 @@ void updateConfiguration(WebServer &serverWeb, SystemConfigStruct &configSys, Ne
{
case API_PAGE_GENERAL:
{
if (serverWeb.hasArg(coordMode))
/*if (serverWeb.hasArg(coordMode))
{
const uint8_t mode = serverWeb.arg(coordMode).toInt();
if (mode <= 2 && mode >= 0)
Expand All @@ -360,7 +358,7 @@ void updateConfiguration(WebServer &serverWeb, SystemConfigStruct &configSys, Ne
}
}
configSys.keepWeb = serverWeb.hasArg(keepWebKey) == true;
configSys.keepWeb = serverWeb.hasArg(keepWebKey) == true;*/

configSys.disableLedPwr = serverWeb.hasArg(disableLedPwrKey) == true;

Expand Down Expand Up @@ -507,13 +505,22 @@ void updateConfiguration(WebServer &serverWeb, SystemConfigStruct &configSys, Ne
break;
case API_PAGE_ZIGBEE:
{
if (serverWeb.hasArg(coordMode))
{
const uint8_t mode = serverWeb.arg(coordMode).toInt();
if (mode <= 2 && mode >= 0)
{
configSys.workMode = static_cast<WORK_MODE_t>(mode);
}
}

const char *baud = "baud";
if (serverWeb.hasArg(baud))
{
configSys.serialSpeed = serverWeb.arg(baud).toInt();
}

if (serverWeb.hasArg(baud))
if (serverWeb.hasArg(portKey))
{
configSys.socketPort = serverWeb.arg(portKey).toInt();
}
Expand Down Expand Up @@ -754,7 +761,7 @@ void serializeMqttConfigToJson(const MqttConfigStruct &config, JsonObject obj)
// Serialization SystemConfigStruct into JSON
void serializeSystemConfigToJson(const SystemConfigStruct &config, JsonObject obj)
{
obj[keepWebKey] = config.keepWeb;
//obj[keepWebKey] = config.keepWeb;
obj[disableWebKey] = config.disableWeb;
obj[webAuthKey] = config.webAuth;
obj[webUserKey] = config.webUser;
Expand Down Expand Up @@ -784,7 +791,7 @@ void serializeSysVarsToJson(const SysVarsStruct &vars, JsonObject obj)
obj[hwBtnIsKey] = vars.hwBtnIs;
obj[hwLedUsbIsKey] = vars.hwLedUsbIs;
obj[hwLedPwrIsKey] = vars.hwLedPwrIs;
obj[hwUartSelIsKey] = vars.hwUartSelIs;
//obj[hwUartSelIsKey] = vars.hwUartSelIs;
obj[hwZigbeeIsKey] = vars.hwZigbeeIs;

obj[connectedClientsKey] = vars.connectedClients;
Expand Down Expand Up @@ -812,7 +819,6 @@ void serializeSysVarsToJson(const SysVarsStruct &vars, JsonObject obj)

bool loadFileConfigHW()
{
String tag = "HW";
const char *board = "board";
const char *addr = "addr";
const char *pwrPin = "pwrPin";
Expand Down Expand Up @@ -1091,7 +1097,7 @@ bool loadFileConfigGeneral()
systemCfg.disableLedPwr = (uint8_t)doc[disableLedPwrKey];
systemCfg.disableLedUSB = (uint8_t)doc[disableLedUSBKey];
vars.disableLeds = (uint8_t)doc[disableLedsKey];
systemCfg.keepWeb = (uint8_t)doc[keepWebKey];
//systemCfg.keepWeb = (uint8_t)doc[keepWebKey];
strlcpy(systemCfg.timeZone, doc[timeZoneKey] | NTP_TIME_ZONE, sizeof(systemCfg.timeZone));

configFile.close();
Expand Down
20 changes: 15 additions & 5 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct SysVarsStruct
bool hwBtnIs = false;
bool hwLedUsbIs = false;
bool hwLedPwrIs = false;
bool hwUartSelIs = false;
//bool hwUartSelIs = false;
bool hwZigbeeIs = false;

bool connectedSocket[MAX_SOCKET_CLIENTS]; //[10]
Expand Down Expand Up @@ -175,7 +175,7 @@ void loadMqttConfig(MqttConfigStruct &config);

struct SystemConfigStruct
{
bool keepWeb; // when usb mode active
//bool keepWeb; // when usb mode active

bool disableWeb; // when socket connected
bool webAuth;
Expand Down Expand Up @@ -297,23 +297,33 @@ uint8_t temprature_sens_read();

// Conditional logging macros
#if CURRENT_LOG_LEVEL >= LOG_LEVEL_WARN
#define LOGW(format, ...) Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_RED "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__)
#define LOGW(format, ...) \
if (systemCfg.workMode == WORK_MODE_NETWORK) { \
Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_RED "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__); \
}
#else
#define LOGW(format, ...) // Nothing
#endif

#if CURRENT_LOG_LEVEL >= LOG_LEVEL_INFO
#define LOGI(format, ...) Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_GREEN "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__)
#define LOGI(format, ...) \
if (systemCfg.workMode == WORK_MODE_NETWORK) { \
Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_GREEN "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__); \
}
#else
#define LOGI(format, ...) // Nothing
#endif

#if CURRENT_LOG_LEVEL >= LOG_LEVEL_DEBUG
#define LOGD(format, ...) Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_YELLOW "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__)
#define LOGD(format, ...) \
if (systemCfg.workMode == WORK_MODE_NETWORK) { \
Serial.printf(ANSI_COLOR_PURPLE "%d " ANSI_COLOR_RESET ANSI_COLOR_YELLOW "[%s] " ANSI_COLOR_RESET format "\n", millis(), __func__, ##__VA_ARGS__); \
}
#else
#define LOGD(format, ...) // Nothing
#endif


/* ----- Define functions | END -----*/

enum LEDMode
Expand Down
3 changes: 2 additions & 1 deletion src/const/hw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ BrdConfigStruct brdConfigs[] = {
{"TubesZB-poe", .ethConfigIndex = 0, .zbConfigIndex = 5, .mistConfigIndex = 0},
{"TubesZB-poe-2022", .ethConfigIndex = 0, .zbConfigIndex = 6, .mistConfigIndex = 0},
{"TubesZB-poe-2023", .ethConfigIndex = 0, .zbConfigIndex = 7, .mistConfigIndex = 0},
};
{"CZC-1.0", .ethConfigIndex = 2, .zbConfigIndex = 0, .mistConfigIndex = 1},
};
2 changes: 1 addition & 1 deletion src/const/hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct BrdConfigStruct
#define ETH_CFG_CNT 3
#define ZB_CFG_CNT 8
#define MIST_CFG_CNT 3
#define BOARD_CFG_CNT 13
#define BOARD_CFG_CNT 14

struct ThisConfigStruct
{
Expand Down
4 changes: 2 additions & 2 deletions src/const/keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const char *discoveryKey = "discovery";
const char *reconnectIntKey = "reconnectInt";

const char *systemConfigKey = "system-config";
const char *keepWebKey = "keepWeb";
//const char *keepWebKey = "keepWeb";
const char *disableWebKey = "disableWeb";
const char *webAuthKey = "webAuth";
const char *webUserKey = "webUser";
Expand All @@ -77,7 +77,7 @@ const char *systemVarsKey = "system-vars";
const char *hwBtnIsKey = "hwBtnIs";
const char *hwLedUsbIsKey = "hwLedUsbIs";
const char *hwLedPwrIsKey = "hwLedPwrIs";
const char *hwUartSelIsKey = "hwUartSelIs";
//const char *hwUartSelIsKey = "hwUartSelIs";
const char *hwZigbeeIsKey = "hwZigbeeIs";
const char *workModeKey = "workMode";
const char *connectedSocketKey = "connectedSocket";
Expand Down
67 changes: 38 additions & 29 deletions src/etc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,35 @@ void zigbeeRestart()

void usbModeSet(usbMode mode)
{
if (vars.hwUartSelIs)
{
String modeStr = (mode == ZIGBEE) ? "ZIGBEE" : "ESP";
bool pinValue = (mode == ZIGBEE) ? HIGH : LOW;
String msg = "Switched USB to " + modeStr + "";
printLogMsg(msg);
digitalWrite(hwConfig.mist.uartSelPin, pinValue);
if (pinValue)
{
ledControl.modeLED.mode = LED_ON;
}
else
{
ledControl.modeLED.mode = LED_OFF;
}
// if (vars.hwUartSelIs)
//{
// String modeStr = (mode == ZIGBEE) ? "ZIGBEE" : "ESP";
bool pinValue = (mode == ZIGBEE) ? HIGH : LOW;
// String msg = "Switched USB to " + modeStr + "";
// printLogMsg(msg);

if (mode == ZIGBEE)
{
Serial.updateBaudRate(systemCfg.serialSpeed);
}
else
{
Serial.updateBaudRate(115200);
}
// digitalWrite(hwConfig.mist.uartSelPin, pinValue);
if (pinValue)
{
ledControl.modeLED.mode = LED_ON;
}
else
{
LOGD("NO vars.hwUartSelIs");
ledControl.modeLED.mode = LED_OFF;
}
//}
// else
//{
// LOGD("NO vars.hwUartSelIs");
//}
}

void getDeviceID(char *arr)
Expand Down Expand Up @@ -387,25 +396,27 @@ void checkDNS(bool setup = false)
{
const char *wifiKey = "WiFi";
const char *ethKey = "ETH";
const char *savedKey = "Saved";
const char *restoredKey = "Restored";
const char *dnsTagKey = "[DNS]";
char buffer[100];

if (networkCfg.wifiEnable)
{
IPAddress currentWifiDNS = WiFi.dnsIP();
if (setup)
{
savedWifiDNS = currentWifiDNS;
LOGI("Saved %s DNS - %s", wifiKey, savedWifiDNS.toString().c_str());
snprintf(buffer, sizeof(buffer), "%s %s %s - %s", dnsTagKey, savedKey, wifiKey, savedWifiDNS.toString().c_str());
printLogMsg(buffer);
}
else
{
if (currentWifiDNS != savedWifiDNS)
{
WiFi.config(WiFi.localIP(), WiFi.gatewayIP(), WiFi.subnetMask(), savedWifiDNS);
LOGI("Updated %s DNS - %s", wifiKey, savedWifiDNS.toString().c_str());
}
else
{
//LOGD("No update on %s DNS", wifiKey);
snprintf(buffer, sizeof(buffer), "%s %s %s - %s", dnsTagKey, restoredKey, wifiKey, savedWifiDNS.toString().c_str());
printLogMsg(buffer);
}
}
}
Expand All @@ -416,18 +427,16 @@ void checkDNS(bool setup = false)
if (setup)
{
savedEthDNS = currentEthDNS;
LOGI("Saved %s DNS - %s", ethKey, savedEthDNS.toString().c_str());
snprintf(buffer, sizeof(buffer), "%s %s %s - %s", dnsTagKey, savedKey, ethKey, savedEthDNS.toString().c_str());
printLogMsg(buffer);
}
else
{
if (currentEthDNS != savedEthDNS)
{
ETH.config(ETH.localIP(), ETH.gatewayIP(), ETH.subnetMask(), savedEthDNS);
LOGI("Updated %s DNS - %s", ethKey, savedEthDNS.toString().c_str());
}
else
{
//LOGD("No update on %s DNS", ethKey);
snprintf(buffer, sizeof(buffer), "%s %s %s - %s", dnsTagKey, restoredKey, ethKey, savedEthDNS.toString().c_str());
printLogMsg(buffer);
}
}
}
Expand Down Expand Up @@ -507,7 +516,7 @@ void setTimezone(String timezone)

String timeNow = asctime(&timeinfo);
timeNow.remove(timeNow.length() - 1);
printLogMsg("Local time: " + timeNow);
printLogMsg("[Time] " + timeNow);
}

const char *getGmtOffsetForZone(const char *zone)
Expand Down
Loading

0 comments on commit a566040

Please sign in to comment.