Introduction: IoT Manual - Digital Remote for LEDstrip

Hi! Welcome to this manual,

You are going to learn how to control a LED light strip with your phone through Telegram.

Supplies

What you will need for this experiment:

  • A 'Node MCU' (version B) development kit (USB cable and NodeMCU)
  • A Neopixel LED strip that is already connected to three female-female cables. You can do this yourself and solder the ends of the cables to the tinnen strips at the end of the LED strip. Tip: secure these cables with some glue from a glue gun.
  • A computer with USB-port
  • Three female-female cables to connect your LED to your node

Step 1: Step 1: Download Files and Libraries

The first step is to download the Arduino program for your computer.

If you click on this link: https://www.arduino.cc/en/Main/Software you will enter the website of Arduino. Follow the steps that are given on the page.

Once you have downloaded this application, you have to move it from downloads to your apps. Open the app on your laptop after you have done this

Checkpoint: the Arduino application has to open on your screen.

Next you have to download these files, click on clone or download and open the zip file. https://github.com/MPParsley/ch340g-ch34g-ch34x-m...

Restart your laptop

After you have done this, go to your Security & Privacy settings and give the driver permission. (shown in this image below)

Now we have to add the NodeMCU to Arduino. We do that when we go to Arduino > Preferences and paste the following line at the bottom of the page at "Additional Boards Manager URLs"

http://arduino.esp8266.com/stable/package_esp8266c...

Then press OK.

After this go to Sketch > Include Library > Manage Libraries. In the top right corner you can search for different libraries that we need to achieve our goal.

These are:

  • esp8266 by ESP8266 Community (version 2.5.0)
  • UniversalTelegramBot (by Brian Lough)
  • ArduinoJson (by Benoit Blanchon, version 5.13.3)

When you have installed these libraries you have to restart your laptop again (I know it's a pain but it's necessary)

The code that we will be using is on Github: https://github.com/witnessmenow/Universal-Arduino-...

Clone or download these files and open up the Zip file. Place the file in your Arduino file.

!Don't put the file in the libraries! When I placed the files in the libraries I got an error right away and we don't want that. Just simply put it in "Arduino".

Checkpoint: You have downloaded three different libraries on your computer and restarted your laptop twice. When you get an error in your code later on in the manual, you can come back to this step and update these three libraries. Sometimes it will help to get rid of the error you are facing.

Step 2: Step 2: More Downloading and Using Telegram

Welcome to step two!

First you have to download Telegram in the Appstore or Playstore, depending on what kind of phone you have. Once you have downloaded the app, open it and enter you number and username. You may see a few people from you contacts that also have the app, fun! You can chat later with them because we have to continue.

First we have to create a bot that will be kind of our partner in crime that will serve as the remote for the LED lights.

Open the file LedControl in Arduino. A bunch of code opens up, don't get overwhelmed because we are going through this step by step.

char ssid[] = "XXX";         // your network SSID (name)<br>char password[] = "ZZZ"; // your network password
#define TELEGRAM_BOT_TOKEN "SSSS"

The code above is the first thing we are going to look at. The ssid is the name of you Wi-Fi network, replace the X with the name of your Wi-Fi. Enter your password at the Z.

You also see something that is called a Telegram Bot Token. How do you get such a thing? Simple, we have to ask The Botfather. Start the Telegram app on your phone and start a conversation with him.

Type in Botfather in your search bar, not The Botfather. You will not get the correct one.

Type in /start and follow his directions. A tip from me is that you think of an easy name to give your bot so you can type it in correctly again if you have to. The name username is something else than the name you gave before. Type in the name you just used and put "bot" behind the name. If you have spaces in the name of your bot: Light bot for example, you now name it Light_bot_bot.

Botfather will give you your own personal key that you have to fill in at the S we spoken about before.

Checkpoint: You have named your bot and got a key from Botfather. You also typed in your Wi-Fi information in your code.

The code will be explained in detail in step 4.

Step 3: Step 3: Connect Your LED Strip to Your NodeMCU

