0

我不知道为什么我的草图不能正常工作。我想构建一个服务器来从我的 Arduino 获取模拟输入值(使用 AJAX 请求)。我有带 SD 读卡器的 Ethernet Shield。在我的 SD 卡上,我有包含我网页内容的“index.htm”文件(它包含带有 AJAX 请求发送功能的 HTML 和 JavaScript)。这是我的代码:

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,101); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File pageFile;
String HttpReq = String();

void setup()
{
    Serial.begin(9600);

    // initialize SD card
    Serial.println("Initializing SD card...");
    if (!SD.begin(4)) {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
    }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm")) {
        Serial.println("ERROR - Can't find index.htm file!");
        return;  // can't find index file
    }
    Serial.println("SUCCESS - Found index.htm fileeee.");

    pageFile = SD.open("index.htm", FILE_READ);
    if (pageFile)
    {
      Serial.println("SETUP: Reading file...");
      char c;
      while(pageFile.available())
      {
          c = pageFile.read();
          Serial.print(c);
      }
      Serial.println("SETUP: End of file reading - OK");
    } else Serial.println("SETUP: Can't read file");
    pageFile.close();

    Ethernet.begin(mac, ip);  // initialize Ethernet device
    server.begin();           // start to listen for clients
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
}

void loop()
{    
  EthernetClient client = server.available();  // try to get client   

    if (client) 
    {  // got client?
        Serial.println("New client:");    
        boolean currentLineIsBlank = true;
        while (client.connected()) 
        {
            if (client.available()) 
            {   // client data available to read
                char c = client.read(); // read 1 byte (character) from client
                // last line of client request is blank and ends with \n
                // respond to client only after last line received
                HttpReq += c;
                Serial.print(c);

                if (c == '\n' && currentLineIsBlank) //end of client request, now have to send server response
                {
                    // send a standard http response header
                    client.println("HTTP/1.1 200 OK");
                    client.println("Content-Type: text/html");
                    client.println("Connection: keep-alive");
                    client.println();

                    // send web page
                    Serial.print("Position of ajax_down: ");
                    Serial.println(HttpReq.indexOf("ajax_down"));
                    if (HttpReq.indexOf("ajax_down") > -1)
                   {
                      Serial.println("!!!!! Here should be ajax_down string:");
                      Serial.println(HttpReq);
                      Serial.println("!!!!! END");

                      GetResults(client);
                   }
                   else //it wasn't ajax_down request
                   {
                      pageFile = SD.open("index.htm", FILE_READ);
                      if (pageFile)
                      {
                        Serial.println("Reading...");
                        while(pageFile.available())
                        {
                            char c = pageFile.read();
                            client.print(c);
                            Serial.print(c);                            
                        }
                        pageFile.close();
                      } 
                      else
                      {
                        Serial.println("Can't read file!");
                        client.print("Can't get results!");
                      }
                    }

                    HttpReq = "";
                    break;
                }
                // every line of text received from the client ends with \r\n
                if (c == '\n') {
                    // last character on line of received text
                    // starting new line with next character read
                    currentLineIsBlank = true;
                } 
                else if (c != '\r') {
                    // a text character was received from client
                    currentLineIsBlank = false;
                }
            } // end if (client.available())
        } // end while (client.connected())
        delay(1);      // give the web browser time to receive the data
        client.stop(); // close the connection
    } // end if (client)

}

void GetResults(EthernetClient cl)
{
  Serial.print("Getting results...");
    int temperatury = 0;
    for (int i=0; i<20; i++)
    {
      temperatury += analogRead(0);
    }
    int odczytTemperatury = (temperatury/20)*(5000/1023);

    int odczytSwiatla = analogRead(5);

    cl.print(odczytTemperatury);
    cl.print(" ");
    cl.print(odczytSwiatla);
    Serial.println("OK");

}

setup()函数中,我检查 SD 卡上是否有“index.htm”文件,然后打开它以将其内容打印到串行。我在 COM8 上得到以下结果:

Initializing SD card...
SUCCESS - SD card initialized.
SUCCESS - Found index.htm fileeee.
SETUP: Reading file...
<!doctype html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Arduino odczyt</title>
</head>
    <body>
    ...
    </body>
</html>SETUP: End of file reading - OK
server is at 192.168.1.101

...我的代码是什么时候。我也开始在setup(). 还行吧。

loop()我等一个客户。如果有客户端连接,我会逐字符读取他的请求:

char c = client.read(); // read 1 byte (character) from client
HttpReq += c;

当请求结束时(if (c == '\n' && currentLineIsBlank))服务器开始响应。首先它检查它是否是“ajax_down”请求:if (HttpReq.indexOf("ajax_down") > -1)这里我遇到了第一个问题:当 HttpReq 没有​​“ajax_down”字符串时,它应该返回 -1 时返回 0(http://arduino.cc/en/Reference/StringIndexOf)。在 Web 浏览器上访问 192.168.1.101 时的 COM8 输出:

New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Ge
!!!!! END
Getting results...OK
New client:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-La
!!!!! END
Getting results...OK

它的行为好像在 HttpReq 的开头有“ajax_down”,但没有!然后它GetResults(client);在我的浏览器窗口中调用,我可以看到两个数字(模拟输入值)。但是好的,假设它不是从 0 开始计数,而是从 1 开始计数,所以当我改变

if (HttpReq.indexOf("ajax_down") > -1)

为了

if (HttpReq.indexOf("ajax_down") > 0)

它将到达else这里:

else //it wasn't ajax_down request

setup()并且正在从 SD 卡读取文件(与!!!中的代码相同)但现在我无法读取文件(但首先读取setup()工作)。COM8:

New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
**Can't read file!**
New client:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4

Position of ajax_down: 0
**Can't read file!**

我得到“无法得到结果!” 在我的浏览器中:/

我不知道为什么我不能第二次打开文件(当我删除读取时它也不起作用setup())。也许它是由一些内存泄漏引起的?草图的二进制大小为 24 568 字节(最大值:32 256),但我不知道在这种情况下是否有话要说。

4

2 回答 2

0

不确定我是否可以,但是如果您希望在设置部分声明 indexOf 文件,我认为也应该在 Loop 中声明。

问候,

于 2014-01-17T00:15:40.803 回答
0

草图的二进制大小为 24 568 字节(最大值:32 256)
可能是由于某些内存泄漏引起的?

你在ATMega168上运行代码吗?
在大多数情况下,168 无法处理 SD,因为它的 RAM 很小。

您应该使用 ATMega328 (Uno) 或更高版本来处理 SD。

于 2014-02-18T02:58:22.467 回答