Skip to content

Instantly share code, notes, and snippets.

@TheGroundZero
Created April 15, 2021 13:14
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheGroundZero/4ea90aa9788ac91c3193d6fbe03b23c3 to your computer and use it in GitHub Desktop.
Save TheGroundZero/4ea90aa9788ac91c3193d6fbe03b23c3 to your computer and use it in GitHub Desktop.
Simple loop using cURL to change the config of all Shellys in a network
# See docs on HTTP API
# https://shelly-api-docs.shelly.cloud/#settings
# Linux / Mac
for i in {2..254}; do curl -m 1 http://192.168.1.$i/settings?<setting parameters>; done
# Windows
# Note: use %% instead of % when saving this in a BATCH script (.bat)
FOR /L %I IN (2,1,254) DO curl -m 1 http://192.168.1.%I/settings?<setting parameters>
# --- EXAMPLES ---
# -- Installing FW upgrade --
# MQTT
# See https://shelly-api-docs.shelly.cloud/#common-mqtt-commands
Send `update_fw` to `shellies/command`
# HTTP API
# See https://shelly-api-docs.shelly.cloud/#ota
# Linux / Mac
for i in {2..254}; do curl -m 1 http://192.168.1.$i/ota?update=true; done
# Windows
FOR /L %I IN (2,1,254) DO curl -m 1 http://192.168.1.%I/ota?update=true
# -- Enabling CoIoT --
# Linux/Mac
for i in {2..254}; do curl -m 1 http://192.168.1.$i/settings?coiot_enable=true&coiot_peer=192.168.x.x; done
# Windows in CMD (in a .bat use %% instead of %)
FOR /L %I IN (2,1,254) DO curl -m 1 http://192.168.1.%I/settings?coiot_enable=true&coiot_peer=192.168.x.x
# -- Disable MQTT update period --
# Linux/Mac
for i in {2..254}; do curl -m 1 http://192.168.1.$i/settings?mqtt_update_period=0; done
# Windows in CMD (in a .bat use %% instead of %)
FOR /L %I IN (2,1,254) DO curl -m 1 http://192.168.1.%I/settings?mqtt_update_period=0
@bartgrefte
Copy link

Do you happen to have a list of settings that can be changed this way? Having trouble finding this, "coiot_enable" and "coiot_peer" (don't you need a port?) only seem to appear in a changelog.

@TheGroundZero
Copy link
Author

Do you happen to have a list of settings that can be changed this way? Having trouble finding this, "coiot_enable" and "coiot_peer" (don't you need a port?) only seem to appear in a changelog.

Have a look at the Shelly API Reference for more info on what you can change via the API. Note that there's a difference between Gen1 and Gen2 (Plus, Pro, Mini).

coiot_peer might need a port. I seem to recall it auto-filled the default port, but if it doesn't you can add :5683 as port (thus coiot_peer=192.168.x.x:5683)

@bartgrefte
Copy link

@TheGroundZero I saw that page, but nothing seems to be correct. Every time I try to change something using http, I seem to have to replace the . with _ to get it to work. So if that page says "sntp.server", I have to use "sntp_server", "coiot.peer" has to become "coiot_peer" and I've got that with more options.

@TheGroundZero
Copy link
Author

TheGroundZero commented Nov 28, 2023

@TheGroundZero I saw that page, but nothing seems to be correct. Every time I try to change something using http, I seem to have to replace the . with _ to get it to work. So if that page says "sntp.server", I have to use "sntp_server", "coiot.peer" has to become "coiot_peer" and I've got that with more options.

Be wary of the difference between the response attributes and request parameteres.

A request to e.g. http://192.168.x.x/settings will return a JSON object containing e.g.

{
    "device": {
        "type": "SHSW-21",
        "mac": "16324CAABBCC",
        "hostname": "shelly1-B929CC"
    },
    # ...
    "sntp": {
        "server": "time.google.com",
        "enabled": true
    },
    # ...
}

So you can extract the key-value pairs device.type = SHSW-21 and sntp.server = time.google.com.

To modify e.g. the SNTP server, you need to look at the HTTP Parameters section and send a GET request to http://192.168.x.x/settings?sntp_server=my_timeserver.domain.tld

@bartgrefte
Copy link

@TheGroundZero I know about the JSON-response, that part was obvious and is as easy as using

response = requests.get("http://IP-address/settings", timeout=0.500)
hostname = response.json()["device"]["hostname"]

but having to use _ instead of . for the configuring part (from Python) was not. But now that I know, I can continue with writing a script that will config future Shelly's with a single command :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment