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

pillaiindu
In django there is a cache framework backed by memcached. Rails also puts a lot of emphasis on caching, and even the idea of russian-doll...
New
arpan
Hello everyone :wave: Today I am very excited to announce a project that I have been working on for almost 3 months now. The project is...
New
chuck
Let me start by stating an assumption: Phoenix is a great approach to building REST APIs. There are many reasons for this, but I will ass...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -&gt; H; nth(N, [_|T]) when N &gt; 1 -&gt; nth(N - 1, T). Elixir Enum.at … coo...
New
marciol
Please, let me know if this kind of discussion already took place in another topic . Hi all, how do you consider if is better to build ...
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
100phlecs
Are there any downsides, like perf issues, to putting all functional components in CoreComponents as long as you prefix it with the conte...
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
joeerl
I’m playing with Elixir - It’s fun. I think @rvirding does give Elixir courses these days. Re: files and database - when I given Erlang ...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52673 488
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

We're in Beta

About us Mission Statement