Site icon Renzo Mischianti

Flash size error OTA

Hello, I have problem to update flash OTA Connecting to: XXX.s3.amazonaws.com Fetching Bin: /XXX.littlefs.bin Got application/octet-stream payload. Got 1507328 bytes from server contentLength : 1507328, isValidContentType : 1 Not enough space to begin OTA Can someone help me?


void execOTA() {
	Serial.println("Connecting to: " + String(host));
	if (ota_h_client.connect(host.c_str(), port)) {
		Serial.println("Fetching Bin: " + String(bin2));

		ota_h_client.print(
				String("GET ") + bin2 + " HTTP/1.1\r\n" + "Host: " + host
						+ "\r\n" + "Cache-Control: no-cache\r\n"
						+ "Connection: close\r\n\r\n");

		unsigned long timeout = millis();
		while (ota_h_client.available() == 0) {
			if (millis() – timeout > 5000) {
				Serial.println("Client Timeout !");
				ota_h_client.stop();
				return;
			}
		}
		while (ota_h_client.available()) {
			String line = ota_h_client.readStringUntil(‘\n’);
			line.trim();
			if (!line.length()) {
				break;
			}

			if (line.startsWith("HTTP/1.1")) {
				if (line.indexOf("200") < 0) {
					Serial.println(
							"Got a non 200 status code from server. Exiting OTA Update.");
					break;
				}
			}

			if (line.startsWith("Content-Length: ")) {
				contentLength = atol(
						(getHeaderValue(line, "Content-Length: ")).c_str());
				Serial.println(
						"Got " + String(contentLength) + " bytes from server");
			}

			if (line.startsWith("Content-Type: ")) {
				String contentType = getHeaderValue(line, "Content-Type: ");
				Serial.println("Got " + contentType + " payload.");
				if (contentType == "application/octet-stream") {
					isValidContentType = true;
				}
			}
		}
	} else {
		Serial.println(
				"Connection to " + String(host)
						+ " failed. Please check your setup");
	}

	Serial.println(
			"contentLength : " + String(contentLength)
					+ ", isValidContentType : " + String(isValidContentType));

	if (contentLength &&isValidContentType) {
		bool canBegin = Update.begin(contentLength);

		if (canBegin) {
			Serial.println(
					"Begin OTA. This may take 2 – 5 mins to complete. Things might be quite for a while.. Patience!");
			size_t written = Update.writeStream(ota_h_client);

			if (written == contentLength) {
				Serial.println(
						"Written : " + String(written) + " successfully");
			} else {
				Serial.println(
						"Written only : " + String(written) + "/"
								+ String(contentLength) + ". Retry?");
			}

			if (Update.end()) {
				Serial.println("OTA done!");
				if (Update.isFinished()) {
					Serial.println("Update successfully completed. Rebooting.");
					ESP.restart();
				} else {
					Serial.println(
							"Update not finished? Something went wrong!");
				}
			} else {
				Serial.println(
						"Error Occurred. Error #: "
								+ String(Update.getError()));
			}
		} else {
			Serial.println("Not enough space to begin OTA");
			ota_h_client.flush();
		}
	} else {
		Serial.println("There was no content in the response");
		ota_h_client.flush();
	}
}

Exit mobile version