Sending string via Node by serial port to Arduino

Asked

Viewed 137 times

1

I have a local server that keeps listening to the serial terminal of the Arduino and from certain data, he is able to make a select in a Mysql database to know if an RFID tag is registered (and therefore has access released). If the query returns some result, then this tag is actually registered and from there sends a string to the serial terminal in order for Arduino to read this string and perform some actions.

The problem is that the reading of the string by Arduino is only done the next time an RFID tag is scanned. See the image below to better understand: observing the message Status do cartão - Node: # (is just a console.log, where# symbolizes the string that will be sent to Arduino, a variable), see that just below it appears the message Mensagem: Acesso negado!, but next time below Status do cartão appears true or false, in this case are already the strings that Arduino is actually reading.

Console Bash

What happens: I scanned 2 times a registered tag and 1 time a tag without registration. In case, I received access denied the first time (when it should be released), in the second step I get access released on account of the first scan and in the last step where the status of the card is false, the Arduino read true for passing a previously released card. That is, commands sent by the Node are only being read in the next scan made by the RFID sensor.

This is Arduino’s code:

#include <SPI.h>
#include <MFRC522.h>
#define PORTA_AN 0
#define PORTA_DIG 3
#define SS_PIN 10
#define RST_PIN 9

// Definicoes pino modulo RC522
MFRC522 mfrc522(SS_PIN, RST_PIN);

//Porta do buzzer
int buzzer = 4;

//Leds indicadores de acesso: liberado ou negado
int led_liberado = 7;
int led_negado = 6;

//Condição de acesso
bool condicao = false;

char st[20];

//Portas do relê usado para abrir e fechar o portão
int portaRele = 2;
int portaRele2 = 8;

void setup()
{
    //Setagem dos acessórios conectados
    pinMode(portaRele, OUTPUT);
    digitalWrite(portaRele, HIGH);
    pinMode(portaRele2, OUTPUT);
    digitalWrite(portaRele2, HIGH);
    pinMode(led_liberado, OUTPUT);
    pinMode(led_negado, OUTPUT);

    //Inicia a serial
    Serial.begin(9600); 

    //Inicia  SPI bus
    SPI.begin();

    //Inicia MFRC522
    mfrc522.PCD_Init();

    //Buzzer
    pinMode(buzzer, OUTPUT);
    digitalWrite(buzzer, HIGH);
    delay(150);
    digitalWrite(buzzer, LOW);

    //Mensagens iniciais no serial monitor
    Serial.println("Aproxime o seu cartão do leitor...");
    Serial.println();
}

void LeRFID()
{
    //Aguarda a aproximacao do cartao
    if (!mfrc522.PICC_IsNewCardPresent())
    {
        return;
    }

    //Seleciona um dos cartoes
    if (!mfrc522.PICC_ReadCardSerial())
    {
        return;
    }

    //Mostra UID na serial
    Serial.println("UID da tag:");
    String conteudo = "";
    byte letra;
    for (byte i = 0; i < mfrc522.uid.size; i++)
    {
        Serial.print(mfrc522.uid.uidByte[i] < 0x10 ? " 0" : "");
        Serial.print(mfrc522.uid.uidByte[i], HEX);
    }

  //Lê o input enviado pelo Node 
  while (Serial.available() > 0) {
    String input = Serial.readString();
    Serial.println();
    Serial.println(input);  
    if (input == "true")
    {
      condicao = true;
      input = "";
    }
    else
    {
      condicao = false;
      input = "";
    }
  } 

    Serial.println();
    Serial.print("Mensagem: "); 

    //Abre o portão e acende o led verde
    if (condicao == true)
    {
        Serial.println("Acesso liberado!");
        Serial.println();
        digitalWrite(led_liberado, HIGH);
        digitalWrite(buzzer, HIGH);
        delay(150);
        digitalWrite(buzzer, LOW);
        delay(1000);        
        digitalWrite(buzzer, HIGH);
        delay(150);
        digitalWrite(buzzer, LOW);
        delay(1000);
        digitalWrite(led_liberado, LOW);
        digitalWrite(portaRele, LOW);
        delay(500);
        digitalWrite(portaRele, HIGH);
        delay(3000);
        digitalWrite(portaRele2, LOW);
        delay(400);
        digitalWrite(portaRele2, HIGH);
    }
    else //Nega acesso e pisca o led vermelho
    {
        Serial.println("Acesso negado!");
        Serial.println();               
        for (int i = 1; i < 5; i++)
        {
            digitalWrite(led_negado, HIGH);
            digitalWrite(buzzer, HIGH);
            delay(100);
            digitalWrite(led_negado, LOW);
            digitalWrite(buzzer, LOW);
            delay(100);
        }
    }  
}

void loop()
{
    LeRFID();
    delay(500);
}

The application code in Node:

const MySQL = require('mysql');

const con = MySQL.createConnection({
    host: "localhost",
    port: "3306",
    user: "root",
    password: "",
    database: "Arduino",
    charset: "utf8mb4_general_ci"
});

con.connect((err) => {
    if (err)
        console.error(err);
    console.log("Conectado ao banco de dados");
});

var express = require('express');

var app = express();
var server = app.listen(4000, () => { //Inicia o servidor express na porta 4000
    console.log('Servidor Express iniciado na porta %s', server.address().port);
})

var io = require('socket.io')(server); //Vincula socket.io com o servidor express

app.use(express.static('public'));

// Conexão com o Arduino
const SerialPort = require('serialport'); 
const Readline = SerialPort.parsers.Readline;
const port = new SerialPort('COM3',{ baudRate: 9600 }); //Conecta o serial port à porta COM3
const parser = port.pipe(new Readline({delimiter: '\r\n'})); //Lê a linha quando uma nova linha chega
parser.on('data', (message) => { //Lê os dados    
    console.log(message);

    if (message.length == 8 || message.length == 9) //Compara se a string é do mesmo tamanho da tag que o serial imprime
        InsertRFIDHistorico(message);    
});

io.on('connection', (socket) => {
    console.log("Alguém se conectou.");
})

var cartaoSalvo;
function InsertRFIDHistorico(value) {  
    const sqlSelect = 'SELECT * FROM `Arduino`.`Condominos` WHERE RFID = ?;'; 
    con.query(sqlSelect, [value], function (err, result)
    {
        if (result.length == 0) //Confere se o select feito no BD retorna algum resultado
        {                
            cartaoSalvo = false; console.log('Status do cartão - Node: '+cartaoSalvo);
        }
        else
        {
            cartaoSalvo = true; console.log('Status do cartão - Node: '+cartaoSalvo);                                    
            port.write(cartaoSalvo.toString(), (err) => {
                if (err) {
                  return console.log('Erro no método write: ', err.message);
                }                
              });           
            const sqlInsert = 'INSERT INTO `Rfid` (`rfid`) VALUES (?);';
            con.query(sqlInsert, [value], function (err, result) {            
            if (err)
                console.error(err);
            });
        }

        if (err)
            console.error(err);                
    });      
}

Node modules can be applied with npm install express serialport mysql socket.io.

And the SQL database code:

CREATE DATABASE IF NOT EXISTS `Arduino`
  DEFAULT CHARACTER SET = utf8mb4
  DEFAULT COLLATE = utf8mb4_general_ci;

USE `Arduino`;

CREATE TABLE IF NOT EXISTS `Rfid` (
  `id` int(11) AUTO_INCREMENT NOT NULL,
  PRIMARY KEY (`id`),
  `rfid` varchar(255) NOT NULL,
  `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE = InnoDB 
  DEFAULT CHARSET = utf8mb4
  DEFAULT COLLATE = utf8mb4_general_ci;

  CREATE TABLE IF NOT EXISTS `Condominos` (
  `id` int(11) AUTO_INCREMENT NOT NULL,
  PRIMARY KEY (`id`),
  `nome` varchar(255) NOT NULL,
  `rfid` varchar(255) NOT NULL
) ENGINE = InnoDB 
  DEFAULT CHARSET = utf8mb4
  DEFAULT COLLATE = utf8mb4_general_ci;

  INSERT INTO `Condominos` (`nome`, `rfid`) VALUES ("Tony", "E3D2F71B");
  INSERT INTO `Condominos` (`nome`, `rfid`) VALUES ("Steve", "99AA1E63");

I hope I’ve been clear in passing my situation.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.