From 94bfc4ec61687d635ec20c33490a6ccee9611d44 Mon Sep 17 00:00:00 2001 From: Mit4el Date: Wed, 15 May 2024 22:25:23 +0300 Subject: [PATCH] =?UTF-8?q?Fix=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA=20?= =?UTF-8?q?=D0=B8=20=D0=BF=D0=BE=D0=B4=20esp8266?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/ESPNexUpload/src/ESPNexUpload.cpp | 94 +++++++++---------- lib/ESPNexUpload/src/ESPNexUpload.h | 19 +++- src/modules/display/Nextion/Nextion.cpp | 5 +- src/modules/display/Nextion/modinfo.json | 9 +- .../display/NextionUpload/NextionUpload.cpp | 11 ++- src/modules/display/TM16XX/TM16XX.cpp | 2 +- src/modules/exec/Telegram_v2/Telegram_v2.cpp | 13 ++- 7 files changed, 86 insertions(+), 67 deletions(-) diff --git a/lib/ESPNexUpload/src/ESPNexUpload.cpp b/lib/ESPNexUpload/src/ESPNexUpload.cpp index 0856aa5f..1eaf5403 100644 --- a/lib/ESPNexUpload/src/ESPNexUpload.cpp +++ b/lib/ESPNexUpload/src/ESPNexUpload.cpp @@ -26,33 +26,6 @@ #define DEBUG_SERIAL_ENABLE #include "ESPNexUpload.h" -#if defined ESP8266 - -#include - -#ifndef NEXT_RX -#define NEXT_RX 14 // Nextion RX pin | Default 14 / D5 -#define NEXT_TX 12 // Nextion TX pin | Default 12 / D6 -#endif -#ifndef nexSerial -//SoftwareSerial softSerial(NEXT_RX, NEXT_TX); -#define nexSerial softSerial -#define nexSerialBegin(a, b, c) nexSerial.begin(a) -#endif - -#elif defined ESP32 - -#ifndef NEXT_RX -#define NEXT_RX 17 // Nextion RX pin | Default 16 -#define NEXT_TX 16 // Nextion TX pin | Default 17 -#endif -#ifndef nexSerial -#define nexSerial Serial2 -#define nexSerialBegin(a, rx, tx) nexSerial.begin(a, SERIAL_8N1, rx, tx) -#endif - -#endif - #ifdef DEBUG_SERIAL_ENABLE #define dbSerialPrint(a) Serial.print(a) #define dbSerialPrintHex(a) Serial.print(a, HEX) @@ -77,23 +50,40 @@ } while (0) #endif -ESPNexUpload::ESPNexUpload(uint32_t upload_baudrate, uint8_t rx, uint8_t tx) +ESPNexUpload::ESPNexUpload(uint32_t upload_baudrate, int line, int rx, int tx) { _upload_baudrate = upload_baudrate; - if (rx == 0 || tx == 0) - { - _rx = NEXT_RX; - _tx = NEXT_TX; - }else{ - _rx = rx; - _tx = tx; - } + _rx = rx; + _tx = tx; + _line = line; + #if defined ESP8266 - SoftwareSerial softSerial(_rx, _tx); + nexSerial = new SoftwareSerial(_rx, _tx); +#else + if (line >= 0) { + nexSerial = new HardwareSerial(line); + // ((HardwareSerial*)nexSerial)->begin(_upload_baudrate, SERIAL_8N1, _rx, _tx); + } else { + nexSerial = new SoftwareSerial(_rx, _tx); + // ((SoftwareSerial*)nexSerial)->begin(_upload_baudrate); + } #endif } +void ESPNexUpload::nexSerialBegin(uint32_t _speed, int _line, int _rx, int _tx) +{ +#if defined ESP8266 + nexSerial->begin(_speed); +#else + if (_line >= 0) { + ((HardwareSerial*)nexSerial)->begin(_speed, SERIAL_8N1, _rx, _tx); + } else { + ((SoftwareSerial*)nexSerial)->begin(_speed); + } +#endif +} + bool ESPNexUpload::connect() { #if defined ESP8266 @@ -172,7 +162,7 @@ bool ESPNexUpload::_searchBaudrate(uint32_t baudrate) dbSerialPrint(F("init nextion serial interface on baudrate: ")); dbSerialPrintln(baudrate); - nexSerialBegin(baudrate, _rx, _tx); + nexSerialBegin(baudrate, _line, _rx, _tx); _printInfoLine(F("ESP baudrate established, try to connect to display")); const char _nextion_FF_FF[3] = {0xFF, 0xFF, 0x00}; @@ -231,20 +221,20 @@ void ESPNexUpload::sendCommand(const char *cmd, bool tail, bool null_head) if (null_head) { - nexSerial.write(0x00); + ((HardwareSerial*)nexSerial)->write(0x00); } - while (nexSerial.available()) + while (nexSerial->available()) { - nexSerial.read(); + nexSerial->read(); } - nexSerial.print(cmd); + nexSerial->print(cmd); if (tail) { - nexSerial.write(0xFF); - nexSerial.write(0xFF); - nexSerial.write(0xFF); + nexSerial->write(0xFF); + nexSerial->write(0xFF); + nexSerial->write(0xFF); } _printSerialData(true, cmd); } @@ -270,10 +260,10 @@ uint16_t ESPNexUpload::recvRetString(String &response, uint32_t timeout, bool re while (millis() - start <= timeout) { - while (nexSerial.available()) + while (nexSerial->available()) { - c = nexSerial.read(); + c = nexSerial->read(); if (c == 0) { continue; @@ -344,9 +334,9 @@ bool ESPNexUpload::_setPrepareForFirmwareUpdate(uint32_t upload_baudrate) // because switching to another baudrate (nexSerialBegin command) has an higher prio. // The ESP will first jump to the new 'upload_baudrate' and than process the serial 'transmit buffer' // The flush command forced the ESP to wait until the 'transmit buffer' is empty - nexSerial.flush(); + nexSerial->flush(); - nexSerialBegin(upload_baudrate, _rx, _tx); + nexSerialBegin(upload_baudrate, _line, _rx, _tx); _printInfoLine(F("changing upload baudrate...")); _printInfoLine(String(upload_baudrate)); @@ -421,7 +411,7 @@ bool ESPNexUpload::upload(const uint8_t *file_buf, size_t buf_size) c = file_buf[i]; // write byte to nextion over serial - nexSerial.write(c); + nexSerial->write(c); // update sent packets counter _sent_packets++; @@ -438,7 +428,7 @@ bool ESPNexUpload::upload(Stream &myFile) #endif // create buffer for read - uint8_t buff[2048] = {0}; + uint8_t buff[4096] = {0}; // read all data from server while (_undownloadByte > 0 || _undownloadByte == -1) @@ -492,7 +482,7 @@ void ESPNexUpload::end() this->softReset(); // end Serial connection - nexSerial.end(); + ((HardwareSerial*)nexSerial)->end(); // reset sent packets counter _sent_packets = 0; diff --git a/lib/ESPNexUpload/src/ESPNexUpload.h b/lib/ESPNexUpload/src/ESPNexUpload.h index b9cfe746..3a22ac15 100644 --- a/lib/ESPNexUpload/src/ESPNexUpload.h +++ b/lib/ESPNexUpload/src/ESPNexUpload.h @@ -56,6 +56,13 @@ #include #include +#ifdef ESP8266 +#include +#else +#include +#include +#endif + /** * @addtogroup CoreAPI * @{ @@ -79,7 +86,7 @@ public: /* methods */ * * @param uint32_t upload_baudrate - set upload baudrate. */ - ESPNexUpload(uint32_t upload_baudrate, uint8_t rx=0, uint8_t tx=0); + ESPNexUpload(uint32_t upload_baudrate, int line, int rx, int tx); /** * destructor. @@ -256,6 +263,8 @@ private: /* methods */ */ uint32_t calculateTransmissionTimeMs(String message); + void nexSerialBegin(uint32_t upload_baudrate, int line, int rx, int tx); + private: /* data */ uint32_t _baudrate; /* nextion serail baudrate */ uint32_t _undownloadByte; /* undownload byte of tft file */ @@ -263,8 +272,14 @@ private: /* data */ uint16_t _sent_packets = 0; /* upload baudrate */ uint8_t _rx; uint8_t _tx; - + uint8_t _line; THandlerFunction _updateProgressCallback; + +#ifdef ESP8266 + SoftwareSerial* nexSerial; +#else + Stream* nexSerial; +#endif }; /** * @} diff --git a/src/modules/display/Nextion/Nextion.cpp b/src/modules/display/Nextion/Nextion.cpp index 4fe3af4f..a1a25c33 100644 --- a/src/modules/display/Nextion/Nextion.cpp +++ b/src/modules/display/Nextion/Nextion.cpp @@ -211,9 +211,10 @@ public: if (!updated) { SerialPrint("I", F("NextionUpdate"), "connecting to " + (String)_host); - HTTPClient http; + HTTPClient http; #if defined ESP8266 - if (!http.begin(_host, 80, _url)) + WiFiClient client; + if (!http.begin(client, _host, 80, _url)) SerialPrint("I", F("NextionUpdate"), "connection failed "); #elif defined ESP32 if (!http.begin(String("http://") + _host + _url)) diff --git a/src/modules/display/Nextion/modinfo.json b/src/modules/display/Nextion/modinfo.json index d74991c1..7168875d 100644 --- a/src/modules/display/Nextion/modinfo.json +++ b/src/modules/display/Nextion/modinfo.json @@ -87,12 +87,7 @@ }, "defActive": false, "usedLibs": { - "esp32_4mb": [], - "esp32_4mb3f": [], - "esp8266_4mb": [], - "esp8266_1mb": [], - "esp8266_1mb_ota": [], - "esp8285_1mb": [], - "esp8285_1mb_ota": [] + "esp32*": [], + "esp82*": [] } } \ No newline at end of file diff --git a/src/modules/display/NextionUpload/NextionUpload.cpp b/src/modules/display/NextionUpload/NextionUpload.cpp index f2db9474..b8745217 100644 --- a/src/modules/display/NextionUpload/NextionUpload.cpp +++ b/src/modules/display/NextionUpload/NextionUpload.cpp @@ -42,7 +42,8 @@ public: HTTPClient http; #if defined ESP8266 - if (!http.begin(_host, 80, _url)) + WiFiClient client; + if (!http.begin(client, _host, 80, _url)) { // Serial.println("connection failed"); SerialPrint("I", F("NextionUpdate"), "connection failed "); @@ -118,7 +119,13 @@ public: int contentLength = http.getSize(); SerialPrint("I", F("NextionUpdate"), "File received. Update Nextion... "); bool result; - ESPNexUpload nextion(115200, _NEXT_RX, _NEXT_TX); + #ifdef ESP8266 + ESPNexUpload nextion(115200, -1, _NEXT_RX, _NEXT_TX); + #elif defined(esp32c3m_4mb) || defined(esp32s2_4mb) + ESPNexUpload nextion(115200, 1, _NEXT_RX, _NEXT_TX); + #else + ESPNexUpload nextion(115200, 2, _NEXT_RX, _NEXT_TX); + #endif nextion.setUpdateProgressCallback([]() { SerialPrint("I", F("NextionUpdate"), "... "); }); diff --git a/src/modules/display/TM16XX/TM16XX.cpp b/src/modules/display/TM16XX/TM16XX.cpp index 10199d38..8bbafc2d 100644 --- a/src/modules/display/TM16XX/TM16XX.cpp +++ b/src/modules/display/TM16XX/TM16XX.cpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include TM16xxButtons* buttons = nullptr; // указатель на объект управления кнопками для TM1638 иначе nullptr diff --git a/src/modules/exec/Telegram_v2/Telegram_v2.cpp b/src/modules/exec/Telegram_v2/Telegram_v2.cpp index 30092bb6..b872730a 100644 --- a/src/modules/exec/Telegram_v2/Telegram_v2.cpp +++ b/src/modules/exec/Telegram_v2/Telegram_v2.cpp @@ -8,7 +8,9 @@ // #include // #include // #include "esp_camera.h" - +#ifdef ESP8266 +#define FB_DYNAMIC +#endif #include #include @@ -93,6 +95,7 @@ public: if (fl_rollback) { _myBot->tickManual(); // Чтобы отметить сообщение прочитанным +#ifdef ESP32 if (Update.rollBack()) { SerialPrint("I", F("Update"), F("Откат OTA успешно выполнен")); @@ -104,6 +107,7 @@ public: SerialPrint("E", F("Update"), F("Откат OTA не выполнен!")); _myBot->sendMessage("Откат OTA не выполнен!", _chatID); } +#endif } // была попытка OTA обновления. Обновляемся после ответа серверу! if (_OTAstate >= 0) @@ -369,10 +373,16 @@ public: // ------------------------------------------------------------------------- if (msg.text.indexOf("/rollback") != -1 && msg.chatID == _chatID) { +#ifdef ESP32 _myBot->inlineMenu("Вы уверены, что хотите откатить прошивку? " + jsonReadStr(settingsFlashJson, F("name")) + " \n OTA_roll", F("Rollback \t Cancel")); +#elif ESP8266 + SerialPrint("E", F("Update"), F("Откат OTA не поддерживается на esp8266!")); + _myBot->sendMessage("Откат OTA не поддерживается на esp8266!", _chatID); +#endif } else if (msg.text.indexOf("OTA_roll") != -1) { +#ifdef ESP32 // удаляем последнее сообщение от бота _myBot->deleteMessage(_myBot->lastBotMsg()); if (msg.data.indexOf("Rollback") != -1) @@ -387,6 +397,7 @@ public: _myBot->sendMessage("Откат OTA не возможен!", _chatID); } } +#endif } // -------------- Обработка файлов *.bin для прошивки по OTA -------------- // -------------------------------------------------------------------------