# task_2
## Reference
- Github repo: [esp32-softap-ota](https://github.com/Jeija/esp32-softap-ota/tree/master)
- ESP-IDF Documentation: [Ota the air updates](https://docs.espressif.com/projects/esp-idf/en/v4.4.3/esp32/api-reference/system/ota.html)
## Flow
1. Partition Configuration: Ensure that your partitions are set up properly for OTA updates. The ESP-IDF framework typically uses two OTA partitions - one for the currently running firmware and another for the new firmware being updated. These partitions are switched during the update process.
2. Update Firmware: use the 'esp_ota_begin' 'esp_ota_write' and 'esp_ota_end' functions to write the firmware to the OTA partition.
The reference code (update_post_handler function) already contains the logic for handling the OTA update.
3. Upload Firmware: Access the OTA update portal through a web browser, select the new firmware binary, and initiate the upload process. The ESP32 will handle the firmware update and reboot automatically.
4. Verify Update: The code includes a verification step (esp_ota_set_boot_partition) to ensure that the new firmware is valid. If the verification fails, the update process is aborted.
5. Reboot: After a successful update, the device reboots to run the new firmware.
6. Rollback (Optional): The code checks for the OTA update status and cancels the rollback if the new firmware is successfully verified. You may modify this behavior based on your requirements.
## Important terms:
1. **OTA partitions:** one for the currently running firmware (e.g., "ota_0") and another for the new firmware being updated (e.g., "ota_1").
2. **Partition Table:**
- The partition table occupies an entire flash sector, which size is 0x1000 (4 KB). As a result, any partition following it must be at least located at (default offset) + 0x1000.
- The partition table to flash to the ESP32. The partition table determines where apps, data and other resources are expected to be found.
1. Single factory app no OTA:

3. **Out handle:** On success, returns a handle which should be used for subsequent esp_ota_write() and esp_ota_end() calls. After ota begin allocates memory that remains in use until esp_ota_end() is called with the returned handle(out_handle).
2. Factory app, two OTA definitions
Factory app, two OTA definitions
``` # ESP-IDF Partition Table
# Name, Type, SubType, Offset, Size, Flags
nvs, data, nvs, 0x9000, 0x4000,
otadata, data, ota, 0xd000, 0x2000,
phy_init, data, phy, 0xf000, 0x1000,
factory, app, factory, 0x10000, 1M,
ota_0, app, ota_0, 0x110000, 1M,
ota_1, app, ota_1, 0x210000, 1M,
```
## ERROS and their solutions:
1. **ERROR:**

**SOLUTION:** Changed size of ota_0 and ota_1 from 1MB to 200kB

2. **ERROR:**

**SOLUTION**
Referred links:
[Set OTA on NINA W106 00B](https://forums.freertos.org/t/set-ota-on-nina-w106-00b/17467)
https://github.com/espressif/esp-idf/issues/8593
3. **ERROR:** Ota handle not foud basically ota_0 or 1 not creating after ota begin

**SOLUTION:**
Give proper size1 for ota_0 and ota_1.. size should be greater than size of .bin file
Ref link: https://github.com/espressif/esp-idf/issues/8817
4. **ERROR:** partition is not alligned to 4kb

**SOLUTION**
give proper offests.. if not then this error occure, if you dos'nt know the proper offset then give only 1st offset properly.. because Partitions with blank offsets in the CSV file will start after the previous partition, or after the partition table in the case of the first partition.
If you leave the offset field blank, gen_esp32part.py will automatically align the partition. If you specify an unaligned offset for an app partition, the tool will return an error.
Refer link:
[ESP IDF PARTITION DOCS](https://docs.espressif.com/projects/esp-idf/en/v4.4.3/esp32/api-guides/partition-tables.html)
[How to do ESP32 flash partition? | How ESP32 flash partition work?](https://medium.com/@bhautik.markeye/how-to-do-esp32-flash-partition-how-esp32-flash-partition-work-720a9f6eb0c2#id_token=eyJhbGciOiJSUzI1NiIsImtpZCI6IjBlNzJkYTFkZjUwMWNhNmY3NTZiZjEwM2ZkN2M3MjAyOTQ3NzI1MDYiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhenAiOiIyMTYyOTYwMzU4MzQtazFrNnFlMDYwczJ0cDJhMmphbTRsamRjbXMwMHN0dGcuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJhdWQiOiIyMTYyOTYwMzU4MzQtazFrNnFlMDYwczJ0cDJhMmphbTRsamRjbXMwMHN0dGcuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJzdWIiOiIxMDU4MDgzNTUzODQzNjUyMDI2MTEiLCJoZCI6ImV0LnZqdGkuYWMuaW4iLCJlbWFpbCI6InNyc29uYXdhbmVfYjIxQGV0LnZqdGkuYWMuaW4iLCJlbWFpbF92Z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