«Arduino и Telegram» – Урок 7: кнопка и уведомления

Седьмой урок из цикла «Arduino и Telegram». В нём мы научимся отправлять уведомления на телефон при нажатии кнопки, подключенной к цифровому пину nodemcu.
[vc_row][vc_column][vc_wp_text][intense_spacer type=»block» height=»30″]

Седьмой урок из цикла «Arduino и Telegram».  В нём мы научимся отправлять уведомления на телефон при нажатии кнопки, подключенной к цифровому пину nodemcu.[/vc_wp_text][/vc_column][/vc_row][vc_row id=»» color=»warning» font_color=»» hover_color=»» border_radius=»10px» box_shadow=»» block=»» close=»1″ close_memory=»2″ shadow=»0″ class=»» margin_top=»» margin_bottom=»» margin_left=»» margin_right=»» padding_top=»» padding_bottom=»» padding_left=»» padding_right=»» tab_984862461=»» tab_411412670=»» tab_1479978263=»» tab_1595777515=»» tab_1376433241=»» tab_928372326=»»][vc_column][intense_alert border_radius=»10px» close=»1″ close_memory=»2″ shadow=»0″ margin_bottom=»» tab_984862461=»» tab_411412670=»» tab_1479978263=»» tab_1595777515=»» tab_1376433241=»» tab_928372326=»» tab_600076829=»» tab_498973325=»»]Для прохождения данного урока необходимо настроить ArduinoIDE и подключить телеграм бота, как показано в первом уроке.[/intense_alert][/vc_column][/vc_row][vc_row id=»» color=»warning» font_color=»» hover_color=»» border_radius=»10px» box_shadow=»» block=»» close=»1″ close_memory=»2″ shadow=»0″ class=»» margin_top=»» margin_bottom=»» margin_left=»» margin_right=»» padding_top=»» padding_bottom=»» padding_left=»» padding_right=»» tab_984862461=»» tab_411412670=»» tab_1479978263=»» tab_1595777515=»» tab_1376433241=»» tab_928372326=»»][vc_column][intense_alert color=»warning» border_radius=»10px» close=»1″ close_memory=»2″ shadow=»0″ margin_bottom=»» tab_984862461=»» tab_411412670=»» tab_1479978263=»» tab_1595777515=»» tab_1376433241=»» tab_928372326=»» tab_600076829=»» tab_498973325=»»]Не забывайте о технике безопасности при работе с электрическими устройствами![/intense_alert][/vc_column][/vc_row][vc_row][vc_column][vc_tta_tour][vc_tta_section title=»1. Компоненты» tab_id=»1532119076476-8190d56c-1920″][vc_wp_text]Нам понадобятся NodeMCU, соединительные провода, breadboard, 10 кОм резистор и кнопка (купить можно в любом магазине радиодеталей).[/vc_wp_text][vc_gallery interval=»3″ images=»2915,2935,2938,2993″ img_size=»full»][/vc_tta_section][vc_tta_section title=»2. Схема подключения» tab_id=»1532119076511-897e574e-879b»][vc_wp_text]С одной стороны на кнопку подаем 3.3 Вольта, а с другой подтягиваем резистором к земле и подключаем к пину nodemcu.

[/vc_wp_text][/vc_tta_section][vc_tta_section title=»3. ID чата» tab_id=»1532119690759-79e55322-7470″][vc_wp_text]Для прошивки nodemcu нам понадобится знать свой id. Для этого переходим к боту @myidbot и отправляем две команды: /start, /getid.[/vc_wp_text][/vc_tta_section][vc_tta_section title=»4. Код прошивки» tab_id=»1532119846375-1e8ebdff-0909″][vc_wp_text]Скетч можно скачать на github.

В прошивке можно выбрать другой пин кнопки, id чата, настройки сети и токен бота.

//настройки
#define btn1 5 // пин кнопки
#define id "437751818" //id чата
const char* ssid = "ssid"; // имя сети
const char* password = "password"; // пароль сети
const char BotToken[] = "token:token"; //токен бота

//библиотеки
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <TelegramBot.h>

//соединение и бот
WiFiClientSecure net_ssl;
TelegramBot bot (BotToken, net_ssl);

//первый запуск
void setup() {
  
  Serial.begin(9600);
  
  wifiConnect();
  bot.begin(); 
    
  pinMode(btn1, INPUT); 
}

//основной цикл
void loop() { 
  
  if (digitalRead(btn1)==HIGH){ //если кнопка нажата
    bot.sendMessage(id, "Button pressed"); //отправить сообщение
  }
  
}

//функция для подключения к wifi
void wifiConnect(){
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}
[/vc_wp_text][/vc_tta_section][vc_tta_section title=»5. Демонстрация работы» tab_id=»1532119989087-9df98335-910c»][vc_wp_text]Демонстрация работающего устройства.[/vc_wp_text][/vc_tta_section][/vc_tta_tour][/vc_column][/vc_row][vc_row][vc_column][vc_wp_text]Если вы заметили какие-то ошибки или что-то показалось непонятным, буду очень рад если напишите об этом в комментариях. [/vc_wp_text][/vc_column][/vc_row]

Загрузка ...
The Robot
Adblock
detector