theAntimon

theAntimon

Running Phonix with SQLite for weather-data directly on the Pi (with nerves)

Hi,

I’m new to nerves but already managed to build the Weather Station (and also read the Binary-Clock-Book).

Now I want to modify the Weather-Station to make phoenix actually run directly on the Pi, so that I don’t need another Computer to hold the database. I started with adding hello_phoenix next to my sensor_hub folder. This worked good enough. After flashing the weather station I can see the phoenix default page in my browser. Now I need to add a database. I couldn’t find that much anywhere about that, but finally tried this to run SQLite on the pi. I had dependency problems when trying to add the :sqlite_ecto2 dependency but since the hello_phoenix example already had :ecto_sql and :ecto_sqlite3 in the mix.exs I skipped that step in the article. Then I configured the config.exs as described with

config :ui, Ui.Repo,
  adapter: Sqlite.Ecto2,
  database: "#{Mix.env}.sqlite3"

config :ui, ecto_repos: [Ui.Repo]

Running iex -S mix on my host now creates a database file at the root-level. But that one is not named ‘host.sqlite3’ as I understand it from the article but just ‘ui_dev.db’. It seems ecto is not asking for the database name I specified. Well this is not that much of a problem since I actually want to run it on the pi, right? But since I need to configure (later in the article) the place for the database on the pi by writing

config :ui, Ui.Repo,
  adapter: Sqlite.Ecto2,
  database: "/root/#{Mix.env}.sqlite3"

config :ui, ecto_repos: [Ui.Repo]

into a config/rpi2.exs. Well, that also doesn’t create any file into ‘/root/rpi2.sqlite3’ as far as I can see the filesystem on the pi (I checked with File.ls/1 after ssh-ing to the pi). Ok, Seems I have to fix the root of that problem. Does anyone have an idea what I am doing wrong? Has anybody done that before (running phoenix with a database for the weather-data on the target).

I wonder why I couldn’t find any good project modification for the weather-station anywhere. For me it is a obvious thing to do and I’m really wondering why it is not already included in the book. I don’t see that much of a use for a weather-station on a raspberry pi which needs another computer to run the database…

Most Liked

axelson

axelson

Scenic Core Team

I’d recommend trying Ecto SQLite3 instead of SQLite2: GitHub - elixir-sqlite/ecto_sqlite3: An Ecto SQLite3 adapter. · GitHub

The readme of the project should help you get started with it and there’s not really any nerves-specific setup that you’ll need I think.

To create the database I usually create a release module like:

defmodule Dash.Release do
  @app :dash

  def migrate do
    load_app()

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    load_app()
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  defp repos do
    Application.fetch_env!(@app, :ecto_repos)
  end

  defp load_app do
    Application.load(@app)
  end
end

Then connect to your Pi via ssh and run Dash.Release.migrate() and that should create the sqlite file on the Pi.

Good luck!

axelson

axelson

Scenic Core Team

/data is actually a symlink to /root so database: "/root/#{Mix.env}.sqlite3" will work. Although using /data directly is generally recommended since that may change in the future.

jordan0day

jordan0day

As mentioned in the nerves docs here: Frequently Asked Questions — nerves v1.14.3, you’ll probably want to place your DB somewhere under the /data partition. I believe the root filesystem on nerves is read-only by default.

Where Next?

Popular in Discussions Top

Donovan
Hello everyone, I’m so glad to have discovered this awesome community. Thanks for creating it! This is my second post, and apologies for...
New
Jayshua
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
AstonJ
If a newbie asked you about Phoenix Contexts, how would you explain the basics to them? Feel free to be as concise or in-depth as you li...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
und0ck3d
Hello everyone! A few days ago I’ve created a topic here about how people were creating CMSs with Elixir and Phoenix. I’ve been studying...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
kostonstyle
Hi all How can I compare haskell with elixir, included tools, webservices, ect. Thanks
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement