Hallo,
also ich hab jetzt mal mit dem Beipiel "UDPSendReceive" aus der Bibliothek gespielt. Über Processing kann ich jetzt die LED am Empfänger einschalten sobald ich eine "5" sende. Ausschalten geht noch nicht - ist momentan auch egal.
Wie kann ich jetzt den Sender programmieren damit also ein zweiter Arduino das Processing ersetzt?
Eine Hilfe für eine Grundstruktur würde mir schon reichen.
hier mal der Sketch des Empängers:
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 178, 110);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
int val = 0;
int old_val = 0;
int state = 0;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
pinMode(5,OUTPUT);
}
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println(packetBuffer);
int k;
k = atoi(packetBuffer);
val = k;
if((val == 5)&&(old_val != 5)){
state = 1 - state;
delay(10);
}
old_val = val;
if(state == 1){
digitalWrite(5,HIGH);
}
else{
digitalWrite(5,LOW);
}
Serial.println( state);
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
delay(10);
}
Über jeden Tipp bin ich dankbar.
Grüße