📜 ⬆️ ⬇️

Basis for a smart room, or how an Arduino lives in a dorm


Hello again, Habrozhiteli! Daily reading Habr, I repeatedly came across articles about Arduino and DIY projects based on it. It is worth noting that I didn’t have anything to do with electronics, and my whole experience was visiting a radio circle based on the teenage club Ogonek (Petrozavodsk). Once I got together and decided to make a bracelet on the Arduino with sensors (temperature, pressure, pulse) for a smartphone (BT connection). But, having made a mistake when ordering, I received only the Arduino itself and a pressure sensor. To test the board, I went to the nearest radio parts store for LEDs, and there I came across a magnetic door closing sensor. He prompted me to the idea that it would be possible to slightly improve the life in the room (not to be confused with the apartment). Without wasting time, I placed an order on Ali with a bunch of sensors and an Arduino Mega.

After ordering a gentleman's set of sensors (temperature, humidity, relay, StarterKit, BT, clock, range finder, IR sensor), I decided to look for how others represent the smart home. Most were limited to: the management of chandeliers and warm floors, although there were really cool articles . Some of the functions common to flats in the room were simply superfluous: for example, a gas leak or leak sensor. I decided to create the functionality needed in the smart room:


')
It turned out that all the necessary sensors have already been ordered and it remains only to wait. And all the sensors came!

First step


When you have already flashed LEDs and decided to build a smart home, it would be a logical step to flash the light in the room, but the early heat made you take control of the temperature. For starters, I just tried to connect dht11 from StarterKita. What could be easier if the Internet is full of instructions? As it turned out, there are a lot of libraries for connecting dht11 and not all of them are compatible with the latest IDE versions, but after a couple of hours I admired the temperature displayed in the console, and a couple of minutes later the LED blinking depending on it. (Turn off the sensor, put it in the fridge, get it out of the fridge, connect it ...)

The first problem was the low accuracy of the selected dht11: he lied by 2-3 degrees in both directions - I had to replace it (after that I try to order sensors in several versions).

Then I went to LeroyMerlin and bought the simplest fan there . After a few minutes of fussing with wires and plug the fan was assembled. Together with the neighbor, they removed the window and installed it (the fan, not the neighbor) on the vacant space. We stuck the plug into the socket and ... The fan began to rotate. Slow. So slow.
Needless to say, we incorrectly installed the plug, and the fan rotated from the wind?



Of course, manual shutdown current is available. But how best to control the fan through the relay? Connect directly to the fan? I decided to connect the fan to the relay through the socket: this gives flexibility to the system (you can replace the fan with something else).

Ventilation testing


And the code is rewritten from the LED to the relay. The firmware is loaded, I monitor the temperature in anticipation of the need to start - and here it is that moment! The relay clicks and the COM port disappears. Apparently, at the moment of switching, the load on the USB increases and there is a disturbance to the data. At first I thought that this was due to interference from 220V wires, but they are at a sufficient distance from the board. Later, this gave me a lot of problems: when I try to fill in a new sketch, the Arduino goes into reboot, switches the relay and thus brings up the COM port, and, therefore, the update process. I could not overcome this problem.

Remote control


Of course, the constant work of the fan soon became boring, and it was not cool to turn it off through the switch! So I got the starter kit and the remote control from there. Actually, the problems started from that moment: I had no idea how the receiver looked like. But a few minutes of googling helped me find it, and after another hour I had fun controlling the fan from the console.

At first it was a complex algorithm with several flags, but in the end I came to a small
timer algorithm
If (the signal came) {
manual_time = 1800;
fan mode =! fan mode;
}
If (time of manual_management! = 0) {
manual_time--;
} otherwise {
If (temperature <26) {
fan mode = false;
} otherwise {
fan mode = true;
}
}

But even there, it wasn’t a problem: updating the wires, I fed 5V to DATA and very quickly lost the receiver. This pushed me to the next step ...

Control via BT module


Soon I connected the BT module HC-06, redirected the output there and added the reading of commands. I thought it would be much more difficult, but thanks to the article with robocraft.

Happened



Neighbor and fan


