查看: 1009|回复: 0

[应用笔记] 用于ESP32设备的GITHUB OTA

[复制链接]

185

主题

204

帖子

596

积分

利尔达员工

Rank: 9Rank: 9Rank: 9

积分
596
发表于 2022-12-23 09:05:45 | 显示全部楼层 |阅读模式
使用 Github Actions 自动化您的 OTA 和 CI/CD 管道,以直接从 github 版本现场更新您的 ESP32 设备

特征
●使用引擎盖下的 esp_htps_ota 库更新固件映像
●也可以更新 spiffs/littlefs/fatfs 分区
●使用 SemVer 比较版本并仅在有新版本可用时更新
●与 esp-idf 引导加载程序的 App 回滚和反回滚功能配合得很好
●直接从github发布页面下载固件和分区镜像
●支持具有不同固件映像的多个设备
●包括一个示例 Github Actions,可在推送新标签时构建和发布图像
●可以手动或通过间隔计时器触发更新
●使用流式 JSON 解析器来减少内存使用(Github API 响应可能很大)
●支持私有存储库(需要 Github API 令牌*)

支持 Github 企业版
支持 Github 个人访问令牌以克服 Github API 速率限制

通过 esp_event_loop 发送更新进度
注意:您应该小心处理 GitHub PAT 并将其放入源代码中。我建议您将 PAT 存储在 NVS 中,用户在运行时输入它,否则 PAT 很容易从您的固件映像中提取出来。

用法
esp-idf 通过 Espressif 组件注册表:idf.py add-dependency Fishwaldo/ghota^0.0.1

平台 IO 注册表:
将此添加到您的 platform.ini 文件中:
lib_deps = Fishwaldo/ghota@^0.0.1

您还需要将 Kconfig 的内容复制到项目的 Kconfig 文件中,然后运行 ​​pio run -t menuconfig 来配置组件。

API文档:有关 API 的更多详细信息,请参见此处

例子
初始化网络访问后,启动计时器以定期检查新版本:(如果您是从https://components.espressif.com阅读本文,请注意该网站修改了以下示例。请参阅https://github.com/Fishwaldo/esp_ghota以获取正确的示例)
    ghota_config_t ghconfig = {        .filenamematch = "GithubOTA-esp32.bin", // Glob Pattern to match against the Firmware file        .storagenamematch = "storage-esp32.bin", // Glob Pattern to match against the storage firmware file        .storagepartitionname = "storage", // Update the storage partition        .updateInterval = 60, // Check for updates every 60 minuites    };    ghota_client_handle_t *ghota_client = ghota_init(&ghconfig);    if (ghota_client == NULL) {        ESP_LOGE(TAG, "ghota_client_init failed");        return;    }    esp_event_handler_register(GHOTA_EVENTS, ESP_EVENT_ANY_ID, &ghota_event_callback, ghota_client); // Register a handler to get updates on progress     ESP_ERROR_CHECK(ghota_start_update_timer(ghota_client)); // Start the timer to check for updates

手动检查更新:
    ghota_config_t ghconfig = {        .filenamematch = "GithubOTA-esp32.bin",        .storagenamematch = "storage-esp32.bin",        .storagepartitionname = "storage",        .updateInterval = 60,    };    ghota_client_handle_t *ghota_client = ghota_init(&ghconfig);    if (ghota_client == NULL) {        ESP_LOGE(TAG, "ghota_client_init failed");        return;    }    esp_event_handler_register(GHOTA_EVENTS, ESP_EVENT_ANY_ID, &ghota_event_callback, ghota_client);    ESP_ERROR_CHECK(ghota_check(ghota_client));    semver_t *cur = ghota_get_current_version(ghota_client);    if (cur) {        ESP_LOGI(TAG, "Current version: %d.%d.%d", cur->major, cur->minor, cur->patch);        semver_free(cur);    }    semver_t *new = ghota_get_latest_version(ghota_client);    if (new) {        ESP_LOGI(TAG, "New version: %d.%d.%d", new->major, new->minor, new->patch);        semver_free(new);    }    ESP_ERROR_CHECK(ghota_update(ghota_client));    ESP_ERROR_CHECK(ghota_free(ghota_client));

配置
以下配置选项可用:
* config.filenamematch <- Glob pattern to match against the firmware file from the Github Releases page. * config.storagenamematch <- Glob pattern to match against the storage file from the Github Releases page.* config.storagepartitionname <- Name of the storage partition to update (as defined in partitions.csv)* config.hostname <- Hostname of the Github API (default: api.github.com)* config.orgname <- Name of the Github User or Organization* config.reponame <- Name of the Github Repository* config.updateInterval <- Interval in minutes to check for updates

Github 动作
此存储库中包含的 Github Actions 可用于构建固件映像并将其发布到 Github Releases。这是自动化 CI/CD 管道并在现场更新设备的好方法。在此示例中,我们构建了固件的两种变体 - on 用于 ESP32 和一种用于 ESP32-S3 设备使用 filenamematch 和 storagenamematch 配置选项,我们可以匹配设备的正确固件映像。

on:  push:  pull_request:    branches: [master]permissions:  contents: writename: Buildjobs:  build:    strategy:      fail-fast: true      matrix:         targets: [esp32, esp32s3]    runs-on: ubuntu-latest    steps:    - name: Checkout repo      uses: actions/checkout@v3      with:        submodules: 'recursive'    - name: esp-idf build      uses: Fishwaldo/esp-idf-ci-action@v1.1      with:        esp_idf_version: v4.4.3        target: ${{ matrix.targets }}        path: 'examples/esp_ghota_example'    - name: Rename artifact      run: |        ls -lah         cp examples/esp_ghota_example/build/esp_ghota_example.bin esp_ghota_example-${{ matrix.targets }}.bin        cp examples/esp_ghota_example/build/storage.bin storage-${{ matrix.targets }}.bin    - name: Archive Firmware Files      uses: actions/upload-artifact@v3      with:         name: ${{ matrix.targets }}-firmware        path: "*-${{ matrix.targets }}.bin"  release:    needs: build    runs-on: ubuntu-latest    steps:    - name: Download Firmware Files      uses: actions/download-artifact@v2      with:        path: release    - name: Release Firmware      uses: ncipollo/release-action@v1      if: startsWith(github.ref, 'refs/tags/')       with:        artifacts: release/*/*.bin        generateReleaseNotes: true        allowUpdates: true        token: ${{ secrets.GITHUB_TOKEN }}

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表