Skip to content

Instantly share code, notes, and snippets.

@nrobinson2000
Forked from jenschr/ParticleDetect.sh
Created April 6, 2018 23:59
Show Gist options
  • Select an option

  • Save nrobinson2000/bb6adcd27a2996c4da3d6e8b60f7634b to your computer and use it in GitHub Desktop.

Select an option

Save nrobinson2000/bb6adcd27a2996c4da3d6e8b60f7634b to your computer and use it in GitHub Desktop.

Revisions

  1. @jenschr jenschr revised this gist Apr 6, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    #!/bin/sh

    # Basic setup script for devices
    # Works on OSX + should also work for Unix.
    # Users of Windows 10 (or higher) can likely use the builtin Ubuntu install.
    #
    # We need to do the following:
    # 1. Query the computer for the relevant USB port
  2. @jenschr jenschr revised this gist Apr 6, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,8 @@
    # @rickkas7 for examples in https://github.com/rickkas7/particle-device-helper
    # @jrodas for examples https://community.particle.io/t/firmware-tips-and-tricks/3649/35
    # Written by https://community.particle.io/u/jenschr
    # Feel free use as you like :-)
    #
    # Use as you like on your own responsibility :-)

    # 1. cross platform (unix+OSX) way to grab the USB port name for the Particle device
    a="ls /dev/tty.usbmodem*"
  3. @jenschr jenschr revised this gist Apr 6, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,8 @@
    # Thanks to:
    # @rickkas7 for examples in https://github.com/rickkas7/particle-device-helper
    # @jrodas for examples https://community.particle.io/t/firmware-tips-and-tricks/3649/35
    # Written by @jenschr. Feel free use as you like.
    # Written by https://community.particle.io/u/jenschr
    # Feel free use as you like :-)

    # 1. cross platform (unix+OSX) way to grab the USB port name for the Particle device
    a="ls /dev/tty.usbmodem*"
  4. @jenschr jenschr revised this gist Apr 6, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,6 @@
    #!/bin/sh

    # Basic setup script for devices
    # Written by @jenschr. Feel free use as you like.
    #
    # We need to do the following:
    # 1. Query the computer for the relevant USB port
    @@ -16,7 +15,8 @@
    #
    # Thanks to:
    # @rickkas7 for examples in https://github.com/rickkas7/particle-device-helper
    # @jrodas for examples https://community.particle.io/t/firmware-tips-and-tricks/3649/35?u=jenschr
    # @jrodas for examples https://community.particle.io/t/firmware-tips-and-tricks/3649/35
    # Written by @jenschr. Feel free use as you like.

    # 1. cross platform (unix+OSX) way to grab the USB port name for the Particle device
    a="ls /dev/tty.usbmodem*"
  5. @jenschr jenschr revised this gist Apr 6, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    #!/bin/sh

    # Basic setup script for devices
    # Written by @jenschr. Feel free use as you like.
    #
    # We need to do the following:
    # 1. Query the computer for the relevant USB port
  6. @jenschr jenschr created this gist Apr 6, 2018.
    78 changes: 78 additions & 0 deletions ParticleDetect.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,78 @@
    #!/bin/sh

    # Basic setup script for devices
    #
    # We need to do the following:
    # 1. Query the computer for the relevant USB port
    # 2. Set the device to listening mode (if required)
    # 3. Query the device for the deviceID
    # 4. Set the Wifi Credentials using credentials stored in text file:
    # "particle serial wifi /dev/tty.usbmodem1451 --file defaultwifi.json"
    # 5. Claim the device (make sure you're logged in with the correct user!)
    # 6. Set the device DFU mode, so we can upload firmware
    # 7. Flash our device test & setup firmware via Serial:
    # particle serial flash setup.bin
    #
    # Thanks to:
    # @rickkas7 for examples in https://github.com/rickkas7/particle-device-helper
    # @jrodas for examples https://community.particle.io/t/firmware-tips-and-tricks/3649/35?u=jenschr

    # 1. cross platform (unix+OSX) way to grab the USB port name for the Particle device
    a="ls /dev/tty.usbmodem*"
    b="$(echo $a)"

    # remove the returned part "ls " (3 chars) using cut
    c="$(echo $b | cut -c3-)"

    # 2. Set listen mode (yep - 3 times in a row for those annoying boards that do not respond at once)
    d="$(stty -f $c 28800)"
    echo $d
    sleep 1

    # The above will normally set the device to Listen mode, but since it may fail in some cases we'll confirm here
    read -p "Is the device in Listen Mode (blue blink) (y/n)?" choice
    case "$choice" in
    y|Y ) echo "yes";;
    n|N ) exit;;
    * ) exit;;
    esac

    # 3. Figure out the DeviceID (Requires Particle CLI installed)
    f="$(particle identify)"
    #echo $f

    # extract the 25 char deviceID from what "particle identify" returned
    g=${f:19:25}
    if [ ${#g} -lt 15 ]; then
    # if the deviceID is less than 15 chars, it didn't work so just exit
    echo "Can't find a valid deviceID. Make sure the device is in Listen Mode (Blue blink)"
    exit
    else
    echo "A device with ID $g was detected"
    fi

    # 4. setup the wifi credentials
    h=""
    i="$(particle serial wifi -q $c --file defaultwifi.json)"

    #5 Wait 10 seconds and then claim the device
    echo "Wifi setup finished, now wait a little for the device to connect so we can claim it"
    sleep 10
    d="$(particle cloud claim $g)"
    echo $d

    # 6. Set device to DFU mode
    echo "Getting ready to flash firmware"
    sleep 2
    j="$(stty -f $c 14400)"
    echo $j

    # 7. Flash our test software
    sleep 2
    k="$(particle flash --usb setup.bin)"

    echo "done! $k"

    # since your setup file will usually need to display some output, we'll open the serial monitor so you can check it all worked
    sleep 2
    exec particle serial monitor --follow