Skip to content

Instantly share code, notes, and snippets.

@fhunleth
Last active October 15, 2021 00:42
Show Gist options
  • Save fhunleth/d46255f984ad8602eab83fb9f4bf79da to your computer and use it in GitHub Desktop.
Save fhunleth/d46255f984ad8602eab83fb9f4bf79da to your computer and use it in GitHub Desktop.

ElixirConf Demo

Introduction

This is a simplified version Masatoshi Nishiguchi's bmp280.livemd notebook that's included in Nerves Livebook. Check out that notebook if you'd like to try this out yourself.

Setup

Mix.install([
  :bmp280,
  {:kino, "~> 0.3.0"},
  {:vega_lite, "~> 0.1.0"}
])
alias VegaLite, as: Vl

Make sure it's wired up

Circuits.I2C.detect_devices()

Initializing BMP280

{:ok, bmp} = BMP280.start_link(bus_name: "i2c-1", bus_address: 0x76)

Read data from the sensor

BMP280.measure(bmp)

Graph it with Kino and VegaLite

widget =
  Vl.new(width: 700, height: 400, title: "BME280 Temperature")
  |> Vl.mark(:line)
  |> Vl.encode_field(:x, "x", title: "Sample #", type: :quantitative)
  |> Vl.encode_field(:y, "y", title: "Celsius", type: :quantitative)
  |> Vl.encode_field(:color, "key", type: :nominal, title: nil)
  |> Kino.VegaLite.new()
  |> tap(&Kino.render/1)

Kino.VegaLite.periodically(widget, 100, 0, fn i ->
  {:ok, measurement} = BMP280.measure(bmp)

  points = [
    %{key: "Temperature", x: i, y: measurement.temperature_c},
    %{key: "Dew Point", x: i, y: measurement.dew_point_c}
  ]

  Kino.VegaLite.push_many(widget, points, window: 100)
  {:cont, i + 1}
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment