以太网webserver示例,client.stop导致延迟时间问题

时间:2014-02-27 14:43:03

标签: arduino

使用以太网WebServer示例代码,我得到了我的arduino来托管存储在SD卡上的网站。该网站使用jquery将webbrowser中鼠标的位置发布回arduino。我最终想用这个信息来控制伺服电机,但是,我遇到的问题是在void循环的每次迭代中的client.stop行导致鼠标移动和arduino获取之间的滞后时间很长信息。

有没有办法让这段代码只在鼠标停止移动时使用执行stop.client行。如果没有通过post方法从arduino发送信息的话会有效吗?

这是我的代码。

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

String POST = "";
int count = 0;
const int chipSelect = 4;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,178,30);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);
EthernetClient client;

void setup() {

      // Open serial communications and wait for port to open:
      Serial.begin(9600);
      Serial.setTimeout(10);

      //start the Ethernet connection and the server:
      Ethernet.begin(mac, ip);
      client.setTimeout(1); 

      server.begin();
      Serial.print("server is at ");
      Serial.println(Ethernet.localIP());

      //SD card stuff
      Serial.print("Initializing SD card...");
      // see if the card is present and can be initialized:
      if (!SD.begin(chipSelect)) {
          Serial.println("Card failed, or not present");
          // don't do anything more:
          return;
      }
      Serial.println("card initialized.");  
}

void loop() {
      // listen for incoming clients
      EthernetClient client = server.available();
      if (client) {
          Serial.println("new client");

          // an http request ends with a blank line
          boolean currentLineIsBlank = true;
          while (client.connected()) {
              if (client.available()) {
                  char c = client.read();

                  // if you've gotten to the end of the line (received a newline
                  // character) and the line is blank, the http request has ended,
                  // so you can send a reply
                  if (c == '\n' && currentLineIsBlank) {
                  // send a standard http response header

                      String POST = "";

                      while(client.available()){
                          c = client.read();
                          // save the variables somewhere
                          POST += c;
                      }

                      if(POST != ""){
                              Serial.println(POST);
                      }

                      //load html/css/js for website only once
                      if (count <= 0){

                          client.println("HTTP/1.1 200 OK");
                          client.println("Content-Type: text/html");
                          client.println("Connection: close");  // the connection will be closed after completion of the response
                      //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
                          client.println();

                          File dataFile = SD.open("site.txt");
                          // if the file is available, write to it:
                          if (dataFile) {
                              while (dataFile.available()) {

                                  //Serial.write(dataFile.read());
                                  client.write(dataFile.read());
                              }
                              dataFile.close();
                          }  
                          // if the file isn't open, pop up an error:
                          else {
                              Serial.println("error opening site.txt");
                          }
                      } 

                      //count = 1;
                      break;

                  }

                  if (c == '\n') {
                      // you're starting a new line
                      currentLineIsBlank = true;
                  } 
                  else if (c != '\r') {
                      // you've gotten a character on the current line
                      currentLineIsBlank = false;
                  }
              }
          }
          // give the web browser time to receive the data

          if (count == 0){
            delay(500);
          }
          else{
            delay(1);
          }
          count=1;

          // close the connection:
          client.stop();
          Serial.println("client disonnected");
      } 
}

1 个答案:

答案 0 :(得分:1)

是的,可能但是真的很难,因为你必须实现HTTP \ 1.1,这对于每个鼠标位置都会缓慢,浏览器必须发送完整的HTTP请求,arduino读取并对其进行整理。

最好的解决方案是使用websocket(已经有一些用于arduino的serbsocket服务器lybrary),一旦设置了websocket,你就会像Serial一样进行双向通信!