Hello,
I'm new to this forum and arduino at all, but got some experience in general. I successful make my home automated, can switch relays from my phone, schedule them to turn on/off on specific time, or sensor triggered ... etc. For that purpose I use some PI computers. Until some day I got a task to control led sign (make animation). It was simple task at all and I do not want to waste a PI computer for that purpose, so I look at arduino (much cheaper and simple way) and that is the way it started. Arduino is great for low level control of any kind of devices, it have analog inputs - something that most of PI computers do not have. So after I complete my led sign project I decided to find a way to use PI and Arduino together. In general what am I showing below can be used to control arduino via any PC using serial port and linux.
Here is the idea, i made a little sketch (preconfigured) that listen on serial port and when receive a proper command it do whatever had to do - write analow/digital value to pin or read it and send back the result.
(sketch in separate post, due to post limitations)
I used that code on Arduino UNO R3
command format is:
:ping;
that gonna return message "pong"
that is to check if connection is up and arduino is responding.
commands should begin with : and end with ;
take a look at the sketch to see all of them.
here is the example of turning preconfigured digital pin to high:
:dwrite.1.1;
answer must be "done" if command is accepted.
now, here is the tricky part, doing that from linux in a secure and releable way.
I'm using arduino USB cable to connect it to Orange PI running on ARMBIAN. Bad part is that on every serial initialization arduino restarts. To avoid that I created a little bash script handling it:
#!/bin/bash
echo "Initializing COM port as FD"
stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts
exec 99<>/dev/ttyACM0
sleep 1
echo "Waiting for commands writen in /cmd, result gonna be writen in /answer"
while true
do
if [ -e /cmd ]
then
command=`cat /cmd`
rm /cmd
read -u 99 -t 0.01 -r qssgpio
status=$?
while [ $status -eq 0 ] ; do
qssgpio=""
read -u 99 -t 0.01 -r qssgpio
status=$?
done
echo $command >&99
read -t 1 -u 99 answer
echo $answer > /answer
fi
sleep 0.1
Script must be run in a backgroud process, it monitor for existance of /cmd file, if found it send it via serial, delete it, and output answer in /answer file.
here is usage example:
root@orangepione:/#
root@orangepione:/#
root@orangepione:/# /arduinoserial >/dev/null &
[1] 19627
root@orangepione:/# echo ":ping;" > /cmd
root@orangepione:/# cat /answer
pong
root@orangepione:/# echo ":dwrite.3.1;" > /cmd
root@orangepione:/# cat /answer
done
root@orangepione:/# echo ":aread.1;" > /cmd
root@orangepione:/# cat /answer
222
root@orangepione:/#