为什么 client.available() 返回 0? (阿杜伊诺)
Why is client.available() returning a 0? (Arduino)
我正在开发基于 Arduino 的 RFID 用户访问记录器,但在读取服务器响应时遇到一些问题。
我的设置如下:
-连接到 MFRC522 卡 reader 和 ENC28J60 以太网模块的 Arduino Mega(更改了 MFRC522 的 SS 和 RST 引脚以避免 SPI 总线内的冲突)。
- 包含 MySQL 数据库和必要的 php 文件的本地主机服务器(在端口 100 上)。
目前卡片被正确读取并且 Arduino 正在成功更新数据库中的变量,但是我无法从 php 文件中获取回声以通过串行监视器显示。为了从服务器端测试一切是否正常,我创建了一个小的 html 表单,如果我发送它,我会在浏览器中显示必要的回显。
我正在打印 client.available() returns 的值,尽管 client.connected() returns 为真,但我总是得到一个假值。将我的代码与网上找到的其他代码进行比较,没有太大区别。在这一点上,我不知道该尝试什么,我希望你能帮我解决这个问题!
我附上以下代码:
Arduino:
#include <SPI.h>
#include <MFRC522.h>
#include <UIPEthernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//----RFID----/
MFRC522 rfid(8, 9); //(SS pin, RST pin)
byte nuidPICC[4];
//----ETHERNET----/
#define DEBUG
EthernetClient client;
char server[] = "192.168.xxx.xxx"; //<-Localhost
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x40 };
//----LCD----/
LiquidCrystal_I2C lcd(0x3F, 16, 2);
//----VARIABLES----/
int userid=0;
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
lcd.begin();
pinMode(2,OUTPUT);
//----RFID----/
rfid.PCD_Init(); // Init MFRC522
//----ETHERNET----/
Ethernet.begin(mac);
#ifdef DEBUG
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
lcd.print("IP Address");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
delay(2000);
lcd.clear();
lcd.noBacklight();
#endif
}
void sender(){
/*lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("identifying");
tone(2,900,400);*/
if(client.connect(server, 100)){
Serial.println("Conected");
}
//String query = "GET /uploader.php?userid=";
//query=query+String(userid);
//Serial.println(query);
//client.print(query);
client.print( "GET /uploader.php?userid=");
client.print(12345);
client.println(" HTTP/1.1");
client.print("HOST: ");
client.println(server);
client.println();
client.println();
Serial.println("Data sent");
delay(1000);
Serial.println(client.connected());
Serial.println(client.available());
if(client.available())
{
Serial.println("ARDUINO: HTTP message received");
Serial.println("ARDUINO: printing received headers and script response...\n");
while(client.available())
{
char c = client.read();
Serial.print(c);
}
//Code to display info through the serial monitor and the LCD
}
else
{
Serial.println("ARDUINO: no response received / no response received in time");
}
client.stop();
}
void reader(){
userid=0;
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
userid=userid+nuidPICC[i];
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
sender();
}
void loop(){
reader();
}
Uploader.php
<?php
$conexion = mysql_connect("localhost", "user", "pass");
mysql_select_db("db_name", $conexion);
mysql_query("SET NAMES 'utf8'");
$userid = $_GET ['userid'];
$sql1 = "SELECT nombre, ultima_entrada, ultimo_pago, dentro FROM usuarios WHERE id = '$userid'";
$retval = mysql_query( $sql1, $conexion );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
echo "return error";
}
$row = mysql_fetch_array($retval, MYSQL_ASSOC);
$month = date('n',strtotime($row['ultimo_pago']));
$date = date('n');
if($month < $date){
echo "El usuario:{$row['nombre']} es moroso <br> ";
}else{
if($row['dentro']==0){
echo "Usuario:{$row['nombre']} accediendo al lab <br>";
$sql2 = "UPDATE usuarios SET dentro='1', ultima_entrada = CURRENT_TIMESTAMP WHERE id = '$userid'";
mysql_query($sql2);
}elseif($row['dentro']==1){
echo "Usuario:{$row['nombre']} saliendo del lab <br>";
$sql3 = "UPDATE usuarios SET dentro='0', ultima_salida = CURRENT_TIMESTAMP WHERE id = '$userid'";
mysql_query($sql3);
}
}
mysql_close();
?>
SQL table
CREATE TABLE `usuarios` (
`id` varchar(11) NOT NULL,
`nombre` tinytext NOT NULL,
`admin` tinyint(1) NOT NULL DEFAULT '0',
`ultima_entrada` timestamp NULL DEFAULT NULL,
`ultima_salida` timestamp NULL DEFAULT NULL,
`tiempo_total` time DEFAULT NULL,
`ultimo_pago` date DEFAULT NULL,
`dentro` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `usuarios` (`id`, `nombre`, `admin`, `ultima_entrada`, `ultima_salida`, `tiempo_total`, `ultimo_pago`, `dentro`) VALUES
('12345', 'John', 0, '2018-03-06 16:41:44', '2018-03-06 16:41:36', '00:00:00', '2018-03-01', 1);
注意:我使用的是虚拟用户 ID,以后会更改,现在我只想从服务器获得响应。
提前致谢!
傻我!我在这个博客的评论部分找到了我的问题的解决方案(来自 UIPEthernet 库本身的作者!):
https://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-push/#comment-21572
如果您阅读评论,Norbert 说应该避免延误,这就是我的错误。在我的代码中,我在 GET 请求后有一个漂亮的 1 秒延迟。删除它解决了问题。我的代码基于这个:http://www.smartsustainability.org/CIS508/?page_id=2203 但它是为不同的 WIFI/Ethernet 盾牌制作的,而不是为 ENC28J60 制作的,这就是问题的来源。 (虽然不是说这是一个错误的代码)。
总之,解决方案是替换这一行:
delay(1000);
使用此循环,等待客户端可用:
while(!client.available()){}
希望这会避免未来的麻烦!
我正在开发基于 Arduino 的 RFID 用户访问记录器,但在读取服务器响应时遇到一些问题。
我的设置如下:
-连接到 MFRC522 卡 reader 和 ENC28J60 以太网模块的 Arduino Mega(更改了 MFRC522 的 SS 和 RST 引脚以避免 SPI 总线内的冲突)。
- 包含 MySQL 数据库和必要的 php 文件的本地主机服务器(在端口 100 上)。
目前卡片被正确读取并且 Arduino 正在成功更新数据库中的变量,但是我无法从 php 文件中获取回声以通过串行监视器显示。为了从服务器端测试一切是否正常,我创建了一个小的 html 表单,如果我发送它,我会在浏览器中显示必要的回显。
我正在打印 client.available() returns 的值,尽管 client.connected() returns 为真,但我总是得到一个假值。将我的代码与网上找到的其他代码进行比较,没有太大区别。在这一点上,我不知道该尝试什么,我希望你能帮我解决这个问题!
我附上以下代码:
Arduino:
#include <SPI.h>
#include <MFRC522.h>
#include <UIPEthernet.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//----RFID----/
MFRC522 rfid(8, 9); //(SS pin, RST pin)
byte nuidPICC[4];
//----ETHERNET----/
#define DEBUG
EthernetClient client;
char server[] = "192.168.xxx.xxx"; //<-Localhost
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x40 };
//----LCD----/
LiquidCrystal_I2C lcd(0x3F, 16, 2);
//----VARIABLES----/
int userid=0;
void setup() {
Serial.begin(9600);
SPI.begin(); // Init SPI bus
lcd.begin();
pinMode(2,OUTPUT);
//----RFID----/
rfid.PCD_Init(); // Init MFRC522
//----ETHERNET----/
Ethernet.begin(mac);
#ifdef DEBUG
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
lcd.print("IP Address");
lcd.setCursor(0,1);
lcd.print(Ethernet.localIP());
delay(2000);
lcd.clear();
lcd.noBacklight();
#endif
}
void sender(){
/*lcd.clear();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("identifying");
tone(2,900,400);*/
if(client.connect(server, 100)){
Serial.println("Conected");
}
//String query = "GET /uploader.php?userid=";
//query=query+String(userid);
//Serial.println(query);
//client.print(query);
client.print( "GET /uploader.php?userid=");
client.print(12345);
client.println(" HTTP/1.1");
client.print("HOST: ");
client.println(server);
client.println();
client.println();
Serial.println("Data sent");
delay(1000);
Serial.println(client.connected());
Serial.println(client.available());
if(client.available())
{
Serial.println("ARDUINO: HTTP message received");
Serial.println("ARDUINO: printing received headers and script response...\n");
while(client.available())
{
char c = client.read();
Serial.print(c);
}
//Code to display info through the serial monitor and the LCD
}
else
{
Serial.println("ARDUINO: no response received / no response received in time");
}
client.stop();
}
void reader(){
userid=0;
// Look for new cards
if ( ! rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been readed
if ( ! rfid.PICC_ReadCardSerial())
return;
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
userid=userid+nuidPICC[i];
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
sender();
}
void loop(){
reader();
}
Uploader.php
<?php
$conexion = mysql_connect("localhost", "user", "pass");
mysql_select_db("db_name", $conexion);
mysql_query("SET NAMES 'utf8'");
$userid = $_GET ['userid'];
$sql1 = "SELECT nombre, ultima_entrada, ultimo_pago, dentro FROM usuarios WHERE id = '$userid'";
$retval = mysql_query( $sql1, $conexion );
if(! $retval ) {
die('Could not get data: ' . mysql_error());
echo "return error";
}
$row = mysql_fetch_array($retval, MYSQL_ASSOC);
$month = date('n',strtotime($row['ultimo_pago']));
$date = date('n');
if($month < $date){
echo "El usuario:{$row['nombre']} es moroso <br> ";
}else{
if($row['dentro']==0){
echo "Usuario:{$row['nombre']} accediendo al lab <br>";
$sql2 = "UPDATE usuarios SET dentro='1', ultima_entrada = CURRENT_TIMESTAMP WHERE id = '$userid'";
mysql_query($sql2);
}elseif($row['dentro']==1){
echo "Usuario:{$row['nombre']} saliendo del lab <br>";
$sql3 = "UPDATE usuarios SET dentro='0', ultima_salida = CURRENT_TIMESTAMP WHERE id = '$userid'";
mysql_query($sql3);
}
}
mysql_close();
?>
SQL table
CREATE TABLE `usuarios` (
`id` varchar(11) NOT NULL,
`nombre` tinytext NOT NULL,
`admin` tinyint(1) NOT NULL DEFAULT '0',
`ultima_entrada` timestamp NULL DEFAULT NULL,
`ultima_salida` timestamp NULL DEFAULT NULL,
`tiempo_total` time DEFAULT NULL,
`ultimo_pago` date DEFAULT NULL,
`dentro` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `usuarios` (`id`, `nombre`, `admin`, `ultima_entrada`, `ultima_salida`, `tiempo_total`, `ultimo_pago`, `dentro`) VALUES
('12345', 'John', 0, '2018-03-06 16:41:44', '2018-03-06 16:41:36', '00:00:00', '2018-03-01', 1);
注意:我使用的是虚拟用户 ID,以后会更改,现在我只想从服务器获得响应。
提前致谢!
傻我!我在这个博客的评论部分找到了我的问题的解决方案(来自 UIPEthernet 库本身的作者!):
https://www.tweaking4all.com/hardware/arduino/arduino-ethernet-data-push/#comment-21572
如果您阅读评论,Norbert 说应该避免延误,这就是我的错误。在我的代码中,我在 GET 请求后有一个漂亮的 1 秒延迟。删除它解决了问题。我的代码基于这个:http://www.smartsustainability.org/CIS508/?page_id=2203 但它是为不同的 WIFI/Ethernet 盾牌制作的,而不是为 ENC28J60 制作的,这就是问题的来源。 (虽然不是说这是一个错误的代码)。
总之,解决方案是替换这一行:
delay(1000);
使用此循环,等待客户端可用:
while(!client.available()){}
希望这会避免未来的麻烦!