------------------------------------------------------------------------
Upload code for ESP32-CAM module using Arduino UNO and Arduino IDE:
------------------------------------------------------------------------

Connection ESP32 cam to Arduino UNO:
    Connect Arduino 5volt to esp 32 cam 5 volt.
    Arduino GND to GND
    Arduino RX to Cam board RX (UnR) & TX to TX (UOT)
    Arduino reset pin to GND
    ESP 32cam D0 (IO0) to Gnd
Make sure the cam board DO is connected to gnd. It will enable esp 32 flash mode otherwise you can’t program it. after compleat, the programming remove D0 to gnd!

Now open Arduino IDE on your PC, go to File > Preferences >

Now paste below link in the board manager URL

https://dl.espressif.com/dl/package_esp32_index.json

Now go to Tools > Board > Board manager > & search for ESP 32

You can see esp 32 board Download & install the latest version package

After complete, the installation go to Tools > Select port where arduino UNO is connected

    Now go to board > select board > ESP32 ” Wrover Module” (from the "long" list of ESP32 Arduino)
    Flash Mode > QIO
    Flash Frequency > 40MHZ
    Partition Scheme > Huge App (3mb No OTA)
    Upload speed > 115200
    Programmer > AVR ISP (if no programmer listet, it even works without)

On Mint20 install the following packages:
sudo apt install python-is-python3
sudo apt install python3-serial
Close Arduino IDE and reopen

Now it is ready for uploading sketch
You can test it Go to File > Example > ESP32 > Camera > Camera web server and upload the sample sketch
In the sample sketch uncomment the corect camera module (e.g. #define CAMERA_MODEL_AI_THINKER)

While uploading, press "Reset" on the ESP32 board to initiate upload!

When the device has completed flashing, unplug IO0 from GND.

Open the serial monitor via Tools > Serial Monitor.

Press the reset button on the ESP32-CAM and watch the start up sequence in the serial monitor (you probably have to reset TWICE to make it work!!!):

Camera Web Server Startup

Look for the IP address that the ESP32 has been given on your network.

Type that IP address into your browser. You should be able to see a GUI on the left where you can control elements of the camera, set face detection and face recognition. Click Get Still to take a photo. Click Start Stream to see a video stream from the camera.

------------------------------------------------------------------------
Modify ESP32 for static IP:
Add the lines at the beginning of the Arduino.ino sketch (NOT in the setup or loop section):

// Set your Static IP address
IPAddress local_IP(192, 168, 2, 184);
// Set your Gateway IP address
IPAddress gateway(192, 168, 2, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional


------------------------------------------------------------------------
Modifying ESP32-cam webserver:

Add a toggle switch (checkbox):
In index.html search for:
<div class="input-group" id="face_recognize-group">

and below that </div>, add the lines:
<div class="input-group" id="flash-group">
	<label for="flash">Flash</label>
	<div class="switch">
		<input id="flash" type="checkbox" class="default-action">
		<label class="slider" for="flash"></label>
	</div>
</div>


Add a button:
In index.html search for:
<section id="buttons">

Add drive forward button with additional line:
<button id="drive_forward">Drive Forward</button>

Then search for:
const stillButton = document.getElementById('get-still')

and add line:
const DriveForwardButton = document.getElementById('drive_forward')

Then search for:
enrollButton.onclick = () => {

Add ABOVE that line or below that whole function:
  DriveForwardButton.onclick = () => {
    DriveForwardButton.innerHTML = 'Driving...'
    updateConfig(DriveForwardButton)
  }


Now the index.html must be transformed into compressed format. Copy and paste the index.html online at:
https://gchq.github.io/CyberChef/#recipe=Gzip('Dynamic%20Huffman%20Coding','index.html.gz','',false)To_Hex('0x',16)Split('0x',',0x')

Make sure that the following is adjusted on the page:
Compression: "Dynamic Hoffman Coding"
Delimiter: 0x
Bytes per line: 16
Split delimiter: 0x
Join delimiter: ,0x

Replace the result in the file camera_index.h. Note that you must remove the first ',' in the first line
In the line:
#define index_ov2640_html_gz_len 4390
set the size of the pasted, compressed index.html in bytes! To get the result, count the lines without the final line and multiply by 16 (Bytes per line) and add the bytes of the final line. I haven't seen if the online editor shows the size of the compressed file somewhere...

Now modify the apt_http.cpp:
Search for:
else if(!strcmp(variable, "face_recognize")) {

and add lines ABOVE that line or below that else if block:
    else if(!strcmp(variable, "flash")) {
        #define LED_BUILTIN 33
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, atoi(value));
    }
    else if(!strcmp(variable, "drive_forward")) {
        #define LED_BUILTIN 4
        pinMode(LED_BUILTIN, OUTPUT);
        digitalWrite(LED_BUILTIN, 1);
    }

Compile and upload the modifies code to the ESP32 as described above. Reboot ESP32 afterwards and see the changes

------------------------------------------------------------------------
Download a picture to Raspberry Pi with:
wget "http://192.168.2.184/capture?101.jpg"

Control the buttons with (don't forget the ""):
wget "192.168.2.184/control?var=drive_forward&val=1"


