I'm playing with the UDP Send Receive String example found here; https://www.arduino.cc/en/Tutorial/UDPSendReceiveString
With the code provided I can (obviously) successfully send and receive chars.
I'm trying to go a step further now and perform different tasks (starting with simply printing a different line to serial monitor) depending on what data is received in the UDP packet.
My arduino code looked like this;
#include <Ethernet.h>
//DEFINITIONS-----------------------------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //mac adress
IPAddress ip(2, 0, 0, 100); //ip address
unsigned int localPort = 8888; //udp port to listen for packets on
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
EthernetUDP Udp; //creates Udp instance
char f1 = 10; //char to compare packetBuffer to
char f2 = 24; //char to compare packetBuffer to
void setup() {
Ethernet.init(10); //sets CS pin for ethernet shield
Ethernet.begin(mac, ip); //initializes ethernet with mac and ip addresses
Serial.begin(9600); //initializes serial connection. Waits for serial to be available (native usb only).
while (!Serial) {
;
}
//Checks for presence of Ethernet shield. Halts if no ethernet hardware present.
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
while (true) {
delay(1);
}
}
//Checks for presence of etherner link.Halts if no link present.
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println("Ethernet cable is not connected.");
}
Udp.begin(localPort); //begins udp on port specified above.
}
void loop() {
int packetSize = Udp.parsePacket();
if (packetSize) { //if data available, prints following;
Serial.println("----------------------------------------------");
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i = 0; i < 4; i++) {
Serial.print(remote[i], DEC);
if (i < 3) {
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
Serial.print("packetBuffer = ");
Serial.println(packetBuffer);
if (packetBuffer == f1) {
Serial.println("Contents:");
Serial.println("10 received via UDP!");
} else if (packetBuffer == f2) {
Serial.println("Contents");
Serial.println("24 received via UDP!");
} else {
Serial.println("SOMETHING WENT WRONG");
}
Serial.println("----------------------------------------------");
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write("acknowledged", 13);
Udp.endPacket();
}
}
And my Processing 3 code to send UDP looked like this;
import hypermedia.net.*;
UDP udp; //define the UDP object
void setup() {
udp = new UDP (this, 6000); //create new datagram connection on port 6000
udp.log(true); //print out the connection activity (optional)
udp.listen(true); //wait for incoming message
}
void draw() {
}
void keyPressed() {
int buffer = 24;
String ip = "2.0.0.100"; //remote IP address to send string to
int port = 8888; //destination port
udp.send("24", ip, port); //the message to send
}
void receive( byte[] data ) { //default handler
//void receive( byte[] data, String ip, int port ) { // extended handler
for(int i=0; i < data.length; i++)
print(char(data[i]));
println();
}
When sending either "10" or "24" to the arduino I always get my "Something went wrong message".
I assume this is something to do with the processing sketch sending the 10/24 as chars encoded in ascii, but I'm not sure how to account for that in the arduino code. When I try to change the packetBuffer from char to byte I get an error saying
"no matching function for call to 'println(byte [24])' "
Any help would be much appreciated! Thank you