I thought that having taught a neighbor to operate the system from a smartphone, I would solve the problem “Hey, I'm cold”, but ... If I could tolerate turning off the fan power supply through the switch, but closing the vent window simply made me mad. Why? The fan blew into the glass at a distance of several centimeters and buzzed terribly. Fortunately, I had magnetic sensors lying around, which prompted me to create a system. A closed sensor signals the Arduino that the fan cannot be turned on. Alas, sometimes there was interference, so I had to do the simplest test: if the signal to close comes several times in a row, the window is really closed.



Light control


Now it was possible to take control of the light. After the fan, there is nothing difficult: the duct, a pair of terminal blocks - and the light is already controlled through the Arduino (of course, with the ability to turn off through the old switch). The only thing that worries is that relays are not the best way to control light bulbs .

Sound problems


Well, what is the control of light without claps? I ordered a microphone. So I thought ... Remember about inexperience? In fact, I ordered a sound detector with a manual threshold setting. It seems to be suitable for my purpose? But a pair of “but” appeared:



Return of the console


A little googling, a trip to the store - and instead of a burnt VS1838B, I purchased a more reliable TSOP1836. This time the installation was successful and everything worked right away.



Exit to the street


I figured out the window, but what is behind it? Armed with a soldering iron, I soldered a rain sensor and another dht11 to a five-meter VGA cable. Some U-shaped fasteners, black tape, moss and the system goes on combat duty.



I also learned that you can display Russian text.

It became



Little convenience


It seems that the smartphone is always at hand, but until you find, while you connect ... Sometimes it is faster to turn off the light or the fan through the switch, but this makes it impossible to control via Arduino (impulse relays are too expensive), so I added two ordinary buttons.

Little security


Where without a fire alarm in the tech house? By inexperience, I purchased a fire sensor, not smoke. The definition of a burning match at a distance of 30 cm is, of course, cool, but not enough. Therefore, so far without it.

Speaking of fire alarm in the hostel ...

  • The speaker, from which sometimes a siren or a dead man’s rattle
  • Local smoke detector
  • Network smoke detector (connected to the guard)


General form




The placement on the computer case has a huge drawback: the ability to cause a short circuit. Therefore, soon the board will move to the personal shelf, and instead of the cardboard there will be a homemade case from the HDD case.

Sketch code


Current state
#include <IRremote.h>
#include <IRremoteInt.h>
#include <DS1302.h>
#include <DHT.h>
#define DHT_PIN 2 // pin thermometer ...
#define DHT2_PIN 52 // pin of a street thermometer ...
#define RAIN_PIN 53 // rain sensor pin
#define RECV_PIN 3 // pin to connect the IR receiver
#define FAN_PIN 4 // pin connect relay
#define LAMP_PIN 5 // pin connect relay
#define SOUND_PIN A0 // pin microphone connection
#define WINDOW_PIN A1 // Pin Window

#define LAMP_BUT_PIN 50 // pin microphone connection
#define FAN_BUT_PIN 51 // Pin Window


#define DHTTYPE DHT11

#define TIMER_FAN 0
#define TIMER_WINDOW 1


IRrecv irrecv (RECV_PIN);
decode_results results;
int timers [2];
bool FAN_FLAG = false;
bool LAMP_FLAG = true;
int WINDOW_VAL = 0;

bool WINDOW_OPEN = false;
bool WINDOW_FLAG = false;
bool RAIN_FLAG = false;

char incomingByte;

DHT dht (DHT_PIN, DHTTYPE);
DHT dht2 (DHT2_PIN, DHTTYPE);

