I have written two sketches to test development features. I have the Ethernet Shield and based my sketches on the sample UDP program.
They don't work. They both seem to initialize properly. The debug statements look like everything is working. Except, no packets appear to be received.
First, I have had some confusion on which libraries to use. Of the four libraries listed below,
- Ethernet.h
- Ethernet2.h
- EthernetUDP
- EthernetUDP2
- no UDP library
The sketch compiles with only Ethernet.h and no UDP library.
I am providing listings below, as well as attaching the source code.
Any ideas?
This sketch is used to send a message:
// sends a message first
#include <SPI.h>
#include <Ethernet.h>
// #include <EthernetUdp2.h>
#define localID 10 // local
#define remoteID 20 // remote
#define localPORT 8888 // Port
#define EthMegPin 53
#define debugDelay 2500
#define Halt while(true)
#define Forever while(true)
#define aWait 2000
// define UDP object
EthernetUDP sendUdp;
char UDPRecvBuffer[UDP_TX_PACKET_MAX_SIZE];
char UDPSendBuffer[] = "ack";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, localID };
IPAddress local_IP(10, 1, 1, localID);
IPAddress remote_IP(10, 1, 1, remoteID);
int packetSize;
byte localScriptToken = 0;
int remote_message = 100;
void setup() {
Serial.begin(9600);
pinMode(EthMegPin, OUTPUT);
digitalWrite(EthMegPin, HIGH);
// Ethernet.init(10); // Most Arduino shields
// localID read in setup
// mac[5] = localID;
// local_IP[3] = localID;
// remote_IP[3] = remoteID;
for (int i = 0; i < 4; i++) {
Serial.print(local_IP[i]);
if (i < 3) Serial.print(", ");
}; Serial.println(" <-local");
IPAddress remote_IP(10, 1, 1, remoteID);
for (int i = 0; i < 4; i++) {
Serial.print(remote_IP[i]);
if (i < 3) Serial.print(", ");
}; Serial.println(" <-remote");
Serial.println();
Ethernet.begin(mac, local_IP);
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Setup: Ethernet shield was not found. Sorry, can't run without hardware. :(");
Halt;
} else {
Serial.println("Setup: Found Ethernet Shield");
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Setup: Ethernet cable is not connected.");
Halt;
} else {
Serial.println("Setup: Cable connected");
}
int Udp_Conn = sendUdp.begin(localPORT);
if (Udp_Conn) {
Serial.print("Setup: UDP port connection successful to ");
Serial.println(localPORT);
}
else {
Serial.print("Setup: UDP port connection failed to ");
Serial.println(localPORT);
Halt;
}
Serial.println();
}
void loop() {
// int UdpConn = sendUdp.beginPacket(remote_IP, sendUdp.remotePort());
int UdpConn = sendUdp.beginPacket(remote_IP, localPORT);
for (int i = 0; i < 4; i++) {
Serial.print(remote_IP[i]);
if (i < 3) Serial.print(", ");
}; Serial.print(":");
Serial.print(localPORT);
Serial.println(": <-remote");
if (UdpConn) {
Serial.println("Loop: Begin packet successful");
}
else {
Serial.println("Loop: _UDP packet begin failed. Host not found");
for (int i = 0; i < 4; i++) {
Serial.print(local_IP[i]);
if (i < 3) Serial.print(", ");
}; Serial.println(" <-local");
for (int i = 0; i < 4; i++) {
Serial.print(remote_IP[i]);
if (i < 3) Serial.print(", ");
}; Serial.println(" <-remote");
}
remote_message ++;
itoa(remote_message, UDPSendBuffer, 10);
int charsSent = sendUdp.write(UDPSendBuffer);
// delay(debugDelay);
delay(100);
Serial.print(charsSent);
Serial.println(" characters sent");
int UdpSent = sendUdp.endPacket();
if (UdpSent) {
Serial.println("Loop: Message sent via UDP");
}
else {
Serial.print("Loop: Error occurred during write [");
Serial.print(UdpSent);
Serial.println("]");
}
delay(aWait);
Halt;
}
This sketch is used to receive a message.
/* #include <Dhcp.h>
#include <Dns.h>
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <EthernetServer.h>
#include <EthernetUdp2.h>
#include <Twitter.h>
#include <util.h> */
// receives a message first
#include <SPI.h>
#include <Ethernet.h>
// #include <EthernetUdp2.h>
#define localID 20
#define remoteID 10
#define localPORT 8888
#define EthMegPin 53
#define interpacketDelay 250
#define Halt while(true)
#define Forever while(true)
// define UDP object
EthernetUDP recvUdp;
char UDPRecvBuffer[UDP_TX_PACKET_MAX_SIZE];
char UDPSendBuffer[] = "ack";
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xFE, 0x00 };
byte local_IP[4];
byte remote_IP[4];
int packetSize;
byte localScriptToken;
int remote_message = 0;
void setup() {
Serial.begin(9600);
// localID read in setup
mac[4] = localID;
IPAddress local_ip( 10, 1, 1, localID );
IPAddress remote_IP(10, 1, 1, remoteID);
Ethernet.begin(mac, local_ip);
//print out the IP address
Serial.print("Setup: Local IP = ");
Serial.println(Ethernet.localIP());
Serial.print("Setup: Rem't ID = ");
Serial.println(remote_IP);
/* Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Setup: Ethernet shield was not found. Sorry, can't run without hardware. :(");
Halt;
} */
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Setup: Ethernet cable is not connected.");
Halt;
}
int Udp_Conn = recvUdp.begin(localPORT);
if (Udp_Conn) {
Serial.print("Setup: UDP port connection successful to ");
Serial.println(localPORT);
}
else {
Serial.print("Setup: UDP port connection failed to ");
Serial.println(localPORT);
Halt;
}
Serial.println(F("Starting..."));
}
void loop() {
#define Forever (true)
int displayCount = 0;
const int displayBlock = 10;
while Forever {
packetSize = recvUdp.parsePacket();
if ((displayCount % displayBlock) == 0) {
displayCount = 1;
Serial.print(F("Packet Size "));
Serial.print(packetSize);
Serial.print(" | ");
}
// receive a message
if (packetSize > 0) {
// display buffer
recvUdp.read(UDPRecvBuffer, packetSize);
Serial.print(F("Buffer "));
Serial.println(UDPRecvBuffer);
// display token
localScriptToken = atoi(UDPRecvBuffer);
Serial.print(F("Recv'd "));
Serial.println(localScriptToken);
remote_message = localScriptToken;
delay(interpacketDelay);
// increment display count
displayCount++;
}
}
}