Script to control Wemos from gcode using DSF



  • I spent some time on this earlier and thought I'd share my first success with it. This script is something we used with our printers prior to moving to Duet. It is a script I wrote (based off of a bit of info scraped off the internet of Wemo's undocumented API). It interfaces with local Wemo switches, plugs, and dimmers locally without the Cloud. The only caveat is that you have to know all the IPs for your wemos.. we just made dhcp reservations for all of ours.

    The script primarily turns Wemos on and off; toggles; and can set dimmer brightness. It can also return the current values as well. However, for this post, I'm going to focus on the gCode implementation I just completed.

    Note this service code originally gave me trouble as it turned out "%" had to be escaped as "%%". That may be beneficial for others wanting to implement similar scripts with parameter inputs.

    I chose M5555. It has 3 inputs - I, C, & B. I is the IP address of the Wemo (or potentially hostname). C is the command. B is the brightness value for setting a dimmer.

    Example usage

    M5555 I"192.168.0.123" C"ON"               ; turns on the Wemo
    M5555 I"192.168.0.123" C"OFF"              ; turns off the Wemo
    M5555 I"192.168.0.123" C"TOGGLE"           ; toggles the Wemo (if off, turn on | if on, turn off)
    M5555 I"192.168.0.123" C"SETDIMMER" B"50"  ; sets dimmer to 50%
    

    M5555.service code

    [Unit]
    Description=Duet API listener for Wemo control
    After=duetcontrolserver.service
    Requires=duetcontrolserver.service
    
    [Service]
    ExecStart=/usr/local/bin/execonmcode -mCode 5555 -command "./scripts/wemo.sh %%I %%C %%B"
    Restart=always
    RestartSec=10
    
    [Install]
    WantedBy=multi-user.target
    

    wemo.sh

    #!/bin/sh
    # Wemo Control
    # Usage: ./wemo_control IP Command
    # Example: ./wemo_control 192.168.0.1 ON
    # ON/OFF/STATE/PORT/SIGNAL/GETDIMMER/SETDIMMER
    
    IP=$1
    COMMAND=$2
    BRIGHT=$3
    PORT=49152
    
    if [ $IP = "" ];then
     echo "Invalid Command"
    else
     PORTTEST=$(curl -s $IP:$PORT | grep "404")
     if [ "$PORTTEST" = "" ];then
      PORT=49153
     fi
     if [ $PORT = 49153 ];then
      PORTTEST=$(curl -s $IP:$PORT | grep "404")
      if [ "$PORTTEST" = "" ];then
       PORT=49154
      fi
     fi
     if [ $PORT = 49154 ];then
      PORTTEST=$(curl -s $IP:$PORT | grep "404")
      if [ "$PORTTEST" = "" ];then
       PORT=49155
      fi
     fi
     if [ $PORT = 49155 ];then
      PORTTEST=$(curl -s $IP:$PORT | grep "404")
      if [ "$PORTTEST" = "" ];then
       PORT=0
      fi
     fi
     if [ $PORT = 0 ];then
      echo "Wemo Unavailable on IP $IP"
     else
      if [ "$COMMAND" = "STATE" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       grep "<BinaryState"  | cut -d ">" -f2 | cut -d "<" -f1 | sed 's/0/OFF/g' | sed 's/1/ON/g'
      elif [ "$COMMAND" = "ON" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       if [ "$( grep "<BinaryState"  | cut -d ">" -f2 | cut -d "<" -f1 )" = "0" ];then
        curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
    	grep "<BinaryState" | cut -d ">" -f2 | cut -d "<" -f1 | sed 's/0/OFF/g' | sed 's/1/ON/g'
       else
        echo "ON" #Already On
       fi
      elif [ "$COMMAND" = "OFF" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       if [ "$( grep "<BinaryState"  | cut -d ">" -f2 | cut -d "<" -f1 )" = "1" ];then
        curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
        grep "<BinaryState" | cut -d ">" -f2 | cut -d "<" -f1 | sed 's/0/OFF/g' | sed 's/1/ON/g'
       else
        echo "OFF" #Already Off
       fi
      elif [ "$COMMAND" = "SETDIMMER" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><brightness>'$BRIGHT'</brightness></u:SetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       grep "<brightness"  | cut -d ">" -f2 | cut -d "<" -f1 
      elif [ "$COMMAND" = "GETDIMMER" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       grep "<brightness"  | cut -d ">" -f2 | cut -d "<" -f1
      elif [ "$COMMAND" = "TOGGLE" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:GetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       if [ "$( grep "<BinaryState"  | cut -d ">" -f2 | cut -d "<" -f1 )" = "1" ];then
        curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>0</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
        grep "<BinaryState" | cut -d ">" -f2 | cut -d "<" -f1 | sed 's/0/OFF/g' | sed 's/1/ON/g' | sed 's/Error/OFF/g'
       else
        curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:SetBinaryState xmlns:u="urn:Belkin:service:basicevent:1"><BinaryState>1</BinaryState></u:SetBinaryState></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
        grep "<BinaryState" | cut -d ">" -f2 | cut -d "<" -f1 | sed 's/0/OFF/g' | sed 's/1/ON/g' | sed 's/Error/ON/g'
       fi
      elif [ "$COMMAND" = "SIGNAL" ];then
       curl -0 -A '' -X POST -H 'Accept: ' -H 'Content-type: text/xml; charset="utf-8"' -H "SOAPACTION: \"urn:Belkin:service:basicevent:1#GetSignalStrength\"" --data '<?xml version="1.0" encoding="utf-8"?><s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body><u:GetSignalStrength xmlns:u="urn:Belkin:service:basicevent:1"><GetSignalStrength>0</GetSignalStrength></u:GetSignalStrength></s:Body></s:Envelope>' -s http://$IP:$PORT/upnp/control/basicevent1 |
       grep "<SignalStrength" | cut -d ">" -f2 | cut -d "<" -f1
      elif [ "$COMMAND" = "PORT" ];then
       echo "$PORT"
      else
       echo $COMMAND
      fi
     fi
    fi
    

  • administrators

    @oozeBot I forked this from the previous SBC topic as it stands alone as something useful.



  • Would add that you need execonmcode installed!


Log in to reply