Wall's Corners Wall's Corners Author
Title: Using Arduino with Parts and Sensors Infrared Remote Control (Last Part)
Author: Wall's Corners
Rating 5 of 5 Des:
we learned how to use the infrared receiver module and an infrared LED. This time, let’s put what we’ve learned into practice by creating a ...

we learned how to use the infrared receiver module and an infrared LED. This time, let’s put what we’ve learned into practice by creating a “smart controller” that will be used as a remote control for our home appliances. I think that everybody has several remote controls in your home. When I did this project, I gathered all the remote controls in my home.

Today’s Electronic Recipe

Expected time to complete: 90 minutes
Parts needed:

Smart Controller Specifications

Let’s first determine the specifications.

  1. Use the numerical value from both the light sensor and the temperature sensor to create automatic process.
  2. Being able to operate the smart controller from computer or smartphone to use it with home appliances.

We’ll try to implement these two functions.

1. Sending a Remote Control Signal Depending on the Light Sensor Numerical Value

I can control the light with a remote control in the room I’m working in. I usually notices that at dusk, the room becomes darker and I have to use the remote control to turn on the light. And this happens every day. Let’s see how we could use a light sensor and send automatically a signal to switch on the light when the room is plunged into a certain darkness.

Let’s register the signal to turn on the light. Similarly to what we did last time, we’ll save in Arduino the code we analyze from the remote control infrared with the receiver module.

P1190293

Picture 2 Light remote control and  Arduino

Next, we’ll write the sending program in the Arduino. Let’s make sure that we can turn on the light with Arduino. This time, we analyze the remote control “full light” signal to be sent.

When the remote control signal is ready, add the light sensor to the circuit. Then, create a program that will send the remote control signal when it’s becoming dark. We don’t want the light sensor to react when somebody just passes by it. To avoid that, let’s send the signal only when the state of darkness is continuous.  (Please refer to the following links for a detailed circuit description of the light sensor)

b2042315a9f69fc75de6bbfee9abb51c-e1432360796705
Figure 1 Light sensor and infrared LED transmitter circuit
Program: light will be turned on based on the light sensor value

Code-Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <IRremote.h>
 
IRsend irsend;
 
intpin = A1;
intcount = 0;  //count the continuous dark state
 
voidsetup()
{
  Serial.begin(9600);
}
 
voidloop() {
  ints = analogRead(pin);  //enter the value for the light sensor
  Serial.println(s);
  if(s > 660) {  //when meets the specified input conditions (when it becomes dark)
    count++;
    if(count > 5){
        Serial.println("Light ON");
        for(inti = 0; i < 3; i++) {
          irsend.sendNEC(0xa90, 12); // lighting ON of
          delay(40);
        }
        count = 0;
    }
  }
  else{
    count = 0;  //reset the count when it's getting bright
  }
  delay(5000);
}

Now, we’re done with our first specification: “sending a remote control signal depending on the light sensor condition” feature.

2. Sending a Remote Control Signal Through the Network

We can send a remote control signal depending on the light sensor. Now, let’s try to send  a remote control signal through smartphone or computer. Let’s turn Arduino into a server by using the Ethernet shield, that we used a few time already, and then let’s try to operate the remote control with the HTML displayed on the server.

P1180679
Picture 3 Arduino Ethernet Shield

Let’s begin by operating Arduino as a WEB server, which will allow us to create a turn on/turn off an LED with some HTML displayed on the server. If we can do that, we can replace a part of the LED remote control signal to write a program.

※ In this program, we use the “TextFinder” in the library for text analysis. (Please refer to following link how to add a library)

Code-Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <SPI.h>
#include <Ethernet.h>
#include "TextFinder.h"
 
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0F, 0x3F, 0x0D
};
IPAddress ip(192,168,0,20);
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
 
int lightPin = A0;
int ledPin1 = 3;
int ledPin2 = 2;
 
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
 
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
 
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
 
//**********************************************************
//* When there is a transfer of the data in the GET format
//**********************************************************
TextFinder finder(client);
if (finder.find("GET"))
{
while (finder.findUntil("mode", "\n\r"))
{
int val = finder.getValue();
Serial.println("val="+String(val));
//tv on
if (val == 1)
{
digitalWrite(ledPin1,HIGH);
delay(1000);
digitalWrite(ledPin1,LOW);
Serial.println(" ON");
}
//light on
else if (val == 2)
{
digitalWrite(ledPin2,HIGH);
delay(1000);
digitalWrite(ledPin2,LOW);
Serial.println(" ON");
}
}
}
//*******************************************
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
//client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
 
//*************************************************
//* bootstrap
//*************************************************
client.println("<head>");
client.println("<link rel='stylesheet' href='http://ift.tt/1FHelXo'>");
client.println("<link rel='stylesheet' href='http://ift.tt/1GSvD1Z'>");
client.println("<script src='http://ift.tt/19wK3Lg'></script>");
client.println("</head>");
//*************************************************
client.println("<body>");
 
