Particle-CLI scripts/automation for initial setup on large number of devices

Is there a CLI sdk/api available to setup particle devices over serial without having to enter the same config over and over? I have about 20 devices and to set them up over serial I need to go through the same motions every time.

I was trying to create a Python wrapper around it that would parse the stdout stream using something like Pexpect (python wrapper on Expect). But it turns out that node js app uses inquirer and so it’s waiting for prompt/input which is not something Pexpect can handle.

So, how do I hack or write a script over the particle-cli app so that I can run it without interaction?

If that’s not a possibility, can I just get a list of methods that I need to execute to write wifi credentials on the device along with claiming the device? For example, the token can be acquired from the Cloud REST api. So I am guesssing along with the token and using DFU we can program the wifi data on the device.

Setting the credentials can be done over Serial. If you’ve got a serial terminal, give the following a try when in listening mode:
-hit w, then enter your SSID as prompted
-enter your security type as prompted
-enter your password as prompted.
That should do it.

If you can somehow code those interactions up in a python/node.js script, you should be good to go. The CLI is open source, so you can make adjustments as you please. Rather than waiting for prompts, you could change it to use hard-coded credentials?

Alternatively, you could create a user firmware in which you set the credentials in code. Then flash that to the device over DFU mode. If I’m not mistaken, they should then ‘stick’ until they’re purposely erased. But then you’d still have to find a way to claim it.

1 Like

But you have to start with particle serial as a command right? If I were to plugin 20 devices in one go, I would have to invoke particle serial 20 times.

Yes, that's the issue. It's incredibly hard to code those interactions and that's why I posted the question.

I would love to learn how to do it. The claiming can be done over cloud api, can't it?

I think the best way would be that Particle release their API/SDK for command line, like the did for iOS and Android. I’ve emailed sales but I am still waiting.

An serial terminal will work. I just tried putty for windows to confirm. Actually, that's all the CLI does behind the scenes. It has you enter your credentials, which it then passes over to the Photon by using the said commands. If you can write a script to send those commands, that should work just fine. It should send a "w", then your SSID, followed by your security type, and finally your password. No need to use the CLI for that. You should be able to do that in python I think, if you're more comfortable with that.

You could use this code to set the credentials over firmware: https://docs.particle.io/reference/firmware/photon/#wifi-setcredentials-

Yes, you can also use the CLI for that, but there's an endpoint as well: https://docs.particle.io/reference/api/#claim-a-device

Since this is about the CLI, let's tag @nexxy as well, she tends to know a thing or two about it :wink:

hey @Moors7, thanks for the ping!

This is something I initially planned to support with the CLI along with the Photon release, but I wasn’t able to make it stable enough to feel comfortable releasing at the time. This is the first request I know of for batch setup so far, so I guess now is a good time to revisit the idea!

@pronoyc,

Would you mind giving me a little write-up as far as what you envision as the ideal workflow for this kind of batch setup? Are all devices destined for the same Wi-Fi network, owned by the same user, etc.? A step-by-step list of how you’d like things to go would be helpful!

If anyone else has suggestions for making this useful for them I’d love to hear it as well.

Thanks!
— Nexxy

1 Like

@nexxy thanks for the quick response.

Well, the idea is to setup photon based sensor modules at a customer’s place. They have a working wifi network that the devices need to attach to.

Say I have 20 sensors that need to be deployed. I have a usb hub where I plug in all my devices and I have a utility that recognizes photons have been plugged in. I enter the owner cloud credentials once along with the wifi credentials. The script/utility loops over them and programs the wifi on to the photons.

Ideally I would want to do this on a UI instead of command line. But the first step is a script or some sort of SDK that I can use to call the methods from the UI.

Is that what you meant by a step by step list or would you like it if it were in more detail?

This is seriously cool. So what happens when you have to setup your Photon for the first time? you still have to go through the motion of claiming it right? That's the issue.

  1. Claim the photon
  2. Send it wifi credentials
  3. Repeat for 20 devices.

Here’s an expect script using a *nix command line (note: not the Particle CLI, just plain old Bash or whatever):

#!/usr/bin/expect -f

# SSID:
# Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2:
# Wifi Password:

eval spawn particle serial wifi

expect "Should I scan for nearby Wi-Fi networks?"
send "n\r"
sleep 0.1

expect "SSID:"
send "YourSecureWirelessNetwork\r"
sleep 0.1

expect "WPA2"
send "\r"
sleep 0.1

expect "Wi-Fi Password:"
send "YourAwesomePassword\r"

interact

As long as it’s a WPA2 network, you should be okay. And keep the \r at the end of the lines.

4 Likes

That’s really awesome. I forgot that particle serial outputs only one line so that expect can parse it. It fails however with things like particle setup.

If you have any idea how to automate that part, that would be amazing!

this is beautiful, thank you @wgbartley.

I will of course be adding this type of bulk setup to the CLI itself, but I’m glad @wgbartley has a workaround in the mean time.

@pronoyc what you describe is exactly what I had envisioned. I can’t exactly guarantee this will be a high-priority thing but I will definitely be implementing it at some point (pull requests of course welcome).

Thanks @nexxy I already forked the particle-cli repo on github and I might be working on it later. That being said, I’ve already created a GUI that allows users to claim devices and set them up in bulk. I’ll release it shortly, once I am done testing.

Thanks for all your support.

@pronoyc,

I like the idea of a GUI. I’d love to see what you came up with when you get a chance.

Let me know if there’s anything I can do to help!

Hey guys, just a heads up. Proud to announce the first python-particle release.

https://pypi.python.org/pypi/python-particle/

I’ll be updated the docs soon.

1 Like

@pronoyc, i have tried running python particle.py and nothing happens.

do you have an example of this running?

Thanks,

Hey @wesner0019, sorry I haven’t been able to work on the docs or the package for a while now (work stuff). But here’s how you use it. Once you install the package, you’ll need to import the main class Particle

example:
from particle import Particle
p = Particle(‘your-username’, ‘your-password’)

This will fetch existing valid tokens using the method get_valid_token . You don’t have to worry about fetching the token, it’ll do that automatically.

After that, you can use the following methods:

get_device_list
get_device_info
claim_device
get_variable

You can see arguments each method requires by going through the method definition here. https://github.com/DarkSector/python-particle/blob/master/particle/particle.py#L141

I really should update the docs.

Thanks @pronoyc,

Do you have a quick explanation on how to install the package? I've been trying to learn but its slow going.

Also do you have a quick step by step on how you actually setup a device like:
python particle.py
claim device
send wifi data

etc.

Thanks again I have around 150 devices to setup and this could be much quicker

@wesner0019 Dunno if you got it working, but here’s a bash script that does it all: Can a P1 be setup to connect to the internet without claiming it?