
#define BLYNK_PRINT Serial
#define OUT  D1
#define STOP  D5
#define IN  D7

#define GPIO15  D8

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "XXXXXXXXXX";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "WIFI_SSID";
char pass[] = "WIFI_PASS";

void setup()
{
  // Debug console
  Serial.begin(9600);

  Blynk.begin(auth, ssid, pass);
  Serial.println("Hello");

  pinMode(OUT,OUTPUT);
  pinMode(STOP,OUTPUT);
  pinMode(IN,OUTPUT);
  pinMode(GPIO15,OUTPUT);
  digitalWrite(OUT,HIGH); // Set IN and OUT HIGH so the remote doesn't activate on start IN
  digitalWrite(STOP,HIGH);
  digitalWrite(IN,HIGH);
  digitalWrite(GPIO15,LOW); // Set this to Low in case it draws excess power
}

BLYNK_WRITE(V1) //Out button press on Blynk app
{
  int V1Value = param.asInt(); // assigning incoming value from pin V1 to a variable

  if (V1Value ==1 ) //Virtual pin is high
  {
    Serial.println(V1Value);
    Serial.println("Out button");
    Blynk.virtualWrite(V1, HIGH);  //we force this high, to give feedback on the app button press.
    digitalWrite(OUT,LOW); 
    delay(1000); //This delay results in a button press and hold of a second, to ensure the radio signal is picked up
    digitalWrite(OUT, HIGH);
    Blynk.virtualWrite(V1, LOW);  //we force this low, as IFTTT via Google will leave it high otherwise.
  }
  else digitalWrite(OUT, HIGH); // Clear everything here as the button isn't pressed now
}

BLYNK_WRITE(V2) //In button press on app
{
  int V2Value = param.asInt(); // assigning incoming value from pin V2 to a variable

  if (V2Value ==1 )
  {
    Serial.println(V2Value);
    Serial.println("In button");
    Blynk.virtualWrite(V2, HIGH);  //we force this high, to give feedback on the app button press.
    digitalWrite(IN,LOW); 
    delay(1000);
    digitalWrite(IN, HIGH);
    Blynk.virtualWrite(V2, LOW); //we force this low, as IFTTT via Google will leave it high otherwise.
  }
  else digitalWrite(IN, HIGH); // Clear everything here as the button isn't pressed now
}

BLYNK_WRITE(V3) //Stop button press on app
{
  int V3Value = param.asInt(); // assigning incoming value from pin V3 to a variable

  if (V3Value ==1 )
  {
    Serial.println(V3Value);
    Serial.println("Stop button");
    Blynk.virtualWrite(V3, HIGH);  //we force this high, to give feedback on the app button press.
    digitalWrite(STOP,LOW); 
    delay(1000);
    digitalWrite(STOP, HIGH);
    Blynk.virtualWrite(V3, LOW);  //we force this low, as IFTTT via Google will leave it high otherwise.
  }
  else digitalWrite(STOP, HIGH); // Clear everything here as the button isn't pressed now
}

void loop()
{
  Blynk.run();
}
