Hello, I recently made a program that verifies the RFID, it receives the data using c++ and sends it by serial, using python it treats and creates a database with sqlite, but I need to make a led light up after verifying if the user is registered and not I found a way to read data using arduino (I'm not using an arduino internet device so I'm using sqlite3 with python)
ARDUINO C++ CODE:
#include <MFRC522.h>
#include <SPI.h>
#include <Wire.h>
#define RST_PIN 9
#define SS_PIN 10
#define LED 4
String IDtag = "";// Vai guardar o id da tag
MFRC522 LeitorRFID(SS_PIN, RST_PIN);// Cria uma instancia que usa como parametros o SS_PIN E O RST_PIN
void setup() {
Serial.begin(9600); //Vai iniciar a comunicação atraves do usb usando 9600 bites
SPI.begin(); //Inicia a comunicação SPI
LeitorRFID.PCD_Init(); //Starta o leitor RFID
pinMode(LED, OUTPUT);//DECLARA O PINO COMO SAIDA
}
void loop() {
Leitura();//Chama a função responsavel por ler os ID do RFID
char c = Serial.read();
if (c == "b") {
Serial.println("fooooooi");
digitalWrite(LED, HIGH);
delay(3000);
digitalWrite(LED, LOW);
delay(3000);
}
}
void Leitura()
{
char sim = Serial.read();
IDtag = "";
//VERIFICA SE EXISTE UMA TAG ID
if ( !LeitorRFID.PICC_IsNewCardPresent())
{
delay(50);
return;
}
//SELECIONA O CARTÃO
if (!LeitorRFID.PICC_ReadCardSerial())
{
delay(50);
return;
}
//VAI PEGAR A TAG ID E ARMAZENAR NA VARIAVEL
for (byte i = 0; i < LeitorRFID.uid.size; i++)
{
IDtag.concat(String(LeitorRFID.uid.uidByte[i], HEX));
}
Serial.println(sim);
Serial.println(IDtag);
delay(1000);
}```
PYTHON CODE:
import serial.tools.list_ports
import serial
import sqlite3
connection = sqlite3.connect("condominio.db")
cursor = connection.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS clientes (id TEXT NOT NULL);")
list_ports = []
ports = serial.tools.list_ports.comports()
for p in ports:
print(p.device)
list_ports.append(p.device)
print(len(ports), 'ports found')
serial_port = serial.Serial(str("COM5"))
while True:
serial_data = serial_port.read_until()
data_str = str(serial_data.decode("utf-8"))
data_id = data_str.strip()
cursor.execute(f"SELECT * FROM clientes WHERE id LIKE '%{data_id}%'")
rows = cursor.fetchone()
if rows == None:
print("ID NÃO CADASTRADO")
else:
print("ACESSO LIBERADO(ACENDE LED 3 sec)")