OTA – Over The Air

In order to enable the programming of IoT devices even without a line connection, the WMOS microcontroller can also be programmed with little effort over the air (WIFI).

working task

Integrate the code shown in the information section into your own program to implement the OTA feature

OTA sample code

In order to implement the OTA programming, the program must be supplemented once with code which enables programming via the WIFI connection during operation.

#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#include <DHT.h>

[...]

void setupWifi() {
  [...]
  // default port is 8266
  // ArduinoOTA.setPort(8266);

  //default hostname is esp8266-[ChipID]
  ArduinoOTA.setHostname(HOST_NAME.c_str());

  // add password for authentication
  // ArduinoOTA.setPassword((const char *)"123");

  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("End");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
} // initWIFI

void loop() {
 ArduinoOTA.handle();
 [...]
}

After programming the microcontroller via the USB line with the above program, the Arduino development environment will automatically provide a network programming port.

The following programming can be done already OTA.

Leave a Reply