Skip to content

Instantly share code, notes, and snippets.

@milankragujevic
Created February 20, 2018 06:45
Show Gist options
  • Save milankragujevic/d001769831b23fd68c8ae51c8bcce779 to your computer and use it in GitHub Desktop.
Save milankragujevic/d001769831b23fd68c8ae51c8bcce779 to your computer and use it in GitHub Desktop.
Control PC from TV remote with ESP8266
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
MDNSResponder mdns;
uint16_t RECV_PIN = 14; // connect data pin of ir sensor to pin D5 on ESP8266
const char* ssid = "[wifi]";
const char* password = "[password]";
const char* mDNSName = "[hostname]";
const char* mothership = "[target ip]";
IRrecv irrecv(RECV_PIN);
HTTPClient http;
ESP8266WebServer server(80);
decode_results results;
void setup() {
Serial.begin(115200);
delay(10);
irrecv.enableIRIn();
Serial.println();
Serial.println("IR keyboard Bridge");
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("");
if(mdns.begin(mDNSName)) {
Serial.println("MDNS responder started");
String identifyMessage = "Identified as ";
identifyMessage += mDNSName;
Serial.println(identifyMessage);
}
server.on("/", [](){
server.send(200, "text/plain", "OK");
});
server.onNotFound([](){
server.send(404, "text/plain", "PAGE_NOT_FOUND");
});
server.begin();
Serial.println("Server started on port 80");
Serial.println("Mothership IP: " + String(mothership));
Serial.println("Sending init command to mothership");
http.begin("http://" + String(mothership) + "/command");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST("cmd=init");
http.end();
Serial.println("Sent init cmd.");
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println("Sending button " + uint64ToString(results.value));
http.begin("http://" + String(mothership) + "/command");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.POST("cmd=btn&data=" + uint64ToString(results.value));
http.end();
Serial.println("Sent button.");
irrecv.resume();
}
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment