Author Topic: Check Status and Toggle a WeMo Smart Outlet  (Read 7199 times)

Doctor_Mod

  • Guest
Check Status and Toggle a WeMo Smart Outlet
« on: April 03, 2019, 08:34:20 PM »
Lemme give a BIG Shout out to the fine folks on the Voice Attack Discord. Y'all are heros.

So lets say you have a Wemo Switch you want to toggle in Voice Attack because screaming at your PC is super fun right?

Just use this handy code here.

Code: [Select]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Net;
using System.IO;

public class VAInline
{
void main()
{
if (VA.Command.Name().Contains("on"))
{
sendRequest("on");
}
else if (VA.Command.Name().Contains("off"))
{
sendRequest("off");
}
else
{
sendRequest("toggle");
}
}

void sendRequest(string action)
{
string IPADDR = "192.168.1.251";
string PORT = "49153";
string TARGETSTATUS = "0";
if (action == "on")
{
TARGETSTATUS = "1";
}

HttpWebRequest req = WebRequest.Create("http://" + IPADDR + ":" + PORT + "/upnp/control/basicevent1") as HttpWebRequest;
string reqContent = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
reqContent += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">";
reqContent += "<s:Body>";


if (action != "toggle")
{
reqContent += "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">";
reqContent += "<BinaryState>"+TARGETSTATUS+"</BinaryState>";
reqContent += "</u:SetBinaryState>";
req.Headers.Add("SOAPACTION:\"urn:Belkin:service:basicevent:1#SetBinaryState\"");
}
else
{
reqContent += "<u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">";
reqContent += "<BinaryState>1</BinaryState>";
reqContent += "</u:GetBinaryState>";
req.Headers.Add("SOAPACTION:\"urn:Belkin:service:basicevent:1#GetBinaryState\"");
}



reqContent += "</s:Body>";
reqContent += "</s:Envelope>";
UTF8Encoding encoding = new UTF8Encoding();

req.ContentType = "text/xml; charset=\"utf-8\"";

req.Method = "POST";

using (Stream requestStream = req.GetRequestStream())
{
requestStream.Write(encoding.GetBytes(reqContent), 0,
encoding.GetByteCount(reqContent));
}

try
{
HttpWebResponse response = req.GetResponse() as HttpWebResponse;
using (Stream rspStm = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(rspStm))
{
VA.WriteToLog("Response Description: " + response.StatusDescription);
VA.WriteToLog("Response Description: " + response.StatusDescription);
VA.WriteToLog("Response Status Code: " + response.StatusCode);
   
string body = reader.ReadToEnd();
VA.WriteToLog("responseBody: " + body);
if (action == "toggle")
{
if (body.Contains(">1<"))
{
sendRequest("off");
}
else
{
sendRequest("on");
}
}
}
}
VA.WriteToLog("Success: " + response.StatusCode.ToString());
}
catch (System.Net.WebException ex)
{
VA.WriteToLog("Exception message: " + ex.Message);
VA.WriteToLog("Response Status Code: " + ex.Status);
}
}
}

Code: [Select]
string IPADDR = "192.168.1.251

string PORT = "49153"


THIS is IMPORTANT. You need to set it to your Wemo Outlet IP address and Port. I figured mine out from using WireShark.

With this if you say "Light Toggle" It will send out a HTTP POST to the outlet and check if it's on or off and send the opposite command. But it ALSO supports an on/off command as well in the same function.

So you can use that fancy on;off;toggle;switch semicolon feature to set up a three in one command so that you two can look like a badass and go "Lights Off" when you want the room to be dark for showing your friends Netflix.

All you need to do is make your command and use Advanced to add an inline function. Paste the code in and make sure the IP and PORT are right and it should test run/compile just fine. Then you can avoid having to pull out your phone or wait for Alexa to talk to Amazon.

CREDIT to Modern Toil for showing me that direct control was possible. http://moderntoil.com/?p=839

CREDIT To commentator "Miljbee" for posting the C# code that was modified to make this work.
« Last Edit: April 03, 2019, 09:06:26 PM by Pfeil »

01G8R

  • Newbie
  • *
  • Posts: 2
Re: Check Status and Toggle a WeMo Smart Outlet
« Reply #1 on: December 03, 2022, 07:23:02 PM »
Has anyone gotten this working with other brands of smart plugs?  I'm trying with Teckin brand.