Infrared Record And Playback With Puck.js
About the project
This video shows you how to control Infrared devices using Puck.js.
Project info
Difficulty: Easy
Platforms: Espruino
Estimated time: 3 hours
License: Apache License 2.0 (Apache-2.0)
Items used in this project
Hardware components
Story
Recording
The code used in the video is:
- digitalWrite(D2,0);
- pinMode(D1,"input_pullup");
- var d = [];
- setWatch(function(e) {
- d.push(1000*(e.time-e.lastTime));
- }, D1, {edge:"both",repeat:true});
You can use the following to output the contents of the d
array rounded to 1 decimal place (which is much more readable):
- console.log(d.map(a=>a.toFixed(1)).toString())
In fact adding the following code will automatically print the received Infrared code as soon as you have sent it:
- var lastLen = 0;
- setInterval(function() {
- if (d.length && d.length==lastLen) {
- d.shift(); // remove first element
- console.log(d.map(a=>a.toFixed(1)).toString());
- d=[];
- }
- lastLen = d.length;
- },200);
For the particular light bulb used in the video, the codes are as follows:
- var commands = {
- off:[8.9,4.5,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.8,0.5,0.6,0.5,1.7,0.5,1.7,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,0.6,0.5,0.6,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,39.9,8.9,2.3,0.5,96.2,8.9,2.3,0],
- on:[8.9,4.5,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.7,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.7,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,39.9,8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0.6,0.5,1.7,0.5,1.8,0.5,1.8,0.5,1.7,0.5,39.9,8.9,2.3,0.4],
- fade:[8.9,4.8,0.8,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.7,0.5,1.7,0.5,1.8,0.5,1.8,0.5,0.6,0.5,0.6,0.5,1.8,0.5,0.5,0.5,0.6,0.5,0.6,0.5,0.6,0.5,0.6,0.5,1.7,0.5,1.8,0.5,0.6,0.5,1.7,0.5,1.7,0.5,1.7,0.5,39.9,8.9,2.3,0.5,106.8,0.4]
- };
Playback
All you need to do to play back is to pass an array into Puck.IR
. For example if you have a lightbulb like the one I used, simply typing Puck.IR(commands.on)
will turn it on.
You can also use Web Bluetooth to send data from a webpage to any Puck - it doesn't have to be pre-programmed as the Web Page can send the code and data at once.
Buying
You can buy the IR remote control lights like the ones I used from eBay. They are also available in strip form.
Note: while some of these lights look identical, they often use a different set of control codes. About the only way to be sure is to record your own codes.
Credits
Espruino
Espruino, Espruino Pico and Puck.js are low-power Microcontrollers that run JavaScript. Espruino is a JavaScript Interpreter for Microcontrollers that is designed to make development quick and easy. The Espruino interpreter is firmware that runs on a variety of different microcontrollers, but we also make Espruino Boards that come with the interpreter pre-installed and are the easiest devices to get started with. However Espruino itself isn't just the interpreter firmware or hardware - there's also the Web IDE, command-line tools, documentation, tutorials, and modules that form a complete solution for embedded software development.
Leave your feedback...