Wireless communication with ID-12LA RFID reader


#1

Hi,

Currently I’m trying to get a ID-12LA RFID reader to work with my spark core. This is not very difficult, since the communication goes through the serial port. The code needed for that is the following:

char val = 0; // variable to store the data from the serial port

void setup() {
  Serial.begin(9600); // connect to the serial port
}

void loop () {
  // read the serial 
  
  if (Serial.available() > 0) {
    val = Serial.read();
    Serial.print(val);
  }
}

However, i want to access the RFID tag through the cloud, but since the serial communication goes through the RX port that is not possible. Is there someone who knows how to fix this, so i can access the RFID tag ID’s through the Spark Cloud?

Regards


#2

You could expose the variables using Spark.variable() by polling repeatedly. The more elegant solution would be using Spark.publish() which allows you to push the ID as soon as it’s scanned, making it as real-time as possible.
Take a look at these topics to get a feeling on how to use either:




Let me know if that helped and/or you need additional help.
Good luck!


#3

@KLFSBK

Seems your case is like this (from the documentation):

Imagine you have a temperature sensor attached to the A0 pin of your Spark Core and your firmware has exposed the value of the sensor as a Spark variable.

See docs (Cloud code API section).


#4

Thanks @Moors7 for your reply. The thing is that i do not succeed in reading the RX port through the cloud. I think this is only possible by using a serial USB connection. I know how to publish and use variables using curl etc, but in this case i won’t succeed. I’m using this code specific for the spark core, but it doesn’t work over the cloud. Hope you have any suggestions.

char rfid = 0;

void setup()
{
    Serial.begin(9600);
 Spark.variable("mess", rifd, STRING);
}

void loop()
{
  if (Serial.available() > 0) {
  rfid = Serial.read();
  Serial.write(rfid); 
}
}

#5

Hi @KLFSBK

You have some type problems in your code. If you are trying to send only one character over the cloud (which is strange) you can try this:

char rfid[2] = "T";  //leave room for terminating null

void setup()
{
    Serial.begin(9600);
    Spark.variable("mess", rifd, STRING);
}

void loop()
{
  if (Serial.available() > 0) {
  rfid[0] = Serial.read();  //one character only
  Serial.write(rfid); 
  }
}

Otherwise you probably want to have rfid[] be larger, the length of your read string plus one for the terminating 0 and build into it when serial data is available.

I don’t see how you know when you are done reading serial data, but when you are done, you can signal the cloud using Spark.publish().


#6

@KLFSBK, from the looks of it, you want the Core to simply relay each character read from the serial port to the cloud. Instead, have the Core read the entire RFID from the serial port and publish it to the cloud. :smile:

UPDATE: You may have missed the fact that the Core has 2 other Serial ports to which you can connect the reader!


#7

It getting weirder and weirder to me! @peekay123 great tip, indeed that should be not serial.begin().

@bko If i upload your code i get some errors with the tip of @peekay123 included , and even if I fix these errors stil it is not possible to acces the RFID tag id. I changed the char into an int, both at the declaration and the spark.variable(), and if i do so, i can see the int changing over the cloud when I use different tags. So, the issue is definitely in parsing the STRING to the cloud, but still no solution.

This is the code i’m currently using; the spark web IDE does not detect any issues, but when i try to access the variables says: ok; false, error; variables not found.

char rfid[13];  //leave room for terminating null

void setup()
{
    Serial1.begin(9600);
}

void loop()
{
  if (Serial1.available() > 0) {
  rfid[0] = Serial1.read();  //one character only
  rfid[1] = Serial1.read();  //one character only
  rfid[2] = Serial1.read();  //one character only
  rfid[3] = Serial1.read();  //one character only
  rfid[4] = Serial1.read();  //one character only
  rfid[5] = Serial1.read();  //one character only
  rfid[6] = Serial1.read();  //one character only
  rfid[7] = Serial1.read();  //one character only
  rfid[8] = Serial1.read();  //one character only
  rfid[9] = Serial1.read();  //one character only
  rfid[10] = Serial1.read();  //one character only
  rfid[11] = Serial1.read();  //one character only
  }
      Spark.variable("rfid", &rfid, STRING);

}



#8

I could be mistaken, but increasing the size of your array won’t have a lot of effect if you’re only ever writing to the first position? You’ll want to iterate over the positions, assigning new values to each, thus creating the whole array. Also, if the tag is 12 digits long, you should create a [13] array, since you’ll need room for the terminator, like @bko mentioned.


#9

Hi @KLFSBK

Your current code still has potential problems. I think the thing you are missing is that char array strings in C and Arduino Wiring need one extra byte at the end to hold a zero value. If you don’t have that zero byte at the end, string operations will continue to read off the end of the string until they hit a random zero in memory. This can cause your program to crash very quickly. In the code above, you are depending on rfid[] being initialized to all zeros by the compiler which is not really a good practice.

Once you code crashes, the core may decide to run the backup firmware which is typically Tinker, the default app.
Tinker has no variables, just functions.

As @peekay123 says, if you build up a char array with the data from the RFID reader and then send it to the cloud, it will work a lot better.


#10

@KLFSBK, to add to @bko’s comments, Spark.variable() is declared in setup() ONCE and then you modify the variable referred. The way you have it in loop() will cause a problem. Look at this code on Arduino Playground to give you some ideas.


#11

I’ve id-12LA, which the right connection to core?
Is there a schema?

thanks


#12

@blondie63, are you using an ID-12LA shield or just the module? There is no Spark library for this device at this time.

Do you mean schematic?


#13

I’ve id-12la module and also Rfid usb adapter, i’ve tested using serial over usb and it work fine
Now i don’t know if use module alone or not…
Looking the datasheet (http://dlnmh9ip6v2uc.cloudfront.net/datasheets/Sensors/ID/ID-2LA,%20ID-12LA,%20ID-20LA2013-4-10.pdf) i see D0 is Rx data
Can i try to connect it directly to serial core pin ?


#14

@blondie63, if you power the ID-12LA with 3.3v from the Core and share a common GND then you can connect the D0 to the Rx pin of the Core. You will NOT be able to power any relays or other I/O on the ID-12LA since the 3.3v of the Core is not able to supply enough current. :smile:


#15

I am trying to do the same, any updates @KLFSBK ?