Arduino 代码不断返回 bool 1/true
所以我用arduino做了一个RFID阅读器,arduino会读取卡的UID,然后通过GET方法将UID发送给PHP,然后PHP验证/匹配UID与数据库,然后如果UID是与数据库匹配,PHP 应该将 1(bool) 返回给 arduino,它运行良好并且 arduino 将其读取为 1(bool),但是当我尝试发送不匹配的 UID 时,arduino 将其读取为 1(bool),应该是0.
这是我的代码
Arduino代码(RFID_uid.ino)
#include <SPI.h>
#include <MFRC522.h>
#include <Ethernet.h>
#define SS_PIN 53
#define RST_PIN 5
MFRC522 rfid(SS_PIN, RST_PIN);
String id_kartu;
bool clientRead;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.25.159";
IPAddress ip(192,168,25,20);
EthernetClient client;
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
if (client.connect(server, 80) || !client.connect(server, 80)) {
Serial.println("Please Scan Your Card");
}
pinMode(LED_BUILTIN, OUTPUT);
delay(1000);
}
void loop() {
//Check if there any card on RFID reader
if (!rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) {
return;
}
id_kartu = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
rfid.PICC_HaltA(); //Stop reading card
rfid.PCD_StopCrypto1(); //Stop reading card
Sending_To_phpmyadmindatabase();
delay(2000);
if (client.available()) {
clientRead = client.read();
Serial.println(clientRead);
}
if(clientRead == 1){
digitalWrite(LED_BUILTIN, HIGH);
delay(1500);
} else {
digitalWrite(LED_BUILTIN, LOW);
}
}
void Sending_To_phpmyadmindatabase() { //CONNECTING WITH MYSQL
if (client.connect(server, 80)) {
// Make a HTTP request:
Serial.print("GET /rfid/tes.php?id=");
client.print("GET /rfid/tes.php?id="); //My URL
Serial.println(id_kartu);
client.print(id_kartu);
//client.print(" "); //SPACE BEFORE HTTP/1.1
client.print(" HTTP/1.1");
client.println();
client.println("Host: 192.168.25.159");
client.println("Connection: close");
client.println();
} else {
// if didn't get a connection to the server:
Serial.println("connection failed");
}
}
PHP 代码 (tes.php)
<?php
$server = "localhost";
$username = "****";
$password = "****";
$dbname = "****";
// Create connection
$conn = new mysqli($server, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get the QR code from the request
$id_kartu = $_GET["id"];
// Construct the query to check if the QR code matches an authorized student
$query = "SELECT * FROM kartu WHERE id_kartu = '$id_kartu'";
// Execute the query
$result = $conn->query($query);
// Check if the query returned any rows (indicating a match)
if ($result->num_rows > 0) {
// Return "1" to the Arduino
return 1;
} else {
// Return "nothing" to the Arduino
die;
}
?>
这是串行监视器
Please Scan Your Card
GET /rfid/tes.php?id=1902042134
1
所以 1902042134 UID 在我的数据库中显然不可用,但是 arduino 仍然返回 1(bool) 值,请帮助我