INTERACT FORUM

Please login or register.

Login with username, password and session length
Advanced search  
Pages: [1]   Go Down

Author Topic: Wemo Devices  (Read 3962 times)

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14463
  • I won! I won!
Wemo Devices
« on: June 19, 2016, 05:24:00 pm »

Belkin's Wemo Home Automation devices include:
- Power Switches
- Light Switches
- Light Bulbs
- Cams
- Controller (for external DC devices)

They connect via WiFi to the network and use a combination of the "cloud" to control them (including IFTTT), with Apps available for iOS and Android.   They have a developers SDK available here.




Logged
JRiver CEO Elect

jmone

  • Administrator
  • Citizen of the Universe
  • *****
  • Posts: 14463
  • I won! I won!
Re: Wemo Devices
« Reply #1 on: June 19, 2016, 05:35:22 pm »

I've been playing with their "Insight WeMo Power Point Switch" to control pool pumps on an Off Peak Power Circuit (they type that the power company control when it is energised - and I want to limit the run time of the pumps when the power comes on). 

I found that the WeMo devices can be controlled using std SOAP Calls to turn On/Off/Check Status etc and wrote a script to do this, here is an example of the calls (from my AutoHotKey script):

Code: [Select]
GetStatus:
  Wemo_Call = http://%WEMO_IP%:%WEMO_Port%/upnp/control/basicevent1
  WinHTTP := ComObjCreate("Microsoft.XMLHTTP")
  ComObjError(false)
  WinHTTP.Open("GET", Wemo_Call, False)
  WinHTTP.SetRequestHeader("Content-Type", "text/xml;charset=utf-8")
  WinHTTP.SetRequestHeader("SoapAction", """urn:Belkin:service:basicevent:1#GetBinaryState""")
  Body := "<?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>"
  WinHTTP.Send(Body)

  Result := WinHTTP.ResponseText
  Status := WinHTTP.Status
  RegExMatch(Result,"(?<=<BinaryState>)(.*)(?=</BinaryState>)", Wemo_Status)

  If Wemo_Status =
    Wemo_Status := "Not Found"
  If Wemo_Status = 0
    Wemo_Status := "Off"
  If Wemo_Status = 8
    Wemo_Status := "On"
Return

TurnOn:
  Wemo_Call = http://%WEMO_IP%:%WEMO_Port%/upnp/control/basicevent1
  WinHTTP := ComObjCreate("Microsoft.XMLHTTP")
  ComObjError(false)
  WinHTTP.Open("GET", Wemo_Call, False)
  WinHTTP.SetRequestHeader("Content-Type", "text/xml;charset=utf-8")
  WinHTTP.SetRequestHeader("SoapAction", """urn:Belkin:service:basicevent:1#SetBinaryState""")
  Body := "<?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>"
  WinHTTP.Send(Body)

  Result := WinHTTP.ResponseText
  Status := WinHTTP.Status
  Wemo_Status := "Turning On"
Return

TurnOff:
  Wemo_Call = http://%WEMO_IP%:%WEMO_Port%/upnp/control/basicevent1
  WinHTTP := ComObjCreate("Microsoft.XMLHTTP")
  ComObjError(false)
  WinHTTP.Open("GET", Wemo_Call, False)
  WinHTTP.SetRequestHeader("Content-Type", "text/xml;charset=utf-8")
  WinHTTP.SetRequestHeader("SoapAction", """urn:Belkin:service:basicevent:1#SetBinaryState""")
  Body := "<?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>"
  WinHTTP.Send(Body)

  Result := WinHTTP.ResponseText
  Status := WinHTTP.Status
  Wemo_Status := "Turning Off"
Return

You can also get a bunch of Info from the device if you happen to know it's IP address and Port, on http://IPAddress:Port/setup.xml  Unfortunalty Belkin have done much to prevent device discovery of what the IPAddy and Port is for their devices as it will not respond to UPnP discovery requests.  For now I've had to write a bit of code that while ugly works by looking for Belkin issued MAC Address in the ARP table then trying a the port range they use for the Wemo, and if I get a valid response store the IP Address, Port, and Friendly Name of the device from it's XML description.
Code: [Select]
; Description - Belkin WEMO Discovery
; Author - jmone
; Version - 160619
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;============= Find WEMO Details ===========================
  filedelete, arp.txt ; delete old file if present
  filedelete, wemo_details.txt ; delete old file if present

  RunWait, %comspec% /c arp -a > arp.txt,,Hide ; dump arp table to a file
  Loop, read, arp.txt
  {
    IfInString, A_LoopReadLine, 94-10-3e ; search arp file for Belkin MAC addresses
    {
      StringLeft, WEMO_IP, A_LoopReadLine, 18
      StringReplace , WEMO_IP, WEMO_IP, %A_Space%,,All ; extract IP Address

      WEMO_Port := 49152 ; loop through ports that the Wemo could be on
      Loop, 3
      {
        filedelete, wemoxml.txt ; delete old file if present
        WEMO_Port := WEMO_Port + 1
        URLDownloadToFile, http://%WEMO_IP%:%WEMO_Port%/setup.xml, wemoxml.txt ; dump Wemo XML to file if it exists
        Loop, read, wemoxml.txt
        {
           IfInString, A_LoopReadLine, friendlyName ; Search for Friendly name and if it exist write details out
     FileAppend, %A_LoopReadLine% <IP_Address>%WEMO_IP%:%WEMO_Port%</IP_Address>`n, wemo_details.txt
        }
      }
    }
  }
;=================== APP END ACTIONS ==========================
exitapp

What this means is that one the device is setup using the Belkin WeMo up the first time, you can control their devices without it or the "Cloud"

Thanks
Nathan
Logged
JRiver CEO Elect
Pages: [1]   Go Up