Go Down

Topic: Weigand26 RFID + UDP doesn't send variable (Read 1 time) previous topic - next topic

what5150

I'm new to Arduino but have made a few things with it easily. However, this project is driving me insane.
I'm using a Weigand26 RFID reader with borrowed code and sending the output of reader1 to a server running
a PHP script that listens on UDP port 60000. The php script will convert the UDP packet data into the card number
and check it against MySQL database. If it's good the PHP script will send and UDP packet back to the Arduino.
Arduino will read the UDP packet and HIGH a pin for the electric strike.

Now, my problem is that Udp.write(reader1); sends nothing while Serial.println(reader1); prints the data.
The packet is making to my server. I can do Udp.write("hello") and the PHP script prints hello on a card read.
so the receiving UDP and electric strike part isn't in there but it works in a separate sketch. If any one wants
it I'd be happy to share.

I've never used volatile long, I don't know how it works really.
I'd appreciate any advice. Thanks.

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { x, x, x, x };
byte server[] = { x, x, x, x };
volatile long reader1 = 0;
volatile int reader1Count = 0;

unsigned int localPort = 60000;      // local port to listen on
int remotePort = 60000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

EthernetUDP Udp;

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;
}

void setup() {
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
    pinMode(i, INPUT);
    digitalWrite(i, HIGH); // enable internal pull up
  }

  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  //digitalWrite(13, HIGH);  // show Arduino has finished initilisation
 
}

void loop() {
  if(reader1Count >=26){
    Serial.println(reader1);
    Udp.beginPacket(server, remotePort);
    Udp.write("hello");
    Udp.endPacket();
    }

    reader1 = 0;
    reader1Count = 0;
    digitalWrite(13,HIGH);
    delay(2000);
    digitalWrite(13,LOW);
    }

johnwasser

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

what5150

I just notice in the code I pasted I left "hello" in the Udp.write from where I was testing. I changed it and pasted below
So Serial.println will print an integer but Udp.write won't?

Thanks.

Code: [Select]

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { x, x, x, x };
byte server[] = { x, x, x, x };
volatile long reader1 = 0;
volatile int reader1Count = 0;

unsigned int localPort = 60000;      // local port to listen on
int remotePort = 60000;
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,

EthernetUDP Udp;

void reader1One(void) {
  reader1Count++;
  reader1 = reader1 << 1;
  reader1 |= 1;
}

void reader1Zero(void) {
  reader1Count++;
  reader1 = reader1 << 1;
}

void setup() {
  Ethernet.begin(mac, ip);
  Udp.begin(localPort);
  Serial.begin(9600);
  // Attach pin change interrupt service routines from the Wiegand RFID readers
  attachInterrupt(0, reader1Zero, RISING);//DATA0 to pin 2
  attachInterrupt(1, reader1One, RISING); //DATA1 to pin 3
  delay(10);
  // the interrupt in the Atmel processor mises out the first negitave pulse as the inputs are already high,
  // so this gives a pulse to each reader input line to get the interrupts working properly.
  // Then clear out the reader variables.
  // The readers are open collector sitting normally at a one so this is OK
  for(int i = 2; i<4; i++){
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH); // enable internal pull up causing a one
    digitalWrite(i, LOW); // disable internal pull up causing zero and thus an interrupt
    pinMode(i, INPUT);
    digitalWrite(i, HIGH); // enable internal pull up
  }

  delay(10);
  // put the reader input variables to zero
  reader1 = 0;
  reader1Count = 0;
  //digitalWrite(13, HIGH);  // show Arduino has finished initilisation
 
}

void loop() {
  if(reader1Count >=26){
    Serial.println(reader1);
    Udp.beginPacket(server, remotePort);
    Udp.write(reader1);
    Udp.endPacket();
    }

    reader1 = 0;
    reader1Count = 0;
    digitalWrite(13,HIGH);
    delay(2000);
    digitalWrite(13,LOW);
    }

johnwasser

The argument for EthernetUDP::write() is shown in the documentation as type "char" but they probably meant "char *" (string).

If EtherenetUDP inherits from Print you should be able to say "Udp.println(reader1)" and have it sent as a decimal integer.  If not, you'll have to convert the integer to a character string.  Probably the easiest way is:

Code: [Select]

String buff = reader1;
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

what5150

Thanks for the help. But it will not convert the Long to char. I'm obviously not getting something.
But I switched the sending to the http client and the receiving is still UDP and it's working great

Thanks again.

Go Up