myLord

esp8266_led_bot

Jul 24th, 2020 (edited)
436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.43 KB | None | 0 0
  1. /*******************************************************************
  2.    A telegram bot for your ESP8266 that responds
  3.     with whatever message you send it.
  4.  
  5.     Parts:
  6.     D1 Mini ESP8266 * - http://s.click.aliexpress.com/e/uzFUnIe
  7.     (or any ESP8266 board)
  8.  
  9.       = Affilate
  10.  
  11.     If you find what I do useful and would like to support me,
  12.     please consider becoming a sponsor on Github
  13.     https://github.com/sponsors/witnessmenow/
  14.  
  15.  
  16.     Written by Brian Lough
  17.     YouTube: https://www.youtube.com/brianlough
  18.     Tindie: https://www.tindie.com/stores/brianlough/
  19.     Twitter: https://twitter.com/witnessmenow
  20.  *******************************************************************/
  21.  
  22. // NOTE: The version of ESP8266 core needs to be 2.5 or higher
  23. // or else your bot will not connect.
  24.  
  25. // ----------------------------
  26. // Standard ESP8266 Libraries
  27. // ----------------------------
  28.  
  29. #include <ESP8266WiFi.h>
  30.  
  31. #include <WiFiClientSecure.h>
  32.  
  33. // ----------------------------
  34. // Additional Libraries - each one of these will need to be installed.
  35. // ----------------------------
  36.  
  37. #include <UniversalTelegramBot.h>
  38. #include <FastLED.h>
  39. // Initialize Wifi connection to the router
  40. char ssid[] = "askl";     // your network SSID (name)
  41. char password[] = "horseeatmybike"; // your network key
  42.  
  43. // Initialize Telegram BOT
  44. #define BOTtoken "1177476897:AAEhjLmblDwt6eJq-ZeDdwG2tka5rV0ypdg"  // your Bot Token (Get from Botfather)
  45.  
  46. WiFiClientSecure client;
  47. UniversalTelegramBot bot(BOTtoken, client);
  48.  
  49. //Checks for new messages every 1 second.
  50. int botRequestDelay = 500;
  51. unsigned long lastTimeBotRan;
  52.  
  53.  
  54. #define NUM_LEDS 41
  55. #define DATA_PIN 4
  56. CRGB leds[NUM_LEDS];
  57. int led_brightness = 2.55 * 50, warm_temp = 150;
  58. CHSV led_color;
  59. String mainKeyboard = "[[\"цвет\",\"яркость\"],[ \"режим\", \"выключить\"]]";
  60. String colorKeyboard = "[[\"холодный\", \"теплый\", \"холоднее\", \"теплее\"],[\"красный\", \"зеленый\", \"синий\", \"кастом\", \"назад\"]]";
  61. String modeKeyboard = "[[\"нормальный\"],[\"пульсации\", \"волна\", \"назад\"]]";
  62. String brightnessKeyboard = "[[\"+10\",\"+5\",\"+1\",\"-1\",\"-5\", \"-10\"],[\"10%\", \"30%\", \"50%\", \"70%\", \"100%\", \"назад\"]]";
  63. String customKeyboard = "[[\"R+\",\"G+\",\"B+\",\"тык\"],[\"R-\",\"G-\",\"B-\",\"назад\"]]";
  64. void setup() {
  65.   Serial.begin(115200);
  66.  
  67.   // This is the simplest way of getting this working
  68.   // if you are passing sensitive information, or controlling
  69.   // something important, please either use certStore or at
  70.   // least client.setFingerPrint
  71.   client.setInsecure();
  72.  
  73.   // Set WiFi to station mode and disconnect from an AP if it was Previously
  74.   // connected
  75.   WiFi.mode(WIFI_STA);
  76.   WiFi.disconnect();
  77.   delay(100);
  78.  
  79.   // Attempt to connect to Wifi network:
  80.   Serial.print("Connecting Wifi: ");
  81.   Serial.println(ssid);
  82.   WiFi.begin(ssid, password);
  83.  
  84.   while (WiFi.status() != WL_CONNECTED) {
  85.     Serial.print(".");
  86.     delay(500);
  87.   }
  88.  
  89.   Serial.println("");
  90.   Serial.println("WiFi connected");
  91.   Serial.print("IP address: ");
  92.   Serial.println(WiFi.localIP());
  93.   FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  94.   int i = 0;
  95.   do {
  96.     leds[i] = CRGB::Black;
  97.     i++;
  98.   } while (i < NUM_LEDS) ;
  99.   FastLED.show();
  100.   FastLED.setBrightness(led_brightness);
  101. }
  102.  
  103. void loop() {
  104.   if (millis() > lastTimeBotRan + botRequestDelay)  {
  105.     int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  106.  
  107.     while (numNewMessages) {
  108.       Serial.println("got response");
  109.       for (int i = 0; i < numNewMessages; i++) {
  110.         message_handler(bot.messages[i].text, String(bot.messages[i].chat_id));
  111.         //  bot.sendMessage(bot.messages[i].chat_id, bot.messages[i].text, "");
  112.         // Serial.println(bot.messages[i].text);
  113.       }
  114.       numNewMessages = bot.getUpdates(bot.last_message_received + 1);
  115.     }
  116.  
  117.     lastTimeBotRan = millis();
  118.   }
  119. }
  120. void message_handler(String text, String chat_id) {
  121.  
  122.   if (text == "/start" || text == "назад") {
  123.     bot.sendMessageWithReplyKeyboard(chat_id, "Главное меню", "", mainKeyboard, true);
  124.   }
  125.   else if (text == "цвет") {
  126.     bot.sendMessageWithReplyKeyboard(chat_id, "Настрока цвета", "", colorKeyboard, true);
  127.   }
  128.   else if (text == "режим") {
  129.     bot.sendMessageWithReplyKeyboard(chat_id, "Выбор режима", "", modeKeyboard, true);
  130.   }
  131.   else if (text == "яркость") {
  132.     int percent_brightness = led_brightness / 2.55;
  133.     bot.sendMessage(chat_id, "Текущая яркость:" + String(percent_brightness) + "%", "");
  134.     bot.sendMessageWithReplyKeyboard(chat_id, "Настройка яркости", "", brightnessKeyboard, true);
  135.   }
  136.   else if (text == "кастом") {
  137.     /*int percent_brightness = led_brightness / 2.55;
  138.       bot.sendMessage(chat_id, "Текущая яркость:" + String(percent_brightness) + "%", "");*/
  139.     bot.sendMessageWithReplyKeyboard(chat_id, "Свой цвет", "", customKeyboard, true);
  140.   }
  141.   else if (text == "+10") {
  142.     led_brightness += 10 * 2.55;
  143.     led_brightness > 255 ? led_brightness = 255 : led_brightness * 1;
  144.     int percent_brightness = led_brightness / 2.55;
  145.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  146.     FastLED.setBrightness(led_brightness);
  147.     FastLED.show();
  148.   }
  149.   else if (text == "+5") {
  150.     led_brightness += 5 * 2.55;
  151.     led_brightness > 255 ? led_brightness = 255 : led_brightness * 1;
  152.     int percent_brightness = led_brightness / 2.55;
  153.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  154.     FastLED.setBrightness(led_brightness);
  155.     FastLED.show();
  156.   }
  157.   else if (text == "+1") {
  158.     led_brightness += 1 * 2.55;
  159.     led_brightness > 255 ? led_brightness = 255 : led_brightness * 1;
  160.     int percent_brightness = led_brightness / 2.55;
  161.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  162.     FastLED.setBrightness(led_brightness);
  163.     FastLED.show();
  164.   }
  165.   else if (text == "-10") {
  166.     led_brightness -= 10 * 2.55;
  167.     led_brightness < 1 ? led_brightness = 1 : led_brightness * 1;
  168.     int percent_brightness = led_brightness / 2.55;
  169.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  170.     FastLED.setBrightness(led_brightness);
  171.     FastLED.show();
  172.   }
  173.   else if (text == "-5") {
  174.     led_brightness -= 5 * 2.55;
  175.     led_brightness < 1 ? led_brightness = 1 : led_brightness * 1;
  176.     int percent_brightness = led_brightness / 2.55;
  177.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  178.     FastLED.setBrightness(led_brightness);
  179.     FastLED.show();
  180.   }
  181.   else if (text == "-1") {
  182.     led_brightness -= 1 * 2.55;
  183.     led_brightness < 1 ? led_brightness = 1 : led_brightness * 1;
  184.     int percent_brightness = led_brightness / 2.55;
  185.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  186.     FastLED.setBrightness(led_brightness);
  187.     FastLED.show();
  188.   }
  189.   else if (text == "10%") {
  190.     led_brightness = 10 * 2.55;
  191.     int percent_brightness = led_brightness / 2.55;
  192.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  193.     FastLED.setBrightness(led_brightness);
  194.     FastLED.show();
  195.   }
  196.   else if (text == "30%") {
  197.     led_brightness = 30 * 2.55;
  198.     int percent_brightness = led_brightness / 2.55;
  199.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  200.     FastLED.setBrightness(led_brightness);
  201.     FastLED.show();
  202.   }
  203.   else if (text == "50%") {
  204.     led_brightness = 50 * 2.55;
  205.     int percent_brightness = led_brightness / 2.55;
  206.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  207.     FastLED.setBrightness(led_brightness);
  208.     FastLED.show();
  209.   }
  210.   else if (text == "70%") {
  211.     led_brightness = 70 * 2.55;
  212.     int percent_brightness = led_brightness / 2.55;
  213.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  214.     FastLED.setBrightness(led_brightness);
  215.     FastLED.show();
  216.   }
  217.   else if (text == "100%") {
  218.     led_brightness = 100 * 2.55;
  219.     int percent_brightness = led_brightness / 2.55;
  220.     bot.sendMessage(chat_id, String(percent_brightness) + "%", "");
  221.     FastLED.setBrightness(led_brightness);
  222.     FastLED.show();
  223.   }
  224.   else if (text == "холодный") {
  225.     led_color = CHSV(60, 0, 255);
  226.     for (int i = 0; i <= NUM_LEDS; i++) {
  227.       leds[i] = led_color;
  228.     }
  229.     FastLED.show();
  230.     bot.sendMessage(chat_id, "White_ok", "");
  231.   }
  232.   else if (text == "теплый") {
  233.     led_color = CHSV(60, warm_temp, 255);
  234.     for (int i = 0; i <= NUM_LEDS; i++) {
  235.       leds[i] = led_color;
  236.     }
  237.     FastLED.show();
  238.     bot.sendMessage(chat_id, "WarmWhite_ok", "");
  239.   }
  240.   else if (text == "теплее") {
  241.     warm_temp += 10;
  242.     warm_temp > 255 ? warm_temp = 255 : warm_temp * 1;
  243.     //  int percent_warm = warm_temp / 2.55;
  244.     led_color = CHSV(60, warm_temp, 255);
  245.     for (int i = 0; i <= NUM_LEDS; i++) {
  246.       leds[i] = led_color;
  247.     }
  248.     FastLED.show();
  249.     bot.sendMessage(chat_id, String(warm_temp) + "%", "");
  250.   }
  251.   else if (text == "холоднее") {
  252.     warm_temp -= 10;
  253.     warm_temp < 1 ? warm_temp = 1 : warm_temp * 1;
  254.     // int percent_warm = warm_temp / 2.55;
  255.     led_color = CHSV(60, warm_temp, 255);
  256.     for (int i = 0; i <= NUM_LEDS; i++) {
  257.       leds[i] = led_color;
  258.     }
  259.     FastLED.show();
  260.     bot.sendMessage(chat_id, String(warm_temp) + "%", "");
  261.   }
  262.   else if (text == "синий") {
  263.   //  led_color = CRGB::Blue;
  264.     for (int i = 0; i <= NUM_LEDS; i++) {
  265.       leds[i] = CRGB::Blue;
  266.     }
  267.     FastLED.show();
  268.     // delay(500);
  269.     bot.sendMessage(chat_id, "Синий_ok", "");
  270.   }
  271.   else if (text == "красный") {
  272.    // led_color = CRGB::Red;
  273.     for (int i = 0; i <= NUM_LEDS; i++) {
  274.       leds[i] = CRGB::Red;
  275.     }
  276.     FastLED.show();
  277.     //  delay(500);
  278.     bot.sendMessage(chat_id, "Красный_ok", "");
  279.   }
  280.   else if (text == "зеленый") {
  281.    // led_color = CRGB::Green;
  282.     for (int i = 0; i <= NUM_LEDS; i++) {
  283.       leds[i] = CRGB::Green;
  284.     }
  285.     FastLED.show();
  286.     //  delay(500);
  287.     bot.sendMessage(chat_id, "Зеленый_ok", "");
  288.   }
  289.   else if (text == "выключить") {
  290.     //led_color = CRGB::Black;
  291.     for (int i = 0; i <= NUM_LEDS; i++) {
  292.       leds[i] = CRGB::Black;
  293.     }
  294.     FastLED.show();
  295.     //  delay(500);
  296.     bot.sendMessage(chat_id, "Зеленый_ok", "");
  297.   }
  298.   else if (text == "тык") {
  299.     bot.sendMessage(chat_id, "Не тыкай", "");
  300.   }
  301.  
  302.  
  303. }
Add Comment
Please, Sign In to add comment