DS1302 rtc (49, 46, 48);
void setup () {
Serial1.begin (9600);

rtc.halt (false);
rtc.writeProtect (false);
//rtc.setDOW (SUNDAY); // Set Day-Of-Week to FRIDAY
//rtc.setTime (0, 37, 40); // Set the time to 0:37:00 (24hr format)
//rtc.setDate (25, 5, 2014); // Set the date to May 25

timers [TIMER_FAN] = 0;
timers [TIMER_WINDOW] = 0;

pinMode (RAIN_PIN, INPUT);
pinMode (LAMP_BUT_PIN, INPUT);
pinMode (FAN_BUT_PIN, INPUT);

pinMode (FAN_PIN, OUTPUT);
pinMode (LAMP_PIN, OUTPUT);

digitalWrite (FAN_PIN, LOW);
irrecv.enableIRIn (); // Turn on the receiver

dht.begin ();
dht2.begin ();
}
int volume;
void loop () {
WINDOW_VAL = analogRead (WINDOW_PIN); // magnetic sensor
if (WINDOW_VAL> 950) {
if (WINDOW_FLAG) {
timers [TIMER_WINDOW] ++;
if (timers [TIMER_WINDOW]> 10) {
WINDOW_OPEN = false;
}
} else {
WINDOW_FLAG = true;
}
} else {
WINDOW_OPEN = true;
WINDOW_FLAG = false;
timers [TIMER_WINDOW] = 0;
}

if (digitalRead (RAIN_PIN) == HIGH) {
RAIN_FLAG = true;
} else {
RAIN_FLAG = false;
}

volume = analogRead (SOUND_PIN);
if (volume <40) {
LAMP_FLAG =! LAMP_FLAG;
}


if (digitalRead (LAMP_BUT_PIN) == HIGH) {
input_signal (1);
}
if (digitalRead (FAN_BUT_PIN) == HIGH) {
input_signal (0);
}

if (irrecv.decode (& results))
{
if (results.value == 16750695) // Button code
{
input_signal (0);
}
if (results.value == 16756815) // Button code
{
input_signal (1);
}
irrecv.resume (); // Get the next value
}
//Serial.println(rtc.getTime().hour);
//Serial.println(rtc.getTime().min);
Serial1.print (rtc.getDOWStr ());
Serial1.print ("");
Serial1.print (rtc.getDateStr ());
Serial1.print ("-");
Serial1.println (rtc.getTimeStr ());

if (Serial1.available ()> 0) {
incomingByte = Serial1.read ();
if (incomingByte == '0') {
Serial1.println (“Manual Deactivated”);
input_signal (0);
} else if (incomingByte == '1') {
Serial1.println (“Manual Mode Activated”);
input_signal (0);
} else if (incomingByte == '2') {
input_signal (1);
}
}

float h = dht.readHumidity ();
float t = dht.readTemperature ();

if (isnan (t) || isnan (h)) {
Serial1.println ("Error reading from the internal thermometer");
} else {
if (timers [TIMER_FAN] <= 0) {
if (t <26) {
FAN_FLAG = false;
} else if (t> 26) {
FAN_FLAG = true;
}
} else {
timers [TIMER_FAN] -;
}
Serial1.print (“Humidity:„);
Serial1.print (h);
Serial1.println (“% \ t”);
Serial1.print (“Temperature:„);
Serial1.print (t);
Serial1.println (“* C”);
}


if (timers [TIMER_FAN]> 0) {
Serial1.print (“Manual mode left to work:„);
Serial1.print (timers [TIMER_FAN]);
Serial1.println (“sec.”);
} else {
Serial1.println (“Manual Off”);
}
if (FAN_FLAG && WINDOW_OPEN) {
digitalWrite (FAN_PIN, HIGH);
Serial1.println (“Fan on”);
} else {
digitalWrite (FAN_PIN, LOW);
Serial1.println ("Fan off");
}

if (WINDOW_OPEN) {
Serial1.println ("Window open");
} else {
Serial1.println ("Window closed");
}


if (LAMP_FLAG) {
digitalWrite (LAMP_PIN, HIGH);
} else {
digitalWrite (LAMP_PIN, LOW);
}


h = dht2.readHumidity ();
t = dht2.readTemperature ();
Serial1.println ("Outside the window:");
if (isnan (t) || isnan (h)) {
Serial1.println ("Error reading from a street thermometer");
} else {
Serial1.print (“Humidity:„);
Serial1.print (h);
Serial1.println (“% \ t”);
Serial1.print (“Temperature:„);
Serial1.print (t);
Serial1.println (“* C”);
}
if (RAIN_FLAG) {
Serial1.println ("No rain");
} else {
Serial1.println ("Rain");
}

Serial1.println ("*****");
delay (100);
}

