Skip to content

Instantly share code, notes, and snippets.

@ringvold
Created September 20, 2022 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ringvold/84f40c20d1b6b2206591d36487e823a5 to your computer and use it in GitHub Desktop.
Save ringvold/84f40c20d1b6b2206591d36487e823a5 to your computer and use it in GitHub Desktop.
Livebook notebook for interacting with the bmp280 sensor

BMP280 temperatur sensor

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.6.2"},
  {:vega_lite, "~> 0.1.6"}
])
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: 0x77)

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