Der ESP8266 lässt sich einfach mit der passenden Bibliothek als Infrarot-Fernbedienung einsetzen, die sich bequem über HTTP-Requests steuern lässt. Dazu ist lediglich die vom Arduino bekannte Bibliothek „IRremote“ in einer für den ESP8266 angepassten Form notwendig sowie eine IR-Diode mit passenden Widerstand bzw. ggf. noch ein Transistor zur Ansteuerung der LED.
Ein passender Sketch könnte wie folgt aussehen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include <ESP8266WiFi.h> #include <WiFiClient.h> #include <ESP8266WebServer.h> #include <ESP8266mDNS.h> #include <IRremoteESP8266.h> // WLAN data const char* ssid = "xxx"; const char* password = "xxx"; // static IP IPAddress ip(192,168,11,60); IPAddress gateway(192,168,11,1); IPAddress subnet(255,255,255,0); MDNSResponder mdns; ESP8266WebServer server(80); // set your pin here IRsend irsend(2); void handleRoot() { server.send(200, "text/html", "Please specify command! Form: /ir?code=xxx&bits=xx&protocol=x"); } void handleIr(){ String codestring=server.arg("code"); String protocol=server.arg("protocol"); String bitsstring=server.arg("bits"); String webOutput = "Protocol: "+protocol+"; Code: "+codestring+"; Bits: "+bitsstring; if ((codestring != "")&&(bitsstring != "")){ unsigned long code = codestring.toInt(); int bits = bitsstring.toInt(); if (protocol == "NEC"){ server.send(200, "text/html", webOutput); irsend.sendNEC(code, bits); } else if (protocol == "Sony"){ server.send(200, "text/html", webOutput); irsend.sendSony(code, bits); } else if (protocol == "Whynter"){ server.send(200, "text/html", webOutput); irsend.sendWhynter(code, bits); } else if (protocol == "LG"){ server.send(200, "text/html", webOutput); irsend.sendLG(code, bits); } else if (protocol == "RC5"){ server.send(200, "text/html", webOutput); irsend.sendRC5(code, bits); } else if (protocol == "RC6"){ server.send(200, "text/html", webOutput); irsend.sendRC6(code, bits); } else if (protocol == "DISH"){ server.send(200, "text/html", webOutput); irsend.sendDISH(code, bits); } else if (protocol == "SharpRaw"){ server.send(200, "text/html", webOutput); irsend.sendSharpRaw(code, bits); } else if (protocol == "Samsung"){ server.send(200, "text/html", webOutput); irsend.sendSAMSUNG(code, bits); } else { server.send(200, "text/html", "Protocol not implemented!"); } } else { server.send(200, "text/html", "Missing code or bits!"); } } void handleNotFound(){ server.send(404, "text/plain", "404"); } void setup(void){ irsend.begin(); Serial.begin(9600); WiFi.begin(ssid, password); WiFi.config(ip, gateway, subnet); Serial.println(""); // Wait for connection while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("Connected to "); Serial.println(ssid); Serial.print("IP address: "); Serial.println(WiFi.localIP()); if (mdns.begin("esp8266", WiFi.localIP())) { Serial.println("MDNS responder started"); } server.on("/", handleRoot); server.on("/ir", handleIr); server.onNotFound(handleNotFound); server.begin(); Serial.println("HTTP server started"); } void loop(void){ server.handleClient(); } |
Gesteuert werden kann der ESP8266 nun einfach über HTTP-Requests an URL der Form
[IP]/ir?code=xxx&bits=xx&protocol=x
Die Codes, Bits und das Protokoll lässt sich von eigenen Fernbedienungen ebenfalls mit der Bibliothek und einem passenden Empfänger auslesen.
Verwendet werden kann das Ganze dann z.B. über Smartphone/Tablet-Apps wie NetIO oder auch zur Sprachsteuerung via AIVC.
Hi Alex!
Ich finde das Thema sehr interessant. Gerade da der esp so billig ist, kann man gleich mehrere Geräte in der Wohnung verteilen, um alle Infrarot Geräte bedienen zu können. Ich finde es nur Schade, dass dein Artikel hier so kurz geraten ist ?
Ich werde mich mal in die Sache reinfuchsen und schauen, wie weit ich bei der Umsetzung komme.
Danke trotzdem für den Code!
Hallo Phil,
leider fehlt mir aktuell für längere Artikel die Zeit, da ich meine verbleibende Freizeit momentan mehr mit Projektumsetzungen und anderen Dingen verbringe.
Die praktische Umsetzung sollte aber nicht allzu schwierig sein.
Hallo, ich wäre ebenso interessiert an der Hardwareschaktung und weiteren Details zu diesem Projekt. Momentan habe ich einen Raspberry Pi mit Lirc, aber wenn das mit dem ESP8266 auch geht wäre das natürlich sehr praktisch. Hast du da weitere Infos zu dem Ganzen? ?
Hallo Torben,
in meinem neuen Artikel ist mehr dazu zu lesen.
Ich bin noch etwas neu mit den Sachen, falls die Frage dumm ist bitt ich um Nachsicht: Der ESP8266 ist ein eigenständiger Server?
Ich habe einen Raspi2b und PC (Win o Linux) zur Verfügung. Wie bekomme ich das „Sketch“ auf dieses Gerät drauf?
Hallo Tim,
ja – mit diesem Sketch wird aus dem ESP8266 ein kleiner Webserver, der eigenständig arbeitet.
Der Sketch ist für die Arduino IDE (ist für so gut wie jede Plattform zu bekommen) – weiteres dazu habe ich in meinem Artikel zur Funksteckdosensteuerung über den ESP8266 beschrieben. Dort wird sowohl die Hardware- als auch die Softwareseite zum Programmieren über die Arduino IDE beschrieben.
Hallo Tim, könntest du bitte ein beispiel [IP]/ir?code=xxx&bits=xx&protocol=x schicken was funktioniert. Ich weis nicht ob es an meiner hardware oder an dem falschen string liegt. Ausgelesen habe ich zum Beispiel nur: 308A6D5C kein Protokoll und keinenen Code oder bits.?
Ich meinte natürlich Alex nicht Tim :-)
Hallo Matthias,
klar. Hier ein Beispiel: http://192.168.11.60/ir?code=16666799&bits=32&protocol=NEC
Normal sollte in der seriellen Ausgabe beim Einlesen der Codes alles notwendige mitangegeben werden.
Hallo Alex, danke für die Hilfe. In der Seriellen Ausgabe hatte ich nur zahlen wie diese: B4E2CC98 bei RC5 sogar nur sowas: 802 oder 1 das muss ich mir scheinbar nochmal genau ansehen.
Hi,
I tried this sketch, but it doesn’t work correctly :-(
I have a Arduino set up as a receiver to watch what this sketch is sending.
When i send 4BB640BF (NEC, 32bits), it is received as NEC, but it only displays 0x4 as being received on the Arduino?? My amplifier is also not responding to it…. The hardware is working correctly, with the code from here: https://www.hackster.io/BuddyC/wifi-ir-blaster-af6bca i can control my amplifier fine…..
Do you have any idea what i am doing wrong?
Already figured it out…
Your code expects decimal as input, i was sending hexcode.
When converting my hexcode to decimal with http://www.asciitohex.com/ it was working and i saw the hex code that i converted appearing in the Arduino receiver
So to power on my Onkyo 5.1 amplifier, the code as reported by the Arduino receiver from original remote is 4BB620DF which translates to 1270227167 decimal. When sending that decimal value, the amplifier turns on!
Now only to figure out some hardware to improve the range… it now only works when IR transmitter is 30cm in front of the amplifier. I think i need something with a NPN-transistor that gives the IR transmitter 5V or so
Hi, I tried your way. I have a samsung tv that sent me this code:
Encoding : SAMSUNG
Code : E0E040BF (32 bits)
Timing[67]:
+4500, -4500 + 600, -1700 + 600, -1700 + 600, -1700
+ 600, – 600 + 600, – 600 + 600, – 600 + 600, – 600
+ 600, – 600 + 600, -1700 + 600, -1700 + 600, -1700
+ 600, – 600 + 600, – 600 + 600, – 600 + 600, – 600
+ 600, – 600 + 600, – 600 + 600, -1700 + 600, – 600
+ 600, – 600 + 600, – 600 + 600, – 600 + 600, – 600
+ 600, – 600 + 600, -1700 + 600, – 600 + 600, -1700
+ 600, -1700 + 600, -1700 + 600, -1700 + 600, -1700
+ 600, -1700 + 600
unsigned int rawData[67] = {4500,4500, 600,1700, 600,1700, 600,1700, 600,600, 600,600, 600,600, 600,600, 600,600, 600,1700, 600,1700, 600,1700, 600,600, 600,600, 600,6
00, 600,600, 600,600, 600,600, 600,1700, 600,600, 600,600, 600,600, 600,600, 600,600, 600,600, 600,1700, 600,600, 600,1700, 600,1700, 600,1700, 600,1700, 600,1700, 600,
1700, 600}; // SAMSUNG E0E040BF
unsigned int data = 0xE0E040BF;
That gave me after converting the hex to dec this url:
/ir?code=3772793023&bits=32&protocol=Samsung
When sending this i get this as a respons:
Encoding : SAMSUNG
Code : 7FFFFFFF (32 bits)
Timing[67]:
+5050, -4900 + 600, – 550 + 600, -1500 + 650, -1500
+ 600, -1550 + 600, -1600 + 550, -1550 + 600, -1550
+ 600, -1600 + 550, -1550 + 600, -1550 + 600, -1550
+ 600, -1550 + 600, -1550 + 600, -1550 + 600, -1550
+ 600, -1550 + 600, -1550 + 600, -1550 + 600, -1550
+ 600, -1550 + 600, -1550 + 600, -1550 + 600, -1550
+ 600, -1550 + 600, -1550 + 600, -1550 + 600, -1550
+ 600, -1500 + 650, -1550 + 600, -1500 + 650, -1500
+ 650, -1550 + 600
unsigned int rawData[67] = {5050,4900, 600,550, 600,1500, 650,1500, 600,1550, 600,1600, 550,1550, 600,1550, 600,1600, 550,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1550, 600,1500, 650,1550, 600,1500, 650,1500, 650,1550, 600}; // SAMSUNG 7FFFFFFF
unsigned int data = 0x7FFFFFFF;
No idea why :(
Found the issue.
3772793023 is above the max value that 32-bit can be. When i issue 3772793023 as code the actual number being sent is 2147483647. Aka 0x7FFFFFFF
Have not found the solution thou.
Hi,
have a look at the comment of Daniel. This might solve your issue.
From where you get the ir codes? is there any database?
Hi Daniela,
I recorded all IR codes with an Arduino and a IR receiver – same can be done with ESP8266 and receiver. Don´t know if there´s a database somewhere out there for IRRemote, only saw one for lirc.
Do you know how to make a Sony Ir code repeat say 3 time with this method?
Hey,
if you want to send all Sony codes three times you can simply make a loop around irsend.sendSony part. To be mor flexible you could add another parameter to the URL for specifying how often to send a command and use that for building a loop around the send commands.
Alexander thanks for the reply!
Would you be kind enough to give me an example of sending all sony 3 times with this code
else if (protocol == „Sony“){
irsend.sendSony(code, bits);
server.send(200, „text/html“, webOutput);
}
Hey,
sure. Should be quite easy:
Depending on your receiver it might be useful to add a small delay after sending.
I’ve made a couple of small changes to your code and posted on github (https://github.com/Daniel-t/esp8266IRServer), full credit provided.
For me inputs in HEX are more convenient (as my IR capture tool outputs HEX), so I’ve changed that.
There was an integer overflow when parsing codes for my Samsung TV, the resulting number overflows the allowable range for integers. While the variable for ‚code‘ was an unsigned long, the parsing function ‚toInt‘ only returned an integer, which overflowed. So I’ve changed the parsing function to ’strtoul‘
Hi Daniel,
thanks for your code! Your changes are pretty useful, gonna have a closer look at it next weekend :)
Hi Alex,
wäre es möglich eine genauere Anleitung zu bekommen wie die Umsetzung mit der App NetIO funktioniert? Das wäre wirklich sehr hilfreich!
Vielen Dank schon einmal im Voraus für deine Antwort!:)
Hallo Robin,
die Umsetzung mit NetIO sollte eigenltich selbsterklärend sein. NetIO bietet einen Webdienst, um Fernbedienungen anzulegen. Den einzelnen Tasten können URLs zugeordnet werden. Hier einfach die entsprechenden URLs eintragen – fertig! ;)
hi alex,
great post.
my goall is to control 10 similar TVs with dedicated ESP8266 board at each one among with an ir diode
as a control a web page or NETIO as you decribe is ok , further more maybe a raspberry with domoticz on it it would be just fine.
i need some help and i am willing to donate for it if you are interesting.
Hey, what firmware is flashed to your ESP8266? Or is the .ino file just flashed over to it? ?
Hi Caleb,
the .ino file can be opened with Arduino IDE. There it gets compiled and flashed to the ESP8266, so there is no firmware needed.
Hi, thanks for the tutorial….I tried to figure out what Pin Mapping to use for my Wemos D1 mini… tried several but cannot get the device to send IR codes. Anyone have a clue?
Hi,
Pin D4 on your Wemos D1 mini should be GPIO2 of ESP8266.
Moin, ich hab nen WeMos D1 mini…. welches PinMapping muss ich da nutzen bzw. welcher PIN entspricht dem im Sketch verwendeten Pin2?
Danke
gruß
Jan
Bei dem Code funktionierte eine Codezeile nicht für mich:
unsigned long code = codestring.toInt();
Die Variable ‚code‘ blieb immer leer, das führte dazu, dass die ausgebene IR-Sequenz nichts bewirkte. Ich habe die String to long conversion angepasst, so klappt es:
char irc[11];
codestring.toCharArray(irc, 11);
unsigned long int code = strtoul(irc,0,16);
Hi Alex
Can this be used to send raw data? If so can you show an example?
Thanks Al
Do you have a version of this that works with esp32 . thanks