Arduino Web服务器响应是"它工作!"

时间:2016-11-29 11:02:43

标签: arduino arduino-ide intel-galileo

我对Arduino很新,所以这可能是一个简单的问题,但我找不到解决方案。

我有一块英特尔Galileo Gen. 2主板。我正在尝试编写一个提供Web服务器的简单程序,根据请求,它返回从传感器获得的值(任何文本都可以解决我的问题)。我已成功配置网络并获得了价值,但每次向服务器发出的请求都会得到一个"它有效!"消息作为回应。我尝试过不同的浏览器(从chromw到lynx)和不同的nework位置。

这是我正在使用的代码:

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

byte mac[] = { 0x98, 0x4F, 0xEE, 0x05, 0x65, 0x02 };
char SENSORNAME[ ] = "galileo01";
IPAddress ip(192, 168, 15, 177);
IPAddress dnServer(8, 8, 8, 8);

EthernetServer server(80);

int sensorPin = A0;    // select the input pin for the potentiometer
int readValue = 0;
int prevValue = 0;

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

  system("ifconfig enp0s20f6 down ");
  system("ip link set enp0s20f6 name eth0");
  system("ifconfig eth0 up");
  Ethernet.begin(mac, ip, dnServer);
  server.begin();

}

void loop() {
  readValue = analogRead(sensorPin);
  EthernetClient client = server.available();
  if (client) {
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        if (c == '\n' && currentLineIsBlank) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();
          client.print("{\"sensor\":\"");
          client.print(SENSORNAME);
          client.print( "\",\"value\":\"");
          client.print(readValue);
          client.println("\"}");
          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
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
}

我做错了什么?

感谢。

更新:

我看到的是,在发出请求时,loop()函数没有处理它。我可以看到传感器读取结果,但它永远不会通过if子句:

 EthernetClient client = server.available();
  if (client) {

是否有内部网络服务器?如何将其关闭?

1 个答案:

答案 0 :(得分:0)

我终于找到了它。默认系统附带一个正在运行的Web服务器,该服务器不允许在端口80上设置新服务器。添加此代码已解决了问题:

system("systemctl stop lighttpd > /dev/ttyGS0");
system("systemctl stop disable > /dev/ttyGS0");