📜 ⬆️ ⬇️

As we did a small security system on the RPi. Part 2

Hello! In the previous article I talked a little about the iron part of the project of the security system and about the system in Python, which works with this iron. In this article I continue the story about the server part.

image

The security system will be located outside of the wi-fi, and it will only have mobile Internet at low speed, so I decided that the data exchange should be done as less as possible. In this case, the simplest solution is to use digital codes. Those. we send to server 1 and he realizes that the motion sensor has triggered, 2 is a smoke sensor, etc., for each event its own code. In turn, the server decrypts the codes and sends what it considers necessary, in the form of a push-notification in the iOS application, already in text. In addition, each event is recorded both in the log on the RPi side and in the server log, this log can also be viewed in the application. By the way, all work with the server in Python is done using Requests .

The server is written in PHP, MySQL is also there, it contains a log and current settings, i.e. just two tables, here's one of them:
')


All requests are accepted using normal POST, and are sent to JSON. It looks like this:

print json_encode(getRequest($_POST)); 

To work, the server needs not so many methods:


Now, regarding the settings, I thought for a long time how to implement the control of the security system from the application, and came to the conclusion that the method is quite suitable for me when the settings are stored on the server, and the security system updates them every minute. Those. if I want to turn off the alarm, in the worst case, it will turn off after a minute. At the same time, she will inform me that she has turned off, so there will be no unpleasant surprise.

The server also ensures that RPi reports every minute that it is online and nothing has happened to it. If this does not happen within 5 minutes, the server starts to sound the alarm. This check is made with the help of cron

 */5 * * * * php /aliceserver/checkAlice.php 

Every 5 minutes, he runs a simple checkout script.

 include_once $dir . 'defines.php'; include_once $dir . 'db.php'; $db = new DataBase; $db->connect(); $query = "SELECT alice, UNIX_TIMESTAMP(online) online FROM " . SQLTableStatus; $result = $db->query($query); $alice_is_on = $result[0]['alice']; $online = (time() - $result[0]['online'] < 5*60); if ($alice_is_on and !$online) { include_once $dir . 'send_push.php'; $text = "Alice offline!"; $query = "INSERT INTO `" . SQLTableLog . "` (text, sender) VALUES ('$text', 1)"; $db->query($query); send_push_message($text, AppToken, 'alice_offline.caf', $dir); } $db->disconnect(); 

Sending push notifications is implemented using ApnsPHP . Those of you who have at least once encountered sending push notifications to Apple servers are probably familiar with this wonderful open-sourse solution. For those who do not know what it is, I can only say that at the moment it is one of the best wrappers that allows you to easily send notifications, while at the same time you need a minimum of time and effort to set it up.

This is where the story about the server part comes to an end, the next time I will tell you how the iOS application was made. Thank you all for your attention, and if possible I will answer all questions on hardware and software in the comments.

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


All Articles