我很难转移我的 RFID 标签号。到mysql数据库。当我编译给定的代码时,服务器已连接并打印 rfid 号。还说数据发送到服务器,但是当我打开我的 php 文件(也是 mysql)时,没有存储数据。我究竟做错了什么!! P.s 抱歉我的英语不好。
#include <Ethernet.h>
#include <SPI.h>
#include <Client.h>
#include<SoftwareSerial.h>
SoftwareSerial RFID(2,3);
int i;
int rfid;
char newtag[14];// = {2,49,51,48,48,57,69,66,48,51,57,48,52,3};
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte subnet[] = {255,255,255,0};
//IPAddress gateway(192,168,1,1);
//IPAddress DBserver(192, 168, 1, 2);
IPAddress server(192,168,1,7);
//char server[] = "localhost/te";
IPAddress ip(192,168,1,2);
EthernetClient client;
void setup()
{
Serial.begin(9600);
RFID.begin(9600);
delay (1000);
Serial.print("*********Initializing Ethernet*********n");
if (Ethernet.begin(mac) == 0)
{
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
//Ethernet.begin(mac, server, gateway);
delay(1000);
Serial.println("connecting...");
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());
}
void loop()
{
// while(1)
{
if (client.connect(server, 80))
{
Serial.print("Connected to server:");
Serial.println(Ethernet.dnsServerIP());
while(1)
{
if (RFID.available() > 0)
{
Serial.println("reading rfid tags");
delay(100);
for (i=0; i < 14; i++)
{
rfid = RFID.read();
newtag[i]=rfid;
}
RFID.flush();
Serial.print("rfid tag no:");
Serial.print(newtag);
Serial.println("Sending to Server...");
client.println("GET 192.168.1.7/te/add_data.php?sensor=2323 HTTP/1.1");
client.print(newtag);
client.println(" HTTP/1.1");
client.println("Host: 192.168.1.7/te/");
client.println("User-Agent: Arduino");
client.println("Accept: text/html");
Serial.println("data sent to server");
}
}
}
else
{
Serial.println("Cannot connect to Server");
}
}
我的 php 代码是
<?php
// Start MySQL Connection
include('dbconnect.php');
?>
<html>
<head>
<title> Log</title>
<style type="text/css">
.table_titles, .table_cells_odd, .table_cells_even {
padding-right: 20px;
padding-left: 20px;
color: #000;
}
.table_titles {
color: #FFF;
background-color: #666;
}
.table_cells_odd {
background-color: #CCC;
}
.table_cells_even {
background-color: #FAFAFA;
}
table {
border: 2px solid #333;
}
body { font-family: "Trebuchet MS", Arial; }
</style>
</head>
<body>
<h1>Arduino Log</h1>
<table border="0" cellspacing="0" cellpadding="4">
<tr>
<td class="table_titles">ID</td>
<td class="table_titles">Date and Time</td>
<td class="table_titles">RFID tag</td>
</tr>
<?php
// Retrieve all records and display them
$result = mysql_query("SELECT * FROM FYP ORDER BY id ASC");
// Used for row color toggle
$oddrow = true;
// process every record
while( $row = mysql_fetch_array($result) )
{
if ($oddrow)
{
$css_class=' class="table_cells_odd"';
}
else
{
$css_class=' class="table_cells_even"';
}
$oddrow = !$oddrow;
echo '<tr>';
echo ' <td'.$css_class.'>'.$row["id"].'</td>';
echo ' <td'.$css_class.'>'.$row["event"].'</td>';
echo ' <td'.$css_class.'>'.$row["tag"].'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
最佳答案
您正在 C++ 程序中逐行实现 HTTP。查找适当的 RFC 可能对您的学习有所帮助,即使它们有点难以阅读。 http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5
鉴于您向我们展示的 GET 请求,您的 add_data.php
程序需要接受 $_REQUEST['sensor']
参数。我找不到你在哪里这样做。
Arduino 上的 HTTP 客户端程序需要将这种 block 发送到运行 php 程序的 Web 服务器。首先,它打开与服务器的连接。您似乎已经在端口 80 上正确完成了此操作。然后它发送了类似这样的内容。
GET /te/add_data.php?sensor=2323 HTTP/1.1\r\n
Host: 192.168.1.7\r\n
User-Agent: Arduino\r\n
Accept: text/html\r\n
\r\n
注意两件事。
- 您的有效负载(发送到 php 程序的有用信息)完全位于 GET 行中。
- 每行都需要以回车/换行符结束。并且,在请求 block 的末尾,需要有一个空行,并以额外的回车符/换行符结尾。
你的C++代码——你的client.println()
代码——似乎没有这样做。特别是,newtag
的值将在您的请求 block 中放错位置。
另请注意,您发送到 php 程序的唯一数据项是“传感器”,并且您向其发送硬编码值 2323
。我无法从你的程序中确切地看出你想要做什么。但你正在做的事情并不是很有趣,所以也许这是一个让事情正常运转的测试。
我希望这有帮助。当您让它工作时,您将实现一个相当复杂的系统。
关于php - 传输 RFID 标签编号到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27446179/