dsljpwi494719 2014-12-12 14:51
浏览 54

转移RFID标签号。 到mysql

I am having tough time to transfer my RFID tag no. to mysql database. When I compile the given code server gets connected so and prints the rfid no. and also says data sent to server but when I open my php file (also mysql) there is no data stored. What am I doing wrong!! P.s sorry for my bad english.

#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");
        }
    }

and my php code is

<?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>
  • 写回答

1条回答 默认 最新

  • doumu8217 2014-12-12 15:34
    关注

    You're implementing HTTP line by line in your C++ program. It's probably helpful for your learning to look up the appropriate RFCs even if they're a little hard to read. http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5

    Given the GET request you've shown us, your add_data.php program needs to accept the $_REQUEST['sensor'] parameter. I can't find where you do that.

    Your HTTP client program, on your Arduino, needs to send this kind of block to your web server running your php program. First, it opens a connection to the server. You seem to have done this correctly on port 80. Then it sends something like this.

    GET /te/add_data.php?sensor=2323 HTTP/1.1
    
    Host: 192.168.1.7
    
    User-Agent: Arduino
    
    Accept: text/html
    
    
    
    

    Notice two things.

    1. Your payload -- your useful information being sent to the php program -- is entirely in the GET line.
    2. Each line needs to end with carriage return/newline. And, at the end of the request block, there needs to be a blank line that ends with an extra carriage return / newline.

    Your C++ code -- your client.println() code -- doesn't seem to do that. In particular, the value of newtag will be misplaced in your request block.

    Notice also that the only item of data you're sending to the php program is 'sensor', and you're sending it the hard-coded value 2323. I can't tell from your program exactly what you're trying to do. But what you're doing isn't very interesting, so maybe it's a test to get things working.

    I hope this helps. When you get it working, you will have implemented a reasonably complex system.

    评论

报告相同问题?