First of all, hello! My name is Emrah and I’m fairly new in this industry.
This might seem like a small and insignificant issue for some of you, but for me, it feels like an insurmountable topic. That's why I'm creating this thread to get support and experience from you, and I hope someone can help me with this.
Let me first introduce my device:
Device Name: Wemos D1 R32
Features:
- Chip: ESP32-D0WD-V3 (revision v3.1)
- Features: WiFi, BT, Dual Core, 240MHz
- Crystal: 40MHz
- Microcontroller: ESP-WROOM-32
- Operating Voltage: 3.3V
- Digital I/O Pins: 18 (of which 4 provide PWM output)
- Analog Input Pins: 5 ADC
- Serial Support: I2C, SPI, UART, I2S, CAN
- Flash Memory: 4MB
- On-board Wi-Fi: 802.11 b/g/n, BT 5
- Bluetooth v4.2 BR/EDR
- 240 MHz dual-core ESP32 chip
- USB-to-Serial converter: iFT-232-S16
- Dimensions: 68.6mm x 53.4mm
- Supply Voltage: DC 5V to 12V
I have a piece of code for this device that performs a role-triggering action, though it has other functionalities as well, but its main purpose is this.
From time to time, I may need to update this device, and that’s why I cannot always keep it connected via cable. I need to perform OTA updates, but there are three different files in my project. Let me show you the structure of my project directory:
main (folder)
- main.ino
- main_code.cpp
- main_code.h
In the .h file, I make definitions, in the .cpp file, I write the code, and finally, in the .ino file, I call these files.
Here’s where my problem starts. I have the following line:
// Show connection status every 5 seconds
if (millis() - lastStatusTime >= 5000) { // 5 second check
logSleepStatus();
lastStatusTime = millis();
if (...) {
Serial.println("System active!");
} else {
Serial.println("System inactive!");
Serial.println("The device will be reset.");
ESP.restart();
}
}
When I upload this code to the board via USB, I see "System active!" in the serial output.
Then I added another serial print like this:
// Show connection status every 5 seconds
if (millis() - lastStatusTime >= 5000) { // 5 second check
logSleepStatus();
lastStatusTime = millis();
if (...) {
Serial.println("System active!");
Serial.println("Everything is fine!"); // NEW PART ADDED HERE
} else {
Serial.println("System inactive!");
Serial.println("The device will be reset.");
ESP.restart();
}
}
After that, I didn’t upload it directly; I wanted to update via OTA. Here’s my OTA code:
void checkForUpdates() {
HTTPClient http;
http.begin("<BIN_FILE_URL>");
int httpCode = http.GET();
// 301 Redirect check
if (httpCode == HTTP_CODE_MOVED_PERMANENTLY || httpCode == HTTP_CODE_FOUND) {
String newLocation = http.header("Location");
Serial.printf("Redirecting to new URL: %s\n", newLocation.c_str());
http.end();
http.begin(newLocation); // Continue with new URL
httpCode = http.GET();
}
if (httpCode == HTTP_CODE_OK) {
int contentLength = http.getSize();
WiFiClient *stream = http.getStreamPtr();
if (Update.begin(contentLength)) {
size_t written = Update.writeStream(*stream);
if (written == contentLength) {
if (Update.end()) {
Serial.println("OTA Update Successful, device restarting...");
ESP.restart();
}
} else {
Serial.println("OTA Update Failed: The full file was not written.");
}
} else {
Serial.println("Not enough space for OTA Update.");
}
} else {
Serial.printf("HTTP Error: %d\n", httpCode);
}
http.end();
}
Then, I selected "Sketch -> Export Compiled Binary," placed the "main.ino.bin" file in my server, and triggered the "checkForUpdates()" function on the device. It successfully downloaded the file, installed it, and reset the device. However, when I checked the serial output, I didn’t see the additional serial log I added. It’s as if the changes I made in the .cpp file aren’t included in the .bin file, and the update didn’t reflect them. I can’t make sense of this situation. In the end, I decided to try uploading the "main.ino.merged.bin" file, but this file was 4096 KB in size, and the upload failed due to the size. The other file was 1093 KB.
Right now, I’m stuck and I’ve tried everything without success. Perhaps someone here has experience with this and can guide me.
The device's usage statistics are as follows:
"
Sketch uses 1112193 bytes (84%) of program storage space. Maximum is 1310720 bytes.
Global variables use 48000 bytes (14%) of dynamic memory, leaving 279680 bytes for local variables. Maximum is 327680 bytes.
"
Here is my settings:
Thanks to everyone in advance, and sorry for the long message.

