Skip to content

Instantly share code, notes, and snippets.

@fllaca
Created March 11, 2018 18:46
Show Gist options
  • Save fllaca/93d8e625d8a10622691d464d4052e0a7 to your computer and use it in GitHub Desktop.
Save fllaca/93d8e625d8a10622691d464d4052e0a7 to your computer and use it in GitHub Desktop.
/*
* IRremoteESP8266: IRServer - demonstrates sending IR codes controlled from a webserver
* Version 0.2 June, 2017
* Copyright 2015 Mark Szabo
*
* An IR LED circuit *MUST* be connected to ESP8266 pin 4 (D2).
*
* TL;DR: The IR LED needs to be driven by a transistor for a good result.
*
* Suggested circuit:
* https://github.com/markszabo/IRremoteESP8266/wiki#ir-sending
*
* Common mistakes & tips:
* * Don't just connect the IR LED directly to the pin, it won't
* have enough current to drive the IR LED effectively.
* * Make sure you have the IR LED polarity correct.
* See: https://learn.sparkfun.com/tutorials/polarity/diode-and-led-polarity
* * Typical digital camera/phones can be used to see if the IR LED is flashed.
* Replace the IR LED with a normal LED if you don't have a digital camera
* when debugging.
* * Avoid using the following pins unless you really know what you are doing:
* * Pin 0/D3: Can interfere with the boot/program mode & support circuits.
* * Pin 1/TX/TXD0: Any serial transmissions from the ESP8266 will interfere.
* * Pin 3/RX/RXD0: Any serial transmissions to the ESP8266 will interfere.
* * ESP-01 modules are tricky. We suggest you use a module with more GPIOs
* for your first time. e.g. ESP-12 etc.
*/
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <WiFiClient.h>
const char* ssid = "***";
const char* password = "***";
MDNSResponder mdns;
ESP8266WebServer server(80);
IRsend irsend(4); // An IR LED is controlled by GPIO pin 4 (D2)
uint32_t buttonToCode(String code_str){
// If null, return 0;
if(code_str == NULL) return 0;
else if (code_str.equals("KEY_POWER")) return 0x40BE629D;
else if (code_str.equals("BTN_0")) return 0x40BE00FF;
else if (code_str.equals("BTN_1")) return 0x40BE807F;
else if (code_str.equals("BTN_2")) return 0x40BE40BF;
else if (code_str.equals("BTN_3")) return 0x40BEC03F;
else if (code_str.equals("BTN_4")) return 0x40BE20DF;
else if (code_str.equals("BTN_5")) return 0x40BEA05F;
else if (code_str.equals("BTN_6")) return 0x40BE609F;
else if (code_str.equals("BTN_7")) return 0x40BEE01F;
else if (code_str.equals("BTN_8")) return 0x40BE10EF;
else if (code_str.equals("BTN_9")) return 0x40BE906F;
else if (code_str.equals("KEY_VOLUMEUP")) return 0x40BE30CF;
else if (code_str.equals("KEY_VOLUMEDOWN")) return 0x40BE9867;
else if (code_str.equals("KEY_CHANNELUP")) return 0x40BEF00F;
else if (code_str.equals("KEY_CHANNELDOWN")) return 0x40BE5AA5;
else if (code_str.equals("BTN_MODE")) return 0x40BED22D;
else if (code_str.equals("KEY_CANCEL")) return 0x40BEB04F;
else if (code_str.equals("KEY_ENTER")) return 0x40BE52AD;
else if (code_str.equals("KEY_UP")) return 0x40BE12ED;
else if (code_str.equals("KEY_DOWN")) return 0x40BEB24D;
else if (code_str.equals("KEY_LEFT")) return 0x40BE728D;
else if (code_str.equals("KEY_RIGHT")) return 0x40BE926D;
else if (code_str.equals("KEY_MUTE")) return 0x40BE32CD;
// If no code is found, try to convert the string to a number
else return strtoul(code_str.c_str(), NULL, 10);;
}
void handleRoot() {
server.send(200, "text/html",
"<html>" \
"<head><title>ESP8266 Demo</title></head>" \
"<body>" \
"<h1>Hello from ESP8266, you can send NEC encoded IR" \
"signals from here!</h1>" \
"<p><a href=\"ir?code=KEY_POWER,\">KEY_POWER</a></p>" \
"<p><a href=\"ir?code=BTN_0,\">BTN_0</a></p>" \
"<p><a href=\"ir?code=BTN_1,\">BTN_1</a></p>" \
"<p><a href=\"ir?code=BTN_2,\">BTN_2</a></p>" \
"<p><a href=\"ir?code=BTN_3,\">BTN_3</a></p>" \
"<p><a href=\"ir?code=BTN_4,\">BTN_4</a></p>" \
"<p><a href=\"ir?code=BTN_5,\">BTN_5</a></p>" \
"<p><a href=\"ir?code=BTN_6,\">BTN_6</a></p>" \
"<p><a href=\"ir?code=BTN_7,\">BTN_7</a></p>" \
"<p><a href=\"ir?code=BTN_8,\">BTN_8</a></p>" \
"<p><a href=\"ir?code=BTN_9,\">BTN_9</a></p>" \
"<p><a href=\"ir?code=KEY_VOLUMEUP,\">KEY_VOLUMEUP</a></p>" \
"<p><a href=\"ir?code=KEY_VOLUMEDOWN,\">KEY_VOLUMEDOWN</a></p>" \
"<p><a href=\"ir?code=KEY_CHANNELUP,\">KEY_CHANNELUP</a></p>" \
"<p><a href=\"ir?code=KEY_CHANNELDOWN,\">KEY_CHANNELDOWN</a></p>" \
"<p><a href=\"ir?code=BTN_MODE,\">BTN_MODE</a></p>" \
"<p><a href=\"ir?code=KEY_CANCEL,\">KEY_CANCEL</a></p>" \
"<p><a href=\"ir?code=KEY_ENTER,\">KEY_ENTER</a></p>" \
"<p><a href=\"ir?code=KEY_UP,\">KEY_UP</a></p>" \
"<p><a href=\"ir?code=KEY_DOWN,\">KEY_DOWN</a></p>" \
"<p><a href=\"ir?code=KEY_LEFT,\">KEY_LEFT</a></p>" \
"<p><a href=\"ir?code=KEY_RIGHT,\">KEY_RIGHT</a></p>" \
"<p><a href=\"ir?code=KEY_MUTE,\">KEY_MUTE</a></p>" \
"</body>" \
"</html>");
}
void handleKeyboard() {
server.send(200, "text/html",
"<html>" \
"<head><title>ESP8266 Demo</title></head>" \
"<body>" \
"<h1>Hello from ESP8266, you can send NEC encoded IR" \
"signals from here!</h1>" \
"<p><a href=\"ir?code=KEY_POWER,\">KEY_POWER</a></p>" \
"<p><a href=\"ir?code=BTN_0,\">BTN_0</a></p>" \
"<p><a href=\"ir?code=BTN_1,\">BTN_1</a></p>" \
"<p><a href=\"ir?code=BTN_2,\">BTN_2</a></p>" \
"<p><a href=\"ir?code=BTN_3,\">BTN_3</a></p>" \
"<p><a href=\"ir?code=BTN_4,\">BTN_4</a></p>" \
"<p><a href=\"ir?code=BTN_5,\">BTN_5</a></p>" \
"<p><a href=\"ir?code=BTN_6,\">BTN_6</a></p>" \
"<p><a href=\"ir?code=BTN_7,\">BTN_7</a></p>" \
"<p><a href=\"ir?code=BTN_8,\">BTN_8</a></p>" \
"<p><a href=\"ir?code=BTN_9,\">BTN_9</a></p>" \
"<p><a href=\"ir?code=KEY_VOLUMEUP,\">KEY_VOLUMEUP</a></p>" \
"<p><a href=\"ir?code=KEY_VOLUMEDOWN,\">KEY_VOLUMEDOWN</a></p>" \
"<p><a href=\"ir?code=KEY_CHANNELUP,\">KEY_CHANNELUP</a></p>" \
"<p><a href=\"ir?code=KEY_CHANNELDOWN,\">KEY_CHANNELDOWN</a></p>" \
"<p><a href=\"ir?code=BTN_MODE,\">BTN_MODE</a></p>" \
"<p><a href=\"ir?code=KEY_CANCEL,\">KEY_CANCEL</a></p>" \
"<p><a href=\"ir?code=KEY_ENTER,\">KEY_ENTER</a></p>" \
"<p><a href=\"ir?code=KEY_UP,\">KEY_UP</a></p>" \
"<p><a href=\"ir?code=KEY_DOWN,\">KEY_DOWN</a></p>" \
"<p><a href=\"ir?code=KEY_LEFT,\">KEY_LEFT</a></p>" \
"<p><a href=\"ir?code=KEY_RIGHT,\">KEY_RIGHT</a></p>" \
"<p><a href=\"ir?code=KEY_MUTE,\">KEY_MUTE</a></p>" \
"</body>" \
"</html>");
}
void handleIr() {
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "code") {
String code_list = server.arg(i);
Serial.println("Arg: " + code_list);
int commaIndex = code_list.indexOf(',');
while (commaIndex > 0) {
String code_str = code_list.substring(0, commaIndex);
Serial.println("Sending: " + code_str);
uint32_t code = buttonToCode(code_str);
irsend.sendNEC(code, 32);
delay(200);
code_list = code_list.substring(commaIndex + 1);
commaIndex = code_list.indexOf(',');
}
}
}
handleRoot();
}
void handleRawIr() {
Serial.println("Handling RAW");
for (uint8_t i = 0; i < server.args(); i++) {
if (server.argName(i) == "code") {
String code_str = server.arg(i);
uint32_t code = strtoul(code_str.c_str(), NULL, 10);
Serial.println("RAW - Arg: " + code);
irsend.sendNEC(code, 32);
delay(200);
}
}
handleRoot();
}
void handleNotFound() {
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i = 0; i < server.args(); i++)
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
server.send(404, "text/plain", message);
}
void setup(void) {
irsend.begin();
//Serial.begin(115200);
Serial.begin(9600);
WiFi.begin(ssid, password);
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.on("/raw_ir", handleRawIr);
server.on("/inline", [](){
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment