LoginSignup
11
5

More than 3 years have passed since last update.

NervesKeyなしでNervesHubからOTAでファームウェア更新をしてみる(Elixir)

Last updated at Posted at 2020-06-28

はじめに

  • NervesHub is an extensible web service that allows you to manage over-the-air (OTA) firmware updates of devices in the field.
    • NervesHubとは、野に咲くデバイスをOTAでのファームウェア更新を可能にする優れものです
  • 私はNervesKeyを持っていません:sweat:
  • また私は、令和二年ですがいまだにRaspberry Pi 2で楽しんでいます
    • Raspberry Pi 2しかもっていません:mask::mask:
  • 公式はこちらなのですが、2020/6/28現在においては、以下のような状況とのことでドキュメントの順番が前後していたり、手順がわかりにくかったりしますが、ここでは私がうまく行った手順をまとめておきたいとおもいます
    • 一応OTAまで一通りできましたというくらいのふわっとした記事です
    • 間違いなどございましたら、修正取り込みをしますので、ご指摘いただけるとありがたいです!

We're busy adding and updating the NervesHub documentation to make it easier to use NervesHub. In particular, the NervesHub web user interface is being reorganized so we're only lightly documenting it now. If something doesn't make sense, please let us know via a GitHub issue or by contacting us on the #nerves-hub channel on the Elixir lang Slack.

  • この記事で使用したバージョンは以下の通りです
elixir            1.10.3-otp-23
erlang            23.0.1
nerves_bootstrap  1.8.1
nerves            1.6.3

ハイライト

スクリーンショット 2020-06-28 10.37.44.png

  • OTAでファームウェア更新が進行中です!

0. インストール

1. プロジェクト作成

  • おなじみですね
$ mix nerves.new hello_nerves_hub
$ cd hello_nerves_hub
$ export MIX_TARGET=rpi2
$ export NERVES_NETWORK_SSID=ssid
$ export NERVES_NETWORK_PSK=psk
$ mix deps.get
  • MIX_TARGETに指定できるターゲットは、Targetsをご参照ください
  • NERVES_NETWORK_SSIDNERVES_NETWORK_PSKは次のWi-Fiの設定で使います

2. Wi-Fiの設定

config/target.exs
config :vintage_net,
  regulatory_domain: "JP",
  config: [
    {"usb0", %{type: VintageNetDirect}},
    {"eth0",
     %{
       type: VintageNetEthernet,
       ipv4: %{method: :dhcp}
     }},
    {"wlan0",
     %{
       type: VintageNetWiFi,
       vintage_net_wifi: %{
         networks: [
           %{
             key_mgmt: :wpa_psk,
             ssid: System.get_env("NERVES_NETWORK_SSID"),
             psk: System.get_env("NERVES_NETWORK_PSK")
           }
         ]
       },
       ipv4: %{method: :dhcp}
     }}
  ]
  • wlan0の設定をいい感じに書き換えます
  • 詳細は、vintage_netを参照してください
    • ちなみに以前(2019/11くらい) mix nerves.newしたプロジェクトにはvintage_netの設定はなかったので、どこかのバージョンから追加されたのだとおもいます
  • 私はRaspberry Pi 2をWi-Fiでネットワークにつないでいますが、有線LAN等でもよいとおもいます

3. NervesHubの設定

mix.exs
  defp deps do
    [
      ...
      {:nerves_hub_cli, "~> 0.10.0", runtime: false},
      {:nerves_hub_link, "~> 0.8.2", targets: @all_targets},
      {:nerves_hub_link_http, "~> 0.8.1", targets: @all_targets},
      {:nerves_time, "~> 0.4.1"}
    ]
  end

依存関係の解決

$ mix deps.get

ユーザー登録

$ mix nerves_hub.user register
Email address: 
Username: 
NervesHub password: 

このあとの操作でNervesHubにデータを書き込んだりする際に使うローカルパスワードを設定します

$ mix nerves_hub.user auth
Username or email address: 
NervesHub password:

Authenticating...
Success

NervesHub uses client-side SSL certificates to authenticate CLI requests.

The next step will create an SSL certificate and store it in your 
'~/.nerves-hub' directory. A password is required to protect it. This password
does not need to be your NervesHub password. It will never be sent to NervesHub
or any other computer. If you lose it, you will need to run
'mix nerves_hub.user auth' and create a new certificate.

Please enter a local password: 
Certificate created successfully.

Firmware signing keys

$ mix nerves_hub.key create devkey

config/config.exsの書き換え

config/config.exs
config :nerves, :firmware, provisioning: :nerves_hub_link_http

config :nerves_hub_link_http,
  socket: [
    json_library: Jason,
    heartbeat_interval: 45_000
  ],
  ssl: [
    cert: "some_cert_der",
    keyfile: "path/to/keyfile"
  ]

config :nerves_hub,
  fwup_public_keys: [:devkey]
  • cert, keyfileはあとで書き換えます

Products

$ mix nerves_hub.product create

スクリーンショット 2020-06-28 8.04.37.png

  • この記事の手順ではhello_nerves_hubが登録されます

