Go Down

Topic: Using strcat to construct a variable URL/ adaptable url for php request (Read 10352 times) previous topic - next topic

SurferTim

What is the last message displayed on the serial monitor after the fail?

GoForSmoke

Instead of loading a buffer with a big string and printing it out, why not just print each piece in turn? If you need to print it to two devices then print each piece to each device before the next.

The concatenation will occur in the serial transmissions. Those could be done 1 byte at a time and still the Arduino would be idle _most_ of the time. You save the ram that may be crashing your sketch for no sacrifice except waste.

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

C220amg

Actually, there is no fail, it just keeps printing 0's from the rfid_val to the serial monitor after connecting and uploading. It just doesn't allow me to read a tag and thus doesn't start to connect to the server.

PaulS

Code: [Select]
    if (rfid_val == 1|rfid_val==2){
This is probably NOT doing what you think it is. Big difference between | and ||.
The art of getting good answers lies in asking good questions.

GoForSmoke

1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

nickgammon

Code: [Select]
  while(Serial.available()){

    int readByte = Serial.read(); //read next available byte
...
  }


You are probably going to read one byte here ... unless there was a previous delay, which you had in setup.

http://gammon.com.au/serial
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

C220amg


Code: [Select]
    if (rfid_val == 1|rfid_val==2){
This is probably NOT doing what you think it is. Big difference between | and ||.


Thanks, dumb mistake! Though it didn't solve my problem yet, is it possible there is some sort of while loop it doesn't get out from or whatever?

Maybe I need to say it only reads an rfid tag right after having it reset, when the program runs it just doesn't, even when it hasn't read something yet.

Niels,

PaulS

Quote
Maybe I need to say it only reads an rfid tag right after having it reset, when the program runs it just doesn't, even when it hasn't read something yet.

Then, you need to add more Serial.print() statements, to find out why the function is not being called.
The art of getting good answers lies in asking good questions.

C220amg

If i put different Serial.print() command it only seems to run the void .... sequences in my program.

PaulS

Quote
If i put different Serial.print() command it only seems to run the void .... sequences in my program.

There are no "void ... sequences" in your program. There are functions, some of which have a void return type.

Post your code!
The art of getting good answers lies in asking good questions.

C220amg

This is the code i'm using.

Code: [Select]
#include <SPI.h>
#include <Ethernet.h>


IPAddress ip( );
IPAddress gateway( );
IPAddress subnet( );
byte mac[] = {  };
IPAddress server( );


EthernetClient client;
int totalCount = 0;
int loopCount = 0;
int connectLed=5;
int buttonState=0;

int connectedToServer=6;


/////////////////RFID/////////////////
int RFIDResetPin = 13;
int rfid_val;
//Register your RFID tags here
char tag1[13] = "4500530E9F87";
char tag2[13] = "450052E8E51A";

int led1=2;
int led2=3;

void setup() {
  Serial.begin(9600);

  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);
  Ethernet.begin(mac, ip, gateway, gateway, subnet);
  delay(2000);
  Serial.println("Ready");

  /////////////////RFID/////////////////
   
  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
 
  pinMode(connectedToServer, OUTPUT);
}

void loop()
{

////////////////Ethernet////////
    Serial.print("RFID TAG ");
    Serial.println(rfid_val);
 
    if (rfid_val == 1||rfid_val==2){
    digitalWrite(connectLed, HIGH);
    Serial.print("If statement works");
    if(!getPage(server,"/add_db.php")) Serial.print("Fail ");
    else Serial.print("Pass ");
   
   
    Serial.println(totalCount,DEC);
   
  }
 
   else {
    // turn LED off:
    digitalWrite(connectLed, LOW);
 
}
 
    /////////////////RFID/////////////////
 
  char tagString[13];
  int index = 0;
  boolean reading = false;

  while(Serial.available()){
    Serial.println("Serial.available");
    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag
    Serial.print(readByte);
    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      Serial.println("(reading && readByte != 2 && readByte != 10 && readByte != 13)");
      index ++;
      Serial.print("index");Serial.println(index);
    }
  }
 
 
  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //eset the RFID reader

}

byte getPage(IPAddress ipBuf,char *page)
{
  int inChar;
  char outBuf[64];

  Serial.print("connecting...");

  if(client.connect(ipBuf,80))
  {
    Serial.println("connected");
   
   



    sprintf(outBuf,"GET /add_db.php?Rfid=%u HTTP/1.0\r\nHost: www.**************.nl\r\n\r\n",rfid_val);
    client.write(outBuf);
   
  }
   
 
  else
  {
    Serial.println("failed");
    return 0;
  }

  while(client.connected())
  {
    while(client.available())
    {
      inChar = client.read();
      Serial.write(inChar);
    }
    digitalWrite(connectedToServer, HIGH);
    delay(500);
    digitalWrite(connectedToServer, LOW);
  }

  Serial.println();

  Serial.println("disconnecting.");
  client.stop();

  return 1;
}

