#include <deprecated.h>
#include <MFRC522.h>
#include <MFRC522Extended.h>
#include <require_cpp11.h>
#include <SPI.h>
#include <WiFi.h>
#include <HTTPClient.h>
#define ANALOG_PIN_0 36

int analog_value = 0;
int freq = 5000;
int ledChannel = 0;
int resolution = 8;
int dutyCycle = 0; 

const int RST_PIN = 22; // Reset pin
const int SS_PIN = 21; // Slave select pin
 
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance
 //C1 56 72 5B

const char* ssid = "OTE6EE3B9";
const char* password =  "3013902284794329";
bool canscan=true;
bool val=false;
String value;

void setup() {
  Serial.begin(115200);
  while (!Serial); // Do nothing if no serial port is opened (added for Arduinos based on ATMEGA32U4)
  SPI.begin(); // Init SPI bus
  mfrc522.PCD_Init(); // Init MFRC522
  mfrc522.PCD_DumpVersionToSerial(); // Show details of PCD - MFRC522 Card Reader details
  delay(4000);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi..");
  }
 
  Serial.println("Connected to the WiFi network");
 
}
 
void loop() {
  
  if ( ! mfrc522.PICC_IsNewCardPresent()) {
  return;
  }
 
  // Select one of the cards
  if ( ! mfrc522.PICC_ReadCardSerial()) {
   
   
    value="";
  return;
  }
 analog_value = analogRead(ANALOG_PIN_0);
  Serial.println(analog_value);
  dutyCycle = map(analog_value, 0, 4095, 0, 255);
   Serial.println("Authorized access");
    Serial.println();
    delay(500);
  
 Serial.print("UID tag :");
  String content= "";
  byte letter;
  for (byte i = 0; i < mfrc522.uid.size; i++) 
  {
     Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " ");
     Serial.print(mfrc522.uid.uidByte[i], HEX);
     content.concat(String(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : " "));
     content.concat(String(mfrc522.uid.uidByte[i], HEX));
  }
  Serial.println();
  Serial.print("Message : ");
  content.toUpperCase();
  if (content.substring(1) == "F2 DF 96 BB") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?My_Light=ON"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
   delay(1000);
 
  }
   if (content.substring(1) == "DE D3 73 5B") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?My_Light=OFF"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
   delay(1000);
 
  }
  if (content.substring(1) == "C1 56 72 5B") //change here the UID of the card/cards that you want to give access
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=100") && ("http://195.251.170.86:8080/basicui/CMD?Light2_ColorTemperature=1"); //change brightness and temperature
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
   delay(1000);
 
  }

   if (content.substring(1) == "10 AA 73 5B") //select default light mode
  {
    Serial.println("Authorized access");
    Serial.println();
    delay(500);
    HTTPClient http;
    (http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=80") && ("http://195.251.170.86:8080/basicui/CMD?Light2_ColorTemperature=49")); //change brightness and temperature
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
   delay(500);
 
  }
 
   else {
     Serial.println("Error on HTTP request");
   }
  //EA 21 74 5B
  //CE 5B 74 5B
  
  if (content.substring(1) == "CE 5B 74 5B") //change here the UID of the card/cards that you want to give access
  {
     if (analog_value == 0)//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;AqAAAAAp--33000-000000000z
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=5"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}
//analog_value=50; analog_value<500 analog_value++ 
else if (analog_value<500 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=10"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<900 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=20"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<1300 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=30"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<1700 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=40"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<2100 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=50"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else  if (analog_value<2500 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=60"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<3000 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=70"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}


else if (analog_value<3400 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=80"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else  if (analog_value<3800 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=90"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}


 else if (analog_value==4095 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_Brightness=100"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}
   
  }
    //EA 21 74 5B
  //CE 5B 74 5B
  
  if (content.substring(1) == "EA 21 74 5B") //change here the UID of the card/cards that you want to give access
  {
     if (analog_value == 0)//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=100"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}
//analog_value=50; analog_value<500 analog_value++ 
 else if (analog_value<500 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=90"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

 else if (analog_value<900 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=80"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

 else if (analog_value<1300 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=70"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

 else if (analog_value<1700 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=60"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<2100 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=50"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

 else if (analog_value<2500 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=40"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

else if (analog_value<3000 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=30"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}


 else if (analog_value<3400 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=20"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}

  else if (analog_value<3800 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=10"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}


 else if (analog_value==4095 )//change here the UID of the card/cards that you want to give access
  {
    HTTPClient http;
    http.begin("http://192.168.1.16:8080/basicui/CMD?Light2_ColorTemperature=5"); //Specify the URL
    Serial.println("Pushed!!");
    
   int httpCode = http.GET();                                        //Make the request
  
  delay(500);
}
   
  }
  }
