I have an Arduino Uno sending a UDP packet to a server listening on port 8888 which prints the UDP packet in a console window, it then sends either a 1 or a 0 back to the Arduino over port 8888.
The trouble is, the Arduino is not getting the UDP packet when monitoring through the Serial window. If I create a Console Application to listen for this response, it is received so the problem is with the Arduino. Any ideas why this is? My code is below -
#include <UIPEthernet.h>
//Wired configuration
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
unsigned char local_ip[] = { 192, 168, 0, 7 };
unsigned char gateway_ip[] = { 192, 168, 0, 1 };
unsigned char dns_ip[] = { 192, 168, 0, 1 };
unsigned char subnet_mask[] = { 255, 255, 255, 0 };
//declare and initilize output pins
int ledRed = 7;
int ledYellow = 6;
int ledGreen = 5;
//Server IP
unsigned char remote_ip[] = { 192, 168, 0, 5 };
unsigned char scannedcard[14] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
String lockId = "610453474";
unsigned int local_Port = 8888;
unsigned int remote_Port = 8888;
String stringOne;
WIEGAND wg;
EthernetUDP udp;
unsigned long next;
void setup() {
Serial.begin(9600);
wg.begin();
pinMode(ledRed, OUTPUT); //Firmware or hardware error
pinMode(ledYellow, OUTPUT); //Waiting for RFID tag
pinMode(ledGreen, OUTPUT); //Scanned RFID tag
// Start Ethernet connection
Ethernet.begin(mac, local_ip,dns_ip,gateway_ip,subnet_mask);
udp.begin(local_Port);
}
void loop() {
int success;
int len;
if(wg.available())
{
Serial.print("CARD NO: = ");
Serial.println(wg.getCode());
Serial.print("DOOR NO: = ");
Serial.println(lockId);
int success;
int len = 0;
if (((signed long)(millis()-next))>0)
{
do
{
success = udp.beginPacket(IPAddress(remote_ip),remote_Port);
}
while (!success && ((signed long)(millis()-next))<0);
if (!success )
goto stop;
stringOne = "C" + (String)wg.getCode() + ",L" + lockId;
success = udp.print(stringOne);
Serial.print("bytes written: ");
Serial.println(success);
success = udp.endPacket();
do
{
//check for new udp-packet:
success = udp.parsePacket();
}
while (!success && ((signed long)(millis()-next))<0);
Serial.print("received: ");
do
{
int c = udp.read();
Serial.println(c);
len++;
}
while ((success = udp.available())>0);
//finish reading this packet:
udp.flush();
stop:
udp.stop();
next = millis()+5000;
}
}
}