12. This is ‘Ard work

As I often work with the code while away from home, this post explains how to program Arduino board through Raspberry Pi remotely.

Hardware

Simplest, working configuration – plugin Arduino USB cable to the Raspberry Pi, that’s it. There are more complex ways to power Arduino, but for the beginning it is not necessary.

Software

Download and install Arduino IDE on the Raspberry Pi from an official website. Why not apt-get?

apt search arduino

Shows older version (as of now, apt-get searches version 1.5, where the website shows 1.8.9). So downloading from a website gives a newer version. 

Working with Arduino code and uploading it to the Arduino board through Arduino IDE is simple, but not always comfortable. While away from the setup some sort of remote connection is needed (like a VNC server), which on the Raspberry Pi is sluggish most of the time and not very secure (unless you know how to configure extra fail2ban and firewall rules). One way is to deal with these issues and live with sluggish interfaces. 

Another way is to use ssh console, and yes, it is possible and simple. The benefits are obvious, a console is fast and secure, it is also educational. 

Let’s see what SSH console need to be able to do:

  1. debug code (compile it) 
  2. upload code to the Arduino board
  3. use serial monitor through ssh 

Debug and upload using ssh

To debug and upload code to the Arduino board using console, install this program: http://www.raspberryvi.org/stories/arduino-cli.html

Give a try in the console:
1. Create a directory, 
2. Create a file (any name with .ino extension), copy Blink example code into it.
3. Create a file named “Makefile’ with content:

#this is a code sample of makefile for an arduino-mk
#arduino folder 
ARDUINO_DIR = /usr/share/arduino 

#arduino port, or just have a look in the IDE what port IDE is using for this board
ARDUINO_PORT = /dev/ttyUSB0

#extra libraries path if needed
USER_LIB_PATH = /usr/local/include/Whaever

#Board types are described /usr/share/arduino/hardware/arduino/boards.txt
BOARD_TAG = uno

#Include arduino mk, to compile and upload
include /usr/share/arduino/Arduino.mk

4. Tryout command:

make upload clean

Where:
make – building it
upload – uploading to the Arduino board
clean – deletes the building files

Serial monitor 

Working with things like ‘Serial.println‘ requires serial monitor, to look at that information in the console, use one of the followings: https://wiki.archlinux.org/index.php/working_with_the_serial_console

This is an example uses ‘screen‘  
https://linuxize.com/post/how-to-use-linux-screen/

screen /dev/ttyUSB0 9600

Where
/dev/ttyUSB0 is a port that Arduino connected to
9600 is a baud rate
*To end screen session: ctr+a+\ then press Y
*To exit screen and leave it running ctr+a+d