Monday, November 20, 2017

Connecting to a Wifi SSID from the macOS command line

Sometimes it's useful to be able to connect to an SSID (or switch from one to the other) from the command line. Here is how to do this on macOS (tested on High Sierra):

networksetup -setairportnetwork [interface] [SSID] [password]

For example:

networksetup -setairportnetwork en0 SSID1
networksetup -setairportnetwork en0 SSID2
networksetup -setairportnetwork en0 MyHouseSSID MyPassword

Using this command to connect to the same SSID your already connected acts as a reconnect.

Thursday, September 28, 2017

Get wifi details from macOS command line

GUI is great but CLI is awesome. Here are a few macOS CLI commands go get wifi details:

Get details about your current connection

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I

Sample output:

     agrCtlRSSI: -61
     agrExtRSSI: 0
    agrCtlNoise: -90
    agrExtNoise: 0
          state: running
        op mode: station
     lastTxRate: 162
        maxRate: 300
lastAssocStatus: 0
    802.11 auth: open
      link auth: wpa2-psk
          BSSID: 9c:3d:cf:d0:e2:34
           SSID: Maison_5GHz
            MCS: 12
        channel: 36,1

Get the list of an SSID broadcast scan (aka list of available visible SSID)


/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s

Sample output:

                            SSID BSSID             RSSI CHANNEL HT CC SECURITY (auth/unicast/group)
                         VLAN805 54:3d:37:d1:b5:c8 -42  11      Y  US NONE
                         VLAN205 54:3d:37:91:b5:c8 -43  11      Y  US NONE
                         VLAN805 54:3d:37:f4:dc:18 -55  6       Y  US NONE
                         VLAN205 54:3d:37:b4:dc:18 -56  6       Y  US NONE
                      Maison_EXT a0:40:a0:57:d1:bd -36  1       Y  US WPA2(PSK/AES/AES)
                          Maison 9c:3d:cf:d0:e2:30 -47  1       Y  US WPA2(PSK/AES/AES)
                    ESPsoftAP_01 5e:cf:7f:fd:8c:50 -29  1       N  -- WPA2(PSK/AES,TKIP/TKIP)
                     Maison_5GHz 9c:3d:cf:d0:e2:34 -58  36      Y  US WPA2(PSK/AES/AES)

What is interesting with the above output is that you can see that for example the SSIDs VLAN205 and VLAN805 are being broadcasted by 2 different access points or radios at different frequencies.

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

Monday, January 2, 2017

Omega2 with Arduino Dock2 review

Ok, so this is a Kickstarter project (Kickstarter Link) that I backed in 2016. I opted for the Omega2 with the Arduino Dock 2 option cause I'm a big fan of Arduino ;-)

I got delivery of the unit on December 29th 2016 (a nick xmas present !). Loved the simple yes effective packaging:






As you can see they even took care or putting a label on the box with the device MAC address (the same label is on the device itself) which makes it easier to know the IP address from your DHCP logs.

As I said I opted for the Omega2 with the Arduino Dock. The pinout is 100% pin compatible with an Arduino Uno.



There is however one flaw... There are no, I mean zero mounting holes on the Arduino dock.


This means there is no way to tie a shield to the Arduino dock and/or tie the dock in an enclosure securely.

Setting up the unit was pretty straight forward. Like a ChromeCast the units boots in an access point mode broadcasting an SSID matching the last 2 bytes of it's MAC address. From there it's as simple as connecting to this access point, opening a browser to http://192.168.3.1 and follow the setup wizard (here is the wiki link: Omega2 get started).

Once completed (after the firmware update) the unit will reboot. Now here is a tricky part, it takes between 50 and 60s for it to boot and have ssh and http access to it. To me it's a bit long for an embedded system.


Once it booted, as shown above you have a fully functional Linux computer. The Onion team even included a nice WebUI to do various things like controlling IO and even streaming a USB cam which is the first thing I tried. 

I connected a Logitech USB camera and within a few minutes it was streaming:



Pretty impressive !

My next task was to try to upload an Arduino sketch. Now the Arduino is 'private' to the Omega2, meaning it can only be programmed via the Omega2 via the SPI link. There are 2 ways to do this, either via the Omaga2 or via the Arduino IDE wirelessly. I chose the wireless link out out simplicity for a first test.

