Hi to all, here is the code which I have developt for Arduino with Keypad and Database.
// Include libraries
#include <SPI.h>
#include <Ethernet.h>
#include <Keypad.h>
#include <EEPROM.h>
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x34, 0x99 }; //Arduino MAC
byte ip[] = { 172,20,12,28 }; // Arduino IP
byte router[] = { 172,20,12,253 }; // Network Router IP
byte server[] = { 172,20,12,145 }; // Server IP
Client client(server, 80); // Serverinformation for client, were to send data
//---------------------------------------
const byte ROWS = 4; // Four rows
const byte COLS = 3; // Three columns
int i = 0; // Counter for password
char passwort[] = {'0','0','0','0','0','0'}; // Password array of 6 signs
//----------------------------------------------------------
char keys[ROWS][COLS] = // Keymap define
{
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','%'} // # can't be used, it is a used variable of apache Server, replaced by %
};
byte rowPins[ROWS] = {3, 8, 7, 5}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 2, 6}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Define keypad variables from keypad.h and
//----------------------------------------------------------
void setup() //Single Task
{
pinMode(12, OUTPUT); // Define Pin 12 as OUTPUT 13= not so strong for output current(used by system as check led)
Serial.begin(9600); // Boudrate at 9600 bits/s
//for (int i = 0; i < 512; i++) // Write a 0 to all 512 bytes of the EEPROM
//EEPROM.write(i, 0);
//Serial.println("EEPROM empty" );
}
//----------------------------------------------------------
void loop() //Repeat Task
{
char pass = '1'; // While true check for key pressed
while ( 1 )
{
pass = keypad.getKey(); // Read in key's
if (pass != NO_KEY) break; // Break to stop loop
}
Serial.println(pass); // Print key
passwort[i] = pass; // Write single key in char array passwort
i++;
if ( i == 6 ) // When password array of 6 charecters are full, go to functions
{
sendpass(passwort); // Take variable password to function sendpass
char c = getdata(); // Start funktion getdata with c as get variable
ckeckdata(c); // Check server information about password
i = 0; // Reset password to zero
}
}
//----------------------------------------------------------
void sendpass(char* passwortsend) // Send password to Server
{
String p; // Counting password array together to be ready fpr TCP/IP
for ( int j=0; j<6; j++) // Convert password array to string password
{
p += passwortsend[j];
}
for (int i = 0; i < 512; i++) // Write a 0 to all 512 bytes of the EEPROM
EEPROM.write(i, 0);
Serial.println("EEPROM empty" );
Serial.println(p);
Serial.println("ethernet...");
Ethernet.begin(mac, ip, router); // Start the Ethernet connection MAC,IP,Router
Serial.begin(9600); // Start the serial library
delay(3000); // Give the Ethernet shield 3 second to initialize
Serial.println("connecting...");
if (client.connect()) // If you get a connection, report back via serial
{
String s = ("GET /cgi-bin/post.pl?I=2&W="); // Send password to URL with GET order /cgi-bin/=Apache Server post.pl=perl script for database communication ?=start parameter &=link data
s += p; // Combine s=URL and p=password
Serial.println("connected");
Serial.println(s); // Show password string with URL
client.println(s); // GET password send to server
}
else {
Serial.println("connection failed"); // Didn't get a connection to the server
}
}
//----------------------------------------------------------
char getdata() //Get Y or N from Server
{
char data;
while (1)
{
if (client.available())
{
Serial.println("Data Read"); // Waiting for answer from Apache
data = client.read();
Serial.print(data);
break;
}
}
if (!client.connected()) // If server's disconnected, stop the client
{
Serial.println(data);
Serial.println("disconnecting.");
client.stop();
}
return data;
}
//----------------------------------------------------------
void ckeckdata(char data)
{
if (data == 'Y') // Y Password check
{
for (int i = 0; i < 512; i++) // Write 0 to all 512 bytes of the EEPROM
EEPROM.write(i, 0);
Serial.println("EEPROM empty" );
Serial.println("Accepted");
digitalWrite(12, HIGH);
delay(1000);
digitalWrite(12, LOW);
delay(10);
}
else // Y Password check
{
Serial.println("Denied");
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
delay(10);
for (int i = 0; i < 512; i++) // Write 0 to all 512 bytes of the EEPROM
EEPROM.write(i, 0);
Serial.println("EEPROM empty" );
}
}