Advertisement
Guest User

Untitled

a guest
Sep 17th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Module extérieur du projet Take Out The Trash (TOTT)
  2. // Détecte les poubelles, et en fonction du jour de la semaine
  3. // indique si l'on doit sortir ou non les poubelles.
  4.  
  5. #include <SPI.h>
  6. #include <RH_RF69.h>
  7. #include <TimeLib.h>
  8. #include <Wire.h>
  9. #include <Adafruit_PN532.h>
  10.  
  11. /************ Radio Setup ***************/
  12.  
  13. #define RF69_FREQ 915.0
  14. #define RFM69_INT     9
  15. #define RFM69_CS      4
  16. #define RFM69_RST     8
  17. RH_RF69 rf69(RFM69_CS, RFM69_INT); // Singleton instance of the radio driver
  18.  
  19. /********* Time Fucntion Setup **********/
  20.  
  21. #define TIME_HEADER  "T"   // Header tag for serial time sync message
  22. #define TIME_REQUEST  7    // ASCII bell character requests a time sync message
  23. String weekDays[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
  24.  
  25. /********* RFID Fucntion Setup *********/
  26.  
  27. #define PN532_IRQ   2
  28. #define PN532_RESET 3
  29. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); //Singleton instance of the RFID reader
  30.  
  31. void setup()
  32. {
  33.   Serial.begin(9600);
  34.   pinMode(RFM69_RST, OUTPUT);
  35.   digitalWrite(RFM69_RST, HIGH); delay(10);
  36.   digitalWrite(RFM69_RST, LOW); delay(10);
  37.  
  38.   Serial.println("*** Module extérieur Take Out The Trash ***");
  39.  
  40.   if (!rf69.init()) {
  41.     Serial.println("Impossible d'initialiser le module radio...");
  42.     while (1);
  43.   }
  44.   if (!rf69.setFrequency(RF69_FREQ)) {
  45.     Serial.println("Impossible d'établir la bonne fréquence...");
  46.   }
  47.  
  48.   rf69.setTxPower(20, true);
  49.  
  50.   uint8_t key[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  51.                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  52.   rf69.setEncryptionKey(key);
  53.  
  54.   nfc.begin();
  55.   uint32_t versiondata = nfc.getFirmwareVersion();
  56.   if (! versiondata)
  57.   {
  58.     Serial.print("Didn't find PN53x board");
  59.     while (1);
  60.   }
  61.   nfc.SAMConfig(); // Configure board to read RFID tags
  62.  
  63.   bool timeSync = false;
  64.   setSyncProvider(requestSync);  //set function to call when sync required
  65.   Serial.println("En attente de synchronisation (TIME)");
  66.   while (!timeSync) {
  67.     if (Serial.available()) {
  68.     processSyncMessage();
  69.     }
  70.     if (timeStatus()!= timeNotSet) {
  71.       timeSync = true;  
  72.     }
  73.   }
  74.  
  75.   Serial.println("Le module s'est parfaitement initialisé ");
  76.   Serial.print("Module réglé @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");
  77.   Serial.println("\n************************************\n");
  78. }
  79.  
  80. void loop() {
  81.   uint8_t success;
  82.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  83.   uint8_t uidLength;                        // Length of the UID
  84.   uint8_t data[16];
  85.   label:
  86.   success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
  87.  
  88.   if (success) {
  89.     Serial.println("Found an ISO14443A card");
  90.     Serial.print("  UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
  91.     Serial.print("  UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println("");
  92.    
  93.     Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
  94.     Serial.println("Trying to authenticate block 4 with default KEYA value");
  95.     uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  96.     success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
  97.  
  98.     if (success)
  99.     {
  100.       Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
  101.  
  102.       // Try to read the contents of block 4
  103.       success = nfc.mifareclassic_ReadDataBlock(4, data);
  104.  
  105.       if (success)
  106.       {
  107.         // Data seems to have been read ... spit it out
  108.         Serial.println("Reading Block 4:");
  109.         nfc.PrintHexChar(data, 16);
  110.         Serial.println("");
  111.  
  112.         /*for (int i=0; i<16; i++) {
  113.           Serial.print(i); Serial.print(" = "); Serial.print(data[i]); Serial.println("");
  114.         }*/ //test des valeurs block 4
  115.         /*if (data[0] == 97) {
  116.           Serial.println("oui oui");
  117.         }
  118.         else {
  119.           Serial.println("non non");
  120.         }*/ //test de la valeur ASCII de "a" (=97)
  121.    
  122.         // Wait a bit before reading the card again
  123.         delay(3000);
  124.       }
  125.       else
  126.       {
  127.         Serial.println("Ooops ... unable to read the requested block.  Try another key?");
  128.       }
  129.     }
  130.     else
  131.     {
  132.       Serial.println("Ooops ... authentication failed: Try another key?");
  133.     }
  134.   }
  135.   else {
  136.     Serial.println("bug ?");
  137.     delay(1000);
  138.     goto label;
  139.   }
  140.  
  141.   Serial.println(data[0]);
  142.   char radiopacket[20] ="test";
  143.   Serial.print("En train d'envoyer : "); Serial.println(radiopacket);
  144.  
  145.   // Send a message!
  146.   rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
  147.   rf69.waitPacketSent();
  148.  
  149.   // Now wait for a reply
  150.   uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
  151.   uint8_t len = sizeof(buf);
  152.  
  153.   if (rf69.waitAvailableTimeout(500))  {
  154.     // Should be a reply message for us now
  155.     if (rf69.recv(buf, &len)) {
  156.       Serial.print("Réponse obtenue : ");
  157.       Serial.println((char*)buf);
  158.     } else {
  159.       Serial.println("Échec de la réception");
  160.     }
  161.   } else {
  162.     Serial.println("Aucune réponse ... Un autre RFM69 est-il en ligne ?");
  163.   }
  164.   Serial.println();
  165.   delay(4000);
  166. }
  167.  
  168. void digitalClockDisplay(){
  169.   Serial.print("Aujoud'hui nous sommes : ");
  170.   Serial.println(weekDays[weekday()-1]);
  171. }
  172.  
  173. void processSyncMessage() {
  174.   unsigned long pctime;
  175.   const unsigned long DEFAULT_TIME = 1357041600; // Jan 1 2013
  176.  
  177.   if(Serial.find(TIME_HEADER)) {
  178.      pctime = Serial.parseInt();
  179.      if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
  180.        setTime(pctime); // Sync Arduino clock to the time received on the serial port
  181.      }
  182.   }
  183. }
  184.  
  185. time_t requestSync()
  186. {
  187.   Serial.write(TIME_REQUEST);  
  188.   return 0; // the time will be sent later in response to serial mesg
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement