I have an Arduino mega with Ethernet shield + SD card running sensor data logger with DHT22 sensor writing sensor data to SD card. I'm trying to implement web server to read that data from SD card. I have made an program using Arduino's examples but it fails to connect to the client. I have checkd the IP address of my computer that the ethernet shield is connected to be 192.168.0.107. The programs data logger part works perfectly and even with the webserver implemented the code gives no errors on compile or sending the file to arduino.
The main Problem is that the program never enters the IF (client) because there is? no client.
Here is the code:
#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");
}
}
I'm pretty new with this stuff so any help would be greatly appreciated!