Zesky665

Zesky665

'Process.send_after' doesn't work

Hi,

I’m trying to create a nerves app that will read from a sensor at 10s intervals. I’ve written code for reading from the device and it works. The problem is that when using the ‘Process.send_after’ function in order to read repeatedly, nothing happens.

Here’s my code:

defmodule HelloNerves do
  use GenServer

# Client
  def start_link(options\\[]) do
    GenServer.start_link __MODULE__, [], options
  end

  def find_device(pid) do
    GenServer.call(pid, {:find})
  end

  def start_reading(pid) do
    GenServer.call(pid, {:start})
  end

  def read_output(pid_1, pid_2) do
    GenServer.call(pid_1, {:read, pid_2})
  end  

# Server
  def init(_) do
    {:ok, []}
  end

  def handle_call({:find}, _from, state) do
    devices = Nerves.UART.enumerate
    case Map.has_key?(devices, "ttyUSB0") do
      true -> {:reply, :ok, state}
      false -> {:reply, :not_found, state}
    end
  end

  def handle_call({:start}, _from, state) do
    {:ok, pid} = Nerves.UART.start_link
    response = Nerves.UART.open(pid, "ttyUSB0", speed: 9600, active: false)
    read_data_interval(pid)
    case response == :ok do
      true -> {:reply, {:ok, pid}, state}
      false -> {:reply, :not_ok, state}
    end
  end

  def handle_call({:read, pid}, _from, state) do
    {:ok, output} = Nerves.UART.read(pid, 5000)
    IO.inspect(output, label: "The sensor data: ")
    read_data_interval(pid)
    case Kernel.is_bitstring(output) do
      true -> {:reply, :ok, state}
      false -> {:reply, :not_ok, state}
    end
  end

  defp read_data_interval(pid) do
    Process.send_after(self(), {:read, pid}, 5000)
  end
end

Any idea as to what my problem is?

Thank you

Marked As Solved

kpanic

kpanic

Hello, you should handle messages sent from Process.send_after/2 with the function handle_info/2
Check GenServer — Elixir v1.20.2

Also don’t forget to use Process.cancel_timer/1 when you are done with the timer.

Also Liked

kpanic

kpanic

Glad you made it work!
If Nerves.UART.open/2 is blocking, an small improvement could be also to use the “new” handle_continue/2 callback. So that the OTP Application would not block when booting the init of the GenServer
An example (with nerves) is nerves_morse/fw/lib/nerves_morse/worker.ex at master · kpanic/nerves_morse · GitHub and nerves_morse/fw/lib/nerves_morse/worker.ex at master · kpanic/nerves_morse · GitHub

See also the official doc GenServer — Elixir v1.20.2

btw, nerves is really nice! :+1:
Happy hacking!

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
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
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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

We're in Beta

About us Mission Statement