#define BOT_TOKEN "Токен" #define CHAT_ID "ID чата или юзера" #include#ifdef ESP32 #include #include #elif defined(ESP8266) #include #include #endif #include #include FastBot bot(BOT_TOKEN); AsyncWebServer server(80); const char* WiFi_hostname = "ESP8266-MyHome"; const char* ssid = "SSid сети"; const char* password = "Пароль"; const char* PARAM_STATUS = "status"; const char* PARAM_COM = "com"; const char* PARAM_PIN = "pin"; void notFound(AsyncWebServerRequest *request) { request->send(404, "text/plain", "Not found"); } const int relay1 = 12; //свет const int relay2 = 13; // const int relay3 = 14; // const int relay4 = 15; // void setup() { pinMode(relay1, OUTPUT); pinMode(relay2, OUTPUT); pinMode(relay3, OUTPUT); pinMode(relay4, OUTPUT); digitalWrite(relay1, HIGH); digitalWrite(relay2, HIGH); digitalWrite(relay3, HIGH); digitalWrite(relay4, HIGH); Serial.begin(74880); WiFi.hostname(WiFi_hostname); WiFi.mode(WIFI_STA); WiFi.begin(ssid, password); if (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.printf("WiFi Failed!\n"); return; } Serial.print("IP Address: "); Serial.println(WiFi.localIP()); bot.sendMessage("Старт ESP управления", CHAT_ID); //server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){ // request->send(200, "text/plain", "Hello, world"); //}); // Send a POST request to /post with a form field message set to server.on("/", HTTP_POST, [](AsyncWebServerRequest *request){ String stat; String com; String pin; if (request->hasParam(PARAM_STATUS, true)) { stat = request->getParam(PARAM_STATUS, true)->value(); com = request->getParam(PARAM_COM, true)->value(); pin = request->getParam(PARAM_PIN, true)->value(); int comanda = com.toInt(); int port = pin.toInt(); if(comanda==1){ digitalWrite(port, LOW); } if(comanda==0){ digitalWrite(port, HIGH); } if(comanda==2){ digitalWrite(port, LOW); delay(500); digitalWrite(port, HIGH); } } else { stat = "No message sent"; } Serial.print(stat); request->send(200, "text/plain", "Hello, POST: Stat" + stat +" com"+ com + "pin"+pin); }); server.onNotFound(notFound); server.begin(); } void loop() { }
Подключение библиотеки FastBot.h дает возможность отправки сообщения в телеграмм сообщения. В текущем примере в момент включения контроллера