Last active
May 6, 2017 09:40
-
-
Save julznc/0575a59733d9a1393bc002b686a3e766 to your computer and use it in GitHub Desktop.
ESP8266 IR Remote
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiClient.h> | |
#include <ESP8266WebServer.h> | |
#include <ESP8266mDNS.h> | |
#include <IRremoteESP8266.h> | |
IRsend irsend( 14 /*GPIO pin*/); | |
ESP8266WebServer server(80); | |
const char* ssid = "........"; | |
const char* password = "........"; | |
// soft access point (as backup) | |
static const char *softAPssid = "ir-remote"; | |
static const char *softAPpassword = "12345678"; | |
static const char *indexhtml = "<html>\n" | |
"<head>\n" | |
"<title>IR remote</title>\n" | |
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" | |
"</head>\n" | |
"<style media=\"screen\" type=\"text/css\">\n" | |
"table{width:100%;}\n" | |
"button{display: block; width:96%; padding:12px; margin: 4px; font-weight:bold; font-size:1.5em;}\n" | |
"</style>\n" | |
"<body>\n" | |
"<script type=\"text/javascript\">\n" | |
"function req(cmd)\n" | |
"{ var xmlHttp = new XMLHttpRequest();\n" | |
"console.log(cmd);\n" | |
"xmlHttp.onreadystatechange = function(){}\n" | |
"xmlHttp.open(\"GET\", cmd, true);xmlHttp.send(null); }\n" | |
"var intervals = {};\n" | |
"function pressed(cmd){ req(cmd); intervals[cmd] = setInterval(function() { req(cmd); }, 250); }\n" | |
"function unpressed(){for (var cmd in intervals){ clearInterval(intervals[cmd]); } }\n" | |
"</script>\n" | |
"<h2>Sony AV System Remote</h2>\n" | |
"<table>\n" | |
"<tr><td><button onmousedown=\"req('power')\">POWER</button></td>\n" | |
"<td><button onmousedown=\"req('func')\">FUNC</button></td></tr>\n" | |
"<tr><td><button onmousedown=\"pressed('volup')\" ontouchstart=\"pressed('volup')\" onmouseup=\"unpressed()\" ontouchend=\"unpressed()\">VOL+</button></td>\n" | |
"<td><button onmousedown=\"pressed('tuneup')\" ontouchstart=\"pressed('tuneup')\" onmouseup=\"unpressed()\" ontouchend=\"unpressed()\">TUNE+</button></td></tr>\n" | |
"<tr><td><button onmousedown=\"pressed('voldown')\" ontouchstart=\"pressed('voldown')\" onmouseup=\"unpressed()\" ontouchend=\"unpressed()\">VOL-</button></td>\n" | |
"<td><button onmousedown=\"pressed('tunedown')\" ontouchstart=\"pressed('tunedown')\" onmouseup=\"unpressed()\" ontouchend=\"unpressed()\">TUNE-</button></td></tr>\n" | |
"</table>\n" | |
"</body>\n" | |
"</html>\n"; | |
void handleRoot() { | |
server.send(200, "text/html", indexhtml); | |
} | |
void handlePower() { | |
Serial.println("power"); | |
irsend.sendSony(irsend.encodeSony(15, 21, 80), 15, 2); | |
server.send(200, "text/plain", "power"); | |
} | |
void handleFunc() { | |
Serial.println("func"); | |
irsend.sendSony(irsend.encodeSony(15, 105, 208), 15, 2); | |
server.send(200, "text/plain", "func"); | |
} | |
void handleVolup() { | |
Serial.println("volup"); | |
irsend.sendSony(irsend.encodeSony(15, 18, 80), 15, 2); | |
server.send(200, "text/plain", "volup"); | |
} | |
void handleVoldown() { | |
Serial.println("voldown"); | |
irsend.sendSony(irsend.encodeSony(15, 19, 80), 15, 2); | |
server.send(200, "text/plain", "voldown"); | |
} | |
void handleTuneup() { | |
Serial.println("tuneup"); | |
irsend.sendSony(irsend.encodeSony(20, 52, 16, 16), 20, 2); | |
server.send(200, "text/plain", "tuneup"); | |
} | |
void handleTunedown() { | |
Serial.println("tunedown"); | |
irsend.sendSony(irsend.encodeSony(20, 51, 16, 16), 20, 2); | |
server.send(200, "text/plain", "down"); | |
} | |
void handleNotFound(){ | |
server.send(404, "text/plain", "Not Found"); | |
} | |
void setup() | |
{ | |
Serial.begin(115200); | |
WiFi.begin(ssid, password); | |
WiFi.softAP(softAPssid, softAPpassword); | |
irsend.begin(); | |
// 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()); | |
Serial.print("Soft AP: "); | |
Serial.println(softAPssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.softAPIP()); | |
if (MDNS.begin("irremote")) { | |
Serial.println("MDNS responder started"); | |
} | |
server.on("/", handleRoot); | |
server.on("/power", handlePower); | |
server.on("/func", handleFunc); | |
server.on("/volup", handleVolup); | |
server.on("/voldown", handleVoldown); | |
server.on("/tuneup", handleTuneup); | |
server.on("/tunedown", handleTunedown); | |
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