diff --git a/data_svelte/items.json b/data_svelte/items.json index 3468a035..e244ac84 100644 --- a/data_svelte/items.json +++ b/data_svelte/items.json @@ -7,7 +7,7 @@ "header": "Виртуальные элементы" }, { - "name": "1. Логгирование в график", + "name": "1. Логирование в график", "type": "Writing", "subtype": "Loging", "id": "log", @@ -457,11 +457,23 @@ "apin": -1, "num": 34 }, + { + "name": "35. Телеграм-Лайт", + "type": "Writing", + "subtype": "TelegramLT", + "id": "tg", + "widget": "", + "page": "", + "descr": "", + "token": "", + "chatID": "", + "num": 35 + }, { "header": "Экраны" }, { - "name": "35. LCD экран 2004", + "name": "36. LCD экран 2004", "type": "Reading", "subtype": "Lcd2004", "id": "Lcd", @@ -473,10 +485,10 @@ "size": "20,4", "coord": "0,0", "id2show": "id датчика", - "num": 35 + "num": 36 }, { - "name": "36. LCD экран 1602", + "name": "37. LCD экран 1602", "type": "Reading", "subtype": "Lcd2004", "id": "Lcd", @@ -488,6 +500,6 @@ "size": "16,2", "coord": "0,0", "id2show": "id датчика", - "num": 36 + "num": 37 } ] \ No newline at end of file diff --git a/myProfile.json b/myProfile.json index 4fcac5c5..28cb48c2 100644 --- a/myProfile.json +++ b/myProfile.json @@ -162,6 +162,10 @@ { "path": "src\\modules\\exec\\Telegram", "active": false + }, + { + "path": "src\\modules\\exec\\TelegramLT", + "active": true } ], "Экраны": [ diff --git a/platformio.ini b/platformio.ini index d7470c0d..01416d14 100644 --- a/platformio.ini +++ b/platformio.ini @@ -31,6 +31,7 @@ platform = espressif32 @3.3.0 monitor_filters = esp32_exception_decoder upload_speed = 921600 monitor_speed = 115200 +debug_tool = esp-prog build_src_filter = +<*.cpp> + @@ -89,6 +90,7 @@ build_src_filter = + + + + + + [env:esp32_4mb_fromitems] diff --git a/src/modules/API.cpp b/src/modules/API.cpp index e570b3b6..9ee02105 100644 --- a/src/modules/API.cpp +++ b/src/modules/API.cpp @@ -23,6 +23,7 @@ void* getAPI_IoTServo(String subtype, String params); void* getAPI_Mcp23017(String subtype, String params); void* getAPI_Mp3(String subtype, String params); void* getAPI_Pwm8266(String subtype, String params); +void* getAPI_TelegramLT(String subtype, String params); void* getAPI_Lcd2004(String subtype, String params); void* getAPI(String subtype, String params) { @@ -50,6 +51,7 @@ if ((tmpAPI = getAPI_IoTServo(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mcp23017(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Mp3(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Pwm8266(subtype, params)) != nullptr) return tmpAPI; +if ((tmpAPI = getAPI_TelegramLT(subtype, params)) != nullptr) return tmpAPI; if ((tmpAPI = getAPI_Lcd2004(subtype, params)) != nullptr) return tmpAPI; return nullptr; } \ No newline at end of file diff --git a/src/modules/exec/TelegramLT/TelegramLT.cpp b/src/modules/exec/TelegramLT/TelegramLT.cpp new file mode 100644 index 00000000..d3f13f71 --- /dev/null +++ b/src/modules/exec/TelegramLT/TelegramLT.cpp @@ -0,0 +1,82 @@ +#include "Global.h" +#include "classes/IoTItem.h" + +String _prevMsg = ""; +String _token; +unsigned long _chatID; + +class TelegramLT : public IoTItem +{ + +public: + TelegramLT(String parameters) : IoTItem(parameters) { + jsonRead(parameters, "token", _token); + jsonRead(parameters, "chatID", _chatID); + } + + void sendTelegramMsg(bool often, String msg) + { + if (WiFi.status() == WL_CONNECTED && (often || !often && _prevMsg != msg)) { + WiFiClient client; + HTTPClient http; + http.begin(client, "http://live-control.com/iotm/telegram.php"); + http.addHeader("Content-Type", "application/x-www-form-urlencoded"); + String httpRequestData = "url=https://api.telegram.org/bot" + _token + "/sendmessage?chat_id=" + uint64ToString(_chatID) + "&text=" + msg; + int httpResponseCode = http.POST(httpRequestData); + String payload = http.getString(); + SerialPrint("<-", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", msg: " + msg); + SerialPrint("->", F("Telegram"), "chat ID: " + uint64ToString(_chatID) + ", server: " + httpResponseCode); + + if (!strstr(payload.c_str(), "{\"ok\":true")) { + value.valD = 1; + Serial.printf("Telegram error, msg from server: %s\n", payload.c_str()); + regEvent(value.valD, payload); + } else { + value.valD = 0; + } + http.end(); + _prevMsg = msg; + } + } + + IoTValue execute(String command, std::vector ¶m) + { + if (param.size() == 1) { + String strTmp; + if (param[0].isDecimal && param[0].valS == "") + strTmp = param[0].valD; + else + strTmp = param[0].valS; + + if (command == "sendMsg") + { + if (param.size()) + { + sendTelegramMsg(false, strTmp); + } + } + else if (command == "sendOftenMsg") + { + if (param.size()) + { + sendTelegramMsg(true, strTmp); + } + } + } + return {}; + } + + ~TelegramLT(){}; +}; + +void *getAPI_TelegramLT(String subtype, String param) +{ + if (subtype == F("TelegramLT")) + { + return new TelegramLT(param); + } + else + { + return nullptr; + } +} \ No newline at end of file diff --git a/src/modules/exec/TelegramLT/modinfo.json b/src/modules/exec/TelegramLT/modinfo.json new file mode 100644 index 00000000..4eeb7aab --- /dev/null +++ b/src/modules/exec/TelegramLT/modinfo.json @@ -0,0 +1,51 @@ +{ + "menuSection": "Исполнительные устройства", + + "configItem": [ + { + "name": "Телеграм-Лайт", + "type": "Writing", + "subtype": "TelegramLT", + "id": "tg", + "widget": "", + "page": "", + "descr": "", + "token": "", + "chatID": "" + } + ], + + "about": { + "authorName": "AVAKS", + "authorContact": "https://t.me/@avaks_dev", + "authorGit": "https://github.com/avaksru", + "specialThanks": "", + "moduleName": "TelegramLT", + "moduleVersion": "1.0", + "moduleDesc": "Только отправка уведомлений в телеграм о событиях. Модуль занимает значительно меньше памяти в ESP по сравнению со стандартным. Внимание! для отправки сообщений используется промежуточный сервер http://live-control.com", + "propInfo": { + "token": "Токен для авторизации бота в системе Telegram", + "chatID": "ИД диалога с контактом. Необходим для отправки сообщений именно вам. Найти можно так: https://webhamster.ru/mytetrashare/index/mtb339/157520247213ix05mbe2" + }, + "retInfo": "Элемент данного модуля может иметь два значения 0 - все хорошо, 1 - произошла ошибка отправки сообщения, подробности в консоли. Данный статус можно использовать в сценарии для совершения экстренных действий при ошибке.", + "funcInfo": [ + { + "name": "sendMsg", + "descr": "Отправить сообщение без повторений.", + "params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"] + }, + { + "name": "sendOftenMsg", + "descr": "Отправить сообщение в любом случае, даж если отправляли такое ранее.", + "params": ["Сообщение, может быть строкой, числом или ИД другого элемента для получения значения"] + } + ] + }, + + "defActive": true, + + "devices": { + "esp32_4mb": [], + "esp8266_4mb": [] + } +}