Devices

$ mix nerves_hub.device create
NervesHub server: api.nerves-hub.org:443
NervesHub organization: TORIFUKUKaiou
Identifier (e.g., serial number): 5678
Description: test-5678
One or more comma-separated tags: qa
Local NervesHub user password: 
Device 5678 created.

If your device has an ATECCx08A module or NervesKey that has been
provisioned by a CA/signer certificate known to NervesHub, it is
ready to go.

If not using a hardware module to protect the device's private
key, create and register a certificate and key pair manually by
running:

  mix nerves_hub.device cert create 5678
  • IdentifierDescriptiontagsの入力を求められます
  • 何でもいいとおもうのですが公式を参考に入力しています
  • Deviceが登録されます
  • もちろんまだofflineであります

スクリーンショット 2020-06-28 8.07.08.png

  • コマンド実行結果の最後に書いてある、NervesKeyを持っていない人の手順を実行します
$ mix nerves_hub.device cert create 5678
  • これを行うとnerves-hub/5678-cert.pemnerves-hub/5678-key.pemができています
  • 続いてさきほどとりあえずそのままにしていたsome_cert_derを作ります
$ iex -S mix

iex> IEx.configure(inspect: [limit: :infinity])
:ok

iex> File.read!("nerves-hub/5678-cert.pem") |> X509.Certificate.from_pem!() |> X509.Certificate.to_der()
<<48, 130, ...>>
  • 上記の結果をもとにconfig/config.exsを更新します
config/config.exs
config :nerves_hub_link_http,
  socket: [
    json_library: Jason,
    heartbeat_interval: 45_000
  ],
  ssl: [
    cert:
      <<48, 130, ...>>,
    keyfile: "nerves-hub/5678-key.pem"
  ]
  • certのところには、IExの結果をそのまま書きます
    • 手順は、Jon Carstens さんに教えてもらいました
    • ありがとうございます!
  • インデントがおかしい場合は以下のコマンドでキレイに整形してくれます
$ mix format
  • ファームウェアをつくります
$ mix deps.compile nerves_hub --force
$ mix firmware
  • microSDカードに焼きます
$ mix nerves_hub.device burn 5678
  • microSDカードをRaspberry Pi 2に挿します
  • 電源をオンします
  • うまくいっていれば、https://www.nerves-hub.org/上のDeviceがonlineになっているはずです :tada::tada::tada:

4. OTA

  • 少しプロジェクトに変更を加えておきましょう
mix.exs
defmodule HelloNervesHub.MixProject do
  use Mix.Project

  @app :hello_nerves_hub
  @version "0.1.1" # 変更

...

  def application do
    [
      mod: {HelloNervesHub.Application, []},
      extra_applications: [:logger, :runtime_tools, :inets] # :inets 追加
    ]
  end
mix.exs
  defp deps do
    [
      ...
      {:nerves_time, "~> 0.4.1"},
      {:lwwsx, "~> 0.1.2"} # 追加
    ]
  end
$ mix deps.get
$ mix firmware
$ mix nerves_hub.firmware publish --key devkey
  • 上記が成功すると、https://www.nerves-hub.org/上のFirmwareが追加されます
  • Web画面上でDeploymentsからDeploymentを作ります

スクリーンショット 2020-06-28 11.52.46.png

  • なんでもいいのだろうとはおもいますが公式を参考に下記のような値をいれてみました
    • Name: qa_deployment
    • Version Requirement: 未入力
    • Tag(s): qa
  • 登録が終わったらDeploymentの詳細画面で"Make Active"ボタンを押します
  • すると!
  • ファームウエアアップデートがはじまっています!

スクリーンショット 2020-06-28 10.37.29.png
- 52%!

スクリーンショット 2020-06-28 10.37.44.png
- 65%!!!
- 100%!!!
- 少しだけ待つとファームウェアのバージョンがアップしていることがわかります

スクリーンショット 2020-06-28 11.58.35.png

  • 本当に更新した内容が含まれているのか確かめてみましょう
  • Nervesアプリにsshでログインします
    • 192.168.1.10のところはご利用のネットワーク環境にあわせて読み替えてください
$ ssh 192.168.1.10

iex> use Toolshed
Toolshed imported. Run h(Toolshed) for more info.
:ok 

iex> weather
Weather report: Tsushima, Japan

    \  /       Partly cloudy
  _ /"".-.     27..28 °C      
    \_(   ).    5 km/h       
    /(___(__)  10 km          
               0.0 mm 

iex> Lwwsx.current 400030
{:ok,
 %{
   "copyright" => %{
     "image" => %{
       "height" => 26,
       "link" => "http://weather.livedoor.com/",
       "title" => "livedoor 天気情報",
       "url" => "http://weather.livedoor.com/img/cmn/livedoor.gif",
       "width" => 118
     },

  • 確かにアップデートされています! :rocket::rocket::rocket:

Wrapping Up

  • NervesKeyなしでNervesHubからOTAでファームウェア更新ができました :boy:
  • Enjoy!
11
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
5