Skip to main content
 

Control Your Home Appliances Using Arduino and Relay

69,030

468

27

Introduction: Control Your Home Appliances Using Arduino and Relay

Control Your Home Appliances Using Arduino and Relay
Control Your Home Appliances Using Arduino and Relay

Hello everyone, In this instructable we will be using Arduino And Relay module to control home appliances.

This instructable covers:

  1. Basics of Relays.
  2. Connecting Relays with Arduino.
  3. Controlling AC appliances using Relays.

How it works:

  • The relay uses an electromagnet to mechanically switch electric appliances.
  • A relay can be operated by a relatively small electric current that can turn on or off a much larger electric current.
  • Using relays is safe as there is no any physical contact between Arduino and AC.

Step 1: Gather the Parts

Gather the Parts

In order to Control Your Home Appliances Using Arduino and Relay we'll need:

  1. An Arduino UNO
  2. A 5V Relay module
  3. Female jumper wires
  4. <html>
    <body>
       
        <form action="/rcv" method="get">
            <button name="subject" type="submit" value="fav_HTML">ON</button>
        </form>
        <form action="/rcv2" method="get">
            <button name="subject" type="submit" value="fav_CSS">OFF</button>
        </form>
       
    </body>
    </html>

Step 2: Wiring

Wiring
Wiring

Hookup all the components according to the circuit diagram shown above.

In the above wiring diagram, The pins VCC, GND and IN on the Relay module may vary depending on your Relay module. So, Refer to the data-sheet of your Relay.

For, connecting the AC bulb to the relay properly:

  • Connect live wire from AC wall outlet to Common port on relay and one terminal of AC bulb to Normally open terminal of Relay.
  • Then, Connect the other terminal of AC bulb to GND on the AC wall outlet.

Take proper precautions and care while connecting any wire to mains.

Step 3: Arduino Sketch

from flask import Flask, request, render_template
app = Flask(__name__)
result = b'null'
@app.route("/rcv", methods = ['GET','POST'])
def rcv():
    result = "1"
    return result
@app.route("/rcv2", methods = ['GET','POST'])
def rcv2():
    result = "0"
    return result
@app.route("/rcv3", methods = ['GET','POST'])
def rcv3():
     return result
@app.route("/")
def index():
    global result
    return render_template("index.html", data = result.decode("utf-8"))
if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)



Arduino Sketch

Once you have hooked up everything properly upload the Relay.ino sketch to your arduino.

Unlike LEDs, Relays closes the circuit whenever the signal or input pin is connected to GND. (This may vary for other custom made relay modules.)

So, "digitalWrite(13, LOW);" turns on the bulb. While, "digitalWrite(13, HIGH);" turns off the bulb.

LED On/Off (ESP8266 as client)

/**
BasicHTTPClient.ino
Created on: 24.05.2015
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
int ledPin = D4;
ESP8266WiFiMulti WiFiMulti;
void setup() {
Serial.begin(115200);
// Serial.setDebugOutput(true);
Serial.println();
Serial.println();
Serial.println();
pinMode(ledPin, OUTPUT);
for (uint8_t t = 4; t > 0; t--) {
Serial.printf("[SETUP] WAIT %d...\n", t);
Serial.flush();
delay(1000);
}
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("Yasin Arafat", "yasinarafat931300");
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client, "http://192.168.77.100:5000/client")) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode ==
HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
if(payload == "1")digitalWrite(ledPin, LOW);
else if(payload == "0") digitalWrite(ledPin, HIGH);
else if(payload == "3")digitalWrite(ledPin, LOW);
else if(payload == "2") digitalWrite(ledPin, HIGH);
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
delay(500);
}

FLASK SERVER

from flask import Flask, request, render_template
app = Flask(__name__)
result = "0"
@app.route("/LEDON", methods = ['GET','POST'])
def LEDON():
global result
result = "1"
return render_template("index.html")
@app.route("/LEDOFF", methods = ['GET','POST'])
def LEDOFF():
global result
result = "0"
return render_template("index.html")
@app.route("/LON", methods = ['GET','POST'])
def LEDON():
global result
result = "2"
return render_template("index.html")
@app.route("/LEDOFF", methods = ['GET','POST'])
def LEDOFF():
global result
result = "3"
return render_template("index.html")
@app.route("/client", methods = ['GET','POST'])
def client():
global result
return result
@app.route("/")
def index():
global result
return render_template("index.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)

index.html

<html>
<body>
<p>Button 1</p>
<a href="/LEDON"><button>Turn On </button></a>
<a href="/LEDOFF"><button>Turn Off </button></a>
<p>Button 2</p>
<a href="/LON"><button>Turn On </button></a>
<a href="/LOFF"><button>Turn Off </button></a>
</body>
</html>


Comments