Sending and Reading a UDP packet to activate a switch

Hi, my project is a security system of sorts. I have built a device that shoots flames and I have it set up so that upon entering my room, it is supposed to detect it. I have an NFC chip implanted into my hand and I have a program on my phone that can read the NFC and transmit a UDP packet as a string to disarm the flamethrower. My code is working to the extent that it can receive the UDP packet from my phone but that's about it.

My question for the first code is, can strings be used as a trigger?

//this program is for the Wemos D1 R2 board. You have to connect it to the same wi-fi as the trigger,
//in this case it is my NFC chip read by my phone. Once connected it will be able to recieve a signal as a UDP packet
//which will in turn trigger a relay for a short amount of time.

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid = "TEST";
const char* password = "12345678";

WiFiUDP UDPTestServer;

unsigned int UDPPort = 2807;
int relayPin = D5; //pin relay is attached to

const int packetSize = 2; //max size of the UDP packet
byte packetBuffer[packetSize];

void setup() {
Serial.begin(115200); //set the serial monitor to this setting to debug
delay(10);

Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, password); //this initiates the wifi connection

//chooses a ip address
WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

while (WiFi.status() != WL_CONNECTED) { //lets you know that its trying to connect
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP()); //displays the IP when connection is established

UDPTestServer.begin(UDPPort); //gets ready to accept incoming data

pinMode(relayPin, OUTPUT); //
digitalWrite(relayPin, LOW);
}

void loop() {
handleUDPServer();
delay(1);
}

void handleUDPServer() {
int cb = UDPTestServer.parsePacket();
if (cb) {
UDPTestServer.read(packetBuffer, packetSize);
String myData = "";
for (int i = 0; i < packetSize; i++) {
myData += (char)packetBuffer*;*

  • }*

  • Serial.println(myData);*

  • //myData[] = { packetBuffer[0], packetBuffer[1], '\0'};*

  • if (myData == "aa") {*

  • digitalWrite(relayPin, HIGH);*

  • delay (1000);*

  • digitalWrite(relayPin, LOW);*

  • }*

  • else; {*

  • digitalWrite(relayPin, LOW);*

  • }*

  • myData = "";*

  • }*
    }
    My question for the second code is, what can I do to send a UDP packet with a different value to trigger a different action?
    #include <ESP8266WiFi.h>
    #include <WiFiUdp.h>
    const char* ssid = "TELUS0938-2.4G"; //this must be the same wifi as trigger
    const char* password = "9ppbhcu3ps";
    WiFiUDP UDPTestServer;
    unsigned int UDPPort = 2807;
    int = detection;
    const int packetSize = 2; //max size of the UDP packet
    byte packetBuffer[packetSize];
    void setup() {

  • Serial.begin(115200); //set the serial monitor to this setting to debug*

  • delay(10);*

  • Serial.println();*

  • Serial.println();*

  • Serial.print("Connecting to ");*

  • Serial.println(ssid);*

  • WiFi.begin(ssid, password); //this initiates the wifi connection*

  • WiFi.config(IPAddress(192, 168, 1, 60));*

  • while (WiFi.status() != WL_CONNECTED) { //lets you know that its trying to connect*

  • delay(500);*

  • Serial.print(".");*

  • }*

  • Serial.println("");*

  • Serial.println("WiFi connected");*

  • Serial.println("IP address: ");*

  • Serial.println(WiFi.localIP()); //displays the IP when connection is established*
    *UDPTestServer.begin(UDPPort); *

  • pinMode(detection, INPUT);*
    void loop() {

  • // put your main code here, to run repeatedly:*
    if digitalRead(detection)==HIGH)
    {

}
}
I really appreciate any assistance, I really don't know what I'm doing with C++... :confused:
Wemos_D1_R2_Flamethrower.ino (2.05 KB)
Wemos_D1_R2_Flamethower_Detect.ino (1.14 KB)