Skip to content

Commit 5ebe76d

Browse files
committed
feat(hosted): Document inline the source code
1 parent 0a950cc commit 5ebe76d

File tree

3 files changed

+100
-12
lines changed

3 files changed

+100
-12
lines changed

libraries/ESP_HostedOTA/examples/ESP_HostedOTA/ESP_HostedOTA.ino

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,62 @@
1-
#include "WiFi.h"
2-
#include "ESP_HostedOTA.h"
1+
/*
2+
* ESP-Hosted OTA Update Example
3+
*
4+
* This example demonstrates how to update the ESP-Hosted co-processor firmware
5+
* over-the-air (OTA). The ESP-Hosted solution allows an ESP32 to act as a WiFi
6+
* co-processor for other microcontrollers.
7+
*
8+
* Prerequisites:
9+
* - ESP32 with ESP-Hosted firmware configured as WiFi co-processor
10+
* - Network connectivity to download firmware updates
11+
* - Valid WiFi credentials
12+
*/
313

4-
const char *ssid = "your-ssid"; // Change this to your WiFi SSID
5-
const char *password = "your-password"; // Change this to your WiFi password
14+
#include "WiFi.h" // WiFi library for network connectivity
15+
#include "ESP_HostedOTA.h" // ESP-Hosted OTA update functionality
16+
17+
// WiFi network credentials - CHANGE THESE TO YOUR NETWORK SETTINGS
18+
const char *ssid = "your-ssid"; // Replace with your WiFi network name
19+
const char *password = "your-password"; // Replace with your WiFi password
620

721
void setup() {
22+
// Step 1: Initialize serial communication for debugging output
823
Serial.begin(115200);
924

25+
// Step 2: Initialize the ESP-Hosted WiFi station mode
26+
// This prepares the ESP-Hosted co-processor for WiFi operations
1027
WiFi.STA.begin();
1128

29+
// Step 3: Display connection attempt information
1230
Serial.println();
1331
Serial.println("******************************************************");
1432
Serial.print("Connecting to ");
1533
Serial.println(ssid);
1634

35+
// Step 4: Attempt to connect to the specified WiFi network
1736
WiFi.STA.connect(ssid, password);
1837

38+
// Step 5: Wait for WiFi connection to be established
39+
// Display progress dots while connecting
1940
while (WiFi.STA.status() != WL_CONNECTED) {
20-
delay(500);
21-
Serial.print(".");
41+
delay(500); // Wait 500ms between connection attempts
42+
Serial.print("."); // Show connection progress
2243
}
2344
Serial.println();
2445

46+
// Step 6: Display successful connection information
2547
Serial.println("WiFi connected");
2648
Serial.print("IP address: ");
2749
Serial.println(WiFi.STA.localIP());
2850

51+
// Step 7: Attempt to update the ESP-Hosted co-processor firmware
52+
// This function will:
53+
// - Check if ESP-Hosted is initialized
54+
// - Verify if an update is available
55+
// - Download and install the firmware update if needed
2956
if (updateEspHostedSlave()) {
30-
// Currently it's required to restart the host
57+
// Step 8: Restart the host ESP32 after successful update
58+
// This is currently required to properly activate the new firmware
59+
// on the ESP-Hosted co-processor
3160
ESP.restart();
3261
}
3362
}
Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,159 @@
1+
// Include necessary headers for SDK configuration, Arduino framework, and networking
12
#include "sdkconfig.h"
23
#include <stdbool.h>
34
#if CONFIG_ESP_WIFI_REMOTE_ENABLED
45
#include "Arduino.h"
5-
#include "esp32-hal-hosted.h"
6-
#include "Network.h"
7-
#include "HTTPClient.h"
8-
#include "NetworkClientSecure.h"
6+
#include "esp32-hal-hosted.h" // ESP-Hosted specific functions
7+
#include "Network.h" // Network connectivity management
8+
#include "HTTPClient.h" // HTTP client for downloading updates
9+
#include "NetworkClientSecure.h" // Secure network client for HTTPS
910
#endif
1011

