Skip to content

Instantly share code, notes, and snippets.

@stevemcquaid
Created November 28, 2016 01:13
Show Gist options
  • Select an option

  • Save stevemcquaid/b7a818656a736791ef2603f4ab498d8e to your computer and use it in GitHub Desktop.

Select an option

Save stevemcquaid/b7a818656a736791ef2603f4ab498d8e to your computer and use it in GitHub Desktop.
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Bounce2.h>
// #include <algorithm>
// #include <vector>
// #include <stdlib.h>
// #include <string>
// #include <Ticker.h>
// #include "SSD1306Wire.h"
// #include "OLEDDisplayUi.h"
const char* ssid = "ssid";
const char* password = "password";
const char* host = "IPADDRESS";
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>";
const char BUTTON_PIN = 0;
Bounce button = Bounce();
void toggle();
void turnOn();
void turnOff();
void giveTreat();
void wemo_control(int cmd);
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// Setup button
Serial.println();
Serial.print("Attaching to Button: ");
button.attach(BUTTON_PIN);
button.interval(5);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
if (button.update()) {
if (button.fell()) {
Serial.println("BUTTON PRESS");
// Turn on LED
giveTreat();
// Turn off LED / send success pattern
}
}
}
void giveTreat(){
turnOn();
delay(800);
turnOff();
}
void toggle(){
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);
}
void turnOn(){
Serial.println();
Serial.println("Turning WeMo On...");
wemo_control(1);
}
void turnOff(){
Serial.println();
Serial.println("Turning WeMo On...");
wemo_control(0);
}
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");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment