Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    Script to control Wemos from gcode using DSF

    Scheduled Pinned Locked Moved
    DSF Development
    4
    11
    718
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • oozeBotundefined
      oozeBot
      last edited by oozeBot

      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
      
      T3P3Tonyundefined CrazyCreatorundefined 2 Replies Last reply Reply Quote 2
      • T3P3Tonyundefined
        T3P3Tony administrators @oozeBot
        last edited by

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

        www.duet3d.com

        1 Reply Last reply Reply Quote 0
        • PCRundefined
          PCR
          last edited by

          Would add that you need execonmcode installed!

          1 Reply Last reply Reply Quote 0
          • CrazyCreatorundefined
            CrazyCreator @oozeBot
            last edited by CrazyCreator

            Hello @oozebot

            I think I understand what your script is doing, but unfortunately I have absolutely no idea what I have to change to make it work with my Shelly.

            In my theory, I just want to call a web address. I don't actually need the different parameters.
            It should just be turned off stupidly.

            I think only the web call (http://192.168.1.47/relay/0?turn=off) would have to be stored in the wemo.sh.
            Unfortunately, I don't know whether I need the curl, content-type, charset etc either. i have no idea about these things

            http://www.crazycreatorcube.com

            1 Reply Last reply Reply Quote 0
            • CrazyCreatorundefined
              CrazyCreator
              last edited by

              anybody can help me with a hint

              http://www.crazycreatorcube.com

              oozeBotundefined 1 Reply Last reply Reply Quote 0
              • oozeBotundefined
                oozeBot @CrazyCreator
                last edited by

                @CrazyCreator - this is untested, but all you need is something like the following. Google curl for more info.

                #!/bin/sh
                curl -s 'http://192.168.1.47/relay/0?turn=off' > /dev/null
                
                CrazyCreatorundefined 1 Reply Last reply Reply Quote 0
                • CrazyCreatorundefined
                  CrazyCreator @oozeBot
                  last edited by

                  @oozebot
                  Well that sounds understandable even to me. 😉

                  But I still ask one more question:
                  Where do you create the scripts folder where your wemo.sh is then located?

                  http://www.crazycreatorcube.com

                  CrazyCreatorundefined 1 Reply Last reply Reply Quote 0
                  • CrazyCreatorundefined
                    CrazyCreator @CrazyCreator
                    last edited by

                    @oozeBot Can you give me a little tip where I should create the folder and then wemo.sh must go?

                    http://www.crazycreatorcube.com

                    oozeBotundefined 1 Reply Last reply Reply Quote 0
                    • oozeBotundefined
                      oozeBot @CrazyCreator
                      last edited by

                      @crazycreator the script can go most anywhere in the file system. If you look close at the service script I provided above, you’ll see we use a folder named “scripts” off the root.

                      CrazyCreatorundefined 2 Replies Last reply Reply Quote 1
                      • CrazyCreatorundefined
                        CrazyCreator @oozeBot
                        last edited by

                        @oozebot
                        Many thanks for the answer .. now I can try 🙂

                        http://www.crazycreatorcube.com

                        1 Reply Last reply Reply Quote 0
                        • CrazyCreatorundefined
                          CrazyCreator @oozeBot
                          last edited by

                          @oozebot
                          I could kiss you right now 😉

                          It works absolutely perfectly.

                          I now have a real physical switch on the printer to turn it on and off and can still switch it off automatically at the end of a print 🙂 Love it

                          http://www.crazycreatorcube.com

                          1 Reply Last reply Reply Quote 1
                          • First post
                            Last post
                          Unless otherwise noted, all forum content is licensed under CC-BY-SA