Photon Doorlock problem

Hello guys!

I’m having trouble compiling my adapted code and i don’t really get why…
I’m using a particle photon, a rc522 rfid reader and a relay.
Can any one help me figure this out?

Thank you

code:

#include "MFRC522/MFRC522.h"

/*
  Function           Core Pin      MRFC522 Pin
  Reset             D2            RST
  SPI SS            D1            SDA
  SPI MOSI          A5            MOSI
  SPI MISO          A4            MISO
  SPI SCK           A3            SCK
*/

#define SS_PIN D1
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.

void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC

  mfrc522.setSPIConfig();
  mfrc522.PCD_Init(); // Init MFRC522 card
  
  String ok_rfid_1="25bcefb0";
  int lock=5; //Which pin the lock will be on if using a relay or solenoid or similar 


  RGB.control(true); // take control of onboard RGB led
}

void blink() {
  RGB.color(0, 0, 255);
  delay(150);
  RGB.color(0, 0, 0);
  delay(100);
  RGB.color(0, 0, 255);
  delay(150);
  RGB.color(0, 0, 0);
}

void open_lock() {
  //Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  delay(2000);
  digitalWrite(lock,LOW);
  
  
}

void loop() {
  // Look for new cards
  if ( mfrc522.PICC_IsNewCardPresent()) {
    // Serial.println("New card present...");
    if ( mfrc522.PICC_ReadCardSerial()) {
      // Dump debug info about the card. PICC_HaltA() is automatically called.
      //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

      String UID = "";

      for (byte i = 0; i < mfrc522.uid.size; i++) {
        UID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        UID += String(mfrc522.uid.uidByte[i], HEX);
      }

      mfrc522.PICC_HaltA();
      Serial.print("UID: ");
      Serial.println(UID);
      
      if (UID==ok_rfid_1) {
      //ok, open the door.
      open_lock();
     }

      blink(); // visual feedback of read using onboard led
    }
  }
}

errors:

