Support for Smart Plug commands like TP-LINK (either directly or via HTTP POST request)

Hi,

I have my printer setup hooked up to a TP-LINK smartplug which i use to power of the printer after successfull prints. As the current repetier-server does not support sending event based commands (or HTTP requests) i have it working but its a bit fussy and ideally i would love to have a more direct solution out of the repetier-server if possible.

Basically atm it works like this: If a print is completed i get a notification on the repetier infoermer app on android. I then use Automagic app to trigger a HTTP POST request to my TP-LINK smartplug to switch it off.

Ideally i would love to have such a feature (either TP-LINK support or HTTP requests) from within Repetier-Server to issue this command directly without having to resort to my above method which is prone to possible issues (if the phone is off etc).

Octoprint for instance has a dedicated plugin for the TP-LINK smartplug (https://github.com/jneilliii/OctoPrint-TPLinkSmartplug) which has the option to issue the on/off command triggered by GCODE M80 ip address / M81 ip address. 

Either solution would be fine for me if possible.

Cheers
«1

Comments

  • That would only make sense for us if we can support all smart links and own them for testing.

    None th eless, it is not impossible. That is what the extcommands.xml file is for, please read manual for details. This allows you to add menu entries in printer menu to execute on your pc. It also allows starting a script with @execute server command. So you can even add it in your end event script. Only thing you need to consider is that printer might not be finished when this command gets executed. Typical solution is to add 30 M400 before the @execute line then so printer is finished with everything before that line. The called script should none the less add a 5s pause before turning off power so server can cleanly finish job before printer disconnects.
  • Yes i understand that such a development would only make sense if it would be more widely applicable.

    I also though about the extcommands.xml solution but honestly the documentation on how exactly that works is a bit sparse for me. I am not a linux or python guru so writing a script that creates json or http post commands from scratch is a bit beyond my knowledg. I usually reverse engineer such things if they have been developed for similar purposes and make my own adjustments but i don't know how to write something like this from scratch.
  • If you know the http post command the script is quite easy if you use curl to send it.

    #!/bin/bash
    sleep 5
    curl .....

    thats it. Just needs to check curl syntax. Extcommands is just calling the script file then. Only make sure the user repetierserver is allowed to execute it.
  • Hi,

    I have managed to get a script (hs100.sh) that works if i run it manually from ssh. I have added the following command to extcommands but it does not trigger the script no matter if i use it with @execute TPLINKOFF in the event based gcode tab or if i try to run it directly from the menu. What am i missing?

    <command>
    <name>TPLINKOFF</name>
    <execute>sudo /home/pi/hs100.sh 192.168.1.120 9999 off</execute>
    <local>true</local>
    <remote>true</remote>
    </command>
  • I have also tryed with following format:

    <command>
    <name>TPLINKOFF</name>
    <execute>./hs100.sh 192.168.1.120 9999 off</execute>
    <local>true</local>
    <remote>true</remote>
    </command>
  • I did some more experimenting. So executing from menu now works however it does not seem to work from event based GCODE (@execute TPLINKOFF).

    <command>
    <name>TPLINKOFF</name>
    <confirm>Really turn off TP Link Plug?</confirm>
    <execute>/home/pi/hs100.sh 192.168.1.120 9999 off</execute>
    <local>true</local>
    <remote>true</remote>
    <print-permission>true</print-permission>
    <add-permission>true</add-permission>
    <del-permission>true</del-permission>
    <config-permission>true</config-permission>
    </command>
  • Try to make a script without parameter. That might help with the @execute command. Also note that for execute there is a separate section in extcommands. <command> is only for the menu.
  • Thanks for the tip. I didn't even notice the seperate section in extcommands for execute :P Now that you mentioned it i set it up and it works from GCODE command.
  • Everything works fine now. I have added a 5 minute sleep into the script so that the printer has sufficient time to finish enything and everything like converting the timelapses etc before being shut down. It all works flawlessly now.

    Thanks again for your support and tips!
  • Everything works fine now. I have added a 5 minute sleep into the script so that the printer has sufficient time to finish enything and everything like converting the timelapses etc before being shut down. It all works flawlessly now.

    Thanks again for your support and tips!
    hi there,
    plz tell me more about your work. i would like to switch off my printer also with a hs100.
  • Yeah me too :) Freakadude can you explain how you did that? Thanks in advance
  • Right. So i found the script to this online (don't remember where anymore) so i am not taking any credit for that.
    But in essence with setting a few parameters with that script you can tell your switch to turn on or off. You then build it into repetier server either as a switch or to be triggered with an @execute command using the event dependant g-code settings.

    So what you need to do is following:

    1. Download the script ( i put it up on dropbox here: https://www.dropbox.com/s/00nt6lsma7mbf73/hs100B.sh?dl=0 ). Check the script as it has some instructions in it on how to use it. Basically you run it with passing 3 parameters and then it does its thing.

    2. Copy the script onto your RPI or whatever you are running your repetier server on into a folder.

    3. Open and edit your extcommands.xml file on your repetier server. 
     
    4. Add following line at the very end just before the </config>: <execute name="TPLINKOFF" allowParams="true">/pathtoyourscript/hs100B.sh ipofyourTPLinkswitch 9999 off</execute>

    You can name your command whatever you want. I named mine TPLINKOFF as seen above.

    5. In the repetier server webgui go to Printer Settings->G-Codes->Event Dependent and in the Run after job section add: @execute TPLINKOFF

    Now whenever repetier server has finished your print it will trigger this script as entered with the parameters in the extcommands.xml file. So in this case it will tell the switch to turn itself off.

    Now since i usually also make timelapses of my prints and repetier server does some compiling still even after the print is finished i personally also added a timer into the script to wait for 180 seconds after being activated before it turns off the switch. This gives repetier enough time to finish up the timelapse. If you want to do a similar delay then you need to edit the script. Open it up in notepad++ and search for the line: cmd_switch_off(){
    I added command sleep 180 (this tells the script to wait for 180 seconds at this point before proceeding with the next command which is the actual command it sends to the TP LINK switch to turn itself off) which takes care of the delay.

    And thats it.





  • Thanks a lot Freakadude! :smile: I will give it a try.
  • Hello Freakadude :) In the script I just have to change the first three parameters (ip, port and cmd)? Thanks 
  • You don't need to edit anything inside the hs100B.sh script. You need to add your own ip, port and cmd to repetier servers own extcommands.xml file as menitoned in my post above in point 4. This will trigger the hs100B.sh script on your device and pass on the relevant ip, port and command to trigger.
  • edited February 2020
    Thank you very much for your help  Freakadude but I still couldn't solve it :( And sorry
    Can you confirm that the steps are all correctly executed?
    1-Download script - ok
    2-Copy the script onto my RPI. The folder can be "/home/pi/"?
    3, 4 an 5- Does the attached images seems correct?



    Thanks!
  • You need to change "pathtoyourscript" to actually be the path where you store the hs100B.SH named script on your machine. So if you have stored the script under: /home/pi/ then you need to replace "pathtoyourscript" with "/home/pi/hs100B.sh". Same goes for "ipofyourTPLinkswitch".  You need to change it the actuall ip address of your tp link switch that you want to control on your network. So if your switch would have i.e. IP of 192.168.0.255 then you would put that in the line instead of "ipofyourTPLinkswitch".
  • Ahh OK :) 

    But like that? anything incorrect because it didn't work
  • You have 2 backslashes too many. Use the command like this: <execute name="TPLINKOFF" allowParams="true">/home/pi/hs100B.sh 192.xxx.xxx.xxx 9999 off</execute>
  • Sorry Freakadude but it didn't work :( 

  • can you post some images of your configuration? to compare and check what's wrong.
    Many thanks for your help
  • Just a side node - next release will natively support web requests as commands to e.g. call IFTTT maker webhooks to disable a plug. These commands will also be available in menus then. That makes it very easy to switch any plug with IFTTT support.
  • edited March 2020
    i will step in this discussion also;

    TP-Link and this script also let report the actual Watt consumption .
    Any one does have an idea to set this report as an status symbol in the top nav bar of the printer? That this work I tested with Node-Red.

  • How do you get wattage with a script. Checked IFTTT plugin for KASA and all I can to is control output there.

    The emergency stop will not show it. There is no way at the moment to add anything there. Maybe we allow in future to have a montor script that returns values in special format to be shown on a page. Then we cadd anything users want to see in one place.
  • You van call the Script with the Option emeter.
    Than you geht the value.
  • Repetier said:
    The emergency stop will not show it. 
    is set the arrow to show the place between emergency stop and print travel.
  • What do you mean? Don't understand the comment.
  • edited March 2020
    didn´t work :( my file is under /home/pi and the command are this

    <config>
    <!--
           If you want to run external commands, enter them in this configuration with
           full path. You will see them in the main menu. Copy this file into the
           <storage>/database directory.

           Security consideration: The reason there is no online editor for this is simply
           security. The commands you enter here are executed with the privileges of the
           daemon running the server. If you would allow online configuration, a hacker could
           simply add any command he needs to hack your system.
        -->
    <command>
    <name>Shutdown Server</name>
    <execute>sudo /sbin/shutdown -h now</execute>
    <confirm>Really shut down the server?</confirm>
    <!-- Define if command should show up in local printer interface, default true.-->
    <local>true</local>
    <!-- Define if command should show up in remote printer interface, default true.-->
    <remote>true</remote>
    <!-- Define if command should show up only for users with print permission, default true.-->
    <print-permission>true</print-permission>
    <!-- Define if command should show up only for users with add files permission, default false.-->
    <add-permission>true</add-permission>
    <!-- Define if command should show up only for users with del files permission, default false.-->
    <del-permission>true</del-permission>
    <!-- Define if command should show up only for users with configuration permission, default false.-->
    <config-permission>true</config-permission>
    </command>
    <command>
    <name>Reboot Server</name>
    <execute>sudo /sbin/shutdown -r now</execute>
    <confirm>Really reboot the server?</confirm>
                    <!-- Define if command should show up in local printer interface, default true.-->
                    <local>true</local>
                    <!-- Define if command should show up in remote printer interface, default true.-->
                    <remote>true</remote>
                    <!-- Define if command should show up only for users with print permission, default true.-->
                    <print-permission>true</print-permission>
                    <!-- Define if command should show up only for users with add files permission, default false.-->
                    <add-permission>true</add-permission>
                    <!-- Define if command should show up only for users with del files permission, default false.-->
                    <del-permission>true</del-permission>
                    <!-- Define if command should show up only for users with configuration permission, default false.-->
                    <config-permission>true</config-permission>
    </command>
    <execute name="TPLINKOFF" allowParams="true">/home/pi/hs100B.sh 192.XXX.XXX.XX 9999 off</execute>
    <execute name="shutdown" allowParams="false">sudo /sbin/shutdown -h now</execute>
    </config>

    the ip is 100% right. but why 9999 ? is this right or must i change it ? THX
  • make your hs100B.sh script executeable. without calling bash the script does not run.


  • Repetier said:
    What do you mean? Don't understand the comment.
    Ich schreib es mal schnell auf Deutsch.

    Ich wollte einfach einen Button für den TP-Link einsetzen der mir die Befehle An/Aus gibt und im Symbol selber den aktuellen Watt Verbrauch einblendet wie die Temperaturanzeigen auch.
    Den Button zwischen dem Nothalt und den anderen Buttons einblenden, nicht den Nothalt ersetzen.



Sign In or Register to comment.