Hello. I found an opensource code for an alarm project here. I am trying to make it work without the GPS part but it doesn't work properly.
The RFID code part works fine, but when i add the Cell code it stops working. Both RFID and GSM Shield are started with Serial.begin function, then the RFID code starts and then the cell code starts. But instead of working properly the RFID Reader doesn't detect the tag and the cell shield doesn't connect to the network.
I changed the code so that GSM Shield is initialized after the RFID procedure and it works but there are still some glitches (if RFID tag isn't detected the cell shield does not connect to the network and can't send an alarm SMS). I also had to change all NewSoftSerial references to SoftwareSerial becouse i use Arduino IDE 1.0.5.
Your code structure is horrid. While there are different styles regarding whether the open curly brace goes on the same line as the statement (it does not!) or on a new line (my preference), there is NO disagreement regarding what follows the } at the end of the block. NOTHING! Not an else statement!
Calling cellSetup() after possibly not calling cell.begin() is silly. In that function, you try to read from the possibly non-initialized cell instance.
The call to cell.begin() needs to be made every time. Or, you need to set some flag that says not to use the cell instance (or call any cellXxx functions).
Starting a function somewhere other than column 0 is laziness. It means that you have not returned the cursor to the start of the line, and that the IDE put it there because your indentation is screwed up before that point.
I left the original code intact except a few thing: i removed the GPS related code and i changed NewSoftSerial to SoftwareSerial because it is now included in Arduino IDE 1.0.5.
When you say the code structure is horrid you refer to the original code ? Practicaly i haven't changed it's structure. This is the first part of the code with declarations, shield initialization , setup and loop.
#include <SoftwareSerial.h>
#define CELL_RX_PIN 2
#define CELL_TX_PIN 3
#define RFID_RX_PIN 8
#define RFID_RESET_PIN 10
#define RFID_TX_PIN 12
#define LED_PIN 13
#define RFID_TAG_ALLOWED "4D006AB2F762"
#define ACCEPT_FROM_PHONE_1 "0726025259"
#define ACCEPT_FROM_PHONE_2 "0721228886"
#define SEND_TO_PHONE "0733645413"
// RFID ===============================================================
#define TIME_TO_WAIT_FOR_TAG_IN_MS 30000
SoftwareSerial rfid = SoftwareSerial(RFID_RX_PIN, RFID_TX_PIN); // changed to SoftwareSerial
// RFID tags allowed
char tag1[13] = RFID_TAG_ALLOWED;
// CELL ===============================================================
#define ACTUALLY_SEND_MESSAGE 1
SoftwareSerial cell = SoftwareSerial(CELL_RX_PIN, CELL_TX_PIN); // changed to SoftwareSerial
const int allowedOriginSize = 2;
String allowedOrigin[allowedOriginSize] = {
ACCEPT_FROM_PHONE_1, ACCEPT_FROM_PHONE_2 };
// from millis();
unsigned long lastSmsSent = 0;
int cellErrors = 0;
#define CELL_MAX_ERRORS 10 // Number of errors before reseting the cell shield.
// ALARM STATE ========================================================
boolean alarm = true;
boolean ping = false;
String sendSmsTo = SEND_TO_PHONE;
// This can be tuned via SMS too.
unsigned long alarmFrequencyInMs = 300000; // 5 min.
unsigned long updateFrequencyInMs = 60000; // 1 min.
// ====================================================================
void setup() {
//Initialize serial ports for communication.
Serial.begin(9600); // connect to the serial port
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// RFID
rfid.begin(9600);
// CELL
cell.begin(9600);
digitalWrite(LED_PIN, LOW);
unsigned long start = millis();
Serial.println("Starting RFID");
// Try to read a valid RFID tag for a few seconds.
while (millis() - start < TIME_TO_WAIT_FOR_TAG_IN_MS && alarm) {
alarm = !RFIDread();
}
if (alarm) {
// Uh oh...
Serial.println("No RFID Tag found");
} else {
Serial.println("RFID OK");
}
digitalWrite(LED_PIN, HIGH);
CELLsetup();
digitalWrite(LED_PIN, LOW);
DEBUGstate();
}
void loop() {
CELLturn();
DEBUGstate();
}
...
rest of the code with cell functions and rfid
It doesn't work because of what i modified or is the original code bad?
I attached the whole code below.
It doesn't work because of what i modified or is the original code bad?
The code you posted does something. You want it to do something. You have not defined either one. You have not defined what "It doesn't work" means.
We have hired a psychic. His start date is the 12th of never. If you can't wait that long, you need to provide more info.
Keep in mind that only once instance of SoftwareSerial can listen at any given time. You'll have a hard time listening to a cell phone and the RFID at the same time. If the cell phone is needed only to send messages, you should be listen()ing to the RFID instance until it it necessary to send a message. Then, you should resume listen()ing to the RFID instance.
Ok.
The code should do something like this:
//alarm state = true
Check for RFID tag and if tag is detected in 30 second then change "alarm" state to false. Else leave alarm = true.
Start cell setup (connect to network, reads/delete new messages, check if i want a ping, check if alarm = true and it needs to send a message.
I know only one instance of SoftwareSerial can run at a time. Practicaly RFID needs to run first and only for 30 seconds, then Cell needs to check if a message should be sent. The rest of the code is for reading/analizing/deleting messages so i can enable/disable the alarm or ask for ping via sms. The original project had gps too so you could track your car via sms at any time.
When i say it doesn't work i mean Cell functions doesn't work.
I just removed the rfid code too, and it doesn't connect to network.
Thank you for your advice. It realy helped me.
The original gsm shield was faulty and i had to replace it. I used a simple "send sms" code from this forum and the rfid code.
I had to listen to the serial ports one at a time. I will try to modify the original code as well.
#include <SoftwareSerial.h>
#define RFID_TAG_ALLOWED "4D006AB2F762"
#define CELL_RX_PIN 2
#define CELL_TX_PIN 3
#define RFID_RX_PIN 8
#define RFID_RESET_PIN 9
#define RFID_TX_PIN 12
// CELL ===============================================================
SoftwareSerial cell(CELL_RX_PIN,CELL_TX_PIN);
char* gsm_number = "0040722647413";
String sms_text = "test2";
// RFID ===============================================================
#define TIME_TO_WAIT_FOR_TAG_IN_MS 30000
SoftwareSerial rfid(RFID_RX_PIN,RFID_TX_PIN);
// RFID tags allowed
char tag1[13] = RFID_TAG_ALLOWED;
boolean alarm = true;
void setup() {
//Initialize serial ports for communication.
Serial.begin(9600); // connect to the serial port
// RFID
rfid.begin(9600);
// CELL
cell.begin(9600);
rfid.listen();
unsigned long start = millis();
Serial.println("Read RFID Tag");
// Try to read a valid RFID tag for a few seconds.
while (millis() - start < TIME_TO_WAIT_FOR_TAG_IN_MS && alarm) {
alarm = !RFIDread();
}
cell.listen();
if (alarm) {
// Uh oh...
Serial.print("NO");
send_sms();
} else {
Serial.println("OK");
}
}
void loop() {
}
// RFID =========================================================================
boolean RFIDread()
{
char tagString[13] = "";
int index = 0;
boolean reading = false;
while(rfid.available()){
int readByte = rfid.read(); //read next available byte
if(readByte == 2) {
reading = true; //beginning of tag
index = 0;
} else if(readByte == 3) {
reading = false; //end of tag
}
if(reading && readByte != 2 && readByte != 10 && index < 12){
//store the tag
tagString[index] = readByte;
index ++;
}
}
boolean ok = RFIDcheckTag(tagString); //Check if it is a match
RFIDclearTag(tagString); //Clear the char of all value
RFIDresetReader(); //reset the RFID reader
return ok;
}
boolean RFIDcheckTag(char tag[]) {
if(strlen(tag) == 0) return false;
Serial.println(tag);
if(RFIDcompareTag(tag, tag1)) {
return true;
}
return false;
}
void RFIDresetReader() {
digitalWrite(RFID_RESET_PIN, LOW);
digitalWrite(RFID_RESET_PIN, HIGH);
delay(500);
}
void RFIDclearTag(char one[]) {
for(int i = 0; i < strlen(one); i++){
one[i] = 0;
}
}
boolean RFIDcompareTag(char one[], char two[]) {
if(strlen(one) == 0) return false; //empty
for(int i = 0; i < 12; i++){
if(one[i] != two[i]) return false;
}
return true; //no mismatches
}
void send_sms()
{
Serial.println("start sms send ...");
cell.println("AT"); //query gsm shield status, make sure we are ready to go
delay(1000); //allow time to process
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(1000); //allow time to process
cell.print("AT+CMGS=");
cell.write((byte)34); // ASCII equivalent of ”
cell.print(gsm_number); //use international format: 0040742900800
cell.write((byte)34); // ASCII equivalent of ”
cell.println();
delay(200); // give the module some thinking time
cell.print(sms_text); // “sms content”
cell.write((byte)26); //ASCII equivalent of Ctrl-Z, tell the gsm module we finished the message transmission
cell.println();
delay(15000); // wait the OK status, usually a sms send takes up to 10-12 sec, but we add an extra 3-5 sec for safety
Serial.print("end SMS send");
}