Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Module extérieur du projet Take Out The Trash (TOTT)
- // Détecte les poubelles, et en fonction du jour de la semaine
- // indique si l'on doit sortir ou non les poubelles.
- #include <SPI.h>
- #include <RH_RF69.h>
- #include <TimeLib.h>
- #include <Wire.h>
- #include <Adafruit_PN532.h>
- /************ Radio Setup ***************/
- #define RF69_FREQ 915.0
- #define RFM69_INT 3
- #define RFM69_CS 4
- #define RFM69_RST 9
- RH_RF69 rf69(RFM69_CS, RFM69_INT); // Singleton instance of the radio driver
- /********* Time Fucntion Setup **********/
- #define TIME_HEADER "T" // Header tag for serial time sync message
- #define TIME_REQUEST 7 // ASCII bell character requests a time sync message
- #define GMT -2 //positive value or negative eg : 7 or -1
- String weekDays[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
- String months[12] = {"Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"};
- /********* RFID Fucntion Setup *********/
- #define PN532_IRQ 2
- #define PN532_RESET 3
- Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); //Singleton instance of the RFID reader
- void setup()
- {
- Serial.begin(9600);
- pinMode(RFM69_RST, OUTPUT);
- digitalWrite(RFM69_RST, HIGH); delay(10);
- digitalWrite(RFM69_RST, LOW); delay(10);
- Serial.println(F("*** Module extérieur Take Out The Trash ***"));
- if (!rf69.init()) {
- Serial.println(F("Impossible d'initialiser le module radio..."));
- while (1);
- }
- if (!rf69.setFrequency(RF69_FREQ)) {
- Serial.println(F("Impossible d'établir la bonne fréquence..."));
- }
- rf69.setTxPower(20, true);
- uint8_t key[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
- 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
- rf69.setEncryptionKey(key);
- nfc.begin();
- uint32_t versiondata = nfc.getFirmwareVersion();
- if (! versiondata)
- {
- Serial.print(F("Didn't find PN53x board"));
- while (1);
- }
- nfc.SAMConfig(); // Configure board to read RFID tags
- nfc.setPassiveActivationRetries(0xFE);
- bool timeSync = false;
- setSyncProvider(requestSync); //set function to call when sync required
- Serial.println(F("En attente de synchronisation (TIME)"));
- while (!timeSync) {
- if (Serial.available()) {
- processSyncMessage();
- }
- if (timeStatus()!= timeNotSet) {
- timeSync = true;
- }
- }
- Serial.println(F("Le module s'est parfaitement initialisé "));
- Serial.print(F("Module réglé @")); Serial.print((int)RF69_FREQ); Serial.println(" MHz");
- digitalClockDisplay();
- Serial.println(F("\n************************************\n"));
- }
- void loop() {
- uint8_t success;
- uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
- uint8_t uidLength; // Length of the UID
- uint8_t data[16];
- for (int i=0; i < 10; i++) {
- success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); i++;
- }
- if (success) {
- Serial.println(F("Tag RFID trouvé ! Authentification en cours avec la clé 'KEYA'"));
- uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
- if (success)
- {
- Serial.println(F("Carte identifiée !"));
- // Try to read the contents of block 4
- success = nfc.mifareclassic_ReadDataBlock(4, data);
- if (success)
- {
- Serial.println(F(""));
- }
- else
- {
- Serial.println(F("Ooops ... unable to read the requested block. Try another key?"));
- }
- }
- else
- {
- Serial.println(F("Ooops ... authentication failed: Try another key?"));
- }
- }
- char radiopacket[20];
- String(data[0]).toCharArray(radiopacket, 20);
- Serial.print(F("En train d'envoyer : ")); Serial.println(radiopacket);
- // Send a message!
- rf69.send((uint8_t *)radiopacket, strlen(radiopacket));
- rf69.waitPacketSent();
- // Now wait for a reply
- uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
- uint8_t len = sizeof(buf);
- if (rf69.waitAvailableTimeout(500)) {
- // Should be a reply message for us now
- if (rf69.recv(buf, &len)) {
- Serial.print(F("Réponse obtenue : "));
- Serial.println((char*)buf);
- } else {
- Serial.println(F("Échec de la réception"));
- }
- } else {
- Serial.println(F("Aucune réponse ... Un autre RFM69 est-il en ligne ?"));
- }
- Serial.println();
- delay(4000);
- }
- void digitalClockDisplay() {
- Serial.print(weekDays[weekday()-1]);
- Serial.print(" ");
- Serial.print(day());
- Serial.print(" ");
- Serial.print(months[month()-1]);
- Serial.print(" ");
- Serial.print(year());
- Serial.print(" - ");
- Serial.print(hour() + GMT);
- printDigits(minute());
- printDigits(second());
- }
- void printDigits(int digits) {
- Serial.print(":");
- if (digits < 10)
- Serial.print('0');
- Serial.print(digits);
- }
- void processSyncMessage() {
- unsigned long pctime;
- const unsigned long DEFAULT_TIME = 1111222333; // Jan 1 2013
- if(Serial.find(TIME_HEADER)) {
- pctime = Serial.parseInt();
- if( pctime >= DEFAULT_TIME) { // check the integer is a valid time (greater than Jan 1 2013)
- setTime(pctime); // Sync Arduino clock to the time received on the serial port
- }
- }
- }
- time_t requestSync()
- {
- Serial.write(TIME_REQUEST);
- return 0; // the time will be sent later in response to serial mesg
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement