From 5f1a9b23e5c97cf7b63bd0e711cabfeb00763466 Mon Sep 17 00:00:00 2001 From: Mit4el Date: Mon, 13 Nov 2023 23:55:05 +0300 Subject: [PATCH] new NextionUpload + Telegram_v2 --- include/classes/IoTItem.h | 3 + src/classes/IoTItem.cpp | 3 + .../display/NextionUpload/NextionUpload.cpp | 160 ++++++++++++++++++ .../display/NextionUpload/modinfo.json | 63 +++++++ src/modules/exec/Telegram_v2/Telegram_v2.cpp | 7 +- 5 files changed, 233 insertions(+), 3 deletions(-) create mode 100644 src/modules/display/NextionUpload/NextionUpload.cpp create mode 100644 src/modules/display/NextionUpload/modinfo.json diff --git a/include/classes/IoTItem.h b/include/classes/IoTItem.h index 3ad344f8..8eab3261 100644 --- a/include/classes/IoTItem.h +++ b/include/classes/IoTItem.h @@ -74,6 +74,9 @@ class IoTItem { virtual void onModuleOrder(String& key, String& value); virtual void onTrackingValue(IoTItem* item); // момент, когда ядро заметило изменение отслеживаемого значения + // для обновления экрана Nextion из телеграм + virtual void uploadNextionTlgrm(String &url); + // методы для графиков (будет упрощено) virtual void publishValue(); virtual void clearValue(); diff --git a/src/classes/IoTItem.cpp b/src/classes/IoTItem.cpp index 798ec902..ee588dc2 100644 --- a/src/classes/IoTItem.cpp +++ b/src/classes/IoTItem.cpp @@ -206,6 +206,9 @@ bool IoTItem::isTracking(IoTItem* item) { void IoTItem::sendTelegramMsg(bool often, String msg) {} void IoTItem::sendFoto(uint8_t *buf, uint32_t length, const String &name) {} void IoTItem::editFoto(uint8_t *buf, uint32_t length, const String &name) {} +// для обновления экрана Nextion из телеграм +void IoTItem::uploadNextionTlgrm(String &url) {} + // методы для графиков (будет упрощено) void IoTItem::publishValue() {} void IoTItem::clearValue() {} diff --git a/src/modules/display/NextionUpload/NextionUpload.cpp b/src/modules/display/NextionUpload/NextionUpload.cpp new file mode 100644 index 00000000..65ae8dd6 --- /dev/null +++ b/src/modules/display/NextionUpload/NextionUpload.cpp @@ -0,0 +1,160 @@ + +#define DEBUG_SERIAL_ENABLE +#include "Global.h" +#include "classes/IoTItem.h" +#include "ESPNexUpload.h" +bool updated = false; + +class NextionUpload : public IoTItem +{ +private: + String _url; + String _host; + int _NEXT_RX; + int _NEXT_TX; + bool _UpTelegram; + +public: + NextionUpload(String parameters) : IoTItem(parameters) + { + _url = jsonReadStr(parameters, "url"); + _url = "/" + _url; + _host = jsonReadStr(parameters, "host"); + + _NEXT_RX = jsonReadInt(parameters, "NEXT_RX"); + _NEXT_TX = jsonReadInt(parameters, "NEXT_TX"); + jsonRead(parameters, "UpTelegram", _UpTelegram); + +#define NEXT_RX _NEXT_RX // Nextion RX pin | Default 16 +#define NEXT_TX _NEXT_TX // Nextion TX pin | Default 17 + } + + IoTValue execute(String command, std::vector ¶m) + { + + if (command == "Update") + { + SerialPrint("I", F("NextionUpdate"), "Update .... "); + + if (!updated) + { + SerialPrint("I", F("NextionUpdate"), "connecting to " + (String)_host); + HTTPClient http; + +#if defined ESP8266 + if (!http.begin(_host, 80, _url)) + { + // Serial.println("connection failed"); + SerialPrint("I", F("NextionUpdate"), "connection failed "); + } +#elif defined ESP32 + if (!http.begin(String("http://") + _host + _url)) + { + // Serial.println("connection failed"); + SerialPrint("I", F("NextionUpdate"), "connection failed "); + } +#endif + + SerialPrint("I", F("NextionUpdate"), "Requesting file: " + (String)_url); + int code = http.GET(); + // Update the nextion display + if (code == 200) + { + flashNextion(http); + } + else + { + SerialPrint("I", F("NextionUpdate"), "HTTP error: " + (String)http.errorToString(code).c_str()); + } + + http.end(); + SerialPrint("I", F("NextionUpdate"), "Closing connection "); + } + } + + return {}; + } + + void uploadNextionTlgrm(String &url) + { + if (!_UpTelegram) + return; + if (!updated) + { + SerialPrint("I", F("NextionUpdate"), "connecting to " + url); + + HTTPClient http; + +#ifdef ESP8266 // esp8266 требует SSl + return; + // BearSSL::WiFiClientSecure client; + // client.setInsecure(); + // http.begin(client, url); // пингуем файл +#else // esp32 сама умеет SSL + if (!http.begin(url)) // пингуем файл + { + SerialPrint("I", F("NextionUpdate"), "connection failed "); + } +#endif + SerialPrint("I", F("NextionUpdate"), "Requesting file: " + (String)_url); + int code = http.GET(); + // Update the nextion display + if (code == 200) + { // файл доступен + flashNextion(http); + } + else + { + SerialPrint("I", F("NextionUpdate"), "HTTP error: " + (String)http.errorToString(code).c_str()); + } + + http.end(); + SerialPrint("I", F("NextionUpdate"), "Closing connection "); + } + } + + void flashNextion(HTTPClient &http) + { + int contentLength = http.getSize(); + SerialPrint("I", F("NextionUpdate"), "File received. Update Nextion... "); + bool result; + ESPNexUpload nextion(115200); + nextion.setUpdateProgressCallback([]() + { SerialPrint("I", F("NextionUpdate"), "... "); }); + + result = nextion.prepareUpload(contentLength); + if (!result) + { + SerialPrint("I", F("NextionUpdate"), "Error: " + (String)nextion.statusMessage); + } + else + { + SerialPrint("I", F("NextionUpdate"), "Start upload. File size is: " + (String)contentLength); + result = nextion.upload(*http.getStreamPtr()); + if (result) + { + updated = true; + SerialPrint("I", F("NextionUpdate"), "Succesfully updated Nextion! "); + } + else + { + SerialPrint("I", F("NextionUpdate"), "Error updating Nextion: " + (String)nextion.statusMessage); + } + nextion.end(); + } + } + + ~NextionUpload(){}; +}; + +void *getAPI_NextionUpload(String subtype, String param) +{ + if (subtype == F("NextionUpload")) + { + return new NextionUpload(param); + } + else + { + return nullptr; + } +} diff --git a/src/modules/display/NextionUpload/modinfo.json b/src/modules/display/NextionUpload/modinfo.json new file mode 100644 index 00000000..aed9d0ce --- /dev/null +++ b/src/modules/display/NextionUpload/modinfo.json @@ -0,0 +1,63 @@ +{ + "menuSection": "screens", + "configItem": [ + { + "global": 0, + "name": "Nextion Uploud", + "type": "Reading", + "subtype": "NextionUpload", + "id": "Nextion", + "widget": "", + "page": "", + "descr": "", + "host": "192.168.1.10", + "url": "nextion.tft", + "NEXT_TX": 16, + "NEXT_RX": 17, + "UpTelegram": 1 + } + ], + "about": { + "authorName": "AVAKS", + "authorContact": "https://t.me/@avaks_dev", + "authorGit": "https://github.com/avaksru", + "specialThanks": "", + "moduleName": "NextionUpload", + "moduleVersion": "1.1", + "usedRam": { + "esp32_4mb": 15, + "esp8266_4mb": 15 + }, + "title": "Nextion Upload", + "moduleDesc": "загрузка прошивки в дисплей Nextion. Команда для запуска обновления дисплея: Nextion.Update(); ", + "propInfo": { + "host": "Сервер обновления. Можно использовать LiveServer из VisualCode, указывать ip адрес", + "url": "файл прошивки экрана, указывать с расширением, например nextion.tft или iotm/test.tft", + "UpTelegram": "1 - разрешает прошивать экран через модуль Telegram_v2" + } + }, + "defActive": false, + "usedLibs": { + "esp32_4mb": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp32_4mb3f": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp8266_4mb": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp8266_1mb": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp8266_1mb_ota": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp8285_1mb": [ + "https://github.com/Nredor/ESPNexUpload.git" + ], + "esp8285_1mb_ota": [ + "https://github.com/Nredor/ESPNexUpload.git" + ] + } +} \ No newline at end of file diff --git a/src/modules/exec/Telegram_v2/Telegram_v2.cpp b/src/modules/exec/Telegram_v2/Telegram_v2.cpp index ed50c7e1..ecaaaa4e 100644 --- a/src/modules/exec/Telegram_v2/Telegram_v2.cpp +++ b/src/modules/exec/Telegram_v2/Telegram_v2.cpp @@ -240,11 +240,12 @@ public: } else if (msg.text.indexOf("nextion") != -1 && msg.chatID == _chatID) { - if (downloadFile(msg)) - { - // flashNextion(); + for (std::list::iterator it = IoTItems.begin(); it != IoTItems.end(); ++it) { + if ((*it)->getSubtype() == "NextionUpload") { + (*it)->uploadNextionTlgrm(msg.fileUrl); } } + } } else if (msg.text.indexOf("help") != -1) {