Olá,
Adquiri recentemente um 'real time clock' e um leitor rfid da GROVE, assim como uma arduino ethernet (modolulo completo e não o shield). Desenvolvi o código abaixo para fazer um "relogio de ponto" no entanto queria armazenar as horas de entrada e saida num cartao SD e poder mostralo em HTML ...
No entanto o código está proximo de exceder o limite (32K) do arduino ...
Alguém me consegue dar uma ajuda a optimizar este código ?
#include <SD.h>
File SDFileData;
//rfid
#include <SoftwareSerial.h>
SoftwareSerial rfid(2, 3);
unsigned char buffer[14];
int count=0;
//Ethernet
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192,168,28,150);
EthernetServer server(8080);
//clock
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
void setup() {
// start Serial:
Serial.begin(9600);
while (!Serial) {
;
}
// start the Ethernet:
Ethernet.begin(mac, ip);
server.begin();
Serial.println("Server OK");
// start rfid:
rfid.begin(9600);
// start clock:
setSyncProvider(RTC.get); // get the time from the RTC
if(timeStatus()!= timeSet)
Serial.println("RTC FAIL");
else
Serial.println("RTC OK");
// buzz:
pinMode(8, OUTPUT);
//sd card
pinMode(10,OUTPUT);
if (!SD.begin(4)) {
Serial.println("FAIL Reinsert SD");
return;
}
/* Check if the text file already exists */
while(SD.exists("test.txt"))
{
SD.remove("test.txt");
if(!SD.exists("test.txt")){
Serial.println("Delete OK");
}
}
/* Create a new text file on the SD card */
SDFileData = SD.open("test.txt", FILE_WRITE);
if(SD.exists("test.txt")){
Serial.println("Create OK");}
else{
Serial.println("Error");
}
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (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("Connnection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// add a meta refresh tag, so the browser pulls again every 5 seconds:
client.println("<meta http-equiv=\"refresh\" content=\"2\">");
// output the value of each analog input pin
client.println("CLOCK TEST");
client.println("
");
client.println(digitalClockDisplay());
client.println("
");
//write RFID TAG:
if(rfid.available()){
while(rfid.available())
{
buffer[count++]=rfid.read();
if(count == 14)break;
}
//print value:
//client.write(buffer,count); //default value
client.print(GetInfo(buffer,count));
clearBufferArray();
count = 0;
//buzz(8, 2500, 500); // buzz the buzzer on pin 8 at 2500Hz for 500 milliseconds
WriteOnSD();
}
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(2);
// close the connection:
client.stop();
Serial.println("stop");
}
}
// RFID functions:
void clearBufferArray() // function to clear buffer array
{
for (int i=0; i<count;i++)
{
buffer[i]=NULL;
} // clear all index of array with command NULL
}
String GetInfo(unsigned char buff[14], int count)
{
// remove stx checksum and etx and get name;
String value = GetString(buff).substring(1,11);
String temp = GetData(value);
return temp;
//sd_card_write(temp + " » " +digitalClockDisplay());
}
String GetString(unsigned char* inStrReference)
{
// convert unsigned char to string
unsigned char* bufPtr = inStrReference;
String newstring = (char*)bufPtr;
return newstring;
}
String GetData(String id)
{
String bd[5][2] = {
{
"0F000C9C0E","Joao Pereira" }
,
{
"0F00088CD2","Ricardo Lopes" }
,
{
"0F00086052","Pedro Rosas" }
,
{
"0F00096159","Filipe Macedo" }
,
{
"0F000CF1F5","Mariana Gomes" }
};
String data = id;
for(int i=0; i<5;i++){
if(bd[i][0]==id){
data = bd[i][1];
}
}
return data;
}
// clock functions:
String digitalClockDisplay(){
String clock = String(hour()) + ":" + String(minute()) + ":" + String(second()) + " | " + String(day())+ "/" + String(month())+ "/" + String(year());
return clock;
}
//void buzz(int targetPin, long frequency, long length) {
// long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
// long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//
// for (long i=0; i < numCycles; i++){ // for the calculated length of time...
// digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
// delayMicroseconds(delayValue); // wait for the calculated delay value
// digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
// delayMicroseconds(delayValue); // wait againf or the calculated delay value
// }
//}
void WriteOnSD(){
/* Initiliase the SD card */
if (!SD.begin(4))
{
/* If there was an error output this to the serial port and go no further */
Serial.println("card failed");
delay(2);
}
else
{
SDFileData.println("OK");
SDFileData.close();
Serial.println("done");
delay(2);
}
}