void input_signal (int signal) {
switch (signal) {
case 0:
if (rtc.getTime (). hour> 1 && rtc.getTime (). hour <8) {
timers [TIMER_FAN] = 1800;
} else {
timers [TIMER_FAN] = 18000;
}
FAN_FLAG =! FAN_FLAG;

Serial1.println ("Fan switched");
break;
case 1:
LAMP_FLAG =! LAMP_FLAG;
Serial1.println ("Light switched");
break;
}
}

And what if the code fails


I use a normally closed relay. That is, the default is all on: you can disconnect the Arduino from the network and use simple switches.

Development plans


What I have listed is only the basis for a really smart room. After all, all of its AI - is to enable and disable the fan, depending on the temperature. I plan the following to make the room more comfortable and “autonomous”.

Alarm clock

After adding the clock to the system, the next in line alarm clock. As a signal, I guess: a piezo-sniper, blinking light and turning on the fan. And of course, early awakening, if it is raining outside (-10 to the speed of movement).

Add a system for saving alarms to an SD card using such a module (in case of power failures).

Application on computer and smartphone + widgets

What kind of smart home without control from a smartphone or computer?

I guess something like this:



Why not Wi-Fi? Too expensive.

Derive your own Arduino

I think that comments are superfluous: the danger of USB burning out and constant interference with other devices. In addition, I want to transfer power from the board to a special adapter.

Control router through the outlet

Good old Asus RT56U likes to warm up, especially in summer. Therefore, I want to make an automatic shutdown for a couple of hours at night, to allow it to cool.

Add presence monitoring

Now I have some good old pyroelectric infrared sensors and a pair of ultrasonic distance sensors. This may be enough for the simplest control of presence.

Two people live in the room. Each has four states:



Infrared sensors detect presence in the room, and ultrasound presence in zones (bed / table) and monitor the number of people entering / leaving.

This will allow to implement the simplest scripts:



Add photosensitive resistor

Classics of the genre: automatic light switching on if there is someone in the “Behind the Computer” areas in the room and dark in the room.

Add an electromechanical lock and door closer

I don’t know about you, but I’m too lazy to look for keys all the time, so I really want to install an electromechanical lock together with a proximity card reader or a fingerprint sensor . The entrance to the hostel is carried out with the help of electronic passes - the frequency is known to me, and I can easily order the reader for the Arduino, but the cards are very easy to clone (I myself have a device for this), so when the time comes, I will most likely stop on the prints fingers.

The neighbor often goes to the kitchen / toilet / bath and does not close the door. I sit opposite the entrance and I do not like that everyone who passes by sees my underwear contemplates the disposition in the room. A managed closer would be the perfect solution for such situations.

Upgrade the window

As I already wrote, if the temperature is above a certain limit, the fan turns on. But what if there is nobody in the room, and the temperature is already below 20? I would like to add a window opening / closing to the system. Perhaps it will be simple motors + fishing line.

It is known that if you cool the light bulb and turn it on, it will most likely explode. Thus, it also saves nerves and light bulbs.

light bulb that did not live up to the temperature control system


Replace DHT11

As I wrote, I do not understand the sensors, etc. Therefore, I bought the first available temperature sensors: dht11. Now I want to replace them with dht22 for the room and dht21 for the street. The dht22 has a smaller error, and the dht21 has the support of negative temperatures and a protected case, which is important for the street. Street dht11 already sometimes began to get lost.

Add electric dimmer

I really want to smoothly adjust the level of lighting in the room. If I did not have a smart room, I would buy and hang a simple dimmer, but now I want a dimmer that can exist within the current system. To my regret, I could not find a dimmer for 220V, adjustable 5V. All that while I could come up with is a simple dimmer + a motor attached to it.

Voice control

I would like to add voice to the current management methods. Fortunately, there are systems like this . Of course, the voice response would also be the topic, but these are simple speakers.

Links to acquired sensors




Afterword


I think that it will be possible to write a new article after completing part of the plans, but for now I just dream of an electromechanical lock and prepare for the session.

Have a good upcoming weekend: spend it with good!

Source: https://habr.com/ru/post/224571/


All Articles