Run code only after motion triggered

Hi, Hoping someone can guide me on what to change. I want the finger print code to only run after the motion is triggered. I have it working, sort of. The issue if it is an issue seems when there is no motion, I think it may be connecting and disconnecting from my network over and over and over again. Any advice?

thanks

#include <Adafruit_Fingerprint.h>
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
//LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include "CTBot.h"

SoftwareSerial mySerial(D3, D4);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

const char* ssid = "xxx"; //replace with your own SSID
const char* password = "xxxx"; //replace with your own password
//const char* host = "api.pushingbox.com";
const char* mqtt_server = "xxx";
WiFiClient espClient;
PubSubClient client(espClient);

String member = "";
int flag = 0;
int sensor = 13; // Motion Sensor Digital pin D7
void setup()
{

Serial.begin(115200);
pinMode(sensor, INPUT);
//long state = digitalRead(sensor);
// while (state == LOW);
delay(10);
Serial.println();
client.setServer(mqtt_server, 1883);
Serial.println();
Serial.print("Connecting to WiFi... ");
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

// set the telegram bot token
myBot.setTelegramToken(token);

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

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

while (!Serial);
delay(100);
Serial.println("\n\n Waiting for Fingerprint Sensor");
delay(1500);
finger.begin(57600);

if (finger.verifyPassword())
{
Serial.println("Found Successfully");
delay(1500);
} else
{
Serial.println("Fingerprint sensor not found!!!");
while (1)
{
delay(1);
}
}
}

void loop()
{
Serial.println("Waiting");
long state = digitalRead(sensor);
while (state == LOW);
int fingerprintID = getFingerprintID();
delay(50);
if (fingerprintID == 1)
{
Serial.println("Welcome Bob");
flag = 0;
}
else if (fingerprintID == 2)
{
Serial.println("Welcome Jack");
flag = 0;
}
else if (fingerprintID == 3)
{
Serial.println("Welcome Tara");
flag = 0;
}
else
{
Serial.println("Waiting for valid finger!!!");
}
}

int getFingerprintID()
{
uint8_t p = finger.getImage();
if (p != FINGERPRINT_OK) return -1;

p = finger.image2Tz();
if (p != FINGERPRINT_OK) return -1;

p = finger.fingerFastSearch();
if (p != FINGERPRINT_OK) return -1;

return finger.fingerID;
}

To make it easy for people to help you please modify your post and use the code button </>
codeButton.png

so your code 
looks like this

and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to my text editor.

If this was my project I would get it working without any of the web code to start with and only then add the web code.

...R

doesn't this code trap you? once state is LOW, how can it become ! LOW?

 while (state == LOW);

I reattached the code properly. Whats interesting is when I use the motion parameter like I thought it should work when set to High, the code never actually triggers when I trigger motion. I need to set it to LOW, and that actually triggers the code to run in the loop. But I still haven't figure out how to stop the code in the setup section, wifi setup etc still runs over and over until I trigger the motion.

thanks

#include <Adafruit_Fingerprint.h>
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
//LiquidCrystal_I2C lcd(0x3F, 16, 2);



SoftwareSerial mySerial(D3, D4);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

const char* ssid     = "xxx";   //replace with your own SSID
const char* password = "xxxx";    //replace with your own password
const char* mqtt_server = "xxxx";
WiFiClient espClient;



String member = "";
int flag = 0;
int sensor = 13;  // Motion Sensor Digital pin D7
void setup()
{

Serial.begin(115200);
pinMode(sensor, INPUT);
 //long state = digitalRead(sensor);
// while (state == LOW);
  delay(10);
  Serial.println();
  client.setServer(mqtt_server, 1883);
  Serial.println();
  Serial.print("Connecting to WiFi...       ");
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);


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

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


while (!Serial);
  delay(100);
  Serial.println("\n\n Waiting for Fingerprint Sensor");
  delay(1500);
  finger.begin(57600);

  if (finger.verifyPassword())
  {
    Serial.println("Found Successfully");
    delay(1500);
  } else
  {
    Serial.println("Fingerprint sensor not found!!!");
    while (1)
    {
      delay(1);
    }
  }
}

void loop()
{
  long state = digitalRead(sensor);
 while (state == LOW);
 Serial.println("Waiting");
// long state = digitalRead(sensor);
 while (state == HIGH);
  int fingerprintID = getFingerprintID();
  delay(50);
  if (fingerprintID == 1)
  {
    Serial.println("Welcome John");
    flag = 0;

  }
  else if (fingerprintID == 2)
  {
    Serial.println("Welcome bob");
    flag = 0;
  }
  else if (fingerprintID == 3)
  {
    Serial.println("Welcome Tara");
    flag = 0;
  }
  else
  {
    Serial.println("Waiting for valid finger!!!");
  }
}

int getFingerprintID()
{
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;

  return finger.fingerID;
}

You need to explain what you are trying to achieve with this code - because it won't work

 while (state == LOW);
 Serial.println("Waiting");
// long state = digitalRead(sensor);
 while (state == HIGH);

In general don't use WHILE because it blocks the Arduino until it completes. Use IF and allow loop() to do the repetition.

And both IF and WHILE clauses should end with a { to show theblock of code they apply to - like this

if (state == HIGH) {
  // code to apply when HIGH

}

...R

I changed to using If, everything works perfect now. Thanks so much