Thank you for that bit of magic code.
I don't fully understand it but I sort of get the gist of it, enough to play around and get it to do what I want.Thank you for your help with this.
Paul
#include "Arduino.h"
#include "AsyncUDP.h"
#include "WiFi.h"
#include <ESP32Tone.h>
const char * ssid = "Paul2";
const char * password = "abcdeabcde";
const int buzzer = 4;
const int GarR = 2; // garage Red LED (RGB LED's)
const int GarG = 5; // garage Green LED
const int GatR = 18; // Gate Red LED
const int GatG = 19; // Gate Green LED
AsyncUDP udp;
IPAddress local_IP(192, 168, 0, 32);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
void processPacket(AsyncUDPPacket &packet);
bool comparePacketString(AsyncUDPPacket &packet, const char *str);
const uint16_t udpPort = 1234;
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(GarR, OUTPUT);
pinMode(GarG, OUTPUT);
pinMode(GatR, OUTPUT);
pinMode(GatG, OUTPUT);
Serial.begin(9600);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
Serial.println("STA Failed to configure");
}
// if(udp.listen(1234)) {
// Serial.print("UDP Listening on IP: ");
// Serial.println(WiFi.localIP());
//}
if (udp.listen(udpPort)) {
udp.onPacket(processPacket);
} else {
Serial.println("Failed to initiate UDP Packet Listening");
}
}
void loop() {
}
void processPacket(AsyncUDPPacket &packet) {
if (comparePacketString(packet, "GarageDetect")) {
Serial.println("GarageDetect");
digitalWrite(GarR,HIGH);
delay(100);
digitalWrite(GarR,LOW);
tone(buzzer, 3000, 300);
} else if (comparePacketString(packet, "GarageLive")) {
Serial.println("GarageLive");
digitalWrite(GarG,HIGH);
delay(100);
digitalWrite(GarG,LOW);
}
if (comparePacketString(packet, "GateDetect")) {
Serial.println("GateDetect");
digitalWrite(GatR,HIGH);
delay(100);
digitalWrite(GatR,LOW);
tone(buzzer, 3000, 300);
} else if (comparePacketString(packet, "GateLive")) {
Serial.println("GateLive");
digitalWrite(GatG,HIGH);
delay(100);
digitalWrite(GatG,LOW);
}
}
bool comparePacketString(AsyncUDPPacket &packet, const char *str) {
size_t len = strlen(str);
if (packet.length() < len) {
return false;
}
if (memcmp(str, packet.data(), len) == 0) {
return true;
}
return false;
}
I am still puzzled how this all continues to work despite no code in the loop section.. but it works, thank you .