The final steps to completing your project.
In this final project post we will be discussing
- Two-way serial communications between Linux and the Arduino.
- Using BASH scripts to communicate
- Running Cron jobs to schedule the scripts
At the time of writing these scripts they really were the only way I knew how to make the communication to work. However, now Perl and Python are in my life and there are tentative plans to create new scripts to replace the BASH/Cron solution.
If anyone ends up writing any scripts to replace the ones provided, please make them available. You would be best off forking my GitHub project and making it available through there. Also, let me know so I can make a post about it on this blog and give credit where it is due 🙂
1. Let’s communicate!
There are multiple ways for the Linux OS on the ARM processor to talk to the Arduino’s SAM3X processor, one is via a handy serial comms interface /dev/ttymxc3. This is available on the UDOO as it was designed for this type of communication. See the UDOO website for more details and be sure to check out their technical manual.
If you do not have a UDOO, you could still communicate with the Arduino using the serial pins on the Arduino and a serial interface on your Linux machine. This is a little more difficult to do and not covered here.
The UDOO is still very cutting-edge technology and bugs are present. One such bug I stumbled upon is the inability to communicate with the Arduino via the serial comms after a sketch is uploaded to it. See this forum post for more details.
2. Prepare your script.
The BASH script is simple and scalable. It accepts two arguments via the command line:
- Location of the processes PID file
- Name of the process (this is printing to the Arduino LCD screen, so be careful of character length)
The script assumes that you have given it the correct information. If your PID is renamed or is not able to be read by the script (due to permissions or being locked) then the script will assume the process is down, even though it may not be. Normally this isn’t a problem if you test your script properly.
The script has two main functions:
- Tell the Arduino what to display (or not to) on its LCD screen
- Send the current date and time to the Arduino
The script will read the process ID from the PID file and then look for it in the process list. If it isn’t found it will contact the Arduino and give it an error message to display. Once the process is back up, it will then send another string to the Arduino to delete this message.
By default the script will only ask the Arduino to display processes that have failed, but there are a few commented out lines in the script which when uncommented will allow the script to also tell the Arduino to display running processes on the LCD.
Careful how quickly you write to serial comms. Writing too quick will flood the line, especially when using slower baud rates
The Arduino relies on Linux to get the date and time. This is sent if the last time the date/time was sent was > 30 minutes ago.
Grab the latest version of the script from my GitHub account.
There are other test scripts on GitHub which are explained further in the comments of the code.
3. Schedule frequency of process checks
Cron is used to run the script once a minute. Each process has its own cron line.
Edit your cron by using the crontab -e command. My entries look like:
*/1 * * * * /root/bin/UDOO-check_services.sh /var/run/minidlna/minidlna.pid MiniDLNA
*/1 * * * * /root/bin/UDOO-check_services.sh /var/run/transmission/transmission.pid Transmission
*/1 * * * * /root/bin/UDOO-check_services.sh /var/run/couchpotato/couchpotato.pid CouchPotato
*/1 * * * * /root/bin/UDOO-check_services.sh /var/run/openvpn.clientLA_VPS.pid USA_VPS
*/1 * * * * /root/bin/UDOO-check_services.sh /var/run/openvpn.server_S.pid SuperVPN
Congratulations! At this point you should be able to test your project by killing processes and seeing if the LCD screen shows you any errors. Then start the process again and be sure the error disappears.
If you enjoyed this project please share it with friends via social media or leave a comment.
October 28, 2016 at 1:58 pm
Thanks for the article it teaches me how to prepare a proper script. And how to execute my work in proper way. Thank you for sharing this information on the internet.
October 28, 2016 at 2:36 pm
No problems. Be sure to check out all my other programming projects code on my Github. http://www.github.com/aristocles