0

我有一个带有以太网屏蔽 + SD 卡的 Arduino mega,运行带有 DHT22 传感器的传感器数据记录器,将传感器数据写入 SD 卡。我正在尝试实现 Web 服务器以从 SD 卡中读取该数据。我使用 Arduino 的示例制作了一个程序,但无法连接到客户端。我已经检查了以太网屏蔽连接到的计算机的 IP 地址为 192.168.0.107。程序数据记录器部分工作完美,即使实现了网络服务器,代码在编译或将文件发送到 arduino 时也没有错误。

主要问题是程序永远不会进入IF(客户端),因为有?没有客户。

这是代码:

#include <Dhcp.h>
#include <Dns.h>
#include <Ethernet.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp.h>
#include <SD.h>
#include <DHT.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,0,107);
//IPAddress dns1(192,168,0,1);
//IPAddress gateway(192,168,0,1);
//IPAddress subnet(255,255,255,0);
EthernetServer server(80);

#define DHTPIN 7     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)

DHT dht(DHTPIN, DHTTYPE);

const int chipSelect = 4;

void setup() {
  // put your setup code here, to run once:
    dht.begin();
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.begin(9600);
    digitalWrite(10, HIGH);

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


    Serial.print("Initializing SD card...");
    if (!SD.begin(chipSelect)) {
      Serial.println("Card failed, or not present");
      return;
    }
       Serial.println("card initialized.");
       Serial.println("DHT22 Logging sensor data:!"); 
}

void loop() {
  // put your main code here, to run repeatedly:
  // Wait beetween reading sensors
  delay(4444);
  //Reading sensor data
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit
  float f = dht.readTemperature();

   // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

// Compute heat index
  // Must send in temp in Fahrenheit!
  float hi = dht.computeHeatIndex(t, h);

  File dataFile = SD.open("datalog.CSV", FILE_WRITE);
  //writing sensor data to sd card
  if (dataFile) {

      dataFile.print((float)f);
      dataFile.print(" , ");
      dataFile.println((float)h);
      dataFile.close();
      // print to the serial port too:
      Serial.print("Temperature = ");
      Serial.println(f);
      Serial.print("Humidity = ");
      Serial.println(h);
    } 
    // if the file isn't open, pop up an error:
    else {
      Serial.println("error opening datalog.CSV");
    }

EthernetClient client = server.available();
  if (client) {
    Serial.println("client availble");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        Serial.println("connected");
        char c = client.read(); // Client data readed
        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();
          client.println("<br />");
          client.print("Arduino powered webserver");
          client.println("<br />");
          client.print("Serving temperature and humidity values from a DHT22 sensor");
          client.println("<br />");
          client.print(f);
          client.println("<br />");
          client.print("Humidity (%): ");
          client.print(h);       
          client.println("<br />");
          Serial.println("kikkihiiri");
          break;
        }

        if (c == '\n') {
          // last character on line received
          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(5);
    // close the connection:
    client.stop();
    Serial.println("Client disconnected");
  }


}

我对这些东西很陌生,所以任何帮助将不胜感激!

4

1 回答 1

0

删除此行setup

digitalWrite(10, HIGH);

在以太网开始为 SPI 接口(与 SD 共享)管理此引脚后,您将执行此操作。

另外,我认为我建议Serial.begin进一步向上移动,但那里可能还可以。

于 2016-03-13T20:50:44.920 回答