Friday, August 4, 2017

Sync with remote as you code

If your like me, sometimes you can not test the code your writing on your workstation and the code need to be 'installed' or 'deployed' to a remote server in order to test it because of hardware constrain or physical environment (IO, network,...).

The following is a simple shell script that will monitor a local path and rsync it to a remote server when a file is changed. It can also remote execute some commands (like restarting services for example).

#!/bin/sh

if [ -z ${1} ] || [ -z ${2} ] || [ -z ${3} ] || [ -z ${4} ]; then
cat <<EOF
Usage:
  remote_sync local_path remote_user remote_server remote_path [remote_script]

local_path:    Local path from where the files will be copied from (required)
remote_user:   Remote user to use for rsync  (required)
remote_server: Remote server (IP of FQDN)  (required)
remote_path:   Remote path where the files will be copied to  (required)
remote_script: Remote script to execute after syncing the files (optional)
              Script must be executable by the remote_user
               Must be passed between double quoted

Note: You must have passwordless ssh access to the remote_server or else you will
     be asked for the remote_user password.
EOF
exit 99
fi

echo "`date`: Performing initial sync to ${3}"
rsync -qarvce "ssh -o UserKnownHostsFile=/dev/null" ${1} ${2}@${3}:${4}
if [ "${5}" != "" ]; then
echo "`date`: Running [${5}]"
ssh -q -o UserKnownHostsFile=/dev/null ${2}@${3} ${5} >/dev/null
fi
fswatch -o . | while read f
do 
echo "`date`: Changes detected syncing to ${3}"
rsync -qarvce "ssh -o UserKnownHostsFile=/dev/null" ${1} ${2}@${3}:${4} >/dev/null
if [ "${5}" != "" ]; then
echo "`date`: Running [${5}]"
ssh -q -o UserKnownHostsFile=/dev/null ${2}@${3} ${5} >/dev/null
fi
done

I only tested this on macOS but I believe it should work on a Linux if your workstation is running Linux. If your on Windows, it can probably work under Cygwin.

Hope it's useful for someone.

Wednesday, August 2, 2017

Arduino: Upload timeout and !!! in a string

If you ever see the following when uploading your sketch to an Arduino board (using the Arduino IDE, PlatformIO or any other):

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

Look for !!! (3 exclamation points characters  aka bang) in your code and remove them.

Why ? These 3 exclamation point characters are actually used to tell the Arduino bootloader to switch to 'monitor' mode and when that happens the upload process stops. Simply remove these '!!!' and replace them with something else and your code will compile and upload just fine.

Reference: https://forum.arduino.cc/index.php?topic=83079.120

Embedding hardware parts in 3D prints

Yesterday I did my first attempt at embedding a nut in a print... The idea was to have the nut (8-32 hex nut) be captive in the print so i...