/******* Output pin map************** Ch1 = GPIO16; Ch2 = GPIO14; Ch3 = GPIO12; Ch4 = GPIO13; Ch5 = GPIO15; Ch6 = GPIO0; Ch7 = GPIO4; Ch8 = GPIO5; ************************************/ #include #include #include #include // Wifi network station credentials #define WIFI_SSID "MyWiFiSSID" \\fill with your wifi data #define WIFI_PASSWORD "MyWiFiPWD" \\fill with your wifi data // Telegram BOT Token (Get from Botfather) #define BOT_TOKEN "123456789:ABCDefghIJKlmnOPQrstUVWxyzA-bCdEfGh" const unsigned long BOT_MTBS = 1000; // mean time between scan messages X509List cert(TELEGRAM_CERTIFICATE_ROOT); WiFiClientSecure secured_client; UniversalTelegramBot bot(BOT_TOKEN, secured_client); unsigned long bot_lasttime; // last time messages' scan has been done bool Ch1_state; bool Ch2_state; bool Ch3_state; bool Ch4_state; bool Ch5_state; bool Ch6_state; bool Ch7_state; bool Ch8_state; String msg_text; void All_Off() { digitalWrite(16,0); Ch1_state = false; digitalWrite(14,0); Ch2_state = false; digitalWrite(12,0); Ch3_state = false; digitalWrite(13,0); Ch4_state = false; digitalWrite(15,0); Ch5_state = false; digitalWrite(0,0); Ch6_state = false; digitalWrite(4,0); Ch7_state = false; digitalWrite(5,0); Ch8_state = false; } void ch_icon(bool Chstatus) { if (Chstatus) // Relay ON, icon "V" { msg_text += char(226); msg_text += char(156); msg_text += char(133); msg_text += " "; } else //Relay OFF, icon "X" { msg_text += char(226); msg_text += char(157); msg_text += char(140); msg_text += " "; } } void ArduinoOTASetup() { //IDE OTA ArduinoOTA.begin(); // OTA Upload via ArduinoIDE https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html // OTA Messages ArduinoOTA.onStart([]() { Serial.println(F("OTA Start")); }); ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { static byte previous = 0; byte result = progress / (total / 100); if (previous != result) { if ((result % 10) == 0) { Serial.print(result); Serial.println("%"); } else Serial.print("."); } previous = result; }); ArduinoOTA.onEnd([]() { Serial.println(F("\nOTA End")); }); ArduinoOTA.onError([](ota_error_t error) { Serial.printf("\nOTA Error[%u]: ", error); if (error == OTA_AUTH_ERROR) Serial.println(F("\nAuth Failed")); else if (error == OTA_BEGIN_ERROR) Serial.println(F("\nBegin Failed")); else if (error == OTA_CONNECT_ERROR) Serial.println(F("\nConnect Failed")); else if (error == OTA_RECEIVE_ERROR) Serial.println(F("\nReceive Failed")); else if (error == OTA_END_ERROR) Serial.println(F("\nEnd Failed")); }); } void handleNewMessages(int numNewMessages) { for (int i = 0; i < numNewMessages; i++) { String chat_id = bot.messages[i].chat_id; String text = bot.messages[i].text; if(text.length() > 3) text.setCharAt(3,0); String from_name = bot.messages[i].from_name; if (from_name == "") from_name = "Guest"; Serial.println(chat_id); Serial.println(from_name); Serial.println(text); if (chat_id == "123456789" || chat_id == "-123456789") // Replace with personal ID and group ID { if(text.charAt(0) == '/') //Leading char is '/', string is a command { if (text == "/00") All_Off(); if (text == "/11") {digitalWrite(16, 1); Ch1_state = true;} if (text == "/10") {digitalWrite(16, 0); Ch1_state = false;} if (text == "/21") {digitalWrite(14, 1); Ch2_state = true;} if (text == "/20") {digitalWrite(14, 0); Ch2_state = false;} if (text == "/31") {digitalWrite(12, 1); Ch3_state = true;} if (text == "/30") {digitalWrite(12, 0); Ch3_state = false;} if (text == "/41") {digitalWrite(13, 1); Ch4_state = true;} if (text == "/40") {digitalWrite(13, 0); Ch4_state = false;} if (text == "/51") {digitalWrite(15, 1); Ch5_state = true;} if (text == "/50") {digitalWrite(15, 0); Ch5_state = false;} if (text == "/61") {digitalWrite(0, 1); Ch6_state = true;} if (text == "/60") {digitalWrite(0, 0); Ch6_state = false;} if (text == "/71") {digitalWrite(4, 1); Ch7_state = true;} if (text == "/70") {digitalWrite(4, 0); Ch7_state = false;} if (text == "/81") {digitalWrite(5, 1); Ch8_state = true;} if (text == "/80") {digitalWrite(5, 0); Ch8_state = false;} msg_text = "Output status:\n"; msg_text += char(49); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":one:"; msg_text += char(50); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":two:"; msg_text += char(51); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":three:"; msg_text += char(52); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":four:"; msg_text += char(53); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":five:"; msg_text += char(54); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":six:"; msg_text += char(55); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += " "; //":seven:"; msg_text += char(56); msg_text += char(226); msg_text += char(131); msg_text += char(163);msg_text += "\n"; //":eight:"; ch_icon(Ch1_state); ch_icon(Ch2_state); ch_icon(Ch3_state); ch_icon(Ch4_state); ch_icon(Ch5_state); ch_icon(Ch6_state); ch_icon(Ch7_state); ch_icon(Ch8_state); bot.sendMessage(chat_id, msg_text, ""); } else { msg_text = char(226); msg_text += char(129); msg_text += char(137); // !? icon bot.sendMessage(chat_id, msg_text, ""); msg_text = "Unrecognized command"; bot.sendMessage(chat_id, msg_text, ""); } } else { msg_text = char(226); msg_text += char(155); msg_text += char(148); //Access denied icon bot.sendMessage(chat_id, msg_text, ""); msg_text = "Access denied!"; bot.sendMessage(chat_id, msg_text, ""); } } } void setup() { Serial.begin(115200); Serial.println(); All_Off(); // attempt to connect to Wifi network: Serial.print("Connecting to Wifi SSID "); Serial.print(WIFI_SSID); WiFi.begin(WIFI_SSID, WIFI_PASSWORD); secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.print("\nWiFi connected. IP address: "); Serial.println(WiFi.localIP()); Serial.print("Retrieving time: "); configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP time_t now = time(nullptr); while (now < 24 * 3600) { Serial.print("."); delay(100); now = time(nullptr); } Serial.println(now); ArduinoOTASetup(); pinMode(16, OUTPUT); pinMode(14, OUTPUT); pinMode(12, OUTPUT); pinMode(13, OUTPUT); pinMode(15, OUTPUT); pinMode(0, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); } void loop() { if (millis() - bot_lasttime > BOT_MTBS) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); while (numNewMessages) { Serial.println("got response"); handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } bot_lasttime = millis(); } ArduinoOTA.handle(); // OTA Upload via ArduinoIDE }