diff --git a/.gitignore b/.gitignore index 68e14c8..2f37a29 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ .gcc-flags.json .clang_complete bin -commit \ No newline at end of file +commit +todo \ No newline at end of file diff --git a/platformio.ini b/platformio.ini index ddf141d..7c585f1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -1,41 +1,46 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter +; Upload options: custom upload port, speed and extra flags +; Library options: dependencies, extra library storages +; Advanced options: extra scripting +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + [platformio] default_envs = prod [env] framework = arduino - platform = espressif32 - -lib_deps = +lib_deps = bblanchon/ArduinoJson@^6.18.3 lorol/LittleFS_esp32@^1.0.6 rlogiacco/CircularBuffer@^1.3.3 plerup/EspSoftwareSerial@^6.13.2 knolleary/PubSubClient@^2.8 marian-craciunescu/ESP32Ping@^1.7 - + milesburton/DallasTemperature@^3.9.1 + robtillaart/DS18B20@^0.1.11 board = esp32dev board_build.partitions = min_spiffs.csv - monitor_filters = esp32_exception_decoder monitor_speed = 115200 - [env:prod] build_flags = - ${env.build_flags} - -D=${PIOENV} - + ${env.build_flags} + -D=${PIOENV} extra_scripts = pre:tools/version_increment_pre.py post:tools/build.py [env:debug] build_flags = - ${env.build_flags} - -D=${PIOENV} + ${env.build_flags} + -D=${PIOENV} -DDEBUG - extra_scripts = pre:tools/version_increment_pre.py post:tools/debug_build.py diff --git a/src/Version.h b/src/Version.h index c8f449f..30f31d8 100644 --- a/src/Version.h +++ b/src/Version.h @@ -1,9 +1,9 @@ // AUTO GENERATED FILE, DO NOT EDIT #ifndef VERSION - #define VERSION "0.6.2" + #define VERSION "0.6.3" #endif #ifndef BUILD_TIMESTAMP - #define BUILD_TIMESTAMP "2021-10-31 03:56:11.081030" + #define BUILD_TIMESTAMP "2021-10-31 17:59:39.672556" #endif \ No newline at end of file diff --git a/src/config.h b/src/config.h index 0cd34c0..74c75a9 100644 --- a/src/config.h +++ b/src/config.h @@ -29,7 +29,7 @@ #define ETH_MDC_PIN_2 23 #define ETH_MDIO_PIN_2 18 //ZIGBEE -#define RESTART_ZIGBEE_2 12 +#define RESTART_ZIGBEE_2 16 #define FLASH_ZIGBEE_2 32 #define ZRXD_2 36 #define ZTXD_2 4 @@ -51,7 +51,7 @@ #define PRODUCTION 1 #define FLASH 0 -#define MAX_SOCKET_CLIENTS 10 +#define MAX_SOCKET_CLIENTS 5 #define BAUD_RATE 38400 #define TCP_LISTEN_PORT 9999 @@ -59,6 +59,8 @@ #define FORMAT_LITTLEFS_IF_FAILED true +#define ONE_WIRE_BUS 33 + struct ConfigSettingsStruct { bool enableWiFi; @@ -104,6 +106,7 @@ struct ConfigSettingsStruct bool webAuth; char webUser[50]; char webPass[50]; + bool disableEmerg; }; struct InfosStruct diff --git a/src/etc.cpp b/src/etc.cpp index 7a64113..ad2cd94 100644 --- a/src/etc.cpp +++ b/src/etc.cpp @@ -10,8 +10,39 @@ #include "mqtt.h" #include "web.h" +#include +#include +#include + +OneWire oneWire(ONE_WIRE_BUS); + +DS18B20 sensor(&oneWire); + extern struct ConfigSettingsStruct ConfigSettings; +void oneWireBegin() +{ + DEBUG_PRINTLN(F("oneWire begin")); + sensor.begin(); +} + +void oneWireRead(String &DStemp) +{ + sensor.requestTemperatures(); + float tempC = sensor.getTempC(); + DEBUG_PRINTLN(tempC); + if(tempC != DEVICE_DISCONNECTED_C) + { + DEBUG_PRINTLN(F("oneWire OK")); + DStemp = tempC; + } + else + { + DEBUG_PRINTLN(F("oneWire error")); + DStemp = 255; + } +} + void getReadableTime(String &readableTime, unsigned long beginTime) { unsigned long currentMillis; diff --git a/src/etc.h b/src/etc.h index aaa9587..726a70c 100644 --- a/src/etc.h +++ b/src/etc.h @@ -9,6 +9,9 @@ uint8_t temprature_sens_read(); #endif uint8_t temprature_sens_read(); +void oneWireBegin(); +void oneWireRead(String &DStemp); + void getCPUtemp(String &CPUtemp); void getBlankCPUtemp(String &CPUtemp); //void parse_ip_address(IPAddress &ip, const char *str) diff --git a/src/html.h b/src/html.h index 6d69cc0..c3a0db8 100644 --- a/src/html.h +++ b/src/html.h @@ -63,7 +63,7 @@ const char HTTP_WIFI[] PROGMEM = "
" "
" "" - "" + "" "
" "
" "
" @@ -82,7 +82,7 @@ const char HTTP_WIFI[] PROGMEM = "
" "
" "
" - "" + "" "" "
" "
" @@ -98,6 +98,12 @@ const char HTTP_WIFI[] PROGMEM = "" "" "" + "
" + "
" + "" + "" + "
" + "
" "" ""; @@ -219,7 +225,8 @@ const char HTTP_ROOT[] PROGMEM = "
" "Socket : {{connectedSocket}}" "
Uptime : {{uptime}}" - "
Temperature : {{deviceTemp}} °C" + "
ESP temperature : {{deviceTemp}} °C" + "{{dsTemp}}" "
FW version : " VERSION "
Hardware : {{hwRev}}" "
ESP32 model : {{espModel}}" diff --git a/src/main.cpp b/src/main.cpp index 1b183ac..7367639 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,12 +25,13 @@ #include "mqtt.h" #include + // application config unsigned long timeLog; ConfigSettingsStruct ConfigSettings; InfosStruct Infos; bool configOK = false; -String modeWiFi = "STA"; +//String modeWiFi = "STA"; // serial end ethernet buffer size #define BUFFER_SIZE 256 @@ -39,6 +40,7 @@ String modeWiFi = "STA"; MDNSResponder mdns; void mDNS_start(); +bool setupSTAWifi(); void saveEmergencyWifi(bool state) { @@ -153,6 +155,10 @@ void WiFiEvent(WiFiEvent_t event) ConfigSettings.connectedEther = false; ConfigSettings.disconnectEthTime = millis(); break; + case SYSTEM_EVENT_STA_DISCONNECTED: + DEBUG_PRINTLN(F("WIFI STA DISCONNECTED")); + setupSTAWifi(); + break; default: break; } @@ -253,7 +259,7 @@ bool loadConfigWifi() File configFile = LITTLEFS.open(path, FILE_READ); if (!configFile) { - String StringConfig = "{\"enableWiFi\":0,\"ssid\":\"\",\"pass\":\"\",\"dhcpWiFi\":1,\"ip\":\"\",\"mask\":\"\",\"gw\":\"\"}"; + String StringConfig = "{\"enableWiFi\":0,\"ssid\":\"\",\"pass\":\"\",\"dhcpWiFi\":1,\"ip\":\"\",\"mask\":\"\",\"gw\":\"\",\"disableEmerg\":0}"; writeDefultConfig(path, StringConfig); } @@ -279,6 +285,7 @@ bool loadConfigWifi() strlcpy(ConfigSettings.ipMaskWiFi, doc["mask"] | "", sizeof(ConfigSettings.ipMaskWiFi)); strlcpy(ConfigSettings.ipGWWiFi, doc["gw"] | "", sizeof(ConfigSettings.ipGWWiFi)); ConfigSettings.enableWiFi = (int)doc["enableWiFi"]; + ConfigSettings.disableEmerg = (int)doc["disableEmerg"]; configFile.close(); return true; @@ -524,7 +531,7 @@ void enableWifi() if (!setupSTAWifi()) { setupWifiAP(); - modeWiFi = "AP"; + //modeWiFi = "AP"; ConfigSettings.radioModeWiFi = true; DEBUG_PRINTLN(F("AP")); } @@ -538,7 +545,7 @@ void enableWifi() { DEBUG_PRINTLN(F("Error SSID & PASS")); setupWifiAP(); - modeWiFi = "AP"; + //modeWiFi = "AP"; DEBUG_PRINTLN(F("AP")); ConfigSettings.radioModeWiFi = true; } @@ -584,6 +591,8 @@ void setupEthernetAndZigbeeSerial() DEBUG_PRINT(F("Zigbee serial setup @ ")); DEBUG_PRINTLN(ConfigSettings.serialSpeed); Serial2.begin(ConfigSettings.serialSpeed, SERIAL_8N1, ZRXD_2, ZTXD_2); + + oneWireBegin(); } else { @@ -799,6 +808,7 @@ void setup(void) { mqttConnectSetup(); } + } WiFiClient client[10]; @@ -922,7 +932,7 @@ void loop(void) } } - if (ConfigSettings.connectedEther == false && ConfigSettings.disconnectEthTime != 0 && ConfigSettings.enableWiFi != 1 && ConfigSettings.emergencyWifi != 1) + if (ConfigSettings.connectedEther == 0 && ConfigSettings.disconnectEthTime != 0 && ConfigSettings.enableWiFi != 1 && ConfigSettings.emergencyWifi != 1 && ConfigSettings.disableEmerg == 0) { if ((millis() - ConfigSettings.disconnectEthTime) >= (ETH_ERROR_TIME * 1000)) { diff --git a/src/mqtt.cpp b/src/mqtt.cpp index bd5ec70..8a20581 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -57,21 +57,21 @@ void mqttOnConnect() DEBUG_PRINTLN(F("connected")); mqttSubscribe("cmd"); DEBUG_PRINTLN(F("mqtt Subscribed")); - if (ConfigSettings.mqttInterval > 0) - { - mqttPublishState(); - DEBUG_PRINTLN(F("mqtt Published State")); - } if (ConfigSettings.mqttDiscovery) { mqttPublishDiscovery(); DEBUG_PRINTLN(F("mqtt Published Discovery")); } + if (ConfigSettings.mqttInterval > 0) + { + mqttPublishState(); + DEBUG_PRINTLN(F("mqtt Published State")); + } mqttPublishIo("rst_esp", "OFF"); mqttPublishIo("rst_zig", "OFF"); mqttPublishIo("enbl_bsl", "OFF"); mqttPublishIo("socket", "OFF"); - DEBUG_PRINTLN(F("mqtt Published State")); + DEBUG_PRINTLN(F("mqtt Published IOs")); mqttPublishAvty(); DEBUG_PRINTLN(F("mqtt Published Avty")); } @@ -102,16 +102,20 @@ void mqttPublishState() String CPUtemp; getCPUtemp(CPUtemp); root["temperature"] = CPUtemp; - /* - if (ConfigSettings.connectedSocket) - { - root["socket"] = "ON"; - } - else - { - root["socket"] = "OFF"; + + + if (ConfigSettings.board == 2) { + String OWWstrg; + oneWireRead(OWWstrg); + + if (OWWstrg != "0.00" && OWWstrg != "255") + { + root["ow_temperature"] = OWWstrg; + } } - */ + + root["connections"] = ConfigSettings.connectedClients; + if (ConfigSettings.connectedEther) { if (ConfigSettings.dhcp) @@ -249,7 +253,10 @@ void mqttPublishDiscovery() DynamicJsonDocument via(1024); via["ids"] = ETH.macAddress(); - for (int i = 0; i < 9; i++) + int lastAutoMsg = 10; + if (ConfigSettings.board == 2) lastAutoMsg--; + + for (int i = 0; i <= lastAutoMsg; i++) { switch (i) { @@ -356,7 +363,7 @@ void mqttPublishDiscovery() case 7: { topic = "homeassistant/sensor/" + deviceName + "/temperature/config"; - buffJson["name"] = "CPU temperature"; + buffJson["name"] = "ESP temperature"; buffJson["uniq_id"] = deviceName + "/temperature"; buffJson["stat_t"] = mtopic + "/state"; buffJson["avty_t"] = mtopic + "/avty"; @@ -379,6 +386,32 @@ void mqttPublishDiscovery() buffJson["dev"] = via; break; } + case 9: + { + topic = "homeassistant/sensor/" + deviceName + "/connections/config"; + buffJson["name"] = "Socket connections"; + buffJson["uniq_id"] = deviceName + "/connections"; + buffJson["stat_t"] = mtopic + "/state"; + buffJson["avty_t"] = mtopic + "/avty"; + buffJson["val_tpl"] = "{{ value_json.connections }}"; + buffJson["json_attr_t"] = mtopic + "/state"; + buffJson["icon"] = "mdi:check-network-outline"; + buffJson["dev"] = via; + break; + } + case 10: + { + topic = "homeassistant/sensor/" + deviceName + "/ow_temperature/config"; + buffJson["name"] = "OW temperature"; + buffJson["uniq_id"] = deviceName + "/ow_temperature"; + buffJson["stat_t"] = mtopic + "/state"; + buffJson["avty_t"] = mtopic + "/avty"; + buffJson["val_tpl"] = "{{ value_json.ow_temperature }}"; + buffJson["json_attr_t"] = mtopic + "/state"; + buffJson["icon"] = "mdi:coolant-temperature"; + buffJson["dev"] = via; + break; + } } serializeJson(buffJson, mqttBuffer); DEBUG_PRINTLN(mqttBuffer); diff --git a/src/web.cpp b/src/web.cpp index 5ff386b..a68080f 100644 --- a/src/web.cpp +++ b/src/web.cpp @@ -383,15 +383,24 @@ void handleWifi() { result.replace("{{checkedWiFi}}", ""); } + DEBUG_PRINTLN(ConfigSettings.disableEmerg); + if (ConfigSettings.disableEmerg) + { + result.replace("{{checkedDisEmerg}}", "Checked"); + } + else + { + result.replace("{{checkedDisEmerg}}", ""); + } result.replace("{{ssid}}", String(ConfigSettings.ssid)); result.replace("{{passWifi}}", String(ConfigSettings.password)); if (ConfigSettings.dhcpWiFi) { - result.replace("{{modeWiFi}}", "Checked"); + result.replace("{{dchp}}", "Checked"); } else { - result.replace("{{modeWiFi}}", ""); + result.replace("{{dchp}}", ""); } result.replace("{{ip}}", ConfigSettings.ipAddressWiFi); result.replace("{{mask}}", ConfigSettings.ipMaskWiFi); @@ -595,6 +604,22 @@ void handleRoot() getCPUtemp(CPUtemp); result.replace("{{deviceTemp}}", CPUtemp); + + if (ConfigSettings.board == 2) { + String OWWstrg; + oneWireRead(OWWstrg); + + if (OWWstrg != "0.00" && OWWstrg != "255") + { + OWWstrg = "
OW temperature : " + OWWstrg + " °C"; + result.replace("{{dsTemp}}", OWWstrg); + } + else + { + result.replace("{{dsTemp}}", ""); + } + } + result.replace("{{hwRev}}", ConfigSettings.boardName); result.replace("{{espModel}}", String(ESP.getChipModel())); @@ -678,19 +703,28 @@ void handleRoot() int rssi = WiFi.RSSI(); String rssiWifi = String(rssi) + String(" dBm"); wifiState = wifiState + "STA
SSID : " + ConfigSettings.ssid; - wifiState += "
RSSI : " + rssiWifi; - wifiState += "
Mode : "; - if (ConfigSettings.dhcpWiFi) + wifiState += "
Connected : "; + if (rssi != 0) { - wifiState += "DHCP
IP : " + WiFi.localIP().toString(); - wifiState += "
Mask : " + WiFi.subnetMask().toString(); - wifiState += "
GW : " + WiFi.gatewayIP().toString(); + wifiState += ""; + wifiState += "
RSSI : " + rssiWifi; + wifiState += "
Mode : "; + if (ConfigSettings.dhcpWiFi) + { + wifiState += "DHCP
IP : " + WiFi.localIP().toString(); + wifiState += "
Mask : " + WiFi.subnetMask().toString(); + wifiState += "
GW : " + WiFi.gatewayIP().toString(); + } + else + { + wifiState = wifiState + "STATIC
IP : " + ConfigSettings.ipAddressWiFi; + wifiState = wifiState + "
Mask : " + ConfigSettings.ipMaskWiFi; + wifiState = wifiState + "
GW : " + ConfigSettings.ipGWWiFi; + } } else { - wifiState = wifiState + "STATIC
IP : " + ConfigSettings.ipAddressWiFi; - wifiState = wifiState + "
Mask : " + ConfigSettings.ipMaskWiFi; - wifiState = wifiState + "
GW : " + ConfigSettings.ipGWWiFi; + wifiState += ""; } } } @@ -848,10 +882,18 @@ void handleSaveWifi() String ipAddress = serverWeb.arg("ipAddress"); String ipMask = serverWeb.arg("ipMask"); String ipGW = serverWeb.arg("ipGW"); - + String disableEmerg; + if (serverWeb.arg("disableEmerg") == "on") + { + disableEmerg = "1"; + } + else + { + disableEmerg = "0"; + } const char *path = "/config/configWifi.json"; - StringConfig = "{\"enableWiFi\":" + enableWiFi + ",\"ssid\":\"" + ssid + "\",\"pass\":\"" + pass + "\",\"dhcpWiFi\":" + dhcpWiFi + ",\"ip\":\"" + ipAddress + "\",\"mask\":\"" + ipMask + "\",\"gw\":\"" + ipGW + "\"}"; + StringConfig = "{\"enableWiFi\":" + enableWiFi + ",\"ssid\":\"" + ssid + "\",\"pass\":\"" + pass + "\",\"dhcpWiFi\":" + dhcpWiFi + ",\"ip\":\"" + ipAddress + "\",\"mask\":\"" + ipMask + "\",\"gw\":\"" + ipGW + "\",\"disableEmerg\":\"" + disableEmerg + "\"}"; DEBUG_PRINTLN(StringConfig); DynamicJsonDocument doc(1024); deserializeJson(doc, StringConfig); diff --git a/tools/version b/tools/version index b1d7abc..a0a1517 100644 --- a/tools/version +++ b/tools/version @@ -1 +1 @@ -0.6.2 \ No newline at end of file +0.6.3 \ No newline at end of file