📜 ⬆️ ⬇️

Smart alarm clock based on motion sensor or IP camera

I think that many of you know about different phases of sleep and the existence of smart alarm clocks, there have already been many articles about it. The bottom line is that during the night there is a change of phases of fast and slow sleep. Depending on which phase you wake up in, the morning can be both kind and very gray. During a slow sleep, the person gradually falls asleep stronger and stronger, it is not worth waking him up at this moment, it will be hard to wake up. During the fast phase, the person does not sleep so well, can turn and roll over from side to side. It is at this moment the easiest to wake up.

On this principle, many smart alarm clocks are built. They come in the form of desktop gadgets or wristwatches, as well as applications for smartphones. The principle of their work is very simple. They wake up not at a strictly specified moment, but at a time when it is desirable to wake up, while waiting for some kind of movement. If the person began to move, then the REM sleep phase began, and it was time to wake him up.

In my room, one of the motion sensors is hanging right above the bed. I talked a little about this in my article about my smart home .
')
image

Recently, I looked through the logs and noticed that the phases of my sleep are clearly visible on the movement history from this sensor when I sleep alone. This is not surprising, it is very well captures any of my movement, even if I lie under a blanket. Immediately the thought arose - why not make my alarm clock “smart”?


Motion Sensor


Many asked me how my sensors were arranged, they asked me to give a diagram. In fact, everything is easier nowhere. I went to the nearest electrical installation store and bought an ordinary household motion sensor to turn on the light, it cost me about 200 rubles. He had three conclusions: general, power 220V and output. However, for my purposes, switching such a voltage was completely useless. I disassembled the sensor and found two circuit boards inside: brains and power. The first was powered from a voltage of about 9V and gave the output a logical unit in the presence of motion. The second provided these same 9 volts at the expense of capacitor power and switched the load with an electromagnetic relay. As a result, the board with capacitor power was replaced with my board with a microcontroller:



The task of the microcontroller is extremely simple: wait for a logical unit from the motion sensor and send information about it to the smart home network, but do it no more than once a minute. All this is now powered by 12 volts, which I have extended to all devices.

Alarm scripts


Since all the devices in my house are connected to my Linux-based router, and it is he who is engaged in awakening, all the remaining machinations are reduced to writing shell scripts.

Alarm.sh answers me for the alarm clock, which is started at the right time through cron , while the parameters are transmitted - exactly how to wake up. Now this script should be launched when that interval begins, during which I must wake up. Let it last 30 minutes. It turns out that if I need to wake up no later than 9:30, then the alarm should be set to 9:00.
The script turned out like this:
#!/bin/sh #       MAX_TIME=1800 #    FLAG_FILE=/tmp/motion.flag #     crontab -l | grep -v alarm.sh | crontab - #  -,      ,    if [ -f $FLAG_FILE ]; then rm $FLAG_FILE fi #      for i in `seq 1 $MAX_TIME`; do #   ,   ,     if [ -f $FLAG_FILE ]; then break fi sleep 1 done #  ,   if [ "$1" -eq 1 ] then radio.sh & fi #  ,       if [ "$2" -ge 1 ] then sleep 1 light.sh $2 & fi #  ,   if [ "$3" -eq 1 ] then /usr/local/sbin/wakemypc fi # TODO:    


In a script that responds to packets from smart home devices, I made a flag creation:
 # $3 -  , 21 -     if [ "$3" -eq 21 ] then touch /tmp/motion.flag fi 


If interested, this is how radio is turned on via radio.sh :
 #!/bin/sh #       - echo "04040C017A850976">/tmp/clunet.fifo sleep 10 for i in `seq 1 80`; do #    0.5 dB,      echo "04040C017A851AE5">/tmp/clunet.fifo sleep 6 done 


An alarm.sh is added to crontab by a PHP script, so the alarm clock can be set via the web interface:

image

Or through the application for Android:



In fact, such an alarm clock could be done without a router with Linux, because the motion sensor and remote control transmitter are connected directly, but for this you would have to somehow teach them to count the time, which they were not originally designed for.

Using an IP Camera


Theoretically, for an intelligent alarm clock, you can use an IP camera with infrared illumination, if you direct it to the bed.



Virtually any such camera has the ability to track movement. The admin of my camera looks like this:



Yes, clumsy English from the Chinese :) As you can see, you can specify the URL that is requested when motion is detected. Things are easy - to write a PHP script, which, when called, will create a flag.

Plus the camera is that theoretically it can see the movement under the blanket, because it isolates heat, to the movement of which the usual sensor reacts. However, practice shows that the overall sensitivity and reliability is much lower.

Total


These alarm clocks really help make the morning good. Feelings are very peculiar. For example, I can roll over in my sleep from side to side, and during this the alarm clock will turn on, as a result of which I wake rather above the bed and not on it :)
Alas, this approach to creating a smart alarm clock is only suitable for people who sleep in proud loneliness.

I must say that a separate alarm clock in the form of a radio, which gradually increases the volume, is also very pleasant. During a sound sleep, he does not wake up instantly, quiet music begins to break through the sleep and gradually increase. However, more often I still wake up from the sound of switching on the receiver.

While writing an article, went to the seventh hour of the morning. Good morning, Habr :)

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


All Articles