2016-03-13 45 views
0

我有一个Arduino兆兆以太网屏蔽+ SD卡运行传感器数据记录器与DHT22传感器写入传感器数据到SD卡。我试图实现Web服务器从SD卡读取数据。我使用Arduino的例子制作了一个程序,但没有连接到客户端。我检查了以太网屏蔽连接到的计算机的IP地址是192.168.0.107。程序数据记录器部分完美工作,即使使用web服务器实现的代码也不会在编译或将文件发送给arduino时出错。Arduino Datalogger网络服务器无法连接到客户端

主要问题是程序从不进入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"); 
    } 


} 

我很新的这个东西,所以任何帮助将不胜感激!

回答

0

setup删除此行:

digitalWrite(10, HIGH); 

你这样做以太网已经开始管理该引脚SPI接口(与SD共享)。

此外,我想我会建议进一步移动Serial.begin,但它可能在那里。

相关问题