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 9
- #define RFM69_CS 4
- #define RFM69_RST 8
- 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
- String weekDays[7] = {"Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"};
- /********* 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("*** Module extérieur Take Out The Trash ***");
- if (!rf69.init()) {
- Serial.println("Impossible d'initialiser le module radio...");
- while (1);
- }
- if (!rf69.setFrequency(RF69_FREQ)) {
- Serial.println("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("Didn't find PN53x board");
- while (1);
- }
- nfc.SAMConfig(); // Configure board to read RFID tags
- bool timeSync = false;
- setSyncProvider(requestSync); //set function to call when sync required
- Serial.println("En attente de synchronisation (TIME)");
- while (!timeSync) {
- if (Serial.available()) {
- processSyncMessage();
- }
- if (timeStatus()!= timeNotSet) {
- timeSync = true;
- }
- }
- Serial.println("Le module s'est parfaitement initialisé ");
- Serial.print("Module réglé @"); Serial.print((int)RF69_FREQ); Serial.println(" MHz");
- Serial.println("\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];
- label:
- success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
- if (success) {
- Serial.println("Found an ISO14443A card");
- Serial.print(" UID Length: ");Serial.print(uidLength, DEC);Serial.println(" bytes");
- Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println("");
- Serial.println("Seems to be a Mifare Classic card (4 byte UID)");
- Serial.println("Trying to authenticate block 4 with default KEYA value");
- uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
- success = nfc.mifareclassic_AuthenticateBlock(uid, uidLength, 4, 0, keya);
- if (success)
- {
- Serial.println("Sector 1 (Blocks 4..7) has been authenticated");
- // Try to read the contents of block 4
- success = nfc.mifareclassic_ReadDataBlock(4, data);
- if (success)
- {
- // Data seems to have been read ... spit it out
- Serial.println("Reading Block 4:");
- nfc.PrintHexChar(data, 16);
- Serial.println("");
- /*for (int i=0; i<16; i++) {
- Serial.print(i); Serial.print(" = "); Serial.print(data[i]); Serial.println("");
- }*/ //test des valeurs block 4
- /*if (data[0] == 97) {
- Serial.println("oui oui");
- }
- else {
- Serial.println("non non");
- }*/ //test de la valeur ASCII de "a" (=97)
- // Wait a bit before reading the card again
- delay(3000);
- }
- else
- {
- Serial.println("Ooops ... unable to read the requested block. Try another key?");
- }
- }
- else
- {
- Serial.println("Ooops ... authentication failed: Try another key?");
- }
- }
- else {
- Serial.println("bug ?");
- delay(1000);
- goto label;
- }
- Serial.println(data[0]);
- char radiopacket[20] ="test";
- Serial.print("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("Réponse obtenue : ");
- Serial.println((char*)buf);
- } else {
- Serial.println("Échec de la réception");
- }
- } else {
- Serial.println("Aucune réponse ... Un autre RFM69 est-il en ligne ?");
- }
- Serial.println();
- delay(4000);
- }
- void digitalClockDisplay(){
- Serial.print("Aujoud'hui nous sommes : ");
- Serial.println(weekDays[weekday()-1]);
- }
- void processSyncMessage() {
- unsigned long pctime;
- const unsigned long DEFAULT_TIME = 1357041600; // 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