1. 程式人生 > 其它 >477_Arduino乙太網板盾功能測試

477_Arduino乙太網板盾功能測試

技術標籤:Arduino嵌入式

全部學習彙總: https://github.com/GreyZhang/g_arduino

這個例程我第一次除錯通過其實是花了不少時間的,主要是我對乙太網幾乎沒有什麼基礎的知識積累。經過了一個晚上的煎熬,最終也是沒能夠除錯成功。而更加不幸運的是,我買的兩個擴充套件板有一個是壞掉的,而我第一次的測試其實就是使用了壞掉的那一個。

最初使用Arduino內建的例程的時候,除錯沒有通過。後來,用了一個網路的測試程式測試通過。

程式碼比較精簡,具體如下:

/************************************************/
#include <SPI.h>
/*
 * Web Server
 *
 * A simple web server that shows the value of the analog input pins.
 */
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
byte ip[] = {192, 168, 0, 15};
EthernetServer server(80);
void setup()
{
    Ethernet.begin(mac, ip);
    server.begin();
}
void loop()
{
    EthernetClient client = server.available();
    if (client)
    {
        // an http request ends with a blank line
        boolean current_line_is_blank = true;
        while (client.connected())
        {
            if (client.available())
            {
                char c = client.read();
                // if we've gotten to the end of the line (received a newline
                // character) and the line is blank, the http request has ended,
                // so we can send a reply
                if (c == 'n' && current_line_is_blank)
                {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println();

                    // output the value of each analog input pin
                    client.print("welcome to tinyos electronics");
                    client.println("<br />");
                    client.print("//*************************************");
                    client.println("<br />");
                    client.print("");
                    client.println("<br />");
                    client.print("//*************************************");
                    client.println("<br />");
                    for (int i = 0; i < 6; i++)
                    {
                        client.print("analog input ");
                        client.print(i);
                        client.print(" is ");
                        client.print(analogRead(i));
                        client.println("<br />");
                    }
                    break;
                }
                if (c == 'n')
                {
                    // we're starting a new line
                    current_line_is_blank = true;
                }
                else if (c != 'r')
                {
                    // we've gotten a character on the current line
                    current_line_is_blank = false;
                }
            }
        }
        client.stop();
    }
}

乙太網的基礎知識我還是很欠缺的,但是大概的意思似乎看明白了。上面設定的一個IP地址http://192.168.0.15/應該就是Arduino的地址。而硬體的連線也是很容易的,Arduino供個電(可以USB供電)然後網線接到電腦即可。

軟體燒寫成功之後,可以做如下測試:

如果出現上面的結果,說明測試通過。最初幾次測試我是沒有測試成功的,後來固定了電腦的IP在同一個網段之後測試通過。

之後,瀏覽器輸入相應的IP地址之後可以回讀到一些採集資訊。具體如下:

這裡其實我還是遇到了一個問題,那就是目前測試只在IE瀏覽器測試成功了,在谷歌瀏覽器中是沒有成功過的。具體的原因,也是暫時沒有弄明白。不過,至少初步有一個板子驗證沒有問題了。後面,再深入看看,擴充套件下相應的應用。