ESP8266 Projects

A Dash Button with Telegram and an ESP8266

Dash Button with Telegram and ESP8266

In this project, you will use an ESP8266 to set up a Dash button that sends a message to your Telegram account at the touch of a button. You can receive this message anywhere on your smartphone – as long as you have Internet access.

Beginner

1 – 2 hours

ca. $10

For this project you need (quantities see description):

KINGSO 500 Stück 5mm LED Leuchtdioden Dioden set Elektronik komponenten rund 5 Farben(100er Jede Farbe)
KINGSO LEDs ist Energieeffizient & hochwertige, Bitte seien Sie versichert zu kaufen.
9,99 EUR
10 Stück Metallschichtwiderstände 1/4 Watt - 330 Ohm
10 Stück Metallschichtwiderstände 1/4 Watt - 330 Ohm; Dauerspannung max.: 250 V |...
10 Stück Metallschichtwiderstände 1/4 Watt - 10 kOhm
10 Stück Metallschichtwiderstände 1/4 Watt - 10 kOhm; Dauerspannung max.: 250 V |...
Tactile Switch 25pcs Tactile Push Button Switch 4 Pins SMD PCB Micro Momentary Tact Assortment Kit 12x12x7.3mm with 5 Colors Round Cap and Box for Arduino
25pcs Tactile Push Button Switch with 25pcs 5 Colors Round Cap.; Knopffarbe: Schwarz und...
9,99 EUR
AZDelivery Mini Breadboard 400 Pin mit 4 Stromschienen kompatibel mit Arduino und Jumper Wire Kabeln
✔️ Breadboard – kaufen Sie jetzt zum Vorteilspreis!; ✔️ Steckbrett für...

Prepare Telegram

First you need an account with Telegram – and the corresponding app for your smartphone or computer. In the following we use a Smartphone. Telegram is free, ad-free, and works much like WhatsApp. However, here you have the possibility to create bots that you can interact with.

You can take advantage of that in this project by letting your ESP8266 “talk” to your Telegram bot. The bot in turn will immediately send you a notification.

Create a Telegram bot

Download Telegram from the Play Store or App Store to your smartphone – if you haven’t already done so. Create an account there and then search for Botfather under Chats.

Botfather in Telegram

Open the Botfather and tap Start. After his welcome you will receive instructions. You can read it now or tap on /newbot. After that the Botfather will ask you for a name for your bot and a username:

Create a new bot in Telegram

After you have assigned the names, you will receive a message with your token. You will need this token later in your sketch to connect your ESP8266 to your Telegram bot.

Get the token for your Telegram bot

Get your User ID

You have now created a Telegram bot. Now you only need your User ID to send messages to the bot. This also takes only a few seconds.

In the Telegram app go back to the Chats and search for IDBot:

Search IDBot in Telegram

Now tap on IDBot and in the following chat window tap on /getid. You will immediately receive a message with your User ID:

And these were already the preparations in Telegram. Keep your token and your User ID ready to enter them into the sketch later. And above all, keep it secret so that no one else uses your bot.

Setting up the project

It takes only a few minutes to set up on your breadboard. Just orientate yourself by this schematic:

Set up Dash Button with Telegram

The button is connected to the ESP8266 via a pull-down resistor so that it sends a stable 0 to your microcontroller as long as you don’t press it. Only when this is the case will your ESP8266 receive a signal and send a message to your Telegram bot.

Once the message has been sent, the LED will light up briefly.

The sketch for the Dash button

Now it is time for some code. If you have never used an ESP8266 with the Arduino IDE: In this tutorial you will learn how to make your ESP8266 available and program it in the Arduino IDE.

The required libraries

For your Dash button you need three libraries. Two of them should already be pre-installed on your computer: ESP8266WiFi.h and WiFiClientSecure.h – you need them to connect to your Wi-Fi network and send data.

The library UniversalTelegramBot.h handles the communication with your Telegram bot. You can find it in the Library Manager of the Arduino IDE – but it might be outdated. Therefore we recommend you to download the library here.

Then you need to include this library in your sketch by selecting Sketch > Include Library > Add .ZIP Library from the Arduino IDE Sketch menu and selecting the ZIP file you just downloaded.

Now copy the following sketch, load it onto your ESP8266 and try the alarm right away!

Note: Before your Telegram bot can receive messages, you must first open it and tap Start. You only have to do this once at the beginning.

View Sketch as .txt

/*
   Dash Button with Telegram and an ESP8266 - polluxlabs.net
*/

#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>

//Your WiFi Credentials
const char* ssid = "NETWORK";
const char* password = "PASSWORD";

//Initialize Telegram-Bot
#define botToken "DEIN TOKEN"  //Bot-Token from Botfather)

//Your User ID
#define userID "USER ID"

WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

//State of Button
int switchState = 0;

void setup() {
  Serial.begin(115200);
  client.setInsecure();

  // Verwendete Pins
  pinMode(13, INPUT); //Button
  pinMode(15, OUTPUT); //LED


  //Connect to WiFi
  Serial.print("Connecting to: ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  Serial.println("");
  Serial.println("Connected!");

  bot.sendMessage(userID, "Bot started", "");
}

void loop() {

  switchState = digitalRead(13);
  Serial.println(switchState);
  if (switchState) {
    bot.sendMessage(userID, "Button!", "");

    digitalWrite(15, HIGH);
    delay(200);
    digitalWrite(15, LOW);
    delay(200);
    digitalWrite(15, HIGH);
    delay(200);
    digitalWrite(15, LOW);
  }
}

Now let’s have a closer look at some important parts of the sketch.

First there are some data that you have to replace with your own: Your Wi-Fi access data as well as your token and User ID from Telgram. Enter these data here:

const char* ssid = "NETWORK";
const char* password = "PASSWORD";
#define botToken "TOKEN"
#define userID "USER ID"

Then you create an instance of WiFiClientSecure names clientand also a botwith your botToken and client defined above.

WiFiClientSecure client;
UniversalTelegramBot bot(botToken, client);

In the setup of the sketch you define the two pins you use. The 13 stands for pin D7 on ESP8266 and the 15 for pin D8.

  pinMode(13, INPUT); //Button
  pinMode(15, OUTPUT); //LED

Finally, you start the connection to the Internet. Once connected, your sketch will send a message telling you that the bot has been started.

In the Loop your sketch is now waiting for you to push the button. As soon as this is the case, the function bot.sendMessage() sends a message to the Telegram bot. After that the LED lights up twice briefly.

  switchState = digitalRead(13);
  if (switchState) {
    bot.sendMessage(userID, "Button!", "");

    digitalWrite(15, HIGH);
    delay(200);
    digitalWrite(15, LOW);
    delay(200);
    digitalWrite(15, HIGH);
    delay(200);
    digitalWrite(15, LOW);
  }

The message you want to send can of course be defined in the function bot.sendMessage().

What’s next?

You now have a Dash button that can send messages to your Telegram bot. A similar project could be e.g. a temperature sensor that sends a warning message to your smartphone at a preset temperature.

On Pollux Labs we have many tutorials and projects, which deal with temperature and other sensors.

Have fun exploring! 🙂

Letzte Aktualisierung am 2021-01-09 / Affiliate Links / Bilder von der Amazon Product Advertising API

You may also like

Comments are closed.