client.println("<a class='btn btn-primary' href='/?mode=1'>TV ON</a> ");
client.println("<a class='btn btn-primary' href='/?mode=2'>Light ON</a><hr/>");
// output the value of each analog input pin
int sensorReading = analogRead(lightPin);
client.println("Light:");
client.println(sensorReading);
client.println("<br />");
client.println("</body>");
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}

P1190297
Picture 4 Test circuit for turn on the LED from the web server

I was able to turn on and off the LED from the WEB server. Then, instead of the LED, I was able to update to send a remote control signal. Then, instead of the LED, by updating the program and circuit, I was able to send a remote control signal. When using the Ethernet shield, you should use the pin 4 and pin 10-13 and keep the other pins to create the circuit. Finally, below is the program that will combine the light sensor circuit input. Please try to change the signal and the HTML output depending on your remote control.

Program: Smart controller circuit

Code-Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <SPI.h>
#include <Ethernet.h>
#include "TextFinder.h"
#include <IRremote.h>
 
IRsend irsend;
 
byte mac[] = {
  0x90, 0xA2, 0xDA, 0x0F, 0x3F, 0x0D
};  //Fill in the MAC address of the Ethernet shield you are using
IPAddress ip(192,168,0,20);  //IP address of the network band you are using
 
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
 
int lightPin  = A1;
int count  = 0;
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
  // start the Ethernet connection and the server:
  Ethernet.begin(mac, ip);
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
 
}
 
void loop() {
 
  int sensorValue = analogRead(lightPin);
//***************************************************************************************
//* ON the lighting when the case darker than the specified brightness was followed by
//***************************************************************************************
  if(sensorValue > 660){
    count++;
    if(count > 10){
     // Lighting on
     for (int i = 0; i < 3; i++) {
        irsend.sendNEC(0xC318F7, 32);
        delay(40);
      }
      Serial.println(" ON");
      count = 0;
    }
  }
  else{
    count = 0;
  }
  delay(1000);
 
  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
 
//*********************************************************************************************
//* When there is a transfer of the data in the GET format (operation from the browser side)
//*********************************************************************************************
                TextFinder finder(client);
        if (finder.find("GET"))
        {
            while (finder.findUntil("mode", "\n\r"))
            {
                int val = finder.getValue();
                Serial.println("val="+String(val));
                                //tv on
                if (val == 1)
                {
                                   // TV power ON signal
                                   for (int i = 0; i < 3; i++) {
                                     irsend.sendSony(0xa90, 12);
                                     delay(40);
                                   }
                   Serial.println(" ON");
                }
                                //light on
                else if (val == 2)
                {
                                   // Light on
                                   for (int i = 0; i < 3; i++) {
                                     irsend.sendNEC(0xC318F7, 32);
                                     delay(40);
                                   }
                   Serial.println(" ON");
                }
            }
        }
//*******************************************
 
    Serial.println("new client");
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.write(c);
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response
          //client.println("Refresh: 5");  // refresh the page automatically every 5 sec
          client.println();
          client.println("<!DOCTYPE HTML>");
          client.println("<html>");
 
 //*************************************************
//* bootstrap
 //*************************************************
          client.println("<head>");
          client.println("<link rel='stylesheet' href='http://ift.tt/1FHelXo'>");
          client.println("<link rel='stylesheet' href='http://ift.tt/1GSvD1Z'>");
          client.println("<script src='http://ift.tt/19wK3Lg'></script>");
          client.println("</head>");
 //*************************************************
          client.println("<body>");
 
          client.println("<a class='btn btn-primary' href='/?mode=1'>TV ON</a> ");
          client.println("<a class='btn btn-primary' href='/?mode=2'>Light ON</a><hr/>");
          // output the value of each analog input pin
          int sensorReading = analogRead(lightPin);
          client.println("Light:");
          client.println(sensorReading);
          client.println("<br />");
          client.println("</body>");
          client.println("</html>");
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

We’re able to operate from the WEB server!

Summary

We were able to successfully implement the second pattern in our smart controller and to use it. Now, our home appliances can be controlled automatically depending on the sensor value, and can also be controlled via smartphone or PC. This time, we have been using the light sensor. But we could have combined in with an ultrasonic wave sensor to turn on the light when somebody is there, or with a temperature sensor to turn on the electric fan when it’s becoming hot. These are some examples of features that can be implemented in the smart controller to make his usage more interesting.

Note: When operating smartphone or when doing automatic control, to prevent accident like fire, please experiment in the vicinity.

Share This:

View more at: http://yoursmart.mobi

About Author

Advertisement

Post a Comment

 
Top