Last active
December 1, 2022 23:34
-
-
Save meub/2fa85604ba6a0bf27e10fbcf8bfd79cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Belkin Wemo Plug Toggle, tested on an ESP-01S | |
* | |
This Arduino sketch checks the status of a Belkin Wemo Plug. | |
If the switch is off, it makes a second request to turn it on. If the | |
switch is on, it makes a second request to turn it off. It's highly | |
recommended you give the Wemo Switch and the device running this code | |
static IPs on your network. | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
// WiFi Information | |
const char* ssid = "NETWORK_NAME"; | |
const char* password = "NETWORK_PASSWORD"; | |
// Wemo Switch IP Address, you can get this from your router | |
// Or this project can help: https://github.com/iancmcc/ouimeaux | |
const char* ipAddress = "192.168.1.111"; | |
// Static IP Address/Network Info of your Arduino Device | |
IPAddress local_IP(192, 168, 1, 222); | |
IPAddress gateway(192, 168, 1, 1); | |
IPAddress subnet(255, 255, 0, 0); | |
IPAddress primaryDNS(8, 8, 8, 8); | |
IPAddress secondaryDNS(8, 8, 4, 4); | |
void setup() { | |
Serial.begin(115200); | |
// Connect to WiFi | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
// Explicitly set the ESP8266 to be a WiFi-client, not a server | |
WiFi.mode(WIFI_STA); | |
// Configure static IP address | |
if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) { | |
Serial.println("STA Failed to configure"); | |
} | |
// Start WiFi Connection | |
WiFi.begin(ssid, password); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println("WiFi connected with IP: " ); | |
Serial.println(WiFi.localIP()); | |
WiFiClient client; | |
HTTPClient http; | |
String postData = "<?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\"></u:GetBinaryState></s:Body></s:Envelope>"; | |
// Start First HTTP POST to retrieve switch status (0 for off, 1 for on) | |
Serial.print("[HTTP] begin...\n"); | |
http.begin(client, (String)"http://" + ipAddress + ":49153/upnp/control/basicevent1"); | |
http.addHeader("Content-Length", (String)postData.length()); | |
http.addHeader("Content-Type", "text/xml; charset=\"utf-8\""); | |
http.addHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#GetBinaryState\""); | |
Serial.print("[HTTP] POST 1...\n"); | |
int httpCode = http.POST(postData); | |
// If HTTP code is not negative, the POST succeeded | |
if (httpCode > 0) { | |
Serial.printf("[HTTP] POST 1... response code: %d\n", httpCode); | |
if (httpCode == HTTP_CODE_OK) { | |
const String& payload = http.getString(); | |
Serial.println("received payload:\n<<"); | |
Serial.println(payload); | |
// Turn switch on by default | |
String action = "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>1</BinaryState></u:SetBinaryState>"; | |
// If switch already on, turn off instead | |
if( payload.indexOf("<BinaryState>1</BinaryState>") > 0) { | |
action = "<u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"><BinaryState>0</BinaryState></u:SetBinaryState>"; | |
} | |
// Build HTTP Post Data for turning switch on (or off) | |
postData.replace( "<u:GetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\"></u:GetBinaryState>", action); | |
// Start second HTTP Post to turn switch on (or off) | |
Serial.print("[HTTP] begin...\n"); | |
http.begin(client, (String)"http://" + ipAddress + ":49153/upnp/control/basicevent1"); | |
http.addHeader("Content-Length", (String)postData.length()); | |
http.addHeader("Content-Type", "text/xml; charset=\"utf-8\""); | |
http.addHeader("SOAPACTION", "\"urn:Belkin:service:basicevent:1#SetBinaryState\""); | |
Serial.print("[HTTP] POST 2...\n"); | |
// start connection and send HTTP header and body | |
int httpCode = http.POST(postData); | |
if (httpCode > 0) { | |
Serial.printf("[HTTP] POST 2... response code: %d\n", httpCode); | |
if (httpCode == HTTP_CODE_OK) { | |
const String& payload = http.getString(); | |
Serial.println("Received payload:\n<<"); | |
Serial.println(payload); | |
Serial.println(">>"); | |
} | |
} | |
else { | |
Serial.printf("[HTTP] POST 2... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
} | |
} | |
else { | |
Serial.printf("[HTTP] POST 1... failed, error: %s\n", http.errorToString(httpCode).c_str()); | |
} | |
// Close the connection | |
http.end(); | |
client.stop(); | |
delay(3000); | |
// Go to sleep to conserve battery | |
ESP.deepSleep(0); | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment