Advertisement
alveoten

Untitled

May 23rd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <EEPROM.h>
  3. #include <SPI.h>
  4. #include <Ethernet.h>
  5. #include <EthernetUdp.h>
  6.  
  7. SoftwareSerial rfid(7, 8);
  8.  
  9. byte mac[] = {
  10.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  11. };
  12. IPAddress ip(192, 168, 0, 42);
  13. unsigned int localPort = 901;
  14.  
  15.  
  16. EthernetUDP Udp;
  17.  
  18. int Quante = 0;
  19. const int Serratura =  13;
  20. long Buona[10];
  21.  
  22. long bytesToLong(byte * letto) {
  23.   long value = 0;
  24.   value += ((long)letto[0] << 24);
  25.   value += ((long)letto[1] << 16);
  26.   value += ((long)letto[2] << 8);
  27.   value += (long)letto[3];    
  28.   return value;
  29. }
  30.  
  31. void setup() {
  32.   Ethernet.begin(mac, ip);
  33.   Udp.begin(localPort);
  34.   Serial.begin(9600);
  35.   rfid.begin(9600);
  36.   pinMode(Serratura, OUTPUT);
  37.   Quante = EEPROM.read(0); // nΒ° di codici tessera presenti nella EEPROM
  38.   Recupera();
  39.   Serial.println("Ready ...");
  40. }
  41.  
  42. void loop() {
  43.   char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; // buffer per i pacchetti in ingresso
  44.   for(int i = 0; i<UDP_TX_PACKET_MAX_SIZE; i++){
  45.     packetBuffer[i] = '\0';
  46.   }
  47.   int packetSize = Udp.parsePacket();
  48.   if (packetSize) { // se ho ricevuto informazioni
  49.     Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
  50.     Serial.print(packetBuffer);
  51.     Serial.print(">");
  52.     Serial.println(packetSize); // RISOLVERE IL PROBLEMA DELLO SVUOTAMENTO BUFFER
  53.  
  54.     Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
  55.     Udp.write("OK");
  56.     Udp.endPacket();
  57.   }
  58.  
  59.  
  60.  
  61.   int i = 0;
  62.   byte buffer[4];
  63.   bool letto_qualcosa = false;
  64.   bool trovata = false;
  65.   while(rfid.available()>0) { //verifico la presenza di dati in arrivo
  66.     buffer[i++] =  rfid.read();    
  67.     letto_qualcosa = true;
  68.   }
  69.  
  70.   if(letto_qualcosa){
  71.    
  72.     long numero_tessera = bytesToLong(buffer);
  73.    
  74.     for(int confronta = 0; confronta < Quante; confronta++){
  75.       if (Buona[confronta] == numero_tessera) {
  76.         trovata = true;
  77.       }
  78.     }
  79.  
  80.     if(trovata) {
  81.        Serial.println("Tessera Valida");
  82.        Sblocca();
  83.        trovata = false;      
  84.     }
  85.     else {
  86.       Serial.println("Tessera SCONOSCIUTA");
  87.     }
  88.   }
  89.  
  90.   delay(1000);
  91. }
  92.  
  93. void Sblocca() {   // Viene invocato quando ho una lettura valida
  94.   digitalWrite(Serratura, HIGH);
  95.   delay(600);
  96.   digitalWrite(Serratura, LOW);
  97. }  
  98.  
  99. void Recupera() {  // Popolo l'array "Buona[]" con i "Quante" valori-tessera letti nella EEPROM
  100.   for(int i = 0; i< Quante; i++){
  101.     byte abilitata[4];
  102.     for(int j=0; j<4; j++){
  103.       //lettura dei numeri di tessera abilitati
  104.       //offsset= 1 (indirizzo di partenza)
  105.       //        + i*4  (inzio di ogni tessera)
  106.       //        + j (byte da 0 a 3 compreso)
  107.       abilitata[j] = EEPROM.read( 1+(i*4)+j );
  108.     }
  109.     Buona[i] = bytesToLong(abilitata);
  110.   }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement