【发布时间】:2018-02-13 00:11:19
【问题描述】:
您好,我正在尝试比较我的 rfid 和数据库中的两个字符串,我尝试对它们进行 Serial.println,它们是相同的,但是当我将它们放入 if 语句时,它没有显示为 true,
这是我的代码,不要介意其他模块,只是我目前正在处理的 rfid 部分。
阿杜诺:
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoHttpClient.h>
#include <Keypad.h>
#include <MFRC522.h>
#include <Servo.h>
/////// RFID Settings ///////
#define SS_PIN 53
#define RST_PIN 10
MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
/////// Ethernet Settings ///////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 222);
char serverAddress[] = "192.168.1.51"; // server address
int port = 80;
/////// HttpClient Settings ///////
EthernetClient ethr;
HttpClient client = HttpClient(ethr, serverAddress, port);
String response;
int statusCode = 0;
/////// Keypad Settings ///////
char key[4];
int i = 0;
String pass;
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columnsffff
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.
byte rowPins[ROWS] = {29, 28, 27, 26}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {25, 24, 23, 22}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
/////// Servo Settings ///////
Servo lock; //declares servo
/////// Status Variables & Strings ///////
int rStatus = 0; // RFID Status
int pStatus = 0; // PIN Status
String registeredCard = "160196116163";
String getReq, tag, rfid, pincode;
void setup() {
/////// Starting Serial ///////
Serial.begin(9600);
delay(1000);
/////// Ethernet Setup ///////
Serial.println("Initializing Ethernet.");
Ethernet.begin(mac, ip);
Serial.println("Connection Success.");
Serial.println("");
delay(1000);
/////// RFID Setup ///////
Serial.println("Initializing RFID Module.");
mfrc522.PCD_Init(); // Initiate MFRC522
Serial.println("RFID Ready.");
Serial.println("");
delay(1000);
/////// Keypad Setup ///////
Serial.println("Initializing Keypad Module...");
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
Serial.println("Keypad Ready.");
Serial.println("");
delay(1000);
Serial.println("Setting up servo motor...");
lock.attach(13);
Serial.println("Servo Motor Ready.");
Serial.println("");
delay(1000);
Serial.println("Please tap RFID Card.");
}
void loop() {
if (rStatus == 0)
{
readRFID();
}
else if (rStatus == 1)
{
keypad.getKey();
}
if (rStatus == 1 && pStatus == 1)
{
lock.write(5);
delay(5000);
lock.write(0);
rStatus = 0;
pStatus = 0;
}
}
/////// Module Methods - RFID ///////
void readRFID()
{
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
{
return;
}
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
{
return;
}
//Show UID on serial monitor
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.print(mfrc522.uid.uidByte[i]);
content.concat(String(mfrc522.uid.uidByte[i]));
}
Serial.println();
Serial.println(content);
tag = String(content);
getRequest();
Serial.println(rfid);
if (tag.equals(rfid)) //change here the UID of the card/cards that you want to give access
{
Serial.print("Message : ");
Serial.println("UID Found.");
Serial.println();
delay(3000);
rStatus = 1;
Serial.println("Please enter 4 digit PIN.");
}
else {
Serial.println(" Access denied");
delay(3000);
rStatus = 0;
}
}
/////// Module Methods - Keypad ///////
void keypadEvent(KeypadEvent eKey)
{
switch (keypad.getState())
{
case PRESSED:
key[i] = eKey;
Serial.print("Enter: ");
Serial.println(eKey);
delay(10);
i++;
switch (eKey)
{
case '#': checkPassword(); delay(1); break;
case '*': i = 0; delay(1); break;
default: delay(1);
}
}
}
void checkPassword()
{
pass = "";
pass.concat(key[0]);
pass.concat(key[1]);
pass.concat(key[2]);
pass.concat(key[3]);
if (pass == String(pincode))
{
Serial.print("Input PIN: ");
Serial.println(pass);
Serial.println("Message: PIN Accepted");
Serial.println("");
i = 0;
pStatus = 1;
delay(10);
} else
{
Serial.println("PIN Denied"); //if passwords wrong keep box locked
i = 0;
pStatus = 0;
delay(10);
}
}
void getRequest() {
//Serial.println("Making GET Request");
getReq.concat("/getTag.php?tag=");
getReq.concat(tag);
client.get(getReq);
// read the status code and body of the response
//statusCode = client.responseStatusCode();
response = client.responseBody();
rfid = response;
//Serial.print("Response: ");
//Serial.println(response);
delay(1000);
}
这是我的 .php 代码,如果在数据库中找到它只会显示 rfid 标记
getTag.php
<?php
include("connect.php");
$link=Connection();
$result= $link->query("SELECT * FROM users WHERE rfid LIKE '".$_GET["tag"]."'");
?>
<?php
while($row = $result->fetch_array()) {
printf("%s", $row["rfid"]);
}
mysqli_free_result($result);
?>
串口监视器上的结果,仍然显示访问被拒绝。
串行监视器:
Initializing Ethernet.
Connection Success.
Initializing RFID Module.
RFID Ready.
Initializing Keypad Module...
Keypad Ready.
Setting up servo motor...
Servo Motor Ready
Please tap RFID Card.
UID tag :
160196116163
160196116163
Access denied
我已经尝试过不同类型的比较,例如 string1 == string2、strcmp 和 string1.equals(string2),但仍然被拒绝访问。
但如果我这样做 >>>> if(tag = "160196116163") 那么它就可以了。 有人知道如何使这项工作吗?
【问题讨论】:
-
也许其中一个字符串的前面或末尾有一个空格或制表符?甚至是最后的
?在比较它们之前尝试修剪它们。在 PHP 中,您将为此使用 trim()函数。 -
我在 php 方面做了修剪,但仍然没有运气,会尝试搜索 c 的修剪函数,希望这会起作用