I have SD card sheild , but not inserted any SD card. yet.
In below code I can change status of pin that get reflected on webserver,. But i could not able edit my own date and time. Date is atomically set to default value.I wanted to say
IF Edit option is checked
Edit date and time
else
keep updating time
IS SD card need to above requirement.
#include <SPI.h>
#include <Ethernet.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
static int dd;
static int mm;
static int yy;
static int s;
static int h;
static int m;
static int wkDay;
int CENTURY=2000;
int RTC_ERROR_FLAG=0;
// MAC address from Ethernet shield sticker under board
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 178); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
String HTTP_req; // stores the HTTP request
boolean LED_status = 0; // state of LED, off by default
void setup()
{
Wire.begin();
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.begin(9600); // for diagnostics
pinMode(8, OUTPUT); // LED on pin 2
}
void loop()
{
//getRTCDateTime();
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
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");
client.println();
// send web page
client.println("<!DOCTYPE html>");
client.println("<html>");
client.println("<head>");
client.println("<title>Arduino LED Control</title>");
client.println("</head>");
client.println("<body>");
client.println("<h1>EDIT</h1>");
client.println("<p>Click to switch LED on and off.</p>");
client.println("<form method=\"get\">");
ProcessCheckbox(client);
client.println("
");
client.println("
");
client.print("LOCAL DATE :");
client.print(dd);
client.print("/");
client.print(mm);
client.print("/");
client.print(yy);
client.println("
");
client.print("LOCAL TIME :");
client.print(h);
client.print("/");
client.print(m);
client.print("/");
client.print(s);
client.println("
");
client.println("</html>");
break;
client.println("</form>");
client.println("</body>");
client.println("</html>");
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
// switch LED and send back HTML for LED checkbox
void ProcessCheckbox(EthernetClient cl)
{
/* if (HTTP_req.indexOf("LED2=2") > -1) { // see if checkbox was clicked
// the checkbox was clicked, toggle the LED
if (LED_status) {
LED_status = 0;
}
else {
LED_status = 1;
}
}
if (LED_status) { // switch LED on
digitalWrite(8, HIGH);
// checkbox is checked
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\" checked>LED2");
}
else { // switch LED off
digitalWrite(8, LOW);
// checkbox is unchecked
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\">LED2");
}
*/
if (HTTP_req.indexOf("LED2=2") > -1)
{
if (LED_status) {
LED_status = 0;
}
else {
LED_status = 1;
}
}
if (LED_status)
{
rtcWrite(s,m,h,2,dd,mm, yy);
cl.print("IN SET MODE");
//setDateTime();
digitalWrite(8, HIGH);
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\" checked>LED2");
}else
{
cl.print("read mode");
digitalWrite(8, LOW);
getRTCDateTime();
cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
onclick=\"submit();\">LED2");
}
}
void getRTCDateTime(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
if(RTC_ERROR_FLAG==0)
{
//Serial.println("RTC IS ACTIVE :\n");
s = bcdToDec(Wire.read());
m = bcdToDec(Wire.read());
h = bcdToDec(Wire.read() & 0b111111); //24 hour time
wkDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
dd = bcdToDec(Wire.read());
mm = bcdToDec(Wire.read());
yy = CENTURY + bcdToDec(Wire.read());
}
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
int getLastTwoDigOfYear(int y){
return(y%1000);
}
/*
void setDateTime(){
byte second = 12; //0-59
byte minute = 12; //0-59
byte hour = 12; //0-23
byte weekDay = 2; //1-7
byte monthDay = 12; //1-31
byte month = 12; //1-12
byte year = 15; //0-99
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(second));
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(weekDay));
Wire.write(decToBcd(monthDay));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(zero); //start
Wire.endTransmission();
}*/
void rtcWrite(int sec1,int min1,int hour1, int wkday1,int dd1,int mm1,int yy1){
/* Serial.println("In RTC write function");
Serial.print("date");
Serial.print(dd1);
Serial.print("/");
Serial.print(mm1);
Serial.print("/");
Serial.print(yy1);
*/
int year1;
// rtcSetup();
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(sec1));
Wire.write(decToBcd(min1));
Wire.write(decToBcd(hour1)& 0b111111);
Wire.write(decToBcd(wkday1));
Wire.write(decToBcd(dd1));
Wire.write(decToBcd(mm1));
//Wire.write(decToBcd(yy1);
Wire.write(decToBcd(getLastTwoDigOfYear(yy1)));
//year1= Wire.write(decToBcd(getLastTwoDigOfYear(yy1)));
//Serial.print("date in rtc write:");
//Serial.println(year1);
Wire.write(zero); //start
Wire.endTransmission();
}