Can rfid, sd card and ble be used at the same time? I have 3 different folders in the SD card module and my files in these folders. With Ble, I get data from each folder with a file name (data1.txt, data2.txt, data3.txt). Then, I want to access the data in that file, whichever of the 3 different tags was read with RFID (tag1 = folder1).
While RFID and BLE are running in core 1, I need to get the data in the SD card from core 0. But I can't access the SD card. I'm kind of new to this stuff. My modules work when I use them individually or in pairs. There is no problem with the modules or connections. I might have made a stupid mistake. I'm open to any help. Thanks.. I shared my code below.
#include <MFRC522.h> // for the RFID
#include <SPI.h> // for the RFID and SD card module
#include <SD.h> // for the SD card
#include "FS.h"
#include "Arduino.h"
#include "Audio.h"
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLEDevice.h>
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
// define pins for RFID
#define CS_RFID 21
#define RST_RFID 22
// define select pin for SD card module
#define CS_SD 5
//#define SPI_MOSI 23
//#define SPI_MISO 19
//#define SPI_SCK 18
#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRC 26
// Instance of the class for RFID
MFRC522 rfid(CS_RFID, RST_RFID);
// Variable to hold the tag's UID
String uidString;
String file1=,file2=,file3;
int x=0,y=0,z=0,EndOfFile=0;
Audio audio;
// Create a file to store the data
File myFile;
TaskHandle_t Task1;
//---- Split Files Path ----
void SplitFilesPath(String file_path){
...
}
class MyCallbacks: public BLECharacteristicCallbacks {
std::string RxFile = "";
void onWrite(BLECharacteristic *pCharacteristic)
{
std::string rxValue = pCharacteristic->getValue() ;
Serial.print("rxValue : ");
Serial.println(rxValue.c_str());
if (rxValue.length() > 0)
{
//RxFile += rxValue;
for (int i=0;i<rxValue.length();i++)
{
RxFile += rxValue[i];
}
if (rxValue.length()<20)
{
Serial.print("RxFile : ");
Serial.print(RxFile.c_str());
SplitFilesPath(RxFile.c_str());
RxFile = "";
}
}
}
};
void setup() {
// Init Serial port
pinMode(CS_SD, OUTPUT);
Serial.begin(115200);
// Init SPI bus
SPI.begin();
// Init MFRC522
rfid.PCD_Init();
SD.begin(CS_SD);
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(10); // 0...21
myFile = SD.open("/");
printDirectory(myFile, 0);
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task1code, // Task function.
"Task1", // name of task.
4096, // Stack size of task
NULL, // parameter of the task
0, // priority of the task
&Task1, // Task handle to keep track of created task
0); // pin task to core 0
}
void printDirectory(File dir, int numTabs) {
...
}
//Task1code: Read RFID and Start BLE
void Task1code( void * pvParameters ){
Serial.println("Ble Connected....");
BLEDevice::init("HzYusuf");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Anasinun sutiylan adam olmayana sigir ne etsun");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
for(;;){
if(rfid.PICC_IsNewCardPresent()){
rfid.PICC_ReadCardSerial();
uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " +
String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
Serial.println(uidString);
}
else{
uidString = "";
}
//vTaskDelay(3 / portTICK_PERIOD_MS);
}
}
void loop() {
...
}