/* WiFi-SW-Remocon_softAP.ino
   2018 Programmed by gijin77@gmail.com
*/

#include <WiFi.h>

#define RFOUT 23 
#define LED 22

#define SW_A  19
#define SW_B  18
#define SW_C  17
#define SW_D  16

#define L_SWA 11481016
#define L_SWB 11481012
#define L_SWC 11481010
#define L_SWD 11481009

#define ON HIGH
#define OFF LOW

int sw_on=0;

#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();


const char ssid[] = "XXXRemocon";
const char pass[] = "0123456789";
const IPAddress ip(192,168,4,1);
const IPAddress subnet(255,255,255,0);

const char html[] = 
"<!DOCTYPE html><html lang='ja'><head><meta charset='UTF-8'>\
<style>input {margin:8px;width:80px;}\
div {font-size:45pt;font-weight:bold;color:blue;text-align:center;width:600px;border:groove 60px orange;}</style>\
<title>WiFi_Controller</title></head>\
<body><center><div><p>照明リモコン</p>\
<form method='get'>\
<input type='submit' name='sa' value='A' style='font-size:45px; font-weight:bold; color:black; WIDTH: 300px; HEIGHT: 80px' /><br><br>\
<input type='submit' name='sb' value='B' style='font-size:45px; font-weight:bold; color:black; WIDTH: 300px; HEIGHT: 80px' /><br><br>\
<input type='submit' name='sc' value='C' style='font-size:45px; font-weight:bold; color:black; WIDTH: 300px; HEIGHT: 80px' /><br><br>\
<input type='submit' name='sd' value='D(ALL)' style='font-size:45px; font-weight:bold; color:green; WIDTH: 300px; HEIGHT: 80px'/><br>\
</form><font size='3'>2018 by gijin77@gmail.com</font></div></center></body></html>";

void sw_a_on(){
  sw_on=1;mySwitch.send(L_SWA, 24);
  Serial.println("sw-a on/off ...");

}
 
void sw_b_on(){
  sw_on=1;mySwitch.send(L_SWB, 24);
  Serial.println("sw-b on/off ...");

}
void sw_c_on(){
  sw_on=1;mySwitch.send(L_SWC, 24);
  Serial.println("sw-c on/off ...");

}
void sw_d_on(){
  sw_on=1;mySwitch.send(L_SWD, 24);
  Serial.println("sw-d(all) on/off ...");

} 


WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("program start...");

  pinMode(SW_A, INPUT_PULLUP);
  pinMode(SW_B, INPUT_PULLUP);
  pinMode(SW_C, INPUT_PULLUP);
  pinMode(SW_D, INPUT_PULLUP);
  pinMode(RFOUT, OUTPUT);
  pinMode(LED, OUTPUT);

  for(int i=0;i<5;i++) {
    digitalWrite(LED,ON);
    delay(300); 
    digitalWrite(LED,OFF);
    delay(300); 
  }
  // Transmitter is connected to esp32 #23 // Arduino Pin #10  tiny85 pin#0
  mySwitch.enableTransmit(RFOUT);

  // Optional set pulse length.
  mySwitch.setPulseLength(184);
  
  // Optional set number of transmission repetitions.
  mySwitch.setRepeatTransmit(15);

  delay(500);

  WiFi.softAP(ssid,pass);
  delay(100);
  WiFi.softAPConfig(ip,ip,subnet);

  IPAddress myIP = WiFi.softAPIP();

  server.begin();

  Serial.print("SSID: ");
  Serial.println(ssid);
  Serial.print("AP IP address: ");
  Serial.println(myIP);
  Serial.println("Server start!");

}
void getcom(String msg) {

    
  if (msg.startsWith("GET /")) {
    Serial.println("012345678901234567890");     
    if (msg.charAt(7)=='a') {
      sw_a_on();
    } else if (msg.charAt(7)=='b'){
      sw_b_on();
    } else if (msg.charAt(7)=='c') {
      sw_c_on();
    } else if (msg.charAt(7)=='d') {
      sw_d_on();
    }   

  }
}

void loop() {
  WiFiClient client = server.available();

  if (digitalRead(SW_A) == 0) { // SW-A on
    sw_a_on();
  } else if (digitalRead(SW_B) == 0){ // SW-B on
    sw_b_on();
  } else if (digitalRead(SW_C) == 0){ // SW-C on
    sw_c_on();
  } else if (digitalRead(SW_D) == 0){ // SW-D on
    sw_d_on();
  }
  if (sw_on ==1) {
    sw_on=0;
    digitalWrite(LED,ON);
    delay(300);      
    digitalWrite(LED,OFF);
  }
  while ((digitalRead(SW_A)==0)||(digitalRead(SW_B)==0)||(digitalRead(SW_C)==0)||(digitalRead(SW_D)==0)){
    delay(10);
  }

  if (client) {
        String currentLine = "";
        Serial.println("New Client.");
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.write(c);
                if (c == '\n') {
                    getcom(currentLine);
                    if (currentLine.length() == 0) {
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type:text/html");
                        client.println();

                        client.print(html);
                        client.println();
                        break;
                    } else {
                        currentLine = "";
                    }
                } else if (c != '\r') {
                    currentLine += c;
                }
            }
        }
        client.stop();
        Serial.println("Client Disconnected.");
    }
}