Hello everyone. I have been pulling my hair out trying to get one Arduino to send a UDP packet to a second Arduino and then have the second Arduino act on the received packet. So in a nutshell I want the first Arduino to act as a controller for some garage doors throughout my building. The first Arduino will decide which door to open and then send a UDP packet over the LAN to the required Arduino to open that garage door. I have the first Arduino working perfectly. It understands the command I give it then sends the UDP packet to the second Arduino.
The second Arduino receives the packet exactly as I wish. My problem is I am having trouble understanding how to get the second Arduino to act on the received packets. I will include the code that I'm using on the second Arduino. The first Arduino sends the packet "Shop Main Door". The second Arduino receives it just fine and serial prints it exactly how it was sent. I just cant get the second Arduino to act on it. Thanks everyone for who may be able to assist.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#ifndef STASSID
#define STASSID "Cormacs"
#define STAPSK "pkmy64o3"
#endif
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE + 1]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged\r\n"; // a string to send back
WiFiUDP Udp;
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("UDP server on port %d\n", localPort);
Udp.begin(localPort);
pinMode(D1, OUTPUT); // Relay Pin
pinMode(D0, OUTPUT); // Relay Status LED
pinMode(D5, OUTPUT); // Wifi Status LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(D1, LOW);
digitalWrite(D0, HIGH);
digitalWrite(D5, HIGH);
digitalWrite(LED_BUILTIN, HIGH);
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(D5, LOW);
}
else {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(D5, HIGH);
}
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if (packetSize) {
Serial.printf("Received packet of size %d from %s:%d\n (to %s:%d, free heap = %d B)\n",
packetSize,
Udp.remoteIP().toString().c_str(), Udp.remotePort(),
Udp.destinationIP().toString().c_str(), Udp.localPort(),
ESP.getFreeHeap());
// read the packet into packetBufffer
int n = Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
packetBuffer[n] = 0;
Serial.println("Contents:");
Serial.println(packetBuffer);
if (packetBuffer == "Shop Main Door") {
Serial.println("Triggered");
digitalWrite(D1, HIGH);
digitalWrite(D0, LOW);
delay(500);
digitalWrite(D1, LOW);
digitalWrite(D0, HIGH);
}
// 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();
}
}