62 lines
1.5 KiB
C++
62 lines
1.5 KiB
C++
#include <Arduino.h>
|
|
#include <Ethernet.h>
|
|
#include <EthernetUdp.h>
|
|
|
|
#include "config.h"
|
|
|
|
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
|
|
char ReplyBuffer[UDP_TX_PACKET_MAX_SIZE];
|
|
|
|
EthernetUDP Udp;
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("Irrigation Arduino");
|
|
|
|
Ethernet.init(ETHERNET_CS);
|
|
IPAddress ip;
|
|
ip.fromString(IP_ADDRESS);
|
|
Ethernet.begin(mac, ip);
|
|
|
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
|
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
|
|
while (true)
|
|
delay(1);
|
|
}
|
|
|
|
if (Ethernet.linkStatus() == LinkOFF)
|
|
Serial.println("Ethernet cable is not connected.");
|
|
|
|
Udp.begin(IP_PORT);
|
|
}
|
|
|
|
void loop() {
|
|
// if there's data available, read a packet
|
|
int packetSize = Udp.parsePacket();
|
|
if (packetSize) {
|
|
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());
|
|
|
|
// read the packet into packetBuffer
|
|
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
|
Serial.println("Contents:");
|
|
Serial.println(packetBuffer);
|
|
|
|
// 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);
|
|
}
|