How to pass a number to a predefined function that expects char array

Can't figure out how to pass the array number as parameter to the predefined receivedData() function of the Yet_Another_Arduino_Wiegand_Library-2.0.0. The following code compiles w/o issues:

#include <Wiegand.h>
Wiegand WG[5];
void setup() {
  WG[0].onReceive(receivedData, "0");
  WG[1].onReceive(receivedData, "1");
  WG[2].onReceive(receivedData, "2");
  WG[3].onReceive(receivedData, "3");
  WG[4].onReceive(receivedData, "4");
  for(byte r = 0; r < 5; r++) {
    WG[r].begin(Wiegand::LENGTH_ANY, true);
  }
}
void loop() {
// no  need to loop in this example
}
void receivedData(uint8_t* data, uint8_t bits, const char* message) {
// do whatever with the received data
}

I have tried a ton of different ways to convert the 'r' variable into char array, but any permutation of the following code is doomed to a compilation error:

#include <Wiegand.h>
Wiegand WG[5];
void setup() {
  for(byte r = 0; r < 5; r++) {
    String str = String(r);
    WG[r].onReceive(receivedData, str.charAt(0));    
    WG[r].begin(Wiegand::LENGTH_ANY, true);
  }
}
void loop() {
// no  need to loop in this example
}
void receivedData(uint8_t* data, uint8_t bits, const char* message) {
// do whatever with the received data
}

I'm compiling for the ESP32 with Arduino IDE 1.8.16 if it makes any difference.
Any help will be greatly appreciated!

So you want to pass a digit as a cString.

Just build it. You need the ASCII representation of the digit followed by a null character, so an array of two chars

for(byte r = 0; r < 5; r++) {
    char payload[2] = {'0'+r; '\0'};
    WG[r].onReceive(receivedData, payload);    
    WG[r].begin(Wiegand::LENGTH_ANY, true);
  }

1 Like

I don't think you are using that library correctly. You are missing a lot of the code, including setting up the two data pins for each Wiegand device.

Are you REALLY setting up five separate devices? What ten input pins are you using? Are you using interrupts or polling?

@J-M-L, thanks for the suggestion!

for(...)
  const char payload[2] = {'0' + r, '\0'};
  WG[rdr[r].objid].onReceive(receivedData, payload);

compiles ok, but the value of the payload gets lost - the following always produces zero:

void receivedData(uint8_t* wg_data, uint8_t bits, const char* message) { 
  Serial.println(atoi(message));
}

@johnwasser, thanks for your remark, I have indeed omitted all parts of the code that are not directly related to the problem I'm having. I'm using interrupts and in my experience the ESP32 works fine with up to 10 wiegand readers simultaneously, though as you mentioned at that point there are hardly any pins left for other tasks. 5 readers and 5 relays is just about the sweet spot.
D0 pins: 13, 15, 17, 19, 25
D1 pins: 14, 16, 18, 23, 34
Relay pins: 12, 32, 26, 27, 33

Ok so the class does not keep its own copy and the data needs to be permanent

Define a global array

include <Wiegand.h>
const char *  data[] = {"0",  "1", "2", "3", "4"};
const byte count = sizeof data / sizeof *data;
Wiegand WG[count];

And then use

for(byte r = 0; r < count; r++) {
    WG[r].onReceive(receivedData, data[r]);    
    WG[r].begin(Wiegand::LENGTH_ANY, true); 
}

Yay!!

Have fun!

(Pay attention to @johnwasser’s questions)

An alternative to using the "char *" argument to determine which of the interfaces received the input: define a separate "receivedData()" function for each device.

void receivedData0(uint8_t* wg_data, uint8_t bits, const char* message) { 
  Serial.println(0);
}

void receivedData1(uint8_t* wg_data, uint8_t bits, const char* message) { 
  Serial.println(1);
}

void receivedData2(uint8_t* wg_data, uint8_t bits, const char* message) { 
  Serial.println(2);
}
.
.
.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.