I need special Push Button code

this is required part of my code :

#include "esp_camera.h"
#include <WiFi.h>
#include "fd_forward.h"
#include "fr_forward.h"
#include "fr_flash.h"
.
.
.
.
.

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
  digitalWrite(relayPin, LOW);
  pinMode(relayPin, OUTPUT);
.
.
.
.
 WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  webServer();

  Serial.print("Camera Ready! Use 'http://");
  Serial.print(WiFi.localIP());
  Serial.println("' to connect");
}

void function1() {
.
.
{

void loop() {
   function1();
 }

I need to add a physical Push Button on GPIO16 so that when device starts run function1() inside loop
and when I Push the Button it stop running function1() and loop then connect WiFi and run webserver
and next Push on the Button disconnect WiFi and Stop webserver then run function1() inside loop and so on.
Please help

Sure, there are button switch example sketches that come with the IDE. Please look at those, add the switch logic to your code, and re-post it. Then we can help you connect the switch to the function.

new code after adding button:-

#include "esp_camera.h"
#include <WiFi.h>
#include "fd_forward.h"
#include "fr_forward.h"
#include "fr_flash.h"
.
.
.
.
.

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
  digitalWrite(relayPin, LOW);
  pinMode(relayPin, OUTPUT);
.
.
.

}

void function1() {
.
.
{



void loop() {
  switchState = digitalRead(BUTTON); // read the pushButton State
  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn;
    }
  }
  if (lightsOn and wic == 0 )  // need to run this only once
  {
    digitalWrite(LED, HIGH); // set the LED on

    Serial.println("lightsOn led high wic = 0 start wifi");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    webServer();
    Serial.print(WiFi.localIP());
    Serial.println("' to connect");
    wic = 1;
  } else {
    digitalWrite(LED, LOW); // set the LED off
    WiFi.disconnect();
    wic = 0;
    Serial.println("WiFi Disconnected");
    function1();
  }

}

Result:-

Without press button 

lightsOn led high wic = 0 start wifi
......
WiFi connected
Starting web server on port: '80'
Starting stream server on port: '81'
Camera Ready! Use 'http://192.168.60.37' to connect
WiFi Disconnected
Get one frame in 73 ms.
lightsOn led high wic = 0 start wifi
.
WiFi connected
Starting web server on port: '80'
E (8357) httpd: httpd_server_init: error in bind (112)
Starting stream server on port: '81'
E (8359) httpd: httpd_server_init: error in bind (112)
Camera Ready! Use 'http://192.168.60.37' to connect
WiFi Disconnected
Get one frame in 0 ms.
lightsOn led high wic = 0 start wifi
.

after press button

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8

lightsOn led high wic = 0 start wifi
.
WiFi connected
Starting web server on port: '80'
E (8357) httpd: httpd_server_init: error in bind (112)
Starting stream server on port: '81'
E (8359) httpd: httpd_server_init: error in bind (112)
Camera Ready! Use 'http://192.168.60.37' to connect
WiFi Disconnected             
Get one frame in 0 ms.

My physical button is connected to GPIO16 which is internally connected to a 10k Ohm pull-up resistor. So, it is in high state as default. and other pin of the button is connected to ground.
I test button function with simple led code and it works fine.
But with my code it goes through two cases with out pressing the button and when I press the
Button console says it rebooting I do not know where is the problem ??

Please help.

To help you with that, people here need to see your entire sketch. Please post it here, if it's too big just add it as an attachment. Don't cut out any lines.

void function1() {
.
.
{

Oops

very simple this is camera web server in Arduino IDE examples with some modifications:-

Complete Code :-

#include "esp_camera.h"
#include <WiFi.h>

#include "fd_forward.h"
#include "fr_forward.h"
#include "fr_flash.h"
#define relayPin 12 // pin 12 can also be used

#define BUTTON 16 //(gpio16)
#define LED 4 // (gpio4)
int switchState = 0; // actual read value from pin4
int oldSwitchState = 0; // last read value from pin4
int lightsOn = 0; // is the switch on = 1 or off = 0

unsigned long currentMillis = 0;
unsigned long openedMillis = 0;
unsigned long wic = 0;
unsigned long wid = 0;
long interval = 2000;           // open lock for ... milliseconds
#define ENROLL_CONFIRM_TIMES 5
#define FACE_ID_SAVE_NUMBER 7
static inline mtmn_config_t app_mtmn_config()
{
  mtmn_config_t mtmn_config = {0};
  mtmn_config.type = FAST;
  mtmn_config.min_face = 80;
  mtmn_config.pyramid = 0.707;
  mtmn_config.pyramid_times = 4;
  mtmn_config.p_threshold.score = 0.6;
  mtmn_config.p_threshold.nms = 0.7;
  mtmn_config.p_threshold.candidate_number = 20;
  mtmn_config.r_threshold.score = 0.7;
  mtmn_config.r_threshold.nms = 0.7;
  mtmn_config.r_threshold.candidate_number = 10;
  mtmn_config.o_threshold.score = 0.7;
  mtmn_config.o_threshold.nms = 0.7;
  mtmn_config.o_threshold.candidate_number = 1;
  return mtmn_config;
}
mtmn_config_t mtmn_config = app_mtmn_config();

static face_id_list id_list = {0};
dl_matrix3du_t *image_matrix =  NULL;
camera_fb_t * fb = NULL;

dl_matrix3du_t *aligned_face = dl_matrix3du_alloc(1, FACE_WIDTH, FACE_HEIGHT, 3);



//
// WARNING!!! Make sure that you have either selected ESP32 Wrover Module,
//            or another board which has PSRAM enabled
//

// Select camera model
//#define CAMERA_MODEL_WROVER_KIT
//#define CAMERA_MODEL_ESP_EYE
//#define CAMERA_MODEL_M5STACK_PSRAM
//#define CAMERA_MODEL_M5STACK_WIDE
#define CAMERA_MODEL_AI_THINKER

#include "camera_pins.h"

const char* ssid = "KAREEM-NOUR";
const char* password = "New#Password@60";

void startCameraServer();

void setup() {
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  Serial.println();
  digitalWrite(relayPin, LOW);
  pinMode(relayPin, OUTPUT);
  pinMode(BUTTON, INPUT); // push button
  pinMode(LED, OUTPUT); // anything you want to control using a switch e.g. a Led

  camera_config_t config;
  config.ledc_channel = LEDC_CHANNEL_0;
  config.ledc_timer = LEDC_TIMER_0;
  config.pin_d0 = Y2_GPIO_NUM;
  config.pin_d1 = Y3_GPIO_NUM;
  config.pin_d2 = Y4_GPIO_NUM;
  config.pin_d3 = Y5_GPIO_NUM;
  config.pin_d4 = Y6_GPIO_NUM;
  config.pin_d5 = Y7_GPIO_NUM;
  config.pin_d6 = Y8_GPIO_NUM;
  config.pin_d7 = Y9_GPIO_NUM;
  config.pin_xclk = XCLK_GPIO_NUM;
  config.pin_pclk = PCLK_GPIO_NUM;
  config.pin_vsync = VSYNC_GPIO_NUM;
  config.pin_href = HREF_GPIO_NUM;
  config.pin_sscb_sda = SIOD_GPIO_NUM;
  config.pin_sscb_scl = SIOC_GPIO_NUM;
  config.pin_pwdn = PWDN_GPIO_NUM;
  config.pin_reset = RESET_GPIO_NUM;
  config.xclk_freq_hz = 20000000;
  config.pixel_format = PIXFORMAT_JPEG;
  //init with high specs to pre-allocate larger buffers
  if (psramFound()) {
    config.frame_size = FRAMESIZE_UXGA;
    config.jpeg_quality = 10;
    config.fb_count = 2;
  } else {
    config.frame_size = FRAMESIZE_SVGA;
    config.jpeg_quality = 12;
    config.fb_count = 1;
  }

#if defined(CAMERA_MODEL_ESP_EYE)
  pinMode(13, INPUT_PULLUP);
  pinMode(14, INPUT_PULLUP);
#endif

  // camera init
  esp_err_t err = esp_camera_init(&config);
  if (err != ESP_OK) {
    Serial.printf("Camera init failed with error 0x%x", err);
    return;
  }

  sensor_t * s = esp_camera_sensor_get();
  //initial sensors are flipped vertically and colors are a bit saturated
  if (s->id.PID == OV3660_PID) {
    s->set_vflip(s, 1);//flip it back
    s->set_brightness(s, 1);//up the blightness just a bit
    s->set_saturation(s, -2);//lower the saturation
  }
  //drop down frame size for higher initial frame rate
  s->set_framesize(s, FRAMESIZE_QVGA);

  face_id_init(&id_list, FACE_ID_SAVE_NUMBER, ENROLL_CONFIRM_TIMES);
  read_face_id_from_flash(&id_list);// Read current face data from on-board flash


#if defined(CAMERA_MODEL_M5STACK_WIDE)
  s->set_vflip(s, 1);
  s->set_hmirror(s, 1);
#endif

  //  WiFi.begin(ssid, password);

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

  //  startCameraServer();

  //  Serial.print("Camera Ready! Use 'http://");
  //  Serial.print(WiFi.localIP());
  //  Serial.println("' to connect");
}

void function1() {
  currentMillis = millis();
  if (run_face_recognition()) { // face recognition function has returned true
    Serial.println("Face recognised");
    digitalWrite(relayPin, HIGH); //close (energise) relay
    openedMillis = millis(); //time relay closed
  }
  if (currentMillis - interval > openedMillis) { // current time - face recognised time > 5 secs
    digitalWrite(relayPin, LOW); //open relay
  }
}
bool run_face_recognition() {
  bool faceRecognised = false; // default
  int64_t start_time = esp_timer_get_time();
  fb = esp_camera_fb_get();
  if (!fb) {
    Serial.println("Camera capture failed");
    return false;
  }

  int64_t fb_get_time = esp_timer_get_time();
  Serial.printf("Get one frame in %u ms.\n", (fb_get_time - start_time) / 1000); // this line can be commented out

  image_matrix = dl_matrix3du_alloc(1, fb->width, fb->height, 3);
  uint32_t res = fmt2rgb888(fb->buf, fb->len, fb->format, image_matrix->item);
  if (!res) {
    Serial.println("to rgb888 failed");
    dl_matrix3du_free(image_matrix);
  }

  esp_camera_fb_return(fb);

  box_array_t *net_boxes = face_detect(image_matrix, &mtmn_config);

  if (net_boxes) {
    if (align_face(net_boxes, image_matrix, aligned_face) == ESP_OK) {

      int matched_id = recognize_face(&id_list, aligned_face);
      if (matched_id >= 0) {
        Serial.printf("Match Face ID: %u\n", matched_id);
        faceRecognised = true; // function will now return true
      } else {
        Serial.println("No Match Found");
        matched_id = -1;
      }
    } else {
      Serial.println("Face Not Aligned");
    }

    free(net_boxes->box);
    free(net_boxes->landmark);
    free(net_boxes);
  }

  dl_matrix3du_free(image_matrix);
  return faceRecognised;
}

void loop() {
  switchState = digitalRead(BUTTON); // read the pushButton State
  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState;
    if (switchState == HIGH)
    {
      // toggle
      lightsOn = !lightsOn;
    }
  }
  if (lightsOn and wic == 0 )
  {
    digitalWrite(LED, HIGH); // set the LED on
    Serial.println("lightsOn led high wic = 0 start wifi");
    WiFi.begin(ssid, password);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");

    startCameraServer();
    Serial.print("Camera Ready! Use 'http://");
    Serial.print(WiFi.localIP());
    Serial.println("' to connect");
    wic = 1;
  } else {
    digitalWrite(LED, LOW); // set the LED off
    WiFi.disconnect();
    wic = 0;
    Serial.println("WiFi Disconnected");
    function1();
  }

}

It is a esp32-cam board and I get some modification from this site:

In Arduino example there is another two file comes with it but not needed here.

After some trails it works and new working code is attached
but After exiting else statement the device rebooting like this

.
WiFi connected
Starting web server on port: '80'
Starting stream server on port: '81'
Camera Ready! Use 'http://192.168.60.37' to connect
Get one frame in 71 ms.      ===>   here I click button
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.      ===>   here I click button again
     
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4008eb8e  PS      : 0x00060f33  A0      : 0x800909fe  A1      : 0x3ffb1e60  
A2      : 0x02000000  A3      : 0x0000abab  A4      : 0xb33fffff  A5      : 0x00000001  
A6      : 0x00060320  A7      : 0x0000cdcd  A8      : 0x0000abab  A9      : 0x3ffb1f50  
A10     : 0x00000000  A11     : 0x3ffc16d4  A12     : 0x3ffc1e8c  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x3ffb0060  SAR     : 0x0000001d  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x02000000  LBEG    : 0x40002390  LEND    : 0x4000239f  LCOUNT  : 0x00000000  

Backtrace: 0x4008eb8e:0x3ffb1e60 0x400909fb:0x3ffb1e90 0x400910a1:0x3ffb1eb0 0x400873d6:0x3ffb1ed0 0x4008789d:0x3ffb1ef0 0x4000bec7:0x3ffb1f10 0x40102c91:0x3ffb1f30 0x400d199d:0x3ffb1f50 0x400d19af:0x3ffb1f70 0x400d1a5f:0x3ffb1f90 0x400d4055:0x3ffb1fb0 0x4008db91:0x3ffb1fd0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8

.
WiFi connected
Starting web server on port: '80'
Starting stream server on port: '81'
Camera Ready! Use 'http://192.168.60.37' to connect

this not important as it function but I need to start the device on camrecognition() running and
then run camsetup() on button click so I change the loop as follows:

void loop() {
  switchState = digitalRead(BUTTON); // read the pushButton State
  if (switchState != oldSwitchState) // catch change
  {
    oldSwitchState = switchState; //  =1
    if (switchState == HIGH)
    {
      // toggle  as lightsOn=0
      lightsOn = !lightsOn;     // if not pressed lightsOn=1
    }
  }
  if (lightsOn)
  {
    digitalWrite(LED, LOW); // set the LED on
    wic = 0;
    WiFi.disconnect();
    camrecognition();
  } else {
    digitalWrite(LED, HIGH); // set the LED off

    if ( wic == 0 ) {
      camsetup();
    }
    wic = 1;
  }
}

And I get this on serial console:

Get one frame in 0 ms.    ====> device start
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.    ====>  click bottun
Guru Meditation Error: Core  1 panic'ed (StoreProhibited). Exception was unhandled.
Core 1 register dump:
PC      : 0x4008eb8e  PS      : 0x00060433  A0      : 0x800909fe  A1      : 0x3ffb1e60  
A2      : 0x02000000  A3      : 0x0000abab  A4      : 0xb33fffff  A5      : 0x00000001  
A6      : 0x00060820  A7      : 0x0000cdcd  A8      : 0x0000abab  A9      : 0x3ffb1f50  
A10     : 0x00000000  A11     : 0x3ffc16d4  A12     : 0x3ffc1e8c  A13     : 0x00000000  
A14     : 0x00000000  A15     : 0x00000200  SAR     : 0x0000001d  EXCCAUSE: 0x0000001d  
EXCVADDR: 0x02000000  LBEG    : 0x4000c46c  LEND    : 0x4000c477  LCOUNT  : 0x00000000  

Backtrace: 0x4008eb8e:0x3ffb1e60 0x400909fb:0x3ffb1e90 0x400910a1:0x3ffb1eb0 0x400873d6:0x3ffb1ed0 0x4008789d:0x3ffb1ef0 0x4000bec7:0x3ffb1f10 0x40102c91:0x3ffb1f30 0x400d199d:0x3ffb1f50 0x400d19af:0x3ffb1f70 0x400d1a47:0x3ffb1f90 0x400d4055:0x3ffb1fb0 0x4008db91:0x3ffb1fd0

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:1216
ho 0 tail 12 room 4
load:0x40078000,len:9720
ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8


Get one frame in 258 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.
Get one frame in 0 ms.

These means the function camrecognition() runs at start then after click button device rebooting.
Any suggestion ??

MergWork.ino (7.18 KB)