GithubHelp home page GithubHelp logo

telegram-on-esp8266's Introduction

๐Ÿ’ก Controlling leds with Telegram

I used Telegram to control an esp8266 LED, I did this by creating a Telegram bot and using the Universal Telegram Bot Library to interact with my bot through Arduino.

What do you need for this project?

  • NodeMCU esp8266 board
  • RGB LED strip
  • Install: Arduino IDE & Telegram app

1. Creating a Telegram Bot. ๐Ÿค–

I followed a manual from ElectroRules (https://www.electrorules.com/telegram-control-esp32-esp8266-output), because it was one of the more recent ones I could find when Googeling 'telegram on esp8266', it was written on March 8, 2022. After installing Telegram, I opened the app and search for "botfather". Here I typed /newbot and gave my bot a name: Ledbot. After creating a bot you receive a message with your bot token.

2. Get your Telegram user ID. ๐Ÿ™‹

Then I had to get my Telegram user ID, so that the bot knows what messages are coming from my account. The manual I was following (https://www.electrorules.com/telegram-control-esp32-esp8266-output) told me to search for IDBot and type /getid to get my user ID.

Error ๐Ÿšฉ

I couldn't find 'IDBot' but I did see a channel called 'ID Bot news', where there was a pinned message talking about @usernametoidbot. Clicking this link finally send me to a chat with ID Bot, where it immediately gave me my user id after pressing Start.

3. Universal Telegram Bot Library. ๐Ÿ“ฆ

To interact with the Telegram bot I needed to install a library called Universal Telegram Bot Library by Brian Lough. I searched for it in the Arduino Library Manager and installed the lastest version. You can also download the zip file from https://www.arduinolibraries.info/libraries/universal-telegram-bot and include it in your project in the Sketch tab.

4. Code. ๐Ÿ”ฎ

After installing the library, I copied this code into my sketch:

To make this code work for me I had to insert my Bot token, user ID, and network credentials (SSID and password).

What does this code do?

This code checks for new messages every second, it then also checks the chat_id to see if the message is from you or if it should be ignored. If it's from you, it saves the message in a text variable and checks its content, and when it receives the /led_on message it turns on the LED and sends a confirmation message.

5. Upload. ๐Ÿ“ฅ

Before you upload the code to your board, check if the right board and port are selected under Tools. After uploading you can turn your LED on and off by talking to your bot on Telegram: /led_on turns the LED on, /led_off turns the LED off and /state requests the current LED state.

Nice, the code works! ๐ŸŽ‰

Error ๐Ÿšฉ

Now the blue led on the board turns on and off, BUT when is says ON it's actually OFF and vice versa. Why does this happen? The ElectroRules manual says this:

"The on-board LED should turn on and turn off accordingly (the ESP8266 on-board LED works in reverse, itโ€™s off when you send /led_on and on when you send /led_off)."

So, I should send a LOW signal to turn the LED on and a HIGH signal to turn it off. I can change these values in the code so it makes more sense when communicating it on Telegram:

6. RGB Ledstrip ๐ŸŒˆ

Then I tried if I could also control my RGB ledstrip. First I included the Adafruit library and defined the pin and the number of pixels:

#include "Adafruit_NeoPixel.h"
#define PIN          D5
#define NUM_PIXELS   14

Instead of defining LedState as LOW or HIGH when I write /led_on, I wrote these lines instead to turn on the ledstrip:

if (text == "/led_on") {
  bot.sendMessage(chat_id, "LED state set to ON", "");
  pixels.setPixelColor(i, pixels.Color(0, 150, 0));
  pixels.show();
}

Error ๐Ÿšฉ

Multiple things when wrong when I uploaded this code.

๐Ÿšจ Problem 1

Only one of the leds turns on. How can I turn on more leds, or make them blink?

It's because i is not defined. I have to loop through all of the leds in the ledstrip. So I wrote a loop inside of the if-statement that looks like this:

for(int i=0; i<NUM_PIXELS; i++) { // For each pixel...
  pixels.setPixelColor(i, pixels.Color(0, 150, 0));
  pixels.show();   // Send the updated pixel colors to the hardware
  delay(DELAYVAL); // Pause before next pass through loop
}

Now when I turn the leds on, they all turn on one by one.

๐Ÿšจ Problem 2

The led only turns on, not off.

This is what I wrote to turn off the leds:

if (text == "/led_off") {
  bot.sendMessage(chat_id, "LED state set to OFF", "");
  pixels.clear();
}

But appearently pixels.clear() didn't do anything. My solution was to make all pixels turn black (rgb(0,0,0,)) to get the same effect.

๐Ÿšจ Problem 3

I tried to rewrite the /state statement too, but it's broken now. The /state of the led is always seen as ON and never OFF, when I write this;

To fix this I read a some forum pages, like https://forum.arduino.cc/t/trying-to-check-if-a-neopixel-is-on-or-off/415587/8, and rewrote my if-statement using getPixelColor:

Sources ๐Ÿ—ƒ๏ธ

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.