Ymodem ESP32 1.0.0
Loading...
Searching...
No Matches
ReceiveExample.cpp
Go to the documentation of this file.
1
12#include "YmodemCore.h"
13#include <Arduino.h>
14#include <HardwareSerial.h>
15#include <SPIFFS.h>
16#include <Update.h>
17
18// Define CONFIG_SPIFFS_SIZE with an appropriate value
19#define CONFIG_SPIFFS_SIZE (2 * 1024 * 1024)
20#define MAX_FILE_SIZE (CONFIG_SPIFFS_SIZE - 0x2000)
21#define FIRMWARE_UPDATE
36void setup()
37{
38 // Initialize Serial for debugging
39 Serial.begin(115200);
40 while (!Serial) {
41 ; // Wait until Serial is ready
42 }
43
44 // Initialize SPIFFS
45 if (!SPIFFS.begin(true)) {
46 Serial.println("Error mounting SPIFFS");
47 return;
48 }
49
50 // Configure the YModem activity LED pin
51 pinMode(YMODEM_LED_ACT, OUTPUT);
52 digitalWrite(YMODEM_LED_ACT, YMODEM_LED_ACT_ON ^ 1);
53
54 // Configure UART (Serial1)
55 Serial1.begin(115200, SERIAL_8N1, 16, 17); // RX, TX pins
56 if (!Serial1) {
57 log_e("Error initializing Serial1");
58 return;
59 }
60
61 // Setup complete message
62 log_i("Setup complete. Ready to receive files.");
63}
64
79void loop()
80{
81 static int nfile = 1;
82 char fname[128];
83 char orig_name[256];
84 uint32_t max_fsize;
85 int rec_res = -1;
86
87 // ==== File reception ====
88 max_fsize = SPIFFS.totalBytes() - SPIFFS.usedBytes();
89 if (max_fsize > 16 * 1024) {
90 if (max_fsize > MAX_FILE_SIZE) {
91 max_fsize = MAX_FILE_SIZE;
92 }
93
94 sprintf(fname, "/firmware-%d.bin", nfile);
95 File ffd = SPIFFS.open(fname, FILE_WRITE);
96 if (ffd) {
97 Serial.println("\r\nReceiving file, start YModem transfer on the host...\r\n");
98 rec_res = ymodem.receive(ffd, max_fsize, orig_name);
99 ffd.close();
100 Serial.println("\r\n");
101
102 if (rec_res > 0) {
103 log_i("Transfer complete. Size=%d, Original name: \"%s\"", rec_res, fname);
104
105#ifdef FIRMWARE_UPDATE
106 // ==== Load the binary to the ESP32 ====
107 File bin_file = SPIFFS.open(fname, FILE_READ);
108 if (bin_file) {
109 size_t bin_size = bin_file.size();
110 if (Update.begin(bin_size)) {
111 log_d("Starting OTA update...");
112 size_t written = Update.writeStream(bin_file);
113 if (written == bin_size)
114 log_i("Write complete.");
115 else
116 log_w("Incomplete write. Only %d of %d bytes written", written, bin_size);
117
118 if (Update.end()) {
119 if (Update.isFinished()) {
120 log_i("OTA update complete. Restarting...");
121 while (1) {
122 digitalWrite(YMODEM_LED_ACT, YMODEM_LED_ACT_ON ^ 1);
123 vTaskDelay(100);
124 }
125 }
126 else
127 log_e("OTA update not complete.");
128 }
129 else
130 log_e("Error in OTA update: %s", Update.errorString());
131 }
132 else
133 log_e("Could not start OTA update.");
134 bin_file.close();
135 }
136 else
137 log_e("Error opening binary file \"%s\" for OTA.", fname);
138#endif
139 }
140 else {
141 log_e("Transfer error. Error code=%d", rec_res);
142 SPIFFS.remove(fname);
143 }
144 }
145 else
146 log_e("Error opening file \"%s\" for receiving.", fname);
147
148 delay(1000);
149 }
150 else {
151 log_e("Filesystem full. Remaining space: %u bytes", max_fsize);
152 }
153
154 delay(10);
155}
void setup()
Setup function to initialize Serial, SPIFFS, and UART for YModem file reception.
#define MAX_FILE_SIZE
Ymodem ymodem
void loop()
Main loop function for receiving and updating firmware via YModem protocol.
Ymodem core functions.
#define YMODEM_LED_ACT_ON
Definition YmodemDef.h:24
#define YMODEM_LED_ACT
Definition YmodemDef.h:23
Ymodem class.
Definition YmodemCore.h:30
int receive(fs::File &ffd, unsigned int maxsize, char *getname)
Receives data and writes it to the provided file.