12+
/**
13+
* Updates the ESP-Hosted co-processor firmware over-the-air (OTA)
14+
* This function downloads and installs firmware updates for the ESP-Hosted slave device
15+
* @return true if update was successful, false otherwise
16+
*/
1117
bool updateEspHostedSlave() {
1218
#if CONFIG_ESP_WIFI_REMOTE_ENABLED
1319
bool updateSuccess = false;
20+
21+
// Step 1: Verify ESP-Hosted is properly initialized
1422
if (!hostedIsInitialized()) {
1523
Serial.println("ERROR: esp-hosted is not initialized. Did you call WiFi.STA.begin()?");
1624
return updateSuccess;
1725
}
26+
27+
// Step 2: Check if an update is actually available
1828
if (!hostedHasUpdate()) {
19-
// esp-hosted is already the latest version
29+
// esp-hosted is already the latest version - no update needed
2030
return updateSuccess;
2131
}
32+
33+
// Step 3: Ensure network connectivity is available
2234
if (!Network.isOnline()) {
2335
Serial.println("ERROR: Network is not online! Did you call WiFi.STA.connect(ssid, password)?");
2436
return updateSuccess;
2537
}
38+
39+
// Step 4: Begin the update process - display update URL
2640
Serial.print("Updating esp-hosted co-processor from ");
2741
Serial.println(hostedGetUpdateURL());
42+
43+
// Step 5: Create a secure network client for HTTPS communication
2844
NetworkClientSecure *client = new NetworkClientSecure();
2945
if (!client) {
3046
Serial.println("ERROR: Could not allocate client!");
3147
return updateSuccess;
3248
}
49+
50+
// Step 6: Configure client to skip certificate verification (insecure mode)
3351
client->setInsecure();
52+
53+
// Step 7: Initialize HTTP client and attempt to connect to update server
3454
HTTPClient https;
3555
int httpCode = 0;
3656
if (!https.begin(*client, hostedGetUpdateURL())) {
3757
Serial.println("ERROR: HTTP begin failed!");
3858
goto finish_ota;
3959
}
60+
61+
// Step 8: Send HTTP GET request to download the firmware
4062
httpCode = https.GET();
4163
if (httpCode == HTTP_CODE_OK) {
64+
// Step 9: Get the size of the firmware file to download
4265
int len = https.getSize();
4366
if (len < 0) {
4467
Serial.println("ERROR: Update size not received!");
4568
https.end();
4669
goto finish_ota;
4770
}
71+
72+
// Step 10: Get stream pointer for reading firmware data
4873
NetworkClient *stream = https.getStreamPtr();
74+
75+
// Step 11: Initialize the ESP-Hosted update process
4976
if (!hostedBeginUpdate()) {
5077
Serial.println("ERROR: esp-hosted update start failed!");
5178
https.end();
5279
goto finish_ota;
5380
}
81+
82+
// Step 12: Allocate buffer for firmware data transfer (2KB chunks)
5483
#define HOSTED_OTA_BUF_SIZE 2048
5584
uint8_t * buff = (uint8_t*)malloc(HOSTED_OTA_BUF_SIZE);
5685
if (!buff) {
5786
Serial.println("ERROR: Could not allocate OTA buffer!");
5887
https.end();
5988
goto finish_ota;
6089
}
90+
91+
// Step 13: Download and write firmware data in chunks
6192
while (https.connected() && len > 0) {
6293
size_t size = stream->available();
6394
if (size > 0) {
95+
// Show progress indicator
6496
Serial.print(".");
97+
98+
// Limit chunk size to buffer capacity
6599
if (size > HOSTED_OTA_BUF_SIZE) {
66100
size = HOSTED_OTA_BUF_SIZE;
67101
}
102+
103+
// Prevent reading more data than expected
68104
if (size > len) {
69105
Serial.printf("\nERROR: Update received extra bytes: %u!", size - len);
70106
break;
71107
}
108+
109+
// Read firmware data chunk into buffer
72110
int readLen = stream->readBytes(buff, size);
73111
len -= readLen;
112+
113+
// Write the chunk to ESP-Hosted co-processor
74114
if (!hostedWriteUpdate(buff, readLen)) {
75115
Serial.println("\nERROR: esp-hosted update write failed!");
76116
break;
77117
}
118+
119+
// Step 14: Check if entire firmware has been downloaded
78120
if (len == 0) {
121+
// Finalize the update process
79122
if (!hostedEndUpdate()) {
80123
Serial.println("\nERROR: esp-hosted update end failed!");
81124
break;
82125
}
126+
127+
// Activate the new firmware
83128
if (!hostedActivateUpdate()) {
84129
Serial.println("\nERROR: esp-hosted update activate failed!");
85130
break;
86131
}
132+
133+
// Update completed successfully
87134
updateSuccess = true;
88135
Serial.println("\nSUCCESS: esp-hosted co-processor updated!");
89136
break;
90137
}
91138
}
139+
// Small delay to prevent overwhelming the system
92140
delay(1);
93141
}
142+
143+
// Step 15: Clean up allocated buffer
94144
free(buff);
95145
Serial.println();
96146
}
97147

148+
// Step 16: Close HTTP connection
98149
https.end();
150+
99151
finish_ota:
152+
// Step 17: Clean up network client
100153
delete client;
101154
return updateSuccess;
102155
#else
156+
// ESP-Hosted functionality is not enabled in SDK configuration
103157
return false;
104158
#endif
105159
}

libraries/ESP_HostedOTA/src/ESP_HostedOTA.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
#include <stdbool.h>
44

5+
/**
6+
* Updates the ESP-Hosted co-processor firmware over-the-air (OTA)
7+
* This function downloads and installs firmware updates for the ESP-Hosted slave device
8+
* @return true if update was successful, false otherwise
9+
*/
510
bool updateEspHostedSlave();

0 commit comments

Comments
 (0)