2016-03-13 142 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,但它可能在那裏。

相關問題