Last active
October 8, 2015 23:54
-
-
Save gTrigonakis/480c1693ffc7a3f31ed3 to your computer and use it in GitHub Desktop.
Control WeMo switch with Arduino+Ethernet shield
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
#include <Ethernet.h> | |
#include <SPI.h> | |
byte mac[] = { 0x90,0xA2,0xDA,0x00,0x55,0x8D}; | |
EthernetClient client; | |
int inPin = 8; | |
int state = HIGH; | |
int reading; | |
char wemoIP[ ] = "192.168.2.126"; | |
int wemoPort = 49153; | |
void switchON(){ | |
Serial.println("switchON"); | |
String data1; | |
data1+="<?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>"; // Use HTML encoding for comma's | |
if (client.connect(wemoIP,wemoPort)) { | |
client.println("POST /upnp/control/basicevent1 HTTP/1.1"); | |
client.println("Content-Type: text/xml; charset=utf-8"); | |
client.println("SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\""); | |
client.println("Connection: keep-alive"); | |
client.print("Content-Length: "); | |
client.println(data1.length()); | |
client.println(); | |
client.print(data1); | |
client.println(); | |
} | |
if (client.connected()) { | |
client.stop(); | |
} | |
} | |
void switchOFF(){ | |
Serial.println("switchOFF"); | |
String data1; | |
data1+="<?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>"; // Use HTML encoding for comma's | |
if (client.connect(wemoIP,wemoPort)) { | |
client.println("POST /upnp/control/basicevent1 HTTP/1.1"); | |
client.println("Content-Type: text/xml; charset=utf-8"); | |
client.println("SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\""); | |
client.println("Connection: keep-alive"); | |
client.print("Content-Length: "); | |
client.println(data1.length()); | |
client.println(); | |
client.print(data1); | |
client.println(); | |
} | |
if (client.connected()) { | |
client.stop(); | |
} | |
} | |
void setup() | |
{ | |
pinMode(inPin, INPUT); | |
Serial.begin(9600); | |
Ethernet.begin(mac); | |
delay(1000); | |
Serial.println("connecting..."); | |
// print your local IP address: | |
Serial.print("My IP address: "); | |
for (byte thisByte = 0; thisByte < 4; thisByte++) { | |
// print the value of each byte of the IP address: | |
Serial.print(Ethernet.localIP()[thisByte], DEC); | |
Serial.print("."); | |
} | |
Serial.println(); | |
} | |
void loop(){ | |
reading = digitalRead(inPin); | |
if (reading != state){ | |
if (reading==HIGH){ | |
switchON(); | |
delay(1000); | |
}else{ | |
switchOFF(); | |
delay(1000); | |
} | |
state = reading; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment