#include <Servo.h>
#include <SPI.h>
#include <MFRC522.h>
#include <Wire.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>
// Wifi network station credentials
#define WIFI_SSID "Red34"
#define WIFI_PASSWORD "Desaxtr3"
// Telegram BOT Token (Get from Botfather)
#define BOT_TOKEN "Tu Toquen"
// Use @myidbot (IDBot) to find out the chat ID of an individual or a group
// Also note that you need to click "start" on a bot before it can
// message you
#define CHAT_ID "Tu Id"
WiFiClientSecure secured_client;
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
const byte monUID[4] = {51, 66, 241, 11};
bool valid=true;
#define PIN_LED_ROUGE 32
#define PIN_LED_VERTE 12
#define PIN_RST 17
#define PIN_SDA 5
const int servoPin = 14;
Servo myservo = Servo();
MFRC522 rfid(PIN_SDA, PIN_RST);
void setup()
{
SPI.begin();
rfid.PCD_Init();
myservo.write(servoPin, 90); // set the servo position (degrees)
pinMode(PIN_LED_ROUGE, OUTPUT);
pinMode(PIN_LED_VERTE, OUTPUT);
Serial.begin(115200);
Serial.println();
// attempt to connect to Wifi network:
Serial.print("Connecting to Wifi SSID ");
Serial.print(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT); // Add root certificate for api.telegram.org
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
}
Serial.print("\nWiFi connected. IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Retrieving time: ");
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
time_t now = time(nullptr);
while (now < 24 * 3600)
{
Serial.print(".");
delay(100);
now = time(nullptr);
}
Serial.println(now);
bot.sendMessage(CHAT_ID, "Bot started up", "");
}
void loop()
{
if (rfid.PICC_IsNewCardPresent()) // A tag/card is detected : Badge détecté
{
if (rfid.PICC_ReadCardSerial())
{
for (int i = 0; i < rfid.uid.size; i++)
{
if (rfid.uid.uidByte[i] != monUID[i])
{valid=false;}
else
{valid=true;}
}
if (valid)
{
digitalWrite(PIN_LED_VERTE, HIGH);
myservo.write(servoPin, 0); // set the servo position (degrees)
bot.sendMessage(CHAT_ID, "Tarjeta Correcta", "");
bot.sendMessage(CHAT_ID, "Puerta Abierta", "");
delay(4000);
digitalWrite(PIN_LED_VERTE, LOW);
myservo.write(servoPin, 90); // set the servo position (degrees)
bot.sendMessage(CHAT_ID, "Puerta Cerrada", "");
}
else
{
digitalWrite(PIN_LED_ROUGE, HIGH);
delay(1000);
digitalWrite(PIN_LED_ROUGE, LOW);
bot.sendMessage(CHAT_ID, "Tarjeta Incorrecta", "");
}
}
}
}
para mas informacion revisa la entrada anterior