Infrared Cloning With Arduino Command Interface

by RakaAmburo in Circuits > Arduino

124 Views, 3 Favorites, 0 Comments

Infrared Cloning With Arduino Command Interface

introHeader.png
Build a Node.js Interface to Manage Cloned Infrared and Radio Signals with Arduino

In this second chapter of the cloning series, I present to you this NodeJS piece of code that is capable of controlling the Arduino board with the uploaded program that we worked on in the previous tutorial. So this interface will be controlling the cloning and emitting of signals, instead of doing it manually as you would do with, for example, the aid of the Arduino IDE. Furthermore, you will be able to store and retrieve signals to and from files in JSON format. So the idea is to simplify the process of cloning as many devices as you can.

Supplies

irCloneInterfaceSupplies.png

For this tutorial, we will need our estimated NodeJS scripting, an Arduino board, an infrared emitter, and a receiver board (remember from the last episode, you can use directly an emitter led on pin 13, or if you want to get complicated you can assemble you own configuration adding the electrical component required instead of an already assembled board).

As I explained in the last episode, I am using a shield that I made to make it super easy to plug and unplug different types of boards.

Take a Look at the Previous Tutorial

introHeaderIrPointed.png

Take a look at the previous tutorial: YouTube and Instructables

In the tutorial, you will find the code and all the specifications to start playing with infrared cloning.

The Code

irInterfaceCodeIntro.png

In order to make it as simple as I can, I will be breaking down the code into parts. Of course, if you feel you can handle it you can go directly to GitHub download the code and start paying around. The code is tested and ready to go. In spite of that, I will be uploading updates and improvements. There is still room for upgrades.

CloneEmitController.js

Code / Imports

codeImports.png

As you can see we will be using libraries we used in previous tutorials, I recommend you to take a look at those.

Like the one related to serial port, for example. But we added new ones like readLine or readLineParse. The last one is included in the serialPort library.

You will also see the variables declaration required for all the functionalities this simple code provides.

Code / Init

codeInit.png

With this function, we initialize the communication with the Arduino board. We implemented this in our tutorial for the no more logins series. Basically, we list all the serials connected, pick our board by its serial, and establish the connection represented by the serialPort variable. Also, we delegate the incoming messages from the board to the processStringfromArdu function. Bear in mind, you will have to find out the serial number of your board.

Code / Reading Messages From Arduino

codeProcStrFromArdu.png

This function processes the incoming strings from the board. Depending on the different states of the program: it stores the signal in an array, prepares to receive the signal from the receiver IR board, sends the stored signal to the board once is ready, and prints the string as a default in the if-else statement, respectively.

Code / Reading Commands Form Terminal

codeReadLine.png

This function basically takes what you write in the terminal once the program has started, and tries to execute that string as a function, only if it finds it in the actions array. If not it executes a default function (none function). This function prints a default message indicating that what you have just typed is not an expected function. Notice that you can send parameters to the functions executed. We need this for example when we try to clone a signal, we can provide a specific name for that signal. The same happens with saving/retrieving a file with signals, you must provide a name. I managed this by separating the function and the parameter with a dot: dump.someDevice (this will write a file with the stored signals to a file called soemDevice.json) or for example sendStored.power (this will emit the signal under the name 'power')

You can change this behavior by using a space or a more complex strategy. I found this way pretty simple and straightforward.

Code / Send Messages to Arduino

codeSndMsg2Ardu.png

This utility function is in charge of sending the string as a parameter to the Arduino board. Of course, handling possible errors.

Code / Storing and Retrieving Signals to a File

codeWteRd2file.png

These utility functions help us to write and read files to the folder in which the NodeJS code is located. Files containing the array with the different cloned signals in JSON format:

{
"signalName":"1252,420,1256,424,1252,......1260,412,424,1256,420&"
}

You will be able to provide the name of the file. So in this line, you will be able to clone signals of different devices and store them in a specific file. And of course, retrieve it later.

Code / Defining Actions

codeActs1.png
codeActs2.png

Finally, here you have all the possible actions. As you can see actions is an array containing functions. In this way, you can write actions with no limit. As mentioned in previous tutorials you can even have a separate file containing only functions, and import them as a library. I like this way of programming cause is very neat and scalable.

Plug the Arduino and Have Fun

plugArduino.png

In the last step, plug the Arduino with the uploaded code from the previous tutorial:

irClonerAndEmiter.ino

And start testing this code by cloning and emitting signals from different devices.

Coming Next

irInterfacewhatIsNext.png

In the next episode, I will be presenting the Arduino code for doing exactly what the infrared cloner does but for radio signals. I rushed to write this tutorial before the radio cloner since I wanted to solve ASAP the problem of copy and pasting signals. And 'the handling' in general of many signals.

Also for future tutorials, I will use this interface (maybe a little bit enhanced) to take a big lip and use it to automate all we have cloned and connect it to other series like voice control ones (Android and browser).


See you in the next one!