LoginSignup
15
2

More than 1 year has passed since last update.

ローカルにたてたFIWARE Orion Context BrokerにNervesで取得した温度・湿度データを送ってみました

Last updated at Posted at 2021-09-18

はじめに

FIWARE

  • FIWARE technology enables Smart Cities worldwide
  • なんだかすごそうです!

Nerves

とりあえずローカルでFIWARE Orion Context Brokerをイゴかしてみます1

docker-compose.yml
mongo:
  image: mongo:4.4
  command: --nojournal
orion:
  image: fiware/orion
  links:
    - mongo
  ports:
    - "1026:1026"
  command: -dbhost mongo
$ docker-compose up
$ curl localhost:1026/version
{
"orion" : {
  "version" : "3.1.0-next",
  "uptime" : "0 d, 0 h, 0 m, 13 s",
  "git_hash" : "7bd1e43514539bd65caeb30d4e3319202e0f115b",
  "compile_time" : "Mon Jul 26 08:19:44 UTC 2021",
  "compiled_by" : "root",
  "compiled_in" : "dae1c5e3a7d9",
  "release_date" : "Mon Jul 26 08:19:44 UTC 2021",
  "machine" : "x86_64",
  "doc" : "https://fiware-orion.rtfd.io/",
  "libversions": {
     "boost": "1_66",
     "libcurl": "libcurl/7.61.1 OpenSSL/1.1.1g zlib/1.2.11 nghttp2/1.33.0",
     "libmicrohttpd": "0.9.70",
     "openssl": "1.1",
     "rapidjson": "1.1.0",
     "mongoc": "1.17.4",
     "bson": "1.17.4"
  }
}
}

Nervesから温度・湿度データを送ってみます

準備

$ mix nerves.new hello_fiware
mix.exs
       {:nerves_system_rpi4, "~> 1.13", runtime: false, targets: :rpi4},
       {:nerves_system_bbb, "~> 2.8", runtime: false, targets: :bbb},
       {:nerves_system_osd32mp1, "~> 0.4", runtime: false, targets: :osd32mp1},
-      {:nerves_system_x86_64, "~> 1.13", runtime: false, targets: :x86_64}
+      {:nerves_system_x86_64, "~> 1.13", runtime: false, targets: :x86_64},
+      {:httpoison, "~> 1.8"},
+      {:jason, "~> 1.2"},
+      {:aht20, "~> 0.4.0"}
     ]
   end
$ export MIX_TARGET=rpi4
$ mix deps.get
$ mix firmware
  • microSDカードをmacOSに挿して、以下のコマンドで焼き込みます
$ mix burn
  • LANケーブルでRaspberry Pi(Nerves)をmacOSと同じネットワークに接続して電源ON:fire:

Run

  • :coffee: でも飲みながら15秒から30秒程度待ちましたら、sshでつないでIExで楽しみます
$ ssh nerves.local
Interactive Elixir (1.12.1) - press Ctrl+C to exit (type h() ENTER for help)
████▄▖    ▐███
█▌  ▀▜█▙▄▖  ▐█
█▌ ▐█▄▖▝▀█▌ ▐█   N  E  R  V  E  S
█▌   ▝▀█▙▄▖ ▐█
███▌    ▀▜████

Toolshed imported. Run h(Toolshed) for more info.
RingLogger is collecting log messages from Elixir and Linux. To see the
messages, either attach the current IEx session to the logger:

  RingLogger.attach

or print the next messages in the log:

  RingLogger.next

書き込み

iex> {:ok, pid} = AHT20.start_link(bus_name: "i2c-1", bus_address: 0x38)

iex> {:ok, %AHT20.Measurement{humidity_rh: humidity, temperature_c: temperature}} = AHT20.measure(pid)

iex> json = %{id: "Room", type: "Room", temperature: %{value: temperature, type: "Number"}, humidity: %{value: humidity, type: "Number"}} |> Jason.encode!

iex> HTTPoison.post "http://192.168.1.8:1026/v2/entities", json, [{"Content-Type", "application/json"}]                                                   
{:ok,
 %HTTPoison.Response{
 ...
  • :tada::tada::tada:
  • 成功しているようです
  • 192.168.1.8は、docker-compose upしてFIWARE Orion Context Brokerをイゴかしている私の家にあるmacOSのIPアドレスです

読み込み

iex> HTTPoison.get!("http://192.168.1.8:1026/v2/entities") |> Map.get(:body) |> Jason.decode!
[
  %{
    "humidity" => %{
      "metadata" => %{},
      "type" => "Number",
      "value" => 66.05091095
    },
    "id" => "Room",
    "temperature" => %{
      "metadata" => %{},
      "type" => "Number",
      "value" => 27.030181885
    },
    "type" => "Room"
  }
]
  • :tada::tada::tada:

Wrapping Up :lgtm::lgtm::lgtm::lgtm::lgtm:

  • 今日は「誰でもできるスマートシティ向けOSS : FIWAREのはじめかた」を参考にさらーっとなぞってみました
  • これからももっともっと触っていこうとおもっています2
    • FIWAREは2017年ころのスタート、その源流に相当するとおもわれる"Next Generation Service Interfaces" (NGSI)は2012年ころからあったという意味では、10年前の自分に教えてあげたいです
    • まあ私は昨日はじめて知ったということで、「次の10年の技術トレンド予想」の方がしっくりきますのでQiita10th_未来タグをつけました
  • Enjoy Elixir:rocket::rocket::rocket:


  1. イゴかす = 動かす。西日本のほうの方言(たぶん)。NervesJPではおなじみ。 

  2. おもっています。あくまでもおもっています。 

15
2
1

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
15
2