You have your LED strip and your Node ready, now we are going to connect these to your laptop so it's ready to go.

Tip: label your cables so you can't mistake them for each other.

The node has a couple of input points that start with a D. The cable that is attached to Din is the one that goes in one of these points. I put mine in D6, it doesn't really matter which one you choose, as long as you put it correctly in the code. For now we will use D6.

The cable that is attached to +5V is the one that delivers the power to the strip. There is a point on the Node that will suply this kind of voltage but we are not going to use this one because your node will have a chance of overheating. I recommend using the 3V point on your Node. The strip will light up, but not as bright as if you used 5 Volt.

At last you have to connect the remaining cable , the GND cable, to the G on the node. This is the ground cable. This cable is extremely important, it prevents the Node for electric shocks.

Hook your USB cable on your laptop and the Node to provide it with power.

Checkpoint: You have connected your Node to your LED strip with the correct cables to the correct points. It is connected with your laptop for power supply.

Step 4: Step 4: Explaining the Code and Get It Ready to Go

Congratulations! You have made it to step 4. The image above shows the goal of this manual.

Here we will explain more of the code and what you have to do to make it work for you, we are only going to talk about the part of the code that is useful to us. At step 2 you filled in your bot token and your Wi-Fi data. Let's look at the rest of the code:

#define PIXEL_PIN     D6

Here you have to fill in your PIN, for us it is D6.

We also have to define the amount of LEDs on our strip and the kind of strip we use. For the amount of LEDs you just simple count the light dots (probably white squares) and the kind of strip we use is: PIXEL_TYPE NEO_GRB + NEO_KHZ800

So we have to define these two, it will look something like this:

#define PIXEL_COUNT   15
#define PIXEL_TYPE NEO_GRB + NEO_KHZ800

Once you have done this: congrats! you can upload your code to see if it works. To start a conversation with your bot, just simply type in the name you have given it before. Once you have the succes sign of the upload on your screen without any errors, type in /start. Your bot will ask you to say /options. Once you have done this you will get the keyboard on your screen and you can start tapping the buttons. Look in your serial monitor for fun and games to see what your Node is thinking. It is the button on the top right corner of your Arduino application.

If this doesn't work: check your input and output again, have you put the right number in the PIXEL_PIN?

Checkpoint: Your LED strip lights up and you don't have any errors.

The line below will handle the messages that Telegram receives, so what your bot will go through

void handleNewMessages(int numNewMessages)

Next you see a few if and else statements. The bot will go through these statements if the user presses /start at the beginning. That is stated in the following line that you can find almost in the bottom of your code.

if (text == F("/start")) { bot.sendMessage(chat_id, "/options : returns the inline keyboard\n", "Markdown");

It says that if the user says "/start" it will display an inline keyboard. The keyboard is created in the following lines:

String keyboardJson = F("[[{ \"text\" : \"ON\", \"callback_data\" : \"ON\" },{ \"text\" : \"OFF\", \"callback_data\" : \"OFF\" }],");
keyboardJson += F("[{ \"text\" : \"RED\", \"callback_data\" : \"RED\" }, { \"text\" : \"BLUE\", \"callback_data\" : \"BLUE\" }, { \"text\" : \"-\", \"callback_data\" : \"-\" }, { \"text\" : \"+\", \"callback_data\" : \"+\" }]]"); bot.sendMessageWithInlineKeyboard(chat_id, "Sadbhs Stars", "", keyboardJson);

The keyboard exists of 5 buttons, an on and off button, blue, red and + and -. These buttons are created in a few arrays and are in the language of Json. The Node is able to read this language because we downloaden this library in the beginning of this manual. In the if and else statements is stated what happens when the user presses any of the buttons on the keyboard.


Step 5: Step 5: the End

You made it at the end!

You can now access your lights through Telegram, you basically created a digital remote! Cool.

I hope everything was clear and that you went through this manual like butter, or like electricity through the ground :).

The sources that have helped me make this manual: