【问题标题】:Unstable Arduino Web Server不稳定的 Arduino 网络服务器
【发布时间】:2018-06-14 09:51:13
【问题描述】:

我一直在尝试使用 Arduino 设置 Web 服务器。我有一个 UNO 和一个 HanRun HR91105A 我从互联网上下来,我正在使用 WebServer 示例的修改版本来测试我的代码。起初它确实有效。但是设置端口转发后,连接突然变得不稳定。它连接并工作了几分钟,然后突然我什至无法ping通它。尝试 ping Arduino 会导致请求超时。在线研究提出了两种可能性:

1.) 所有内存都用完了
2.) 以太网屏蔽故障

下面是我的代码

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

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
  0x44, 0x00, 0x10, 0x20, 0x8C, 0x0A
};
IPAddress ip(192,168,1,90);

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

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}


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();
          Serial.write(c);
          // 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
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");  
            client.println("Refresh: 2");
            client.println();
            client.println("<!DOCTYPE HTML>");
//-----------------Type in outputs below-------------------------------------
            client.println("<html>");
            client.print("Hello World!");
            client.print("<p id='Header'>");
            client.print("Sensor Data");
            client.println("</p>");
            client.print("<p id='Pressure'>");
            client.print("Pressure:");
            client.println("</p>");
            client.print("<p id='Acceleration'>");
            client.print("Acceleration:");
            client.println("</p>");
            client.println("<br /)");
            client.println("</html>");
            break;
//-----------------End of outputs--------------------------------------------
              }

            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
        delay(1);
        // close the connection:
        client.stop();
        Serial.println("client disconnected");
      }
    }

此外,arduino 确实有一个静态 IP,所以我很确定这不是 DHCP 租约到期的问题。

我高度怀疑防护罩有故障,因为它在运行时会变得非常热。另外,它是仿冒品。但是我不能排除我的编码效率低下的可能性,因为我不是很有经验。任何帮助,将不胜感激。谢谢。

【问题讨论】:

    标签: arduino ethernet


    【解决方案1】:

    当 RAM 不足时,您的 Arduino 将无法预测地运行。 在这段代码中,有很多常量字符串。您应该将这些字符串存储在闪存中以节省 RAM。为此,请使用 F() 宏。例如。 client.println(F("HTTP/1.1 200 OK"));

    如果您的程序很大,我建议使用配备嵌入式网络服务器的盾牌(例如 PHPoC Shield)

    【讨论】:

      猜你喜欢
      • 2013-10-15
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多