Hi everyone! I am using Arduino Nano with the Ethernet shield ENC28J60. With this program Arduino is successfully sending data, I can see it in Wireshark:
#include <EtherCard.h>
static byte mymac[] = { 0x1A,0x2B,0x3C,0x4D,0x5E,0x6F };
byte Ethernet::buffer[700];
static uint32_t timer;
#define STATIC 1 // set to 1 to disable DHCP (adjust myip/gwip values below)
#if STATIC
static byte myip[] = { 192,168,1,50 };
static byte gwip[] = { 192,168,1,1 };
static byte dnsip[] = { 192,168,1,1 };
#endif
static byte dip[] = { 192,168,1,40 };
const int dstPort PROGMEM = 44000;
const int srcPort PROGMEM = 44000;
void setup () {
Serial.begin(9600);
// Change 'SS' to your Slave Select pin, if you arn't using the default pin
if (ether.begin(sizeof Ethernet::buffer, mymac, SS) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
ether.staticSetup(myip, gwip, dnsip);
#else
if (!ether.dhcpSetup())
Serial.println("DHCP failed");
#endif
ether.printIp("IP: ", ether.myip);
ether.printIp("GW: ", ether.gwip);
ether.printIp("DNS: ", ether.dnsip);
ether.printIp("SRV: ", ether.hisip);
}
char textToSend[] = "abc";
void loop () {
if (millis() > timer) {
timer = millis() + 5000;
//static void sendUdp (char *data,uint8_t len,uint16_t sport, uint8_t *dip, uint16_t dport);
ether.sendUdp(textToSend, sizeof(textToSend), srcPort, dip, dstPort );
}
}
However, for days I have been trying to set up a UDP client in Processing by using different examples from the Web, but no luck so far. Here is one:
import hypermedia.net.*; // import the UDP library
UDP udp; // define the UDP object
void setup(){
size(400, 400);
udp = new UDP (this, 44000); // create a new datagram connection on port 44000
udp.listen (true); // and wait for incoming message
}
void draw(){
}
void receive (byte[] data){
// print the incoming data bytes as ASCII characters
for (int i=0; i<data.length; i++) {
print (char (data[i]));
}
println();
}
This one above doesn't print out anything, but there is no error message either. Another one:
import processing.net.*;
Client myClient;
int dataIn;
void setup() {
size(400, 400);
// Connect to the local machine at port 44000.
// This example will not run if you haven't
// previously started a server on this port.
myClient = new Client(this, "192.168.1.50", 44000);
}
void draw() {
if (myClient.available() > 0) {
dataIn = myClient.read();
}
print(char(dataIn));
}
This one gives a message after 15-20 seconds:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at processing.net.Client.<init>(Unknown Source)
at sketch_200307b.setup(sketch_200307b.java:30)
at processing.core.PApplet.handleDraw(PApplet.java:2432)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:313)
It seems that these sketches work for many people, as far as I could see on different message boards. What am I doing wrong?