Not the Onion wiki pages does not seem to mention that but you need to install the Onion library in the Arduino IDE and it's not available from the Library manager. So you have to do it the old fashion way and download the ZIP file from the github repository (Github link) and install in in the Arduino IDE.

And that is where I hit a road block... The device is not seen from the Arduino IDE. Reading the documentation they mention that wireless upload required firmware b220 or later but I have 0.1.6 (b137) and it says it's the latest...


Ok, let's try the Arduino programming from the Omega2+ then... This involves compiling your Uno code using the IDE then taking the hex file to program the Arduino via the Omega2.

[~]$ scp /var/folders/yr/mm_mzpd50wn8j9qv89g4zkw00000gn/T/arduino_build_361557/blink2.ino.hex root@10.0.9.136:/root/
Warning: Permanently added '10.0.9.136' (RSA) to the list of known hosts.
root@10.0.9.136's password:
blink2.ino.hex                                                                                                                                                          100%   14KB 138.0KB/s   00:00

My first flashing attempts failed, but after trying things and doing factory reset on my Omega2 a few times, I finally figured it out... It's an issue with avrdude 6.3-1 as stated here: Bug tracker link

So here is the procedure:

cd /root
opkg remove arduino-dock
opkg remove arduino-dock-2
opkg remove avrdude
wget http://repo.onion.io/omega2/packages/onion/avrdude_6.1-1_mipsel_24kc.ipk
opkg install avrdude_6.1-1_mipsel_24kc.ipk
opkg install arduino-dock-2
After that I was successfully able to flash the Uno part of the dock from the Omega2:

root@Omega-5923:~# sh /usr/bin/arduino-dock flash
avrdude_6.1-1_mipsel_24kc.ipk  blink2.ino.hex
root@Omega-5923:~# sh /usr/bin/arduino-dock flash blink2.ino.hex
> Flashing application 'blink2.ino.hex' ...

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: erasing chip
avrdude: reading input file "0x3f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x3f:
avrdude: load data lock data from input file 0x3f:
avrdude: input file 0x3f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lock verified
avrdude: reading input file "0x05"
avrdude: writing efuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of efuse written
avrdude: verifying efuse memory against 0x05:
avrdude: load data efuse data from input file 0x05:
avrdude: input file 0x05 contains 1 bytes
avrdude: reading on-chip efuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of efuse verified
avrdude: reading input file "0xDA"
avrdude: writing hfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of hfuse written
avrdude: verifying hfuse memory against 0xDA:
avrdude: load data hfuse data from input file 0xDA:
avrdude: input file 0xDA contains 1 bytes
avrdude: reading on-chip hfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of hfuse verified
avrdude: reading input file "0xFF"
avrdude: writing lfuse (1 bytes):

Writing | ################################################## | 100% 0.00s

avrdude: 1 bytes of lfuse written
avrdude: verifying lfuse memory against 0xFF:
avrdude: load data lfuse data from input file 0xFF:
avrdude: input file 0xFF contains 1 bytes
avrdude: reading on-chip lfuse data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lfuse verified

avrdude done.  Thank you.


avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: NOTE: "flash" memory has been specified, an erase cycle will be performed
         To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file "blink2.ino.hex"
avrdude: input file blink2.ino.hex auto detected as Intel Hex
avrdude: writing flash (5084 bytes):

Writing | ################################################## | 100% 3.52s

avrdude: 5084 bytes of flash written
avrdude: verifying flash memory against blink2.ino.hex:
avrdude: load data flash data from input file blink2.ino.hex:
avrdude: input file blink2.ino.hex auto detected as Intel Hex
avrdude: input file blink2.ino.hex contains 5084 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 3.36s

avrdude: verifying ...
avrdude: 5084 bytes of flash verified
avrdude: reading input file "0x0f"
avrdude: writing lock (1 bytes):

Writing | ################################################## | 100% 0.01s

avrdude: 1 bytes of lock written
avrdude: verifying lock memory against 0x0f:
avrdude: load data lock data from input file 0x0f:
avrdude: input file 0x0f contains 1 bytes
avrdude: reading on-chip lock data:

Reading | ################################################## | 100% 0.00s

avrdude: verifying ...
avrdude: 1 bytes of lock verified

avrdude: safemode: Fuses OK (E:05, H:DA, L:FF)

avrdude done.  Thank you.

> Done, flash successful
Following doing this and doing a cold reboot to the unit I now see it in the Arduino IDE:

And was even able to flash the Arduino wirelessly !








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...