/******************************************************************* * An example of bot that show bot action message. * * LED * * D15 = rot * D12 = grün * D13 = blau * * D1 * D3 * D4 * D5 * D14 * D16 * ChatID Mein G4 = 123456789 * ChatID iPhone4 = 123456889 * * PCF8574 Output * Pinleiste rechts * - gekürzter PIN * - gekürzter PIN (LED-Jumper) * - 7: TastLED links * - 6: TastLED rechts * - 5: TastLED mitte * - 4: PC (pcf8574.write(4, HIGH);) * - nc * - nc * - nc * - nc * - 2: Laterne (pcf8574.write(2, HIGH);) -> S2 - blau * - 1: Glühbirne (pcf8574.write(1, HIGH);) -> S4 - grau * - 0: Taschenlampe (pcf8574.write(0, HIGH);) -> S1 - schwarz * - Poti * - 5V * - GND * 3: an R7: TV (pcf8574.write(3, HIGH);) -> S3 - braun * * GND - weiß * * * FritzBox * User: ESP8266_homechecker * Passwort: homechecker_password * * * * * written by Vadim Sinitski * *******************************************************************/ #include #include #include ESP8266WiFiMulti WiFiMulti; #include #include #include #include //Local DNS Server used for redirecting all requests to the configuration portal #include //Local WebServer used to serve the configuration portal #include //https://github.com/tzapu/WiFiManager WiFi Configuration Magic #include // I2C -Bibliothek #include TwoWire testWire; // Initialize a PCF8574 at I2C-address 0x20, using GPIO5, GPIO4 and testWire for the I2C-bus PCF857x pcf8574(0x3F, &testWire); char IC = 0x3F; // Adresse des I2C ICs char output = 0; // Schltzustaende #define region // Initialize Wifi connection to the router char ssid[] = "WLAN_SSID"; // your network SSID (name) char password[] = "WLAN_PASSWORD"; // your network key // Initialize Telegram BOT #define BOTtoken "123456789:############################" // your Bot Token (Get from Botfather) WiFiClientSecure client; UniversalTelegramBot bot(BOTtoken, client); int Bot_mtbs = 1200; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done #include //IP address of your router. This should be "192.168.179.1" for all FRITZ!Boxes const char* IP = "192.168.123.254"; //Port of the API of your router. This should be 49000 for all TR-064 devices. const int PORT = 49000; //The username if you created and account, "admin" otherwise const char* fuser = "ESP8266_homechecker"; //The password for the aforementioned account. const char* fpass = "homechecker_password"; //Put the settings for the devices to detect here //The number of different people/user you want to be able to detect const int numUser = 3; //The maximum amount of devices per user const int maxDevices = 3; const char macsPerUser[numUser][maxDevices][18] = {{"30:9C:23:05:60:4B"}, //Gonz-PC, two devices {"DC:2B:61:45:4E:04"}, //iPhone4, one device {"A0:39:F7:10:05:53"}}; //meinG4, three devices //Do not mess with these :) //TR-064 connection TR064 connection(PORT, IP, fuser, fpass); //Status array. No need to change this! bool onlineUsers[numUser]; //Array-settings. No need to change these! const String STATUS_MAC = "MAC"; const String STATUS_IP = "IP"; const String STATUS_ACTIVE = "ACTIVE"; const String STATUS_HOSTNAME = "HOSTNAME"; const int STATUS_MAC_INDEX = 0; const int STATUS_IP_INDEX = 1; const int STATUS_ACTIVE_INDEX = 3; const int STATUS_HOSTNAME_INDEX = 2; int userPins[numUser] = {15, 12, 13}; bool PCHerunterfahren_aktiv = false; String PCHerunterfahren_chatid = ""; String Erlaubte_ChatIDs[2] = {"123456789","123456889"}; bool ZeitgesteuertesAusschalten_aktiv = false; long ZeitgesteuertesAusschalten_starttime; int LED1_value = 0; int LED2_value = 0; int LED3_value = 0; int pin_out_I2C_Clock = 0; int pin_out_I2C_Data = 2; int pin_in_Tastlinks = 3; int pin_in_Tastmitte = 4; int pin_in_Tastrechts = 5; int pin_in_12VPC = 14; int buttonState_Tastlinks = 0; // current state of the button int buttonState_Tastmitte = 0; // current state of the button int buttonState_Tastrechts = 0; // current state of the button int buttonState_12VPC = 0; // current state of the button int buttonPushCounter = 0; // counter for the number of button presses int lastButtonState_Tastlinks = 0; // previous state of the button int lastButtonState_Tastmitte = 0; // previous state of the button int lastButtonState_Tastrechts = 0; // previous state of the button unsigned long lastDebounceTime_Tastlinks = 0; //last time the pin was toggled, used to keep track of time unsigned long lastDebounceTime_Tastmitte = 0; //last time the pin was toggled, used to keep track of time unsigned long lastDebounceTime_Tastrechts = 0; //last time the pin was toggled, used to keep track of time unsigned long debounceDelay = 50; //the debounce time which user sets prior to run int LUX_value = 0; const uint16_t gamma_table[64] = { 0, 0, 1, 2, 4, 6, 9, 13, 16, 21, 26, 31, 37, 44, 51, 58, 66, 74, 84, 93, 103, 114, 125, 136, 148, 161, 174, 188, 202, 217, 232, 248, 264, 281, 298, 316, 334, 353, 372, 392, 412, 433, 455, 477, 499, 522, 545, 569, 594, 619, 644, 670, 697, 724, 752, 780, 808, 837, 867, 897, 928, 959, 991, 1023 }; void handleNewMessages(int numNewMessages) { Serial.println("handleNewMessages"); Serial.println(String(numNewMessages)); for (int i=0; i Invertiert delay(500); pcf8574.write(6, HIGH); // LED rechts aus delay(500); pcf8574.write(5, HIGH); // LED mitte aus delay(500); pcf8574.write(7, HIGH); // LED links aus pcf8574.write(4, HIGH); // PC Taster ein -> Invertiert bot.sendMessage(chat_id, "PC Herunterfahren..."); PCHerunterfahren_aktiv = true; PCHerunterfahren_chatid = chat_id; }else{ // PC ist aus, soll eingeschaltet werden pcf8574.write(4, LOW); // PC Taster aus -> Invertiert delay(2000); pcf8574.write(4, HIGH); // PC Taster ein -> Invertiert bot.sendMessage(chat_id, "PC Hochfahren..."); } text = "/status"; // Statusmeldung soll ausgegeben werden //pcf8574.write(7, LOW); // LED links ein //pcf8574.write(5, LOW); // LED mitte ein //pcf8574.write(6, LOW); // LED rechts ein //pcf8574.write(7, HIGH); // LED links aus //pcf8574.write(5, HIGH); // LED mitte aus //pcf8574.write(6, HIGH); // LED rechts aus } if (text == "ud83dudd5cud83dudeab") { // Zeitgesteuertes Ausschalten ZeitgesteuertesAusschalten_aktiv = true; ZeitgesteuertesAusschalten_starttime = millis(); } if(text.startsWith("ud83dudd26")){ // Taschenlampe if (text == "ud83dudd26u2705") { // Taschenlampe ON pcf8574.write(0, LOW); ZeitgesteuertesAusschalten_aktiv = false; } if (text == "ud83dudd26ud83dudeab") { // Taschenlampe OFF pcf8574.write(0, HIGH); } } if(text.startsWith("ud83dudca1")){ // Glühbirne if (text == "ud83dudca1u2705") { // Glühbirne ON pcf8574.write(1, LOW); ZeitgesteuertesAusschalten_aktiv = false; } if (text == "ud83dudca1ud83dudeab") { // Glühbirne OFF pcf8574.write(1, HIGH); } } if(text.startsWith("ud83cudfee")){ // Laterne if (text == "ud83cudfeeu2705") { // Laterne ON pcf8574.write(2, LOW); ZeitgesteuertesAusschalten_aktiv = false; } if (text == "ud83cudfeeud83dudeab") { // Laterne OFF pcf8574.write(2, HIGH); } } if(text.startsWith("ud83dudcf")){ // TV if (text == "ud83dudcfau2705") { // TV ON pcf8574.write(3, LOW); ZeitgesteuertesAusschalten_aktiv = false; } if (text == "ud83dudcfaud83dudeab") { // TV OFF pcf8574.write(3, HIGH); } } if (text == "/AllTVLightsOFF") { // All TV Lights OFF pcf8574.write(0, HIGH); pcf8574.write(1, HIGH); pcf8574.write(2, HIGH); text = "/status"; } //#region(}) //#region("Zimmersteuerung-Steuerung", {) if (text == "/ZimmersteuerungKeyboard" || text == "ud83dudcbb") { String keyboardJson = "[[\"\x31\xE2\x83\xA3\xF0\x9F\x94\xBC\", \"\x31\xE2\x83\xA3\xE2\x8F\xAB\", \"\x31\xE2\x83\xA3\xE2\x8F\xAC\", \"\x31\xE2\x83\xA3\xF0\x9F\x94\xBD\", \"\x31\xE2\x83\xA3\xE2\x9C\x85\", \"\x31\xE2\x83\xA3\xF0\x9F\x9A\xAB\"],[\"\x32\xE2\x83\xA3\xF0\x9F\x94\xBC\", \"\x32\xE2\x83\xA3\xE2\x8F\xAB\", \"\x32\xE2\x83\xA3\xE2\x8F\xAC\",\"\x32\xE2\x83\xA3\xF0\x9F\x94\xBD\", \"\x32\xE2\x83\xA3\xE2\x9C\x85\", \"\x32\xE2\x83\xA3\xF0\x9F\x9A\xAB\"],[\"\x33\xE2\x83\xA3\xF0\x9F\x94\xBC\", \"\x33\xE2\x83\xA3\xE2\x8F\xAB\", \"\x33\xE2\x83\xA3\xE2\x8F\xAC\",\"\x33\xE2\x83\xA3\xF0\x9F\x94\xBD\", \"\x33\xE2\x83\xA3\xE2\x9C\x85\", \"\x33\xE2\x83\xA3\xF0\x9F\x9A\xAB\"],[\"\xE2\x9D\x93\",\"\xF0\x9F\x93\xBA\xF0\x9F\x92\xA1\"]]"; bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true); } if(text.startsWith("1u20e3")){ // LED1 if (text == "1u20e3u2705") { // LED ON LED1_value = 63; } if (text == "1u20e3ud83dudeab") { // LED OFF LED1_value = 0; } if (text == "1u20e3ud83dudd3c") { // LED heller LED1_value = LED1_value + 1; if(LED1_value > 63){LED1_value = 63;} } if (text == "1u20e3ud83dudd3d") { // LED dunkler LED1_value = LED1_value - 1; if(LED1_value < 0){LED1_value = 0;} } if (text == "1u20e3u23eb") { // LED 10x heller LED1_value = LED1_value + 10; if(LED1_value > 63){LED1_value = 63;} } if (text == "1u20e3u23ec") { // LED 10x dunkler LED1_value = LED1_value - 10; if(LED1_value < 0){LED1_value = 0;} } analogWrite(12, gamma_table[LED1_value]); bot.sendMessage(chat_id, "LED1_value: " + String(gamma_table[LED1_value]) + " command_value: " + String(LED1_value)); } if(text.startsWith("2u20e3")){ // LED2 if (text == "2u20e3u2705") { // LED ON LED2_value = 63; } if (text == "2u20e3ud83dudeab") { // LED OFF LED2_value = 0; } if (text == "2u20e3ud83dudd3c") { // LED heller LED2_value = LED2_value + 1; if(LED2_value > 63){LED2_value = 63;} } if (text == "2u20e3ud83dudd3d") { // LED dunkler LED2_value = LED2_value - 1; if(LED2_value < 0){LED2_value = 0;} } if (text == "2u20e3u23eb") { // LED 10x heller LED2_value = LED2_value + 10; if(LED2_value > 63){LED2_value = 63;} } if (text == "2u20e3u23ec") { // LED 10x dunkler LED2_value = LED2_value - 10; if(LED2_value < 0){LED2_value = 0;} } analogWrite(13, gamma_table[LED2_value]); bot.sendMessage(chat_id, "LED2_value: " + String(gamma_table[LED2_value]) + " command_value: " + String(LED2_value)); } if(text.startsWith("3u20e3")){ // LED3 if (text == "3u20e3u2705") { // LED ON LED3_value = 63; } if (text == "3u20e3ud83dudeab") { // LED OFF LED3_value = 0; } if (text == "3u20e3ud83dudd3c") { // LED heller LED3_value = LED3_value + 1; if(LED3_value > 63){LED3_value = 63;} } if (text == "3u20e3ud83dudd3d") { // LED dunkler LED3_value = LED3_value - 1; if(LED3_value < 0){LED3_value = 0;} } if (text == "3u20e3u23eb") { // LED 10x heller LED3_value = LED3_value + 10; if(LED3_value > 63){LED3_value = 63;} } if (text == "3u20e3u23ec") { // LED 10x dunkler LED3_value = LED3_value - 10; if(LED3_value < 0){LED3_value = 0;} } analogWrite(15, gamma_table[LED3_value]); bot.sendMessage(chat_id, "LED3_value: " + String(gamma_table[LED3_value]) + " command_value: " + String(LED3_value)); } if (text == "/AllPCLightsOFF") { // All LED OFF LED1_value = 0; LED2_value = 0; LED3_value = 0; analogWrite(12, gamma_table[LED1_value]); analogWrite(13, gamma_table[LED2_value]); analogWrite(15, gamma_table[LED3_value]); text = "/status"; } //#region(}) bot.sendMessage(chat_id,text); if (text == "/send_test_action") { bot.sendChatAction(chat_id, "typing"); analogWrite(12, 512); delay(4000); bot.sendMessage(chat_id, "Did you see the action message?"); analogWrite(12, 0); analogWrite(13, 512); // You can't use own message, just choose from one of bellow //typing for text messages //upload_photo for photos //record_video or upload_video for videos //record_audio or upload_audio for audio files //upload_document for general files //find_location for location data //more info here - https://core.telegram.org/bots/api#sendchataction } if (text == "/start") { String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n"; welcome += "This is Chat Action Bot example.\n\n"; welcome += "/send_test_action : to send test chat action message\n"; welcome += "/getid : Chat ID\n"; welcome += "/ZimmersteuerungKeyboard : PC light control\n"; welcome += "/TVlichtKeyboard : TV and light control\n"; bot.sendMessage(chat_id, welcome); } if (text == "/status" || text == "u2753") { LUX_value = analogRead(A0); String welcome = "/status\n"; welcome += "\xE2\x98\x80 : " + String(LUX_value) + "\n"; welcome += "\xF0\x9F\x8F\xAE: " + String(!pcf8574.read(2)) +"\t\t\t"; // Laterne welcome += "\xF0\x9F\x94\xA6: " + String(!pcf8574.read(0)) +"\t\t\t"; // Taschenlampe welcome += "\xF0\x9F\x92\xA1: " + String(!pcf8574.read(1)) +"\t\t\t /AllTVLightsOFF\n"; // Glübirne welcome += "\xF0\x9F\x93\xBA: " + String(!pcf8574.read(3)) +"\t\t\t"; // TV welcome += "\xF0\x9F\x92\xBB: " + String(!pcf8574.read(4)) +"\t\t\t"; // PC welcome += "\xF0\x9F\x92\xBB 12V: " + String(!digitalRead(pin_in_12VPC)) +"\n"; // PC12V welcome += "\xE2\x8F\xAA: " + String(!pcf8574.read(7)) +"\t\t\t\xE2\x84\xB9: " + String(!pcf8574.read(5)) + "\t\t\t\xE2\x8F\xA9: " + String(!pcf8574.read(6)) +"\n"; // Tasterbeleuchtung links/mitte/rechts welcome += "\x31\xE2\x83\xA3: " + String(gamma_table[LED1_value]) + "; " + String(LED1_value) + " ";// +"\n"; welcome += "\x32\xE2\x83\xA3: " + String(gamma_table[LED2_value]) + "; " + String(LED2_value) + " ";// +"\n"; welcome += "\x33\xE2\x83\xA3: " + String(gamma_table[LED3_value]) + "; " + String(LED3_value) +"\n"; welcome += "/AllPCLightsOFF : All PC lights off\n"; welcome += "/ZimmersteuerungKeyboard : PC light control\n"; welcome += "/TVlichtKeyboard : TV and light control\n"; bot.sendMessage(chat_id, welcome); } } // Ende else = erlaubter User } } void setup() { Serial.begin(115200); analogWriteFreq(200); // setze PM Frequenz auf 200 Hz analogWrite(12, 1); // Alle PWM ansprechen, damit diesen auf 0 gesetzt werden analogWrite(13, 1); analogWrite(15, 1); analogWrite(12, 0); // Alle PWM auf 0 analogWrite(13, 0); analogWrite(15, 0); testWire.begin(2, 0); testWire.setClock(100000L); pcf8574.begin(); /* I have uncommented this part for debugging the Arduino-TR-064-SOAP-Library. I normal operation, I would like to use the WiFi Manager, but this does not have a high priority WiFiManager wifiManager; //first parameter is name of access point, second is the password wifiManager.autoConnect("AP-Heizung", "blubb"); */ //Connect to wifi WiFiMulti.addAP(ssid, password); //Wait for the wifi to connect and flash all LEDs while ((WiFiMulti.run() != WL_CONNECTED)) { for (int i=0;i 0; t--) { Serial.printf("[SETUP] WAIT %d...\n", t); for (int i=0;i 300000){ // 300000 ms = 5 min LED1_value = 0; LED2_value = 0; LED3_value = 0; analogWrite(12, gamma_table[LED1_value]); analogWrite(13, gamma_table[LED2_value]); analogWrite(15, gamma_table[LED3_value]); pcf8574.write(3, HIGH); // TV aus if(millis() - ZeitgesteuertesAusschalten_starttime > 600000){ // 600000 ms = 10 min pcf8574.write(0, HIGH); pcf8574.write(1, HIGH); pcf8574.write(2, HIGH); ZeitgesteuertesAusschalten_aktiv = false; } } } // compare the buttonState to its previous state if (buttonState_Tastlinks != lastButtonState_Tastlinks) { lastDebounceTime_Tastlinks = millis(); } // if the state has changed, increment the counter if ((millis()-lastDebounceTime_Tastlinks) > debounceDelay) { if (buttonState_Tastlinks == LOW) { // if the current state is HIGH then the button went from off to on: bot.sendMessage("123456789", "pin_in_Tastlinks"); buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button went from on to off: //Serial.println("off pin_in_Tastlinks"); } } // save the current state as the last state, for next time through the loop lastButtonState_Tastlinks = buttonState_Tastlinks; // compare the buttonState to its previous state if (buttonState_Tastmitte != lastButtonState_Tastmitte) { lastDebounceTime_Tastmitte = millis(); } // if the state has changed, increment the counter if ((millis()-lastDebounceTime_Tastmitte) > debounceDelay) { if (buttonState_Tastmitte == LOW) { bot.sendMessage("123456789", "pin_in_Tastmitte"); // if the current state is HIGH then the button went from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button went from on to off: //Serial.println("off pin_in_Tastmitte"); } } // save the current state as the last state, for next time through the loop lastButtonState_Tastmitte = buttonState_Tastmitte; // compare the buttonState to its previous state if (buttonState_Tastrechts != lastButtonState_Tastrechts) { lastDebounceTime_Tastrechts = millis(); } // if the state has changed, increment the counter if ((millis()-lastDebounceTime_Tastrechts) > debounceDelay) { if (buttonState_Tastrechts == LOW) { bot.sendMessage("123456789", "pin_in_Tastrechts"); // if the current state is HIGH then the button went from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button went from on to off: //Serial.println("off pin_in_Tastrechts"); } } // save the current state as the last state, for next time through the loop lastButtonState_Tastrechts = buttonState_Tastrechts; if (millis() > Bot_lasttime + Bot_mtbs) { int numNewMessages = bot.getUpdates(bot.last_message_received + 1); //String chat_id = String(bot.messages[0].chat_id); while(numNewMessages) { Serial.println("got response"); handleNewMessages(numNewMessages); numNewMessages = bot.getUpdates(bot.last_message_received + 1); } Bot_lasttime = millis(); } } ///////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////// helper methods////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////// /** * Get the number of devices that were connected to the WIFI lastly * (some of them might not be online anymore, you need to check them individually!) * return (int) */ int getWifiNumber() { String params[][2] = {{}}; String req[][2] = {{"NewTotalAssociations", ""}}; connection.action("urn:dslforum-org:service:WLANConfiguration:1", "GetTotalAssociations", params, 0, req, 1); int numDev = (req[0][1]).toInt(); return numDev; } /** Print the status of all devices that were connected to the WIFI lastly * (some of them might not be online anymore, also gets you the hostnames and macs) * return nothing as of yet */ void getStatusOfAllWifi() { getStatusOfAllWifi(getWifiNumber()); } /** * Print the status of all devices that were connected to the WIFI lastly * (some of them might not be online anymore, also gets you the hostnames and macs) * return nothing as of yet */ void getStatusOfAllWifi(int numDev) { //Query the mac and status of each device for (int i=0;i