Fix ошибок и под esp8266

This commit is contained in:
Mit4el
2024-05-15 22:25:23 +03:00
parent 0a9a1b9387
commit 94bfc4ec61
7 changed files with 86 additions and 67 deletions

View File

@@ -26,33 +26,6 @@
#define DEBUG_SERIAL_ENABLE
#include "ESPNexUpload.h"
#if defined ESP8266
#include <SoftwareSerial.h>
#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;

View File

@@ -56,6 +56,13 @@
#include <Arduino.h>
#include <StreamString.h>
#ifdef ESP8266
#include <SoftwareSerial.h>
#else
#include <HardwareSerial.h>
#include <SoftwareSerial.h>
#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
};
/**
* @}

View File

@@ -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))

View File

@@ -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*": []
}
}

View File

@@ -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"), "... "); });

View File

@@ -4,7 +4,7 @@
#include <TM1637.h>
#include <TM1638.h>
#include <TM16xxDisplay.h>
#include <TM16xxbuttons.h>
#include <TM16xxButtons.h>
TM16xxButtons* buttons = nullptr; // указатель на объект управления кнопками для TM1638 иначе nullptr

View File

@@ -8,7 +8,9 @@
// #include <GyverGFX.h>
// #include <CharPlot.h>
// #include "esp_camera.h"
#ifdef ESP8266
#define FB_DYNAMIC
#endif
#include <FastBot.h>
#include <map>
@@ -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 --------------
// -------------------------------------------------------------------------