Easy Update ESP8266/ESP32 Via Internet (OTA Server for ESP8266)

Introduction: Easy Update ESP8266/ESP32 Via Internet (OTA Server for ESP8266)

When you build a device based on any MCU, you will face a lot of bugs after install device in area. By the way changing of firmware is not always easy, sometimes device installed in out of reach place and you should update the firmware remotely.

The problem gets more serious when you made and installed multiple device and it may be impossible to upgrade new firmware for all devices.

OTA upgrade makes it possible to upgrade firmware of a lot of devices simultaneously.

To use OTA, you should call a function to get the last firmware from OTA server. But OTA server is not something easy to reach.

Step 1: Step 1 : a Simple Blinker With OTA Update

Now, I want to write a simple program for ESP8266 using arduino framework, It's a LED blinker, i will compile this project with two different blink speed and then I will change the firmware via internet.

The source code should be something like this:

#include "Arduino.h"
#include "ESP8266httpUpdate.h"
String version = "1.0.0.0";
void doUpdate();

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
  WiFi.begin("SohaDevice", "DamkpKCk");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.println(".");
    delay(150);
  }

  Serial.print("Wifi IP:");
  Serial.println(WiFi.localIP());

  // Setup LED GPIO
  pinMode(2,OUTPUT);
}

int updateCheckTimer = 0;
void loop()
{
  // Lets blink
  digitalWrite(2, 1);
  delay(100);
  digitalWrite(2, 0);
  delay(100);

  updateCheckTimer++;
  if(updateCheckTimer > 30)
  {
    updateCheckTimer = 0;
    doUpdate();
  }
}

void doUpdate()
{
  String url = "http://otadrive.com/DeviceApi/GetEsp8266Update?k=9a03f4fb-98c5-4963-9d39-cc9181f65edd";
  url += "&s=" + String(CHIPID);
  url += "&v=" + version;

  t_httpUpdate_return ret = ESPhttpUpdate.update(url, version);
  switch (ret)
  {
  case HTTP_UPDATE_FAILED:
    Serial.println("Update faild!");
    break;
  case HTTP_UPDATE_NO_UPDATES:
    Serial.println("No new update available");
    break;
  // We can't see this, because of reset chip after update OK
  case HTTP_UPDATE_OK:
    Serial.println("Update OK");
    break;

  default:
    break;
  }
}

All you need to know is in doUpdate() function. There is a url to otadrive.com server with let you to upgrade and manage devices.

To more info about otadrive.com see following video.

OTAdrive.com take you a unique key for each product (ex: 9a03f4fb-98c5-4963-9d39-cc9181f65edd), you should replace your own product key before compile above program.To get your product key, go to API section.

After that you could make several device groups and move devices to separate groups to get more control in device updates.For example you can upgrade only 10 devices between thousands of devices by grouping them. Each group can have different version of firmware.

All done, just add doUpdate() function and go to otadrive.comota update service.

Be the First to Share

    Recommendations

    • Space Contest

      Space Contest
    • Halloween Contest

      Halloween Contest
    • Tinkercad Student Design Contest

      Tinkercad Student Design Contest

    Comments

    0
    arkhipenko
    arkhipenko

    1 year ago on Step 1

    Thank you for this instructable. I was looking to something like this, so this is very helpful. Obviously you can go with Azure or AWS solutions, but as I am not [yet] dealing with millions of devices, otadrive seems like a good compromise for my fleer of sensors. Cheers!