-->
Page 1 of 2

Controlling WeMo switch...

PostPosted: Mon Aug 03, 2015 10:29 pm
by Douglas C. Santos
I'm very excited to finally be able to control my wemo switches with the esp8266. Below is the code I used for. It is just a simple example, need to add a button and filter the response from the switch to check the wemo status. ar
Hoping to hear some feedback and improvements from you guys.
Code: Select all--------------------------------------------------------------------
/*
 *  This sketch sends data via HTTP POST requests to a wemo device.
 *
 *
 */

#include <ESP8266WiFi.h>

const char* ssid     = "Your_Wifi";
const char* password = "Your_password";

const char* host = "192.168.0.179";
const int port = 49153;

int wemo_status = 0;

String wemo_on="<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>";
String wemo_off="<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>";

void setup() {
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (wemo_status == 0) {
    wemo_status = 1;
    Serial.println("Turning WeMo On...");
  }
  else {
    wemo_status = 0;
    Serial.println("Turning WeMo Off...");
  }
  Serial.println();
  wemo_control(wemo_status);
  delay(5000);
}

void wemo_control(int cmd) {
  Serial.print("Connecting to ");
  Serial.println(host);
 
  // Use WiFiClient class to create TCP connections
  WiFiClient client;

  if (!client.connect(host, port)) {
    Serial.println("Connection failed");
    return;
  }

  // This will send the request to the server
  client.println("POST /upnp/control/basicevent1 HTTP/1.1");
  client.println("Host: " + String(host) + ":" + String(port));
  client.println("User-Agent: ESP8266/1.0");
  client.println("Connection: close");
  client.println("Content-type: text/xml; charset=\"utf-8\"");
  client.print("Content-Length: ");
  if (cmd == 1) {
    client.println(wemo_on.length()); // both wemo_on and wemo_off are the same length, just in case it changes in the future
  }
  else {
     client.println(wemo_off.length());
  }
  client.println("SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"");
  client.println();
  if (cmd == 1) {
      client.println(wemo_on);
  }
  else {
      client.println(wemo_off);
  }
  delay(10);
 
  // Read all the lines of the reply from server and print them to Serial
  while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
 
  Serial.println();
  Serial.println("Closing connection");
}
--------------------------------------------------------------------

I used the following sites to help me reach the outcome.

WeMo Hacking - http://mattenoble.com/2013/08/07/wemo-hacking/
Working HTTP POST example - http://forum.arduino.cc/index.php?topic=155218.0
an-Piet Mens :: Sonos pause switch - http://jpmens.net/2010/03/16/sonos-pause-switch/

Re: Controlling WeMo switch...

PostPosted: Tue Aug 04, 2015 2:46 am
by tytower
Wemo is looking promising as home security devices so this should help the experimenters . Its all a bit pricey yet for me at $80 for a PIR and $130 for a camera plus you have to have a home network running too. Cant do that in my applications yet.

Re: Controlling WeMo switch...

PostPosted: Tue Feb 09, 2016 4:40 am
by raxpa
Hi guys this post is very interesting anyone has found a way to to check the status of the wemo...

Re: Controlling WeMo switch...

PostPosted: Wed Jul 15, 2020 2:35 am
by Ram84
how to find which wemo plug to turn on / off , incase of 2 plug.