1

I have an arduino which I want to send text messages through a rails api. So, directly on a terminal this is as simple as create an file descriptor for the arduino port like:

exec 3<> /dev/ttyACM0

And then, I can send any message like:

echo "Beautiful blue sky floating endlessly in love making the clouds sigh.">&3

Now, I tryed to implement this idea on my rails api using the system method:

api/config/application.rb

class Application < Rails::Application 
  ...
  # This is for declaring the file descriptor when the rails server is on.
  system 'exec 3<> /dev/ttyACM0;'
end

app/controllers/message_controller.rb

class MessageController < ApplicationController
  def send
    system "echo \"#{params[:message]}\">&3;"
    render json: '', status: :ok
  end
end

...But, this throw me a sh: 3: Bad file descriptor message on the terminal.

I barely make it work changing the system execution on the controller like this:

system "exec 3<> /dev/ttyACM0; sleep 2; echo \"#{params[:message]}\">&3;"

but I need to add that sleep command to give enough time to prepare the arduino to catch input data.

In order to make this work faster I like to know a way to keep alive my file descriptor configuration between calls to my controller.

asceta
  • 11
  • 2

0 Answers0