dumpinfo.cpp: In function 'void setup()':
dumpinfo.cpp:24:7: warning: unused variable 'lock' [-Wunused-variable]
 void setup() {
       ^
dumpinfo.cpp: In function 'void open_lock()':

	

	
		
			
			dumpinfo.cpp:42:16: error: 'lock' was not declared in this scope
   RGB.color(0, 0, 255);
                ^
		
	

	
		dumpinfo.cpp:42:16: note: suggested alternative:
In file included from ../wiring/inc/spark_wiring_thread.h:31:0,
                 from ../wiring/inc/spark_wiring_watchdog.h:22,
                 from ../wiring/inc/spark_wiring_cloud.h:31,
                 from ../wiring/inc/spark_wiring.h:48,
                 from ./inc/application.h:36,
                 from MFRC522/MFRC522.h:77,
                 from dumpinfo.cpp:1:
/usr/local/gcc-arm-embedded-gcc-arm-none-eabi-4_8-2014q2-20140609-linux-tar-bz2/arm-none-eabi/include/c++/4.8.4/mutex:708:5: note:   'std::lock'
     lock(_L1& __l1, _L2& __l2, _L3&... __l3)
     ^
dumpinfo.cpp: In function 'void loop()':

	

	
		
			
			dumpinfo.cpp:68:16: error: 'ok_rfid_1' was not declared in this scope
         UID += String(mfrc522.uid.uidByte[i], HEX);
                ^
		
	

	
		make[1]: *** [../build/target/user/platform-6dumpinfo.o] Error 1
make: *** [user] Error 2

@bernas_123, you declared lock as in int in setup() making it a local variable. Trying to use it in open_lock() causes the error since lock is not known globally. Move the declaration for lock outside of setup() to make it global. :wink:

2 Likes

@peekay123 thank for the reply. So i moved lock and ok_rfid_1 outside of setup and the code compiles but the board goes dark and, no lights and no seiral output, any idea why? :confused:
thank you

EDIT: removed the whole blink()function and the board lights up in the healthy blue light but nothing shows in serial…

Probably because neither if ( mfrc522.PICC_IsNewCardPresent()) nor if ( mfrc522.PICC_ReadCardSerial()) evaluate to true.
Add some extra Serial.print() statements fo all other cases too, then you should see something and maybe even guess what’s happening.

1 Like

Funny thing, changed the wiring and it turns out it does display the UID of the tag in serial but i no longer have that code that is running in the photon, it should be be like this but it does not compile

#include "MFRC522/MFRC522.h"

/*
  Function           Core Pin      MRFC522 Pin
  Reset             D2            RST
  SPI SS            D1            SDA
  SPI MOSI          A5            MOSI
  SPI MISO          A4            MISO
  SPI SCK           A3            SCK
*/

#define SS_PIN D1
#define RST_PIN D2

MFRC522 mfrc522(SS_PIN, RST_PIN); // Create MFRC522 instance.
string ok_rfid_1="F5 53 43 A8";
int lock=5; //Which pin the lock will be on if using a relay or solenoid or similar 


void setup() {
  Serial.begin(9600); // Initialize serial communications with the PC

  mfrc522.setSPIConfig();
  mfrc522.PCD_Init(); // Init MFRC522 card
  pinMode(lock, OUTPUT);

  RGB.control(true); // take control of onboard RGB led
}

void blink() {
  RGB.color(0, 0, 255);
  delay(150);
  RGB.color(0, 0, 0);
  delay(100);
  RGB.color(0, 0, 255);
  delay(150);
  RGB.color(0, 0, 0);
}

void open_lock() {
  //Use this routine when working with Relays and Solenoids etc.
  digitalWrite(lock, HIGH);
  delay(2000);
  digitalWrite(lock,LOW);
  
}

void loop() {
  // Look for new cards
  if ( mfrc522.PICC_IsNewCardPresent()) {
    // Serial.println("New card present...");
    if ( mfrc522.PICC_ReadCardSerial()) {
      // Dump debug info about the card. PICC_HaltA() is automatically called.
      //mfrc522.PICC_DumpToSerial(&(mfrc522.uid));

      String UID = "";

      for (byte i = 0; i < mfrc522.uid.size; i++) {
        UID += String(mfrc522.uid.uidByte[i] < 0x10 ? "0" : "");
        UID += String(mfrc522.uid.uidByte[i], HEX);
      }

      mfrc522.PICC_HaltA();
      Serial.print("UID: ");
      Serial.println(UID);
      
      if (UID==ok_rfid_1) {
      //ok, open the door.
      open_lock();
      }

      blink(); // visual feedback of read using onboard led
    }
  }
}

errors:

umpinfo.cpp:16:1: error: 'string' does not name a type
   SPI SCK           A3            SCK
 ^
		
	

	
		dumpinfo.cpp: In function 'void loop()':

	

	
		
			
			dumpinfo.cpp:67:16: error: 'ok_rfid_1' was not declared in this scope
         UID += String(mfrc522.uid.uidByte[i], HEX);
                ^
		
	

	
		make[1]: *** [../build/target/user/platform-6dumpinfo.o] Error 1
make: *** [user] Error 2

sorry for the edits

Try using the actual pin names instead of numbers, it does matter.

really sorry, i accidently edit it all wrong

1 Like

Try adding a blank line at the top of your code :wink:
And it’s String not string.

2 Likes

thanks for the quick responses, it verifies and, UID is displayed in serial, the board’s led blinks like supposed in the code but no relay movement, the serial displays UID : f55343a8 so should it be
String ok_rfid_1="F55343A8";```orString ok_rfid_1=“F5 53 43 A8”;``` ?

I’d rather think it should be neither, you’d need to have upper and lower case exactly as it is in your serial output.

String ok_rfid_1 = "f55343a8";

Or just swap if (UID == ok_rfid_1) for if (UID.equalsIgnoreCase(ok_rfid_1))
https://docs.particle.io/reference/firmware/photon/#equalsignorecase-

But I’d rather go for the binary value and compare it to a uint32_t - this way you won’t have any troubles with upper or lower case or formatting.

So i got it working fine, except for the unlock/locl part so i tried using a simple example to control a servo but i doenst work, any idea why? im using a servo connected to an 5v external power and pin A2 to send the signal but it doesn’t move…

Servo myservo;  // create servo object to control a servo <br>                // a maximum of eight servo objects can be created 
 
int pos = 0;    // variable to store the servo position 
 
void setup() 
{ 
  myservo.attach(A2);  // attaches the servo on pin 2 to the servo object 
} 
 
 
void loop() 
{ 
  for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
}

on the Photon, Servo can be connected to A4, A5, WKP, RX, TX, D0, D1, D2, D3

https://docs.particle.io/reference/firmware/photon/#servo

i´’ve been looking through the forum and and allready tried a few of those pins and nothing…

I have to ask… Do you have a common ground?

i have tried to ground the servo to the external power and the photon, still no results

EDIT: i got it working,i guess the problem was with those cheap usb cable now going to try using the rfid part

So i got it to work, next thing is to package it all up in a nice little box a setup it up to open my house door =) thanks to all who helped me!