Skip to content

Instantly share code, notes, and snippets.

@probonopd
Last active December 18, 2015 19:08
Show Gist options
  • Save probonopd/5830307 to your computer and use it in GitHub Desktop.
Save probonopd/5830307 to your computer and use it in GitHub Desktop.
Decode IR codes using Arduino and irdb.tk
// If one keypress results in multiple codes being output, then
// change in IRremoteInt.h:
// #define _GAP 50000
#include <IRremote.h>
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
int c = 1;
void dump(decode_results *results) {
int count = results->rawlen;
Serial.println(c);
c++;
Serial.println("Raw: ");
for (int i = 1; i < count; i++) {
if ((i % 2) == 1) {
Serial.print("+");
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print(-(int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(" ");
}
Serial.println("");
Serial.println("For Arduino sketch: ");
Serial.print("unsigned int raw[");
Serial.print(count, DEC);
Serial.print("] = {");
for (int i = 1; i < count; i++) {
if ((i % 2) == 1) {
Serial.print(results->rawbuf[i]*USECPERTICK, DEC);
}
else {
Serial.print((int)results->rawbuf[i]*USECPERTICK, DEC);
}
Serial.print(",");
}
Serial.print("};");
Serial.println("");
Serial.print("irsend.sendRaw(raw,");
Serial.print(count, DEC);
Serial.print(",38);");
Serial.println("");
Serial.println("");
}
void loop() {
if (irrecv.decode(&results)) {
dump(&results);
irrecv.resume(); // Receive the next value
}
}
#!/usr/bin/env python
import gtk, os, sys, time, serial, select, urllib2
import simplejson # Must be bundled; sudo apt-get install python-simplejson
def decode(raw):
json = None
encoded = raw.strip().replace(" ","%20")
# print encoded
req = urllib2.Request("http://irdb.tk/api/decode/" + encoded, None, {'user-agent':'python/irclient-1'})
try:
opener = urllib2.build_opener()
f = opener.open(req)
json = simplejson.load(f)
except Exception, e:
print e
print("The code was:" + raw.strip())
print("Press the same key again in order to try again; press Ctrl-C to exit")
return(json)
def handle_code(code):
print(("Protocol %s, device %s, subdevice %s, OBC %s") % ( code["protocol"], code["device"], code["subdevice"], code["function"]))
functionname = raw_input("Enter the button name: ").strip().upper()
print "You entered ", functionname
lines.append(("%s,%s,%s,%s,%s,%s,%s,%s,,py1") % ( brand, devicetype, devicename, code["protocol"], code["device"], code["subdevice"], code["function"], functionname))
print "Now press the next key on the remote; press Ctrl-C when no more keys"
class Arduino():
def __init__(self, port):
self.port = port
def run(self, baud=9600):
try:
self.ser = serial.Serial(self.port, baud, timeout=1)
except:
print "Could not open the serial port " + self.port
sys.exit(1)
self.ser.flushInput()
while True :
inp, outp, err = select.select([sys.stdin, self.ser], [], [], .2)
if self.ser in inp :
line = self.ser.readline().strip()
if("+" in line):
result = decode(line + " -43992")
if result:
code = result["objects"][0]
handle_code(code)
devices = os.listdir("/dev/serial/by-id/")
i = 1
for device in devices:
print (("[%i] %s") % (i, os.path.basename(device)))
j = int(raw_input("Select a device [1-%s]: " % (i)).strip())
port = "/dev/serial/by-id/" + devices[j-1]
print port
if not (os.access(port, os.W_OK)):
print("Device is not writeable, try this again as root")
exit(1)
lines = []
arduino = Arduino(port)
brand = raw_input("Enter the brand manufacturer name: ").strip()
devicename = raw_input("Enter the device name: ").strip()
devicetype = raw_input("Enter the device type: ").strip()
print("Now press the first key on the remote")
try:
arduino.run()
except KeyboardInterrupt, e:
print("")
for line in lines:
print line
JBL Harmony Receiver NEC1 64 47 0 STANDBY
JBL Harmony Receiver NEC1 64 47 1 SLEEP
JBL Harmony Receiver NEC1 64 47 3 VOL+
JBL Harmony Receiver NEC1 64 47 4 MONO
JBL Harmony Receiver NEC1 64 47 5 PTY
JBL Harmony Receiver NEC1 64 47 6 M
JBL Harmony Receiver NEC1 64 47 7 VOL-
JBL Harmony Receiver NEC1 64 47 8 AM/FM
JBL Harmony Receiver NEC1 64 47 9 RDS
JBL Harmony Receiver NEC1 64 47 10 PLAY
JBL Harmony Receiver NEC1 64 47 11 PAUSE
JBL Harmony Receiver NEC1 64 47 12 CD
JBL Harmony Receiver NEC1 64 47 13 DISPLAY
JBL Harmony Receiver NEC1 64 47 15 STOP
JBL Harmony Receiver NEC1 64 47 16 AUX
JBL Harmony Receiver NEC1 64 47 18 I<
JBL Harmony Receiver NEC1 64 47 19 >I
JBL Harmony Receiver NEC1 64 47 22 <<
JBL Harmony Receiver NEC1 64 47 23 >>
JBL Harmony Receiver NEC1 64 47 24 ALARM1
JBL Harmony Receiver NEC1 64 47 25 ALARM2
JBL Harmony Receiver NEC1 64 47 26 RANDOM
JBL Harmony Receiver NEC1 64 47 27 REPEAT
JBL Harmony Receiver NEC1 64 47 28 SNOOZE
JBL Harmony Receiver NEC1 64 47 29 LIGHT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment