Skip to content

Instantly share code, notes, and snippets.

@Gnitset
Created March 9, 2020 10:03
Show Gist options
  • Save Gnitset/57d2cabbaa479f2186c245d3e8217206 to your computer and use it in GitHub Desktop.
Save Gnitset/57d2cabbaa479f2186c245d3e8217206 to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
const char* ssid = "XXXXX";
const char* password = "XXXXX";
const char* www_username = "admin";
const char* www_password = "esp8266";
const uint16_t ir_output_port = D2;
const uint16_t http_server_port = 80;
ESP8266WebServer server(http_server_port);
IRsend irsend(ir_output_port);
typedef struct {
char* command;
uint16_t sequence[34];
} ir_command;
// 38kHz = 26.3158us, 30 -> 789us, 60 -> 1579us, 90 -> 2368us
const ir_command ir_commands[] = {
{"power1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,60000}},
{"power2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,789,1579,789,60000}},
{"swing1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,789,789,60000}},
{"swing2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,1579,789,60000}},
{"speed_up1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,789,789,789,789,60000}},
{"speed_up2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,789,789,1579,789,60000}},
{"timer_up1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,1579,789,1579,789,1579,789,1579,789,789,789,789,789,789,789,60000}},
{"timer_up2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,789,789,1579,789,1579,789,1579,789,1579,789,789,789,789,789,1579,789,60000}},
{"speed_down1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,1579,789,1579,789,1579,789,1579,789,1579,789,1579,789,789,789,60000}},
{"speed_down2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,1579,789,1579,789,1579,789,1579,789,1579,789,1579,789,1579,789,60000}},
{"timer_down1", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,1579,789,789,789,789,789,1579,789,1579,789,789,789,789,789,60000}},
{"timer_down2", {2368,789,789,1579,789,789,789,789,789,1579,789,789,789,789,789,789,789,1579,789,1579,789,789,789,789,789,1579,789,1579,789,789,789,1579,789,60000}}
};
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() {
Serial.begin(115200, SERIAL_8N1);
Serial.println();
WiFi.begin(ssid, password);
Serial.print("Connecting");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected, IP address: ");
Serial.println(WiFi.localIP());
irsend.begin();
server.onNotFound([](){
String request = server.uri();
request.remove(0,1);
if (!server.authenticate(www_username, www_password)) {
Serial.print("Unauthenticated request - ");
Serial.println(request);
return server.requestAuthentication();
}
for(uint8_t i = 0; i < sizeof(ir_commands)/sizeof(ir_command); i++) {
if(request == ir_commands[i].command) {
Serial.print("Sending - ");
Serial.println(request);
irsend.sendRaw(ir_commands[i].sequence, sizeof(ir_commands[i].sequence), 38);
server.send(200, "text/plain", request + "\n");
return;
}
}
Serial.print("404 - ");
Serial.println(request);
handleNotFound();
});
server.begin();
Serial.println("HTTP Server Started");
}
void loop() {
server.handleClient();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment