Skip to content

Instantly share code, notes, and snippets.

@alvarowolfx
Created November 22, 2018 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alvarowolfx/cd9038d17fc15a7f52eaaad71d5430e2 to your computer and use it in GitHub Desktop.
Save alvarowolfx/cd9038d17fc15a7f52eaaad71d5430e2 to your computer and use it in GitHub Desktop.
Google Cloud OTA Update Post - Arduino main.cpp
/*
* Check if needs to update the device and returns the download url.
*/
String getDownloadUrl()
{
HTTPClient http;
String downloadUrl;
USE_SERIAL.print("[HTTP] begin...\n");
String url = CLOUD_FUNCTION_URL;
url += String("?version=") + CURRENT_VERSION;
url += String("&variant=") + VARIANT;
http.begin(url);
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK)
{
String payload = http.getString();
USE_SERIAL.println(payload);
downloadUrl = payload;
} else {
USE_SERIAL.println("Device is up to date!");
}
}
else
{
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
return downloadUrl;
}
/*
* Download binary image and use Update library to update the device.
*/
bool downloadUpdate(String url)
{
HTTPClient http;
USE_SERIAL.print("[HTTP] Download begin...\n");
http.begin(url);
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
if (httpCode > 0)
{
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK)
{
int contentLength = http.getSize();
USE_SERIAL.println("contentLength : " + String(contentLength));
if (contentLength > 0)
{
bool canBegin = Update.begin(contentLength);
if (canBegin)
{
WiFiClient stream = http.getStream();
USE_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(stream);
if (written == contentLength)
{
USE_SERIAL.println("Written : " + String(written) + " successfully");
}
else
{
USE_SERIAL.println("Written only : " + String(written) + "/" + String(contentLength) + ". Retry?");
}
if (Update.end())
{
USE_SERIAL.println("OTA done!");
if (Update.isFinished())
{
USE_SERIAL.println("Update successfully completed. Rebooting.");
ESP.restart();
return true;
}
else
{
USE_SERIAL.println("Update not finished? Something went wrong!");
return false;
}
}
else
{
USE_SERIAL.println("Error Occurred. Error #: " + String(Update.getError()));
return false;
}
}
else
{
USE_SERIAL.println("Not enough space to begin OTA");
client.flush();
return false;
}
}
else
{
USE_SERIAL.println("There was no content in the response");
client.flush();
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
void setup(){
// Check if we need to download a new version
String downloadUrl = getDownloadUrl();
if (downloadUrl.length() > 0)
{
bool success = downloadUpdate(downloadUrl);
if (!success)
{
USE_SERIAL.println("Error updating device");
}
}
}
@AustrofrioIoT
Copy link

Bro the code its not complete here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment