Currency-inverter

Currency-inverter

Circuits.GPIO and multiple buttons/sensors?

Hello! I’m currently building a weather station with sensors for wind, rain and temperature in elixir with Circuits.GPIO lib.

So far it has been a blast but I’ve gotten stuck on reading two sensors (eg buttons) and I assume it’s because the functional nature of elixir. My code:

defmodule WeatherStation do
  use GenServer
  require Logger

  @wind_sensor_pin 5
  @rain_sensor_pin 6

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

  def init(_opts) do
    {:ok, _gpio} = Circuits.GPIO.open(@rain_sensor_pin, :input)
    {:ok, gpio} = Circuits.GPIO.open(@wind_sensor_pin, :input)

    Circuits.GPIO.set_interrupts(gpio, :falling)
    {:ok, gpio}
  end

  def handle_info({:circuits_gpio, @rain_sensor_pin, _timestamp, _value}, state) do
    # handle stuff
  end

Obviously above don’t work (button presses is not sent) as I intended so my question is how to organize your code when you have more than one button/sensor in the GPIO lib.

Should I have a genserver for each sensor?

Marked As Solved

al2o3cr

al2o3cr

I haven’t used Circuits.GPIO, but the warning from the documentation for set_interrupts suggests that you always need to capture the reference returned from open:

NOTE: You will need to store the Circuits.GPIO reference somewhere (like your GenServer’s state) so that it doesn’t get garbage collected. Event messages stop when it gets collected. If you only get one message and you are expecting more, this is likely the case.

Also, set_interrupts appears to operate on one pin at a time. Taking both into account, your init could be:

def init(_opts) do
  {:ok, rain_pin} = Circuits.GPIO.open(@rain_sensor_pin, :input)
  Circuits.GPIO.set_interrupts(rain_pin, :falling)

  {:ok, wind_pin} = Circuits.GPIO.open(@wind_sensor_pin, :input)
  Circuits.GPIO.set_interrupts(wind_pin, :falling)

  {:ok, {rain_pin, wind_pin}}
end

Where Next?

Popular in Questions Top

SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
273 38985 115
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
magnetic
Hey :wave:t3: Elixir community, I’ve been learning Elixir, and working on some side projects. My editor of choice is VSCode, and althoug...
New

We're in Beta

About us Mission Statement