I am working on Arduino Mega and W5100 Ethernet shield. Trying to make an attendance system on local server using fingerprint module, RTC clock, and oled display in my Project.
The final program code, without the ethernet code is working fine. But when ethernet code is included in the program ... it is generating an IP but it is not able to create a server
server.begin() is not working ... Please Help.
#include <SPI.h>
#include <SD.h>
#include <DS3231.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Fingerprint.h>
#include <SoftwareSerial.h>
#include <Ethernet.h>
#define OLED_RESET 3
int CS_PIN = 4;
SoftwareSerial mySerial(11, 12);
DS3231 rtc(SDA, SCL);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
Adafruit_SSD1306 display(OLED_RESET);
File file;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 7}; // ip in lan assigned to arduino
byte gateway[] = { 192, 168, 1, 10 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80);
String push[11][32];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial);
pinMode(CS_PIN , OUTPUT);
pinMode(10 , OUTPUT);
digitalWrite(4,HIGH);
digitalWrite(10,HIGH);
digitalWrite(4,HIGH);
digitalWrite(10,LOW);
Ethernet.begin(mac, ip, subnet, gateway);
server.begin();
if(server.available())
Serial.println("SERVER is active");
else
Serial.println("SERVER is inactive");
Serial.println(Ethernet.localIP());
digitalWrite(10,HIGH);
digitalWrite(4,LOW);
finger.begin(57600);
if (finger.verifyPassword()) {
Serial.println("Found fingerprint sensor!");
} else {
Serial.println("Did not find fingerprint sensor :(");
while (1) { delay(1); }
}
finger.getTemplateCount();
Serial.print("Sensor contains "); Serial.print(finger.templateCount); Serial.println(" templates");
Serial.println("Waiting for valid finger...");
rtc.begin();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
Serial.println("Initializing SD card...");
if (SD.begin(CS_PIN))
Serial.println("SD card is ready to use.");
else
Serial.println("SD card initialization failed");
}
void loop() {
int id=-1;
//fingure print detection code
uint8_t p = finger.getImage();
if (finger.getImage() != FINGERPRINT_OK || finger.image2Tz()!=FINGERPRINT_OK || finger.fingerFastSearch()!=FINGERPRINT_OK)
id=-1;
else{
Serial.print("Found ID #"); Serial.print(finger.fingerID);
Serial.print(" with confidence of "); Serial.println(finger.confidence);
id=finger.fingerID;
}
digitalWrite(10,HIGH);
digitalWrite(4,LOW);
file=SD.open("data.csv");
if(file)
Serial.println("File opened with success!");
else
Serial.println("Error in opening data file");
String temp="";
char ch;
int top=0,top1=0;
while(file.available()){
ch=file.read();
if(ch=='\n'){
temp+=ch;
push[top][top1]=String(temp);
top1=0;
top++;
temp="";
}
else if(ch==','){
push[top][top1]=String(temp);
top1++;
temp="";
}
else
temp+=ch;
}
file.close();
Serial.println("data ftom data file ");
//Serial.println(push);
for(int i=0;i<11;i++){
for(int j=0;j<32;j++)
Serial.print(push[i][j]+"\t");
Serial.println();
}
String sname="";
if(id>0){
sname=push[id][1];
String tdate=rtc.getDateStr(),p="";
int l=tdate.length();
for(int i=0;i<l && tdate[i]!=':';i++)
p=p+tdate[i];
push[id][p.toInt()+1]=rtc.getTimeStr();
SD.remove("data.csv");
file = SD.open("data.csv", FILE_WRITE);
for(int i=0;i<11;i++){
for(int j=0;j<32;j++)
file.print(push[i][j]+',');
file.println();
}
//writeToFile(push);
file.close();
}
display.clearDisplay();
// text display tests
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
if(sname=="")
display.println("WELCOME!");
else
display.println("WELCOME!"+sname);
display.print("DATE ");display.println(rtc.getDateStr());
display.setTextColor(WHITE);
display.print("TIME "); display.println(rtc.getTimeStr());
display.display();
if(sname=="")
delay(1000);
else
delay(4000);
digitalWrite(4,HIGH);
digitalWrite(10,LOW);
EthernetClient client = server.available();
//Serial.println(server);
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while(client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
//
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 disconnected");
}
// delay(5000);
}