/////////////////RFID/////////////////
void checkTag(char tag[]){
   Serial.println("begin of void checkTag");
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0){
   digitalWrite(led1,LOW);
   digitalWrite(led2,LOW);
   rfid_val=0;
    //return; //empty, no need to contunue
  }
  if(compareTag(tag, tag1)){ // if matched tag1, do this
     rfid_val=1;
     digitalWrite(led1, HIGH);
     delay(2000);
     
  }

   
  if(compareTag(tag, tag2)){ //if matched tag2, do this
     rfid_val=2;
     digitalWrite(led1, HIGH);
     delay(2000);
  }

  Serial.println("end of void checkTag");
}

//void lightLED(int pin){
/////////////////////////////////////
////Turn on LED on pin "pin" for 250ms
/////////////////////////////////////
//  Serial.println(pin);
//
//  digitalWrite(pin, HIGH);
//
//}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(200);
  Serial.println("End of void resetReader");
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
     Serial.print("void clearTag i value ");
    Serial.println(i);
  }
  Serial.println("clearTag ");
  Serial.println();

}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  if(strlen(one) == 0) return false; //empty

  for(int i = 0; i < 12; i++){
    if(one[i] != two[i]) return false;
    Serial.print("boolean compareTag i value ");
    Serial.println(i);
  }


  return true; //no mismatches
}





Thanks,
Niels

PaulS

How is the RFID reader really connected to the Arduino? You seem to be using Serial to read from/write to the Serial Monitor AND to read from the RFID reader. You can't do that.
The art of getting good answers lies in asking good questions.

C220amg


How is the RFID reader really connected to the Arduino? You seem to be using Serial to read from/write to the Serial Monitor AND to read from the RFID reader. You can't do that.


Well, that rather strange because I did combine two programs, an ethernet part together with a rfid part.
This is the rfid code only, which works. This code continuously prints the a 0, a 1 or a 2.

0 for no rfid
1 for rfid 1
and
2 for rfid 2

Code: [Select]
int RFIDResetPin = 13;
int rfid_val;
//Register your RFID tags here
char tag1[13] = "4500530E9F87";
char tag2[13] = "450052E8E51A";

int led1=2;
int led2=3;

void setup(){
  Serial.begin(9600);

  pinMode(RFIDResetPin, OUTPUT);
  digitalWrite(RFIDResetPin, HIGH);

  //ONLY NEEDED IF CONTROLING THESE PINS - EG. LEDs

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

void loop(){
 
  char tagString[13];
  int index = 0;
  boolean reading = false;

  while(Serial.available()){

    int readByte = Serial.read(); //read next available byte

    if(readByte == 2) reading = true; //begining of tag
    if(readByte == 3) reading = false; //end of tag

    if(reading && readByte != 2 && readByte != 10 && readByte != 13){
      //store the tag
      tagString[index] = readByte;
      index ++;
    }
  }
  Serial.println(rfid_val);
  checkTag(tagString); //Check if it is a match
  clearTag(tagString); //Clear the char of all value
  resetReader(); //eset the RFID reader
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

  if(strlen(tag) == 0){
   digitalWrite(led1,LOW);
   digitalWrite(led2,LOW);
   rfid_val=0;
    return; //empty, no need to contunue
  }
  if(compareTag(tag, tag1)){ // if matched tag1, do this
     rfid_val=1;
     digitalWrite(led1, HIGH);
     delay(2000);
  }

   
  if(compareTag(tag, tag2)){ //if matched tag2, do this
     rfid_val=2;
     digitalWrite(led1, HIGH);
     delay(2000);
  }

 
}

//void lightLED(int pin){
/////////////////////////////////////
////Turn on LED on pin "pin" for 250ms
/////////////////////////////////////
//  Serial.println(pin);
//
//  digitalWrite(pin, HIGH);
//
//}

void resetReader(){
///////////////////////////////////
//Reset the RFID reader to read again.
///////////////////////////////////
  digitalWrite(RFIDResetPin, LOW);
  digitalWrite(RFIDResetPin, HIGH);
  delay(200);
}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
  for(int i = 0; i < strlen(one); i++){
    one[i] = 0;
  }
}

boolean compareTag(char one[], char two[]){
///////////////////////////////////
//compare two value to see if same,
//strcmp not working 100% so we do this
///////////////////////////////////

  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
}

PaulS

Why not use SoftwareSerial to read the RFID, using two other pins?
The art of getting good answers lies in asking good questions.

nickgammon

He can put the RFID reader on the Rx pin, and leave the Tx pin free for debugging.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up