Advertisement
Guest User

Module extérieur

a guest
Sep 18th, 2018
98
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     3
  15. #define RFM69_CS      4
  16. #define RFM69_RST     9
  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. #define GMT -2 //positive value or negative eg : 7 or -1
  24. String weekDays[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
  25. String months[12] = {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"};
  26.  
  27. /********* RFID Fucntion Setup *********/
  28.  
  29. #define PN532_IRQ   2
  30. #define PN532_RESET 3
  31. Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); //Singleton instance of the RFID reader
  32.  
  33. void setup()
  34. {
  35.   Serial.begin(9600);
  36.   pinMode(RFM69_RST, OUTPUT);
  37.   digitalWrite(RFM69_RST, HIGH); delay(10);
  38.   digitalWrite(RFM69_RST, LOW); delay(10);
  39.  
  40.   Serial.println(F("*** Module extérieur Take Out The Trash ***"));
  41.  
  42.   if (!rf69.init()) {
  43.     Serial.println(F("Impossible d'initialiser le module radio..."));
  44.     while (1);
  45.   }
  46.   if (!rf69.setFrequency(RF69_FREQ)) {
  47.     Serial.println(F("Impossible d'établir la bonne fréquence..."));
  48.   }
  49.  
  50.   rf69.setTxPower(20, true);
  51.  
  52.   uint8_t key[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  53.                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  54.   rf69.setEncryptionKey(key);
  55.  
  56.   nfc.begin();
  57.   uint32_t versiondata = nfc.getFirmwareVersion();
  58.   if (! versiondata)
  59.   {
  60.     Serial.print(F("Didn't find PN53x board"));
  61.     while (1);
  62.   }
  63.   nfc.SAMConfig(); // Configure board to read RFID tags
  64.   nfc.setPassiveActivationRetries(0xFE);
  65.  
  66.   bool timeSync = false;
  67.   setSyncProvider(requestSync);  //set function to call when sync required
  68.   Serial.println(F("En attente de synchronisation (TIME)"));
  69.   while (!timeSync) {
  70.     if (Serial.available()) {
  71.     processSyncMessage();
  72.     }
  73.     if (timeStatus()!= timeNotSet) {
  74.       timeSync = true;  
  75.     }
  76.   }
  77.  
  78.   Serial.println(F("Le module s'est parfaitement initialisé "));
  79.   Serial.print(F("Module réglé @"));  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");
  80.   digitalClockDisplay();
  81.   Serial.println(F("\n************************************\n"));
  82. }
  83.  
  84. void loop() {
  85.   uint8_t success;
  86.   uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 };  // Buffer to store the returned UID
  87.   uint8_t uidLength;                        // Length of the UID
  88.   uint8_t data[16];
  89.   for (int i=0; i < 10; i++) {
  90.     success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); i++;
  91.   }
  92.  
  93.   if (success) {
  94.     Serial.println(F("Tag RFID trouvé ! Authentification en cours avec la clé 'KEYA'"));
  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(F("Carte identifiée !"));
  101.       // Try to read the contents of block 4
  102.       success = nfc.mifareclassic_ReadDataBlock(4, data);
  103.  
  104.       if (success)
  105.       {
  106.         Serial.println(F(""));
  107.       }
  108.       else
  109.       {
  110.         Serial.println(F("Ooops ... unable to read the requested block.  Try another key?"));
  111.       }
  112.     }
  113.     else
  114.     {
  115.       Serial.println(F("Ooops ... authentication failed: Try another key?"));
  116.     }
  117.   }
  118.  
  119.   char radiopacket[20];
  120.   String(data[0]).toCharArray(radiopacket, 20);
  121.   Serial.print(F("En train d'envoyer : ")); Serial.println(radiopacket);
  122.  
  123.   // Send a message!
  124.   rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
  125.   rf69.waitPacketSent();
  126.  
  127.   // Now wait for a reply
  128.   uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
  129.   uint8_t len = sizeof(buf);
  130.  
  131.   if (rf69.waitAvailableTimeout(500))  {
  132.     // Should be a reply message for us now
  133.     if (rf69.recv(buf, &len)) {
  134.       Serial.print(F("Réponse obtenue : "));
  135.       Serial.println((char*)buf);
  136.     } else {
  137.       Serial.println(F("Échec de la réception"));
  138.     }
  139.   } else {
  140.     Serial.println(F("Aucune réponse ... Un autre RFM69 est-il en ligne ?"));
  141.   }
  142.   Serial.println();
  143.   delay(4000);
  144. }
  145.  
  146. void digitalClockDisplay() {
  147.  Serial.print(weekDays[weekday()-1]);
  148.  Serial.print(" ");
  149.  Serial.print(day());
  150.  Serial.print(" ");
  151.  Serial.print(months[month()-1]);
  152.  Serial.print(" ");
  153.  Serial.print(year());
  154.  Serial.print(" - ");
  155.  Serial.print(hour() + GMT);
  156.  printDigits(minute());
  157.  printDigits(second());
  158. }
  159.  
  160. void printDigits(int digits) {
  161.  Serial.print(":");
  162.  if (digits < 10)
  163.  Serial.print('0');
  164.  Serial.print(digits);
  165. }
  166.  
  167. void processSyncMessage() {
  168.   unsigned long pctime;
  169.   const unsigned long DEFAULT_TIME = 1111222333; // Jan 1 2013
  170.  
  171.   if(Serial.find(TIME_HEADER)) {
  172.      pctime = Serial.parseInt();
  173.      if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
  174.        setTime(pctime); // Sync Arduino clock to the time received on the serial port
  175.      }
  176.   }
  177. }
  178.  
  179. time_t requestSync()
  180. {
  181.   Serial.write(TIME_REQUEST);  
  182.   return 0; // the time will be sent later in response to serial mesg
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement