Local Server Pull - Server Files
=================================

This folder holds the files the ESP32 downloads during Experiment 4.

Files:
  latest.txt    The newest version string available (e.g. "1.1").
                The ESP32 reads this and compares it to its own version.
  firmware.bin  The compiled firmware the ESP32 downloads when an
                update is available. (You create this - see below.)

----------------------------------------------------------------
STEP 1 - Start the server (FORCE IPv4)
----------------------------------------------------------------
Open a terminal IN THIS FOLDER. Start the server bound to your
PC's IPv4 address so it listens on IPv4 only:

    python3 -m http.server 8080 --bind 192.168.1.114
            (replace 192.168.1.114 with YOUR PC's IPv4 address)

WHY --bind: On Windows 11, plain "python3 -m http.server 8080"
often listens on IPv6. You will see log lines like:
    ::ffff:192.168.1.111 - - [..] "GET /latest.txt HTTP/1.1" 200
The ESP32 speaks IPv4, and the very first request can fail with
error code -1 against an IPv6-mode server. Binding to your IPv4
address fixes this. After binding, log lines show a plain
192.168.1.x address with no "::ffff:" prefix.

Find your PC's LAN IP:
    Windows:  ipconfig        (look for IPv4 Address)
    macOS:    ipconfig getifaddr en0
    Linux:    hostname -I

Put that same IP into the sketch (SERVER_HOST).

----------------------------------------------------------------
STEP 2 - Create latest.txt WITHOUT a BOM
----------------------------------------------------------------
latest.txt must contain ONLY the version (e.g. 1.0) with no
hidden bytes. Windows Notepad adds an invisible UTF-8 byte-order
mark (BOM) that corrupts the version string - you will see junk
characters in the serial monitor like:  Server version: ??1.1

Create it cleanly one of these ways:
  - VS Code or Notepad++: set encoding to "UTF-8 without BOM"
  - Windows cmd:        echo 1.0> latest.txt   (no space before >)
  - PowerShell:         Set-Content -NoNewline latest.txt "1.0"

The sketch also strips non-version characters defensively, but a
clean file is best.

----------------------------------------------------------------
STEP 3 - Demo "up to date"
----------------------------------------------------------------
Set latest.txt to match the version flashed on the board (1.0):

    echo 1.0> latest.txt

Press the board's pushbutton -> serial shows:
    Current version: 1.0
    Server version:  1.0
    Up to date.

----------------------------------------------------------------
STEP 4 - Make the update available
----------------------------------------------------------------
1. In the Arduino IDE, open 04_LocalServerPull.ino and change:
       const char* LOCAL_VERSION = "1.1";
   plus any visible change (e.g. an OLED message).

2. Sketch > Export Compiled Binary.

3. Find the exported binary in the sketch's build folder:
       04_LocalServerPull.ino.bin
   (NOT the .merged.bin or .partitions.bin - OTA uses the plain
   application .bin only.) Copy it here and rename it to:
       firmware.bin

4. Set the advertised version to 1.1 (no BOM, see Step 2):
       echo 1.1> latest.txt

   No need to restart the Python server - it serves live.

----------------------------------------------------------------
STEP 5 - Trigger the update
----------------------------------------------------------------
Press the board's pushbutton again. Serial Monitor shows:

    Current version: 1.0
    Server version:  1.1
    Update available. Downloading update...

The OLED shows download progress, then the board reboots and
shows v1.1. Press the button again -> "Up to date".

----------------------------------------------------------------
TROUBLESHOOTING
----------------------------------------------------------------
"latest.txt GET failed, code -1"
  - SERVER_HOST in the sketch does not match the PC's IP, OR
  - server not started, OR started with --bind 127.0.0.1, OR
  - server is in IPv6 mode (see STEP 1, use --bind <IPv4>).
  Test from another device's browser: http://<PC-IP>:8080/latest.txt

Reads the file but always says "Up to date" with junk chars
  - latest.txt has a BOM (see STEP 2). Recreate it without one.

Confirm the server sees the request: watch the terminal for a
"GET /latest.txt HTTP/1.1" 200 line when you press the button.
