Geolocation and identification of people in a smart home
Today I was setting up geolocation on my phone, I use it to track the location of people in my smart home, that is, they are in my house or have gone out somewhere.
There are several different ways to assess whether someone is at home or not, you can use Wi-Fi. For example, if a person's phone is connected to Wi-Fi, then in principle it is clear that he is at home. Of course, there are cases when Wi-Fi is not turned on on the phone or forgotten, or on purpose, and then the system will of course not work properly.
It is very important to assess how many people are at home, who exactly is at home, and even more importantly, in order to understand who is at home or not at all, if there is no one at all, then it is advisable, of course, to put the entire smart home in the absence of people mode.
There are also tracking options, for example via Bluetooth or in some other way, for example, it is often suggested to use NFC to determine who is at home, but first of all it is very difficult and also let's say Bluetooth and NFC may not be enabled on the phone, respectively, it will also not work.
Therefore, of course, I tried different options, for example, if the user is at home and connects to Wi-Fi, then there is an option, for example, a smart home system such as Home Assistant can show through its application installed on the phone that the user has appeared online, and various useful parameters are transmitted from the phone such as battery charge, the phone is connected to charging and some more parameters.
For example, the OpenHAB smart home system, which I also use, can also transfer data from the phone, but of course it can detect geolocation, but this must also be enabled in the settings, but the main problem with all applications is that they need to be allowed to work in the background on the phone so that they can request geolocation constantly and accordingly, it will then be able to be sent to servers and to the Home Assistant, for example, there is a problem that it does not work for me through the cloud, and accordingly, of course, it will not be able to send.
I looked at several different applications for tracking people at home, I found a good solution called Owntracker, but in principle it works even well, its settings are also not very complicated, it can transmit this application through an mqtt request or over the network to call some server, some specific address. and send your parameters there.
I do not use the MQTT format because then it is necessary that the MQTT broker, which is installed on the smart home server, can also access the Internet and be accessible via the Internet, so either you can deploy it on some external resource, there are servers where this service is provided, raise your MQTT broker and transfer parameters to the smart home system through it. at home, many of these servers have restrictions, they become paid, or there are restrictions on the amount of information transmitted, or they are simply unavailable.
I use the transmission option via an HTTP request, in principle, the settings in the application itself are simple, the web server address is indicated, I have a website address, there is a special page on the website that can process this request, the json request format is sent, then this page is saved in a special database, in a special table in the database, all received messages are sent in the settings in such a way that it is performed once every 60 minutes, or if, say, the phone is moving, then the application has such a mode for detecting movement, and then it starts sending more often, I have it set up once every 10 seconds, for example, if you drive a car, then of course 60 seconds are long intervals and not very convenient.
This program also transmits some other parameters, but the most valuable thing is probably the charge on the phone, because you can track how much battery is left and make a timely script notifying you that you need to recharge, and the application also has the ability to specify the accuracy of determining coordinates to respond, it can determine through GSM stations. then the accuracy is quite low either through GPS, but then it is necessary that the signal is strong enough or the phone is not located somewhere inside the apartment or, for example, indoors, but still, closer to the window, otherwise, of course, the phone will not be able to pick up, and the program makes a setting that sending will be performed only if the accuracy is higher than a certain one, for example, if the accuracy is 500 meters, usually GSM stations, then in this case do not send values, if the accuracy is higher, then the value will be sent to the server. a smart home.
In the application, you can specify certain regions, these are locations, within a certain radius, if there is a phone there, then you can specify the name accordingly, then the number and name of this location will also be transmitted through the parameters. I don't use this feature because I can already determine it myself in the smart home system, and then I won't need to configure it on each phone separately.
I set it up on a new phone recently and there, already in Android version 12, it does not allow you to automatically launch applications and give geolocation access to this application, so far I have tried, in principle everything works, but apparently you will need to launch the application manually, log in to it, and then, until the next reboot of the phone, in principle everything will work correctly, but a few days of experimentation so far show that everything is working fine.
After the information was recorded in the database in json format, I have a script in my smart home system that periodically polls this database and checks if the values have been changed, but there are several ways to poll the values, you can ask after a few seconds, for example, but then if it is an external hosting, it quickly determines that there are a lot of requests from one server, from one IP address, and accordingly it can slow down work, or even completely block it altogether, I had this several times when the smart home system lost this site, this hosting, and I couldn't access the address in order to send or receive a value, this is especially noticeable when several devices, for example, connected via Wi-Fi, do this, then for an external server it looks like a lot of devices from the same IP address, and therefore this site, of course, begins to block such actions and in order to circumvent all the same we need to severely limit the number of requests, so either I used to request once a minute, which is normal enough if it's done for coordinates, but if, for example, I'm transmitting some kind of command, then of course it's very inconvenient and very long.
For example, this works for me through the Yandex column. When I give a command through it, through the Yandex smart home system, it is sent to this server, the value is written to the database, and of course I have to read from this database faster than once a minute, there is such an exchange format called long polling, such technology allowing the server to respond not immediately, but with some delay, in principle, my site supports this mode, it is used in a very interesting way, that is, the smart home server makes a request to the server, to the site, there, the site does not respond until the value arrives, the smart home server is waiting for this message at this moment, I have about 60 seconds of timeout, usually this session is interrupted and restarted again, so I always have this script in standby mode and when a geolocation message arrives from the phone, this value instantly enters the system After that, I can already run different scenarios, for example, the same technology is used in the Telegram bot, and I did the same thing at first, so it was requested from me once a second, in principle, Telegram itself does not limit the number of requests and you can make them often enough, but there is a function that also supports long polling and you can also specify the waiting parameter, I have about 90 seconds specified, I wait for the telegram message and if they did not arrive, then I ask again and this technology greatly unloads the system itself. the system does not need to constantly make requests and works very quickly, that is, even waiting for one second is not required because messages arrive instantly.
Automation in a smart home should be useful and create additional comfort, but sometimes everything goes wrong… The reasons for this, and how to configure everything, see on my channel.