From 6e44f763859177c6083251c719a81c9b19c26ff9 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Sun, 6 Dec 2020 02:08:37 +0300 Subject: [PATCH] UART working condition --- data/config.json | 6 +++++- data/set.utilities.json | 9 ++++++++- src/SoftUART.cpp | 25 +++++++++++++++++++------ src/Web.cpp | 26 ++++++++++++++++++++++++++ src/main.cpp | 6 ++++++ 5 files changed, 64 insertions(+), 8 deletions(-) diff --git a/data/config.json b/data/config.json index eec796ae..ccae42a2 100644 --- a/data/config.json +++ b/data/config.json @@ -22,5 +22,9 @@ "snaMqtt": "0", "blink": "1", "oneWirePin": "2", - "serverip": "http://206.189.49.244" + "serverip": "http://206.189.49.244", + "uart": "0", + "uartS":"9600", + "uartTX":"12", + "uartRX":"13" } \ No newline at end of file diff --git a/data/set.utilities.json b/data/set.utilities.json index c5d7ebc8..dee60f7c 100644 --- a/data/set.utilities.json +++ b/data/set.utilities.json @@ -84,9 +84,16 @@ { "type": "input", "title": "", - "name": "uartTX-arg", + "name": "uartRX-arg", "state": "{{uartRX}}", "style": "width:40%;float:right" + }, + { + "type": "button", + "title": "{{ButSave}}", + "style": "width:100%;float:left;", + "action": "set?uartS=[[uartS-arg]]&uartTX=[[uartTX-arg]]&uartRX=[[uartRX-arg]]", + "class": "btn btn-block btn-default" } ] } \ No newline at end of file diff --git a/src/SoftUART.cpp b/src/SoftUART.cpp index bf399adb..d142e26e 100644 --- a/src/SoftUART.cpp +++ b/src/SoftUART.cpp @@ -1,4 +1,5 @@ #include "SoftUART.h" +#include "Global.h" #ifdef ESP8266 SoftwareSerial* myUART = nullptr; @@ -7,10 +8,13 @@ HardwareSerial* myUART = nullptr; #endif void uartInit() { + if (!jsonReadBool(configSetupJson, "uart")) { + return; + } if (!myUART) { #ifdef ESP8266 - myUART = new SoftwareSerial(4, 5); - myUART->begin(9600); + myUART = new SoftwareSerial(jsonReadInt(configSetupJson, "uartTX"), jsonReadInt(configSetupJson, "uartRX")); + myUART->begin(jsonReadInt(configSetupJson, "uartS")); #else myUART = new HardwareSerial(2); myUART->begin(4, 5); @@ -19,19 +23,28 @@ void uartInit() { } void uartHandle() { + if (!jsonReadBool(configSetupJson, "uart")) { + return; + } static String incStr; if (myUART->available()) { char inc; inc = myUART->read(); incStr += inc; if (inc == '\n') { - parse(incStr); - incStr = ""; + parse(incStr); + incStr = ""; } } } void parse(String& incStr) { - - + if (incStr.indexOf("set") != -1) { + incStr.replace("\r\n", ""); + incStr.replace("\r", ""); + incStr.replace("\n", ""); + incStr = deleteBeforeDelimiter(incStr, " "); + SerialPrint("I", "UART", incStr); + orderBuf += incStr; + } } \ No newline at end of file diff --git a/src/Web.cpp b/src/Web.cpp index c571ccaf..ead2e617 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -6,6 +6,7 @@ #include "items/vLogging.h" #include "Telegram.h" #include "RemoteOrdersUdp.h" +#include "SoftUART.h" bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) { if (request->hasArg("preset")) { @@ -255,6 +256,31 @@ void web_init() { myNotAsyncActions->make(do_BUSSCAN); request->redirect("/?set.utilities"); } + if (request->hasArg("uart")) { + bool value = request->getParam("uart")->value().toInt(); + jsonWriteBool(configSetupJson, "uart", value); + saveConfig(); + uartInit(); + request->send(200); + } + if (request->hasArg("uartS")) { + jsonWriteStr(configSetupJson, "uartS", request->getParam("uartS")->value()); + saveConfig(); + uartInit(); + request->send(200); + } + if (request->hasArg("uartTX")) { + jsonWriteStr(configSetupJson, "uartTX", request->getParam("uartTX")->value()); + saveConfig(); + uartInit(); + request->send(200); + } + if (request->hasArg("uartRX")) { + jsonWriteStr(configSetupJson, "uartRX", request->getParam("uartRX")->value()); + saveConfig(); + uartInit(); + request->send(200); + } //==============================developer settings============================================= if (request->hasArg("serverip")) { diff --git a/src/main.cpp b/src/main.cpp index 1b146fce..34c19945 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -21,6 +21,7 @@ #include "items/vCountDown.h" #include "items/vSensorUltrasonic.h" #include "Telegram.h" +#include "SoftUART.h" void not_async_actions(); @@ -91,6 +92,9 @@ void setup() { SerialPrint("I", F("Bus"), F("Bus Init")); busInit(); + SerialPrint("I", F("UART"), F("UART Init")); + uartInit(); + #ifdef SSDP_ENABLED SerialPrint("I", F("SSDP"), F("Ssdp Init")); SsdpInit(); @@ -155,6 +159,8 @@ void loop() { handleTelegram(); + uartHandle(); + if (myLogging != nullptr) { for (unsigned int i = 0; i < myLogging->size(); i++) { myLogging->at(i).loop();