1

In this code i am taking the TID data(20bytes of 160bits) in the form of array according documentation and and its working correctly and getting the output of RFID tags.

Now I just need your guidance that how can i convert the array of 20 Bytes into a single variable or into a double long datatype? or another. Because I have to push it on to my MySQL database server for further processing.

I had tried the following code which works fine

byte x[10] = "450210921";
long result = atol(x);
Serial.print("long value of the byte is: ");
Serial.println(result);

but when i put the above code into my main code, getting not any error and I am not getting the required output.!

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  byte response;
  byte myTID[20]; //TIDs are 20 bytes
  byte tidLength = sizeof(myTID);

  //Read unique ID of tag
  response = nano.readTID(myTID, tidLength);
  if (response == RESPONSE_SUCCESS)
  {
    for(byte x = 0 ; x < tidLength ; x++)
    {
      if(myTID[x] < 0x10) Serial.print("0");
      /*temp[x]=(myTID[x], HEX);*/
      //Swapping values into temp variable
      temp[x]=myTID[x];  
      Serial.print(temp[x]);
    }
    /*byte x[10] = "450210921";*/
    //Convert a whole array into a single long variable
    long result = atol(temp);
    Serial.print("long value of the byte is: ");
    Serial.println(result);
  }
  else
    Serial.println("Failed read");
}
5
  • There is no such thing as a single variable that is 160 bits in size. I think you want to format the bytes as a string (and there are plenty of questions about that on here). Commented Nov 4, 2018 at 13:01
  • Ok @Majenko but is there any way to push complete an array to MySQL into a single table cell using nodemcu? Commented Nov 4, 2018 at 13:09
  • I have no idea about the MySQL side of things, but you can take each byte in the array, turn it into a text representation of a hexadecimal pair, concatenate them all in a string, and use that string. Commented Nov 4, 2018 at 13:11
  • arduino.stackexchange.com/questions/53258/… Commented Nov 4, 2018 at 13:12
  • There are several simple errors: 1) "atol(x)" should be "atol(tmp)", 2) but then "tmp" should be null-terminated, 3) which then implies that it is too short. And so on. Commented Nov 4, 2018 at 13:16

1 Answer 1

0

You can't convert 20 bytes to long. Send it as string of hex values. A byte takes two characters in hex, so you will send a 40 characters string, which can be simply decoded back to bytes.

char str[sizeof(myTID) * 2 + 1];
const char* hex = "0123456789ABCDEF";
for (int i = 0; i < sizeof(myTID); i++) {
  str[i * 2] = hex[(myTID[i] >> 4)];
  str[i * 2 + 1] = hex[(myTID[i] & 0x0F)];
}
str[sizeof(str)] = 0;
Serial.println(str);
4
  • It works! Thank, what about for this for(byte x = 0 ; x < sizeof(myTID) ; x++) { content.concat(String(myTID[x] < 0x10 ? " 0" : " ")); content.concat(String(myTID[x], HEX)); } content.toUpperCase(); softSerial.print(content); Commented Nov 5, 2018 at 20:12
  • String is not good Commented Nov 5, 2018 at 20:44
  • Could you explain it that how the string is not good!! Commented Nov 6, 2018 at 5:48
  • hackingmajenkoblog.wordpress.com/2016/02/04/… Commented Nov 6, 2018 at 5:49

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.