Controlling WeMo switch...

I'm very happy to share my code to control wemo switches with the esp8266.
If you guys can help me improve it... you welcome

CODE:


/*

  • 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">1</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">0</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");
}


Sites I used to help me

WeMo Hacking - http://mattenoble.com/2013/08/07/wemo-hacking/
Working HTTP POST example - working HTTP POST example - Home Automation - Arduino Forum
an-Piet Mens :: Sonos pause switch - Jan-Piet Mens :: Sonos pause switch

Hi

I have just ordered a couple of WEMO switches and am researching the Arduino code to control them.

You should use the F() function to reduce SRAM utilisation. Convert this:

client.println("POST /upnp/control/basicevent1 HTTP/1.1");

to this:

client.println(F("POST /upnp/control/basicevent1 HTTP/1.1"));

for all of your client.print() statements that are just Strings.

The wemo_on and wemo_off strings should also be split up and embedded in client.print() statements and then the F() function applied - something like this:

  client.print(F("<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>"));

  if (cmd == 1)
    client.print("1");
  else
    client.print("0");
  //

  client.print(F("</BinaryState></u:SetBinaryState></s:Body></s:Envelope>"));

Since the on/off strings are now not in SRAM and not stored as Strings you need to manually calculate the length of these strings and hard code it into:

client.print("Content-Length: 312"); //312 is just a guess you need to calculate the length of the wemo command strings

Cheers

Catweazle NZ