ESP8266 OTA使用记录

结合上篇使用都挺好用的,仅记录以备后续使用;

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

void ota_setup()
{
    // pinMode(LED_BUILTIN, OUTPUT);
    ticker.attach(blinkInterval, tickerCount); // 设置Ticker对象
    // connectWifi();
    ArduinoOTA.onStart([]()
                       {
                           String type;
                           if (ArduinoOTA.getCommand() == U_FLASH)
                           {
                               type = "sketch";
                           }
                           else
                           { // U_FS
                               type = "filesystem";
                           }
                           Serial.println("Start updating " + type);

                           ota_OLED_onStart();
                       });
    ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
                          {

    ota_OLED_onProgress(4, 32, 120, 8, progress / (total / 100) );

    Serial.printf("Progress: %u%%\r", (progress / (total / 100))); });

    ArduinoOTA.onEnd([]()
                     {
    Serial.println("\nEnd");
    u8g2.clearBuffer();
    u8g2.drawStr(u8g2.getWidth() / 2, u8g2.getHeight() / 2, "Restart");
    u8g2.sendBuffer(); });
    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");
    } });
    // OTA设置并启动
    ArduinoOTA.setHostname("ESP8266");
    ArduinoOTA.setPassword("1234");
    ArduinoOTA.begin();
    Serial.println("OTA ready");

    // pinMode(OTA_KEY_PIN, INPUT_PULLUP);
    // attachInterrupt(OTA_KEY_PIN, key_press, FALLING);
    Serial.println("Ready");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
    Serial.println("ota int done ");
    //   Serial.print("ota set pin = ");
    //   Serial.println(OTA_KEY_PIN);
    u8g2.clearBuffer();
    u8g2.setFont(u8g2_font_wqy13_t_gb2312b);
    u8g2.setCursor(10, 20);
    u8g2.print("系统启动中...");
    u8g2.setCursor(10, 58);
    u8g2.print("OTA 设定完成");
    ota_OLED_start(4, 32, 120, 8, 80);
    u8g2.sendBuffer();
}


void otaloop()
{
    ArduinoOTA.handle();
    Serial.println("OTA start");
    if (led_on_flag == true)
    {
        led_on_flag = false;
        // digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
    }
}

猜你喜欢

转载自blog.csdn.net/yyandad/article/details/130303612