Like we did a small security system on RPi. Part 2

Hello Hawkers! In the previous article, I talked a bit about the iron part of the security system project and about the Python system that works with this iron. In this article, I continue the story about the server side.

image

The security system will be located outside the range of wi-fi, and it will only have mobile Internet at a low speed, so I decided that data exchange should be made as low as possible. In this case, the simplest solution is the use of digital codes. Those. we send to server 1 and he understands that the motion sensor has worked, 2 - the smoke sensor, etc., for each event there is a 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 the form of 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 present there, it stores the log and current settings, i.e. there are only two tables, here is one of them:



All requests are received using the usual POST, and are sent to JSON. It looks something like this:

print json_encode(getRequest($_POST));

To work, the server does not need so many methods:

  • Receive message from RPi
  • Give log
  • Set preferences
  • Give settings

Now, regarding the settings, I thought for a long time how to implement the management 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 notify me that she turned off, so there will be no unpleasant surprise.

Also, the server makes sure that RPi every minute reports that it is online and nothing happened to it. If this does not happen within 5 minutes, then the server starts to sound an alarm. This check is done using cron

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

Every 5 minutes he runs a simple check 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 great 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 makes it easy to send notifications, while it takes a minimum of time and effort to configure it.

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

Also popular now: