#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>
#include <ESP8266WiFi.h> // include all the libraries
int i = 0;
int RecvPin = 2; // this is the pin for the ir reciever
int out = 0; // this is the output pin responsible for relay
bool state = 1; // the boolean value which is used to set the voltage of relay pin (1 -> 5v / 0 -> 0v)
IRrecv irrecv(RecvPin);// declaring that pin as an ir input

decode_results results;// declaring a variable called results to store the ir data

void setup() {
  irrecv.enableIRIn(); // enabling IR input function
  pinMode(out, OUTPUT); // setting output pin as an OUTPUT
}

void loop() {
  if (irrecv.decode(&results)) { // if any signal come from ir sensor
    if (results.value == /*Type your on/off code*/) { // if the value is the value for turning on or off the bulb
      state = !state; // change the state if the state is 0 it will turn 1 and viceversa
      digitalWrite(out, state); // change the voltage according to the state
      delay(500); // 500ms delay to avoid debouncing
    }
    if (results.value == /*Type your WiFi status checking code*/) { // if the value is the value for WiFi status checking
      WiFi.begin("your_SSID", "your_PASSWORD"); // begin a connection with your WiFi network
      while (WiFi.status() != WL_CONNECTED and i <= 20)
      {
        delay(1000);
        i = i + 1;
      }// while loop waits for the WiFi to connect for 20seconds
      if (WiFi.status() == WL_CONNECTED) { //if connected it will change the state otherwise it will be the same
        state = !state;
        digitalWrite(out, state);
        delay(500);// 500ms delay to avoid debouncing
      }
    }
    irrecv.resume();
}

  }
