Problem reading strings from a UDP for flamethrower device

Hi guys,

I learned to code on python and I’m having a really hard time adapting to C++…

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 will detect a presence. 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.

Can Arduino use strings as a variable value to trigger actions?

This is my first code, I still haven’t figured out how to send its string.

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

const char* ssid     = "xxxx"; 
const char* password = "xxxx";

WiFiUDP UDPTestServer;



unsigned int UDPPort = 2807;
int detection;

const int packetSize = 2;   
byte packetBuffer[packetSize];

void setup() {
  Serial.begin(115200);   
  delay(10);

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

  WiFi.begin(ssid, password);

  WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

  while (WiFi.status() != WL_CONNECTED) {   
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   

 UDPTestServer.begin(UDPPort);  

 pinMode(detection, INPUT);
}

void loop(){

  if (digitalRead(detection)==HIGH){
    udp.send("aa", ssid, UDPPort );
  }
}

This is the second code, it doesn’t seem to recognize the string. What am I missing??

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

const char* ssid     = "xxxx";
const char* password = "xxxx;

WiFiUDP UDPTestServer;
String myData;

unsigned int UDPPort = 2807;

int relayPin = D5;  
int skip = 0;

const int packetSize = 2;   
byte packetBuffer[packetSize];

void setup() {
  Serial.begin(115200);   
  delay(10);

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

  WiFi.begin(ssid, password); 

  WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0));

  while (WiFi.status() != WL_CONNECTED) {   
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());   

  UDPTestServer.begin(UDPPort);   

  pinMode(relayPin, OUTPUT);    
  digitalWrite(relayPin, HIGH);
}



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

    if (myData == "aa") {
 
     skip++;
 
     if (skip == 2) {
 
       digitalWrite(relayPin, HIGH);
 
       delay (1000);
 
       digitalWrite(relayPin, LOW);
        }
    }
    
    if (myData == "bb") {
 
     skip = 0;
    }
}


void handleUDPServer() {

  int cb = UDPTestServer.parsePacket();

  if (cb) {
    UDPTestServer.read(packetBuffer, packetSize);

    String myData = "";

    for (int i = 0; i < packetSize; i++) {

      myData += (char)packetBuffer[i];
    }

    Serial.println(myData);

  }
}

This is the second code, it doesn't seem to recognize the string. What am I missing??

You have a global variable

String myData;

Then again a local variable of the same type and name within handleUDPServer(). This local myData is printed.

The conditional tests in loop() use the global variable which is never changed.

Do not redeclare the variable in the function.

 //String myData = "";
myData = "";