On this project I use an arduino uno with ethernet shield to control a Wemo Switch with a On-Off switch.
For this project you need:
- Arduino uno
- Arduino ethernet shield
- ON-OFF Switch
- 10k Ohm Resistor
Schematic
Arduino controls the wemo device over local network. When the status of the hardware switch changes, the arduino sends a SOAP call to the Wemo device to change the power status.
Here is the code for the Arduino:
(You need to change the wemoIP and wemoPort variables with ip/port of the wemo device)
This file contains hidden or 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; | |
| } | |
| } |