dfr0258 and softwareserial.h
![userHead](https://dfimg.dfrobot.com/dfPortrait2.png)
I am having some issues with this shield. I need to some RFID tags from the serial interface. using standard code on the serial I do read the tags.(I will post a copy of the working code beneath as “Working Code”
When I add some more things to the arduino Mega, like RTC, SD CARD, and GSM Modules, I need to use softwareserial.h and then I cannot get this to read the RFID UID'd anymore.
I suspect that I need to define pins for the RFID to be read, as I now have multiple serials to use, as you wil see below in “Non Working Code”.
I cannot figure out what the pins are for the shield on which to read the Serial communications on the RS232.
Can some one please help.
Working Code:
/*
# This code assumes that the DFR0258 Serial Shield is used.
# Editor : Anton Kading
# Date : 2023.04.12
# Ver : 0.1
# Product: RS232 shield
# SKU : DFR0258
# RTC: :DFROBOT 1307
# SKU :DFR0151
{
Serial.begin(9600); //init serial
pinMode(led,OUTPUT);
}
void loop()
{
int temp; //serial data temporary cache
if(Serial.available()) //if serial receives data
{
temp=Serial.read(); //store the received data temporarily
if(temp=='V'){
digitalWrite(led,1-digitalRead(led)); //change the LED statu if receiving the char "V".
Serial.println(temp); //reply OK to show that the char "V" has been received and the LED statu also has been changed
}
}
}
*/
char incomingRFID; // Access storage to store incoming RFID tokens
void setup() {
Serial.begin(9600); //init serial
}
void loop()
{
while (Serial.available() > 0) {
incomingRFID = Serial.read();
Serial.print(incomingRFID);
//Serial.print(' ');
}
Serial.println();
delay(1000);
}
Non WORKING CODE:
// Required libraries
#include <Wire.h>
#include <SoftwareSerial.h>
#include <RTClib.h>
#include <SD.h>
#include <SPI.h>
// Pin definitions
#define RFID_RX_PIN 3
#define RFID_TX_PIN 2
#define SD_CS_PIN 53
#define LED_PIN 13
// Create the software serial ports
SoftwareSerial rfidSerial(RFID_RX_PIN, RFID_TX_PIN); // RFID reader connection
SoftwareSerial gsmSerial(2, 3); // GSM module connection
SoftwareSerial mySerial(10, 11); // Alternative serial connection for debugging
// Instantiate the Real Time Clock object
RTC_DS1307 rtc;
// File object to interact with the SD card
File dataFile;
// Location of the sheep farm
char location[] = "Sheep Farm 1";
int rfidStartTime = 5;
int rfidENDtime = 19;
int GSMsmsTime = 20;
int GSMfileSEND = 21;
// Variable to store unique RFID entries count
unsigned int Daily_Sheep_Count = 0;
void setup() {
// Set the LED pin as output
pinMode(LED_PIN, OUTPUT);
// Set up the serial connections
Serial.begin(9600);
rfidSerial.begin(9600);
gsmSerial.begin(9600);
// Initialize the I2C communication and RTC
Wire.begin();
rtc.begin();
// Initialize the SD card
SD.begin(SD_CS_PIN);
// Set the time if RTC is not running
if (!rtc.isrunning()) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Configure GSM module
gsmSerial.begin(9600);
delay(1000);
gsmSerial.println("AT+CMGF=1"); // Set to text mode
delay(1000);
gsmSerial.println("AT+CNMI=2,2,0,0,0"); // Route SMS to serial
delay(1000);
}
void loop() {
// Call the function to process sheep tags
processSheepTags();
// Get the current time
DateTime now = rtc.now();
// Read sheep tags during daytime (between 5:00 and 19:00 as defined by the integers above)
if (now.hour() >= rfidStartTime && now.hour() < rfidENDtime) {
readSheepTags();
} else {
// Process sheep tags and sleep for 24 hours during nighttime
processSheepTags();
delay(24 * 60 * 60 * 1000); // Sleep for 24 hours
}
// Check if it's 20:00 in the evening
if (now.hour() == GSMsmsTime && now.minute() == 0) {
GSM_SMS(Daily_Sheep_Count); // Send SMS at 20:00
delay(60000); // Wait for 1 minute to avoid sending multiple times at 20:00
}
// Check if it's 21:00
if (now.hour() == GSMfileSEND && now.minute() == 0) {
GSM_FILE_TRANSFER();
delay(60000); // Wait for a minute to avoid multiple transfers in a minute
}
delay(1000);
}
void readSheepTags() {
// Read RFID tags from the RFID reader
if (rfidSerial.available()) {
Serial.println("DEbug A");//...........................................................Debug Point
char incomingRFID = rfidSerial.read();
Serial.print(incomingRFID);
// If the incoming RFID tag is valid (starts with 'V')
if (incomingRFID == 'V') {
digitalWrite(LED_PIN, !digitalRead(LED_PIN)); // Toggle the LED
// Get the current time
DateTime now = rtc.now();
// Create a file name with the current date (e.g., "ddd.txt")
char fileName[20];
sprintf(fileName, "RawRFIDTAgs.txt");
// Open the file in write mode
dataFile = SD.open(fileName, FILE_WRITE);
// If the file is opened successfully, write the data
if (dataFile) {
dataFile.print(now.unixtime());
dataFile.print(", ");
dataFile.println(incomingRFID);
dataFile.close();
} else {
Serial.println("Failed to open the file");
}
}
}
}
void processSheepTags() {
// Open the file containing raw RFID tags
File dataFile = SD.open("RawRFIDTAgs.txt");
// Process the file if it's opened successfully
if (dataFile) {
String currentTag = "";
String prevTag = "";
Daily_Sheep_Count = 0;
// Read the file line by line
while (dataFile.available()) {
char ch = dataFile.read();
// When a new line is encountered, process the current tag
if (ch == '\n') {
if (currentTag != prevTag) {
Daily_Sheep_Count++; // Increment the sheep count
prevTag = currentTag;
}
currentTag = "";
} else {
currentTag += ch;
}
}
// Check for the last tag in the file
if (currentTag != prevTag) {
Daily_Sheep_Count++;
}
// Close the file
dataFile.close();
// Print the daily sheep count
Serial.print("Daily Sheep Count: ");
Serial.println(Daily_Sheep_Count);
} else {
Serial.println("1 Error opening RawRFIDTAgs.txt");
}
}
void GSM_SMS(int Daily_Sheep_Count) {
// Send an SMS with the daily sheep count
mySerial.print("AT+CMGS=\"+31615553214\"\r");
delay(1000);
mySerial.print("Daily sheep count: ");
mySerial.print(Daily_Sheep_Count);
mySerial.write(26); // End the message with Ctrl+Z
delay(1000);
}
void GSM_FILE_TRANSFER() {
// Send the file containing raw RFID tags using GSM module. Still need to incorporate Username and Password
gsmSerial.println("AT+CGSOCKCONT=1,\"IP\",\"internet\"");
delay(2000);
gsmSerial.println("AT+CSOCKSETPN=1");
delay(2000);
gsmSerial.println("AT+CIPMODE=0");
delay(2000);
gsmSerial.print("AT+NETOPEN=,,\"");
gsmSerial.write(34);
gsmSerial.print("192.168.178.91");
gsmSerial.write(34);
gsmSerial.println(",80");
delay(2000);
gsmSerial.println("AT+CIPOPEN=0,\"TCP\",\"192.168.178.91\",80");
delay(2000);
gsmSerial.println("AT+CIPSEND=0");
// Open the file containing raw RFID tags
File dataFile = SD.open("RawRFIDTAgs.txt", FILE_READ);
// Send the file if it's opened successfully
if (dataFile) {
while (dataFile.available()) {
gsmSerial.write(dataFile.read());
}
dataFile.close();
} else {
Serial.println("2 Error opening RawRFIDTAgs.txt");
}
gsmSerial.println("AT+CIPCLOSE=0");
delay(2000);
gsmSerial.println("AT+NETCLOSE");
delay(2000);
}
What power supply are you using? Insufficient current/voltage often makes these GSM modules malfunction.
![userHeadPic](https://dfimg.dfrobot.com/dfPortrait2.png)
After checking the Schemetic for the Shield.
I think the RS232 is direct connect to the Hardware Serial of your Arduino, if that's what you asking.
![userHeadPic](https://dfimg.dfrobot.com/dfPortrait2.png)
![p.name](https://dfimg.dfrobot.com/data/DFR0258/IMG_0154 en.jpg)