#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <IRremoteESP8266.h>
#include <IRsend.h>
#include <Arduino.h>
#include <FS.h>
#include <LittleFS.h>
const char *path_root = "/index.html";
const char *path_css = "/css.css";
const char *path_js = "/script.js";
const char *ssid = "SSID";
const char *password = "PASSPHRASE";
IPAddress local_IP(192, 168, 0, 250);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
//IPAddress primaryDNS(8, 8, 8, 8); //optional
//IPAddress secondaryDNS(8, 8, 4, 4); //optional
const char common_name[40] = "esptv";
const char *OTAName = common_name;
const char *mdnsName = common_name;
#define BUFFER_SIZE 16384
uint8_t buf[BUFFER_SIZE];
ESP8266WebServer server ( 80 );
IRsend irsend(14);
boolean readHTML() {
//File htmlFile = SPIFFS.open(path_root, "r");
File htmlFile = LittleFS.open(path_root, "r");
if (!htmlFile) {
Serial.println("Failed to open index.html");
return false;
}
size_t size = htmlFile.size();
if (size >= BUFFER_SIZE) {
Serial.print("File Size Error:");
Serial.println((int)size);
} else {
Serial.print("File Size OK:");
Serial.println((int)size);
}
htmlFile.read(buf, size);
htmlFile.close();
return true;
}
void on_off() {
//Serial.println("Power");
irsend.sendPanasonic(0x***, 0x***0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***0x***);
delay(2000);
//server.send(200, "text/html", "Power ON/OFF");
}
void ch1() {
//Serial.println("1ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "1ch");
}
void ch2() {
//Serial.println("2ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "2ch");
}
void ch3() {
//Serial.println("3ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "3ch");
}
void ch4() {
//Serial.println("4ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "4ch");
}
void ch5() {
//Serial.println("5ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "5ch");
}
void ch6() {
//Serial.println("6ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "6ch");
}
void ch7() {
//Serial.println("7ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "7ch");
}
void ch8() {
//Serial.println("8ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "8ch");
}
void ch9() {
//Serial.println("9ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "9ch");
}
void ch10() {
//Serial.println("10ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "10ch");
}
void ch11() {
//Serial.println("11ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "11ch");
}
void ch12() {
//Serial.println("12ch");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "12ch");
}
void vol_up() {
//Serial.println("Volume Up");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "Volume Up");
}
void vol_down() {
//Serial.println("Volume Down");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "Volume Down");
}
void prog_list() {
//Serial.println("Program List");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "Program List");
}
void prog_info() {
//Serial.println("Program Info");
irsend.sendPanasonic(0x***, 0x***);
delay(10);
irsend.sendPanasonic(0x***, 0x***);
delay(2000);
//server.send(200, "text/html", "Program Info");
}
void loadFile (String path) {
String contents = "";
String line = "";
char c;
File target_file = LittleFS.open(path, "r");
if (target_file) {
Serial.println("file open succeeded!");
while (target_file.available()) {
c = target_file.read();
if (c == '\n' || c == '\r') {
if (line.length() > 0) {
contents = contents + line + '\n';
}
line = "";
} else {
line = line + String(c);
}
}
if (line.length() > 0) {
contents = contents + line;
}
target_file.close();
} else {
Serial.println("file open failed!");
}
if (path == path_root) {
server.send(200, "text/html", contents);
} else if (path == path_css) {
server.send(200, "text/css", contents);
} else if (path == path_js) {
server.send(200, "text/javascript", contents);
}
}
void handleRoot() {
Serial.println("Access");
loadFile("index.html");
char message[20];
String(server.arg(0)).toCharArray(message, 20);
server.send(200, "text/html", (char *)buf);
if (server.arg(0).indexOf("電源") != -1) {
on_off();
}
else if (server.arg(0).indexOf("1ch") != -1) {
// Serial.println("1ch");
ch1();
}
else if (server.arg(0).indexOf("2ch") != -1) {
// Serial.println("2ch");
ch2();
}
else if (server.arg(0).indexOf("3ch") != -1) {
// Serial.println("3ch");
ch3();
}
else if (server.arg(0).indexOf("4ch") != -1) {
// Serial.println("4ch");
ch4();
}
else if (server.arg(0).indexOf("5ch") != -1) {
// Serial.println("5ch");
ch5();
}
else if (server.arg(0).indexOf("6ch") != -1) {
// Serial.println("6ch");
ch6();
}
else if (server.arg(0).indexOf("7ch") != -1) {
// Serial.println("7ch");
ch7();
}
else if (server.arg(0).indexOf("8ch") != -1) {
// Serial.println("8ch");
ch8();
}
else if (server.arg(0).indexOf("9ch") != -1) {
// Serial.println("9ch");
ch9();
}
else if (server.arg(0).indexOf("ch10") != -1) {
// Serial.println("ch10");
ch10();
}
else if (server.arg(0).indexOf("ch11") != -1) {
// Serial.println("11ch");
ch11();
}
else if (server.arg(0).indexOf("ch12") != -1) {
// Serial.println("12ch");
ch12();
}
else if (server.arg(0).indexOf("音量⇑") != -1) {
// Serial.println("vol_up");
vol_up();
}
else if (server.arg(0).indexOf("音量⇓") != -1) {
// Serial.println("vol_down");
vol_down();
}
else if (server.arg(0).indexOf("番組表") != -1) {
// Serial.println("prog_list");
prog_list();
}
else if (server.arg(0).indexOf("番組情報") != -1) {
// Serial.println("prog_info");
prog_info();
}
}
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 startOTA() { // Start the OTA service
ArduinoOTA.setHostname(OTAName);
// ArduinoOTA.setPassword(OTAPassword);
ArduinoOTA.onStart([]() {
Serial.println("Start");
// turn off the LEDs
for (int i = 0; i < 6; i++) {
// digitalWrite(LED_BUILTIN, HIGH);
// digitalWrite(LED_BUILTIN, LOW);
}
});
ArduinoOTA.onEnd([]() {
Serial.println("\r\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
Serial.println("OTA ready\r\n");
}
void startMDNS() { // Start the mDNS responder
MDNS.begin(mdnsName); // start the multicast domain name server
Serial.print("mDNS responder started: http://");
Serial.print(mdnsName);
Serial.println(".local");
}
void setup() {
Serial.begin(115200);
irsend.begin();
// if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
if (!WiFi.config(local_IP, gateway, subnet)) {
Serial.println("STA Failed to configure");
}
WiFi.begin(ssid, password);
Serial.println("");
// AP+STAモードの設定
// WiFi.mode(WIFI_AP_STA);
WiFi.mode(WIFI_STA);
/*
// APとして振る舞うためのSSIDとPW情報
WiFi.softAP(SOFTAP_SSID, SOFTAP_PW);
Serial.print("Connecting to ");
Serial.println(SOFTAP_SSID);
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());
startOTA();
/*
SPIFFS.begin();
if (!readHTML()) {
Serial.println("Read HTML error!!");
}
*/
if (!LittleFS.begin())
{
Serial.println("Failed to mount file system");
return;
}
startMDNS(); // Start the mDNS responder
server.on("/", handleRoot);
server.on("/css.css", [](){
loadFile(path_css);
});
server.on("/script.js", [](){
loadFile(path_js);
});
server.on("/Power", on_off);
server.on("/1ch", ch1);
server.on("/2ch", ch2);
server.on("/3ch", ch3);
server.on("/4ch", ch4);
server.on("/5ch", ch5);
server.on("/6ch", ch6);
server.on("/7ch", ch7);
server.on("/8ch", ch8);
server.on("/9ch", ch9);
server.on("/10ch", ch10);
server.on("/11ch", ch11);
server.on("/12ch", ch12);
server.on("/vol_up", vol_up);
server.on("/vol_down", vol_down);
server.on("/prog_list", prog_list);
server.on("/prog_info", prog_info);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void loop() {
server.handleClient();
ArduinoOTA.handle(); // listen for OTA events
}