您好我正在尝试比较我的rfid和我的数据库中的两个字符串,我尝试将它们串联起来并且它们是相同的但是当我将它们放在if语句中时它没有显示为真,
这是我的代码,不要介意其他模块只是我目前正在处理的rfid部分。
Arduino的:
#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),但仍然是拒绝访问。
但如果我这样做&gt;&gt;&gt;&gt; if(tag =&#34; 160196116163&#34;)然后它可以工作。 任何人都知道如何使这项工作?
答案 0 :(得分:0)
好的,我发现了原因,在我的php文件中,最上面有一个新行,即第1行,我将其删除并立即正常工作。