Hey everyone, for a project I'm using both a RC522 and a bluefruit LE Shield.
Both sketches work individually. The RFID reader manages to to read the cards and print their UIDs. This component will be used to turn on LEDs according to an array that is part of the Bluefruit sketch. The Bluefruit is used along with the Adafruit app to set 'profiles' with the in-app controller buttons which just stores whatever profile in the arrays. I have 3 UIDs/PICCs that will activate the profile.
the main issue is that I'm not sure where to begin in combining them. I have made a couple attempts and I've run into not knowing if it's the baud rate of the two, or if it has something to do with SPI.
I'm pretty novice, but any help would be really appreciated.
Bluetooth
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the Arduino is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 2
// How many NeoPixels are attached to the Arduino?
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
// example for more information on possible values.
Adafruit_NeoPixel strip = Adafruit_NeoPixel(2, PIN, NEO_GRB + NEO_KHZ800);
int delayval = 500; // delay for half a second
////////////////////////////NEW CODE^^^^^^^^^^^^^^^^^^^^^^^
#include <string.h>
#include <Arduino.h>
#include <SPI.h>
#include "Adafruit_BLE.h"
#include "Adafruit_BluefruitLE_SPI.h"
#include "Adafruit_BluefruitLE_UART.h"
#include "BluefruitConfig.h"
#if SOFTWARE_SERIAL_AVAILABLE
#include <SoftwareSerial.h>
#endif
#define FACTORYRESET_ENABLE 1
#define MINIMUM_FIRMWARE_VERSION "0.6.6"
#define MODE_LED_BEHAVIOUR "MODE"
/* ...hardware SPI, using SCK/MOSI/MISO hardware SPI pins and then user selected CS/IRQ/RST */
Adafruit_BluefruitLE_SPI ble(BLUEFRUIT_SPI_CS, BLUEFRUIT_SPI_IRQ, BLUEFRUIT_SPI_RST);
// A small helper
void error(const __FlashStringHelper*err) {
Serial.println(err);
while (1);
}
// function prototypes over in packetparser.cpp
uint8_t readPacket(Adafruit_BLE *ble, uint16_t timeout);
float parsefloat(uint8_t *buffer);
void printHex(const uint8_t * data, const uint32_t numBytes);
// the packet buffer
extern uint8_t packetbuffer[];
int bar[1];
int school[1];
int work[1];
void setup(void)
{
strip.begin();
///////////////////////////////////////////////////////////////////////
// required for Flora & Micro
// delay(500);
Serial.begin(115200);
Serial.println(F("Adafruit Bluefruit App Controller Example"));
Serial.println(F("-----------------------------------------"));
/* Initialise the module */
Serial.print(F("Initialising the Bluefruit LE module: "));
if ( !ble.begin(VERBOSE_MODE) )
{
error(F("Couldn't find Bluefruit, make sure it's in CoMmanD mode & check wiring?"));
}
Serial.println( F("OK!") );
if ( FACTORYRESET_ENABLE )
{
/* Perform a factory reset to make sure everything is in a known state */
Serial.println(F("Performing a factory reset: "));
if ( ! ble.factoryReset() ){
error(F("Couldn't factory reset"));
}
}
/* Disable command echo from Bluefruit */
ble.echo(false);
Serial.println("Requesting Bluefruit info:");
/* Print Bluefruit information */
ble.info();
Serial.println(F("Please use Adafruit Bluefruit LE app to connect in Controller mode"));
Serial.println(F("Then activate/use the sensors, color picker, game controller, etc!"));
Serial.println();
ble.verbose(false); // debug info is a little annoying after this point!
/* Wait for connection */
while (! ble.isConnected()) {
delay(500);
}
Serial.println(F("******************************"));
// LED Activity command is only supported from 0.6.6
if ( ble.isVersionAtLeast(MINIMUM_FIRMWARE_VERSION) )
{
// Change Mode LED Activity
Serial.println(F("Change LED activity to " MODE_LED_BEHAVIOUR));
ble.sendCommandCheckOK("AT+HWModeLED=" MODE_LED_BEHAVIOUR);
}
// Set Bluefruit to DATA mode
Serial.println( F("Switching to DATA mode!") );
ble.setMode(BLUEFRUIT_MODE_DATA);
Serial.println(F("******************************"));
}
void loop(void)
{
strip.setPixelColor(0, strip.Color(0,150,0));
strip.show();
delay(delayval);
/* Wait for new data to arrive */
// Buttons
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
// Serial.print ("Button ");
Serial.println(buttnum);
//set element values of arrays
if (buttnum == 1) {
school[0] = 66; //prof
strip.setPixelColor(0, strip.Color(100,0,100));
strip.show();
delay(delayval);
}
if (buttnum == 2) {
work[0] = 77; //staff
}
if (buttnum == 3) {
work[0] = 88; //management
}
if (buttnum == 4) {
work[0] = 99; //ceo
}
if (buttnum == 5) {
bar[0] = 22; //single
}
if (buttnum == 8) {
bar[0] = 33; //taken
}
if (buttnum == 6) {
school[0] = 44; //undergrad
}
if (buttnum == 7) {
school[0] = 55; //grad
}
//check values of arrays
if (bar[0] == 22) {
Serial.println("Single");
}
if (bar[0] == 33) {
Serial.println("In a Relationship");
}
if (school[0] == 44) {
Serial.println("Undergraduate");
}
if (school[0] == 55) {
Serial.println("Graduate");
}
if (school[0] == 66) {
Serial.println("Professor");
}
if (work[0] == 77) {
Serial.println("Staff");
}
if (work[0] == 88) {
Serial.println("Facilities");
}
if (work[0] == 99) {
Serial.println("CEO");
}
}
/* Wait for new data to arrive */
uint8_t len = readPacket(&ble, BLE_READPACKET_TIMEOUT);
if (len == 0) return;
/* Got a packet! */
// printHex(packetbuffer, len);
// Buttons
if (packetbuffer[1] == 'B') {
uint8_t buttnum = packetbuffer[2] - '0';
boolean pressed = packetbuffer[3] - '0';
Serial.print ("Button "); Serial.print(buttnum);
if (pressed) {
Serial.println(" pressed");
} else {
Serial.println(" released");
}
}
}
Bluetooth Config
// COMMON SETTINGS
// ----------------------------------------------------------------------------------------------
// These settings are used in both SW UART, HW UART and SPI mode
// ----------------------------------------------------------------------------------------------
#define BUFSIZE 128 // Size of the read buffer for incoming data
#define VERBOSE_MODE true // If set to 'true' enables debug output
#define BLE_READPACKET_TIMEOUT 500 // Timeout in ms waiting to read a response
// SOFTWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins that will be used for 'SW' serial.
// You should use this option if you are connecting the UART Friend to an UNO
// ----------------------------------------------------------------------------------------------
//#define BLUEFRUIT_SWUART_RXD_PIN 9 // Required for software serial!
//#define BLUEFRUIT_SWUART_TXD_PIN 10 // Required for software serial!
//#define BLUEFRUIT_UART_CTS_PIN 11 // Required for software serial!
//#define BLUEFRUIT_UART_RTS_PIN -1 // Optional, set to -1 if unused
// HARDWARE UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the HW serial port you are using. Uncomment
// this line if you are connecting the BLE to Leonardo/Micro or Flora
// ----------------------------------------------------------------------------------------------
#ifdef Serial1 // this makes it not complain on compilation if there's no Serial1
#define BLUEFRUIT_HWSERIAL_NAME Serial1
#endif
// SHARED UART SETTINGS
// ----------------------------------------------------------------------------------------------
// The following sets the optional Mode pin, its recommended but not required
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_UART_MODE_PIN -1 // Set to -1 if unused
// SHARED SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for HW and SW SPI communication.
// SCK, MISO and MOSI should be connected to the HW SPI pins on the Uno when
// using HW SPI. This should be used with nRF51822 based Bluefruit LE modules
// that use SPI (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
#define BLUEFRUIT_SPI_CS 8
#define BLUEFRUIT_SPI_IRQ 7
#define BLUEFRUIT_SPI_RST -1 // Optional but recommended, set to -1 if unused
// SOFTWARE SPI SETTINGS
// ----------------------------------------------------------------------------------------------
// The following macros declare the pins to use for SW SPI communication.
// This should be used with nRF51822 based Bluefruit LE modules that use SPI
// (Bluefruit LE SPI Friend).
// ----------------------------------------------------------------------------------------------
//#define BLUEFRUIT_SPI_SCK 13
//#define BLUEFRUIT_SPI_MISO 12
//#define BLUEFRUIT_SPI_MOSI 11
RFID
-----------------------------------------------------------------------------------------
MFRC522 Arduino Arduino Arduino Arduino Arduino
Reader/PCD Uno/101 Mega Nano v3 Leonardo/Micro Pro Micro
Signal Pin Pin Pin Pin Pin Pin
-----------------------------------------------------------------------------------------
RST/Reset RST 9 5 D9 RESET/ICSP-5 RST
SPI SS SDA(SS) 10 53 D10 10 10
SPI MOSI MOSI 11 / ICSP-4 51 D11 ICSP-4 16
SPI MISO MISO 12 / ICSP-1 50 D12 ICSP-1 14
SPI SCK SCK 13 / ICSP-3 52 D13 ICSP-3 15
*/
#include <SPI.h> // RC522 Module uses SPI protocol
#include <MFRC522.h> // Library for Mifare RC522 Devices
int ledPin = 7;
uint8_t successRead; // Variable integer to keep if we have Successful Read from Reader
byte readCard[4]; // Stores scanned ID read from RFID Module
// Create MFRC522 instance.
constexpr uint8_t RST_PIN = 9; // Configurable, see typical pin layout above
constexpr uint8_t SS_PIN = 10; // Configurable, see typical pin layout above
MFRC522 mfrc522(SS_PIN, RST_PIN);
///////////////////////////////////////// Setup ///////////////////////////////////
void setup() {
//Protocol Configuration
Serial.begin(9600); // Initialize serial communications with PC
SPI.begin(); // MFRC522 Hardware uses SPI protocol
mfrc522.PCD_Init(); // Initialize MFRC522 Hardware
//If you set Antenna Gain to Max it will increase reading distance
//mfrc522.PCD_SetAntennaGain(mfrc522.RxGain_max);
Serial.println(F("Access Control Example v0.1")); // For debugging purposes
ShowReaderDetails(); // Show details of PCD - MFRC522 Card Reader details
// Check if master card defined, if not let user choose a master card
// This also useful to just redefine the Master Card
// You can keep other EEPROM records just write other than 143 to EEPROM address 1
// EEPROM address 1 should hold magical number which is '143'
Serial.println(F("-------------------"));
Serial.println("");
Serial.println(F("-------------------"));
Serial.println(F("Everything is ready"));
Serial.println(F("Waiting PICCs to be scanned"));
}
void loop () {
successRead = getID(); // sets successRead to 1 when we get read from reader otherwise 0
}
uint8_t getID() {
// Getting ready for Reading PICCs
if ( ! mfrc522.PICC_IsNewCardPresent()) { //If a new PICC placed to RFID reader continue
return 0;
}
if ( ! mfrc522.PICC_ReadCardSerial()) { //Since a PICC placed get Serial and continue
return 0;
}
// There are Mifare PICCs which have 4 byte or 7 byte UID care if you use 7 byte PICC
// I think we should assume every PICC as they have 4 byte UID
// Until we support 7 byte PICCs
Serial.println(F("Scanned PICC's UID:"));
for ( uint8_t i = 0; i < 4; i++) { //
readCard[i] = mfrc522.uid.uidByte[i];
Serial.print(readCard[i], HEX);
// Serial.print(readCard[0], HEX);
// Serial.print(readCard[1], HEX);
// Serial.print(readCard[2], HEX);
// Serial.print(readCard[3], HEX);
}
Serial.println("");
////////////////// COMPARE RFID FOR INPUT AND OUT PUT ///////////////////////
String idNum = String(readCard[1]);
Serial.println("RFID IS: " +idNum);
Serial.println("");
Serial.println("");
Serial.println("START COMPARISON");
if (idNum == "234") {
Serial.println("this is id1");
digitalWrite(ledPin, HIGH);
}
if (idNum == "237") {
Serial.println("this is id2");
digitalWrite(ledPin, HIGH);
}
if (idNum == "59") {
Serial.println("this is id3");
digitalWrite(ledPin, HIGH);
}
//
// else if (idNum == "237") {
// Serial.println("this is 89");
// digitalWrite(ledPin, LOW);
// }
else {
Serial.println("SCAN AGAIN");
}
Serial.println("END COMPARISON");
Serial.println("");
Serial.println("");
//
mfrc522.PICC_HaltA(); // Stop reading
return 1;
}
void ShowReaderDetails() {
// Get the MFRC522 software version
byte v = mfrc522.PCD_ReadRegister(mfrc522.VersionReg);
Serial.print(F("MFRC522 Software Version: 0x"));
Serial.print(v, HEX);
if (v == 0x91)
Serial.print(F(" = v1.0"));
else if (v == 0x92)
Serial.print(F(" = v2.0"));
else
Serial.print(F(" (unknown),probably a chinese clone?"));
Serial.println("");
// When 0x00 or 0xFF is returned, communication probably failed
if ((v == 0x00) || (v == 0xFF)) {
Serial.println(F("WARNING: Communication failure, is the MFRC522 properly connected?"));
Serial.println(F("SYSTEM HALTED: Check connections."));
// Visualize system is halted
while (true); // do not go further
}
}