Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <EEPROM.h>
- #include <SPI.h>
- #include <Ethernet.h>
- #include <EthernetUdp.h>
- SoftwareSerial rfid(7, 8);
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
- };
- IPAddress ip(192, 168, 0, 42);
- unsigned int localPort = 901;
- EthernetUDP Udp;
- int Quante = 0;
- const int Serratura = 13;
- long Buona[10];
- long bytesToLong(byte * letto) {
- long value = 0;
- value += ((long)letto[0] << 24);
- value += ((long)letto[1] << 16);
- value += ((long)letto[2] << 8);
- value += (long)letto[3];
- return value;
- }
- void setup() {
- Ethernet.begin(mac, ip);
- Udp.begin(localPort);
- Serial.begin(9600);
- rfid.begin(9600);
- pinMode(Serratura, OUTPUT);
- Quante = EEPROM.read(0); // nΒ° di codici tessera presenti nella EEPROM
- Recupera();
- Serial.println("Ready ...");
- }
- void loop() {
- char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer per i pacchetti in ingresso
- for(int i = 0; i<UDP_TX_PACKET_MAX_SIZE; i++){
- packetBuffer[i] = '\0';
- }
- int packetSize = Udp.parsePacket();
- if (packetSize) { // se ho ricevuto informazioni
- Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
- Serial.print(packetBuffer);
- Serial.print(">");
- Serial.println(packetSize); // RISOLVERE IL PROBLEMA DELLO SVUOTAMENTO BUFFER
- Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
- Udp.write("OK");
- Udp.endPacket();
- }
- int i = 0;
- byte buffer[4];
- bool letto_qualcosa = false;
- bool trovata = false;
- while(rfid.available()>0) { //verifico la presenza di dati in arrivo
- buffer[i++] = rfid.read();
- letto_qualcosa = true;
- }
- if(letto_qualcosa){
- long numero_tessera = bytesToLong(buffer);
- for(int confronta = 0; confronta < Quante; confronta++){
- if (Buona[confronta] == numero_tessera) {
- trovata = true;
- }
- }
- if(trovata) {
- Serial.println("Tessera Valida");
- Sblocca();
- trovata = false;
- }
- else {
- Serial.println("Tessera SCONOSCIUTA");
- }
- }
- delay(1000);
- }
- void Sblocca() { // Viene invocato quando ho una lettura valida
- digitalWrite(Serratura, HIGH);
- delay(600);
- digitalWrite(Serratura, LOW);
- }
- void Recupera() { // Popolo l'array "Buona[]" con i "Quante" valori-tessera letti nella EEPROM
- for(int i = 0; i< Quante; i++){
- byte abilitata[4];
- for(int j=0; j<4; j++){
- //lettura dei numeri di tessera abilitati
- //offsset= 1 (indirizzo di partenza)
- // + i*4 (inzio di ogni tessera)
- // + j (byte da 0 a 3 compreso)
- abilitata[j] = EEPROM.read( 1+(i*4)+j );
- }
- Buona[i] = bytesToLong(abilitata);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement