An easy way to create a voice alert in the Home Automation system (“Smart Home”)

After testing the Linphonec SIP console client, I saw in it the possibility of a simple and quick way to implement voice alerts on a mobile phone for a home automation system.



I think this method is suitable for many DIY home automation systems, I will give my own version.



I have a MajorDoMo Smart Home automation system that is installed on a single-board Raspberry Pi computer (running the Raspbian OS (Debian 9).



We will create the possibility of notification from MajorDoMo to a smartphone without using additional equipment and our VoIP server.



A brief principle of the alert: in the event of a critical accident (everyone can determine their own accident list), the automation system manages the console VoIP client and calls the owner via SIP protocol, after the response it plays the previously recorded sound file with the accident message.



First of all, we need two accounts from the SIP provider with free inbound / outbound within the network. For example, take the Zadarma.com cloud telephony service.



The first step: we register with the provider, while accessing your personal account and your SIP account. We can register with several mailboxes, but we can replenish the account and get additional numbers. In any case, we should have 2 numbers (accounts) and a password to it.



On a smartphone (it works for me under Androd OS) we install a mobile application from the market, any SIP client will do. For convenience and minimizing the initial settings, we can use the proprietary application for Zadarma calls, but we can more functional.

After installation, we make registration, indicating your number and password to it, this completes the setup of the smartphone. So we used one number.



On the Raspberry home automation server, we need a Linphonec SIP terminal client. When installing from the Raspbian repository, a rather old version 3.6.1 is installed, which does not work correctly with the ALSA sound system, but since there is no need to use the voice directly, let's go along the easy way.



Installing the current version of the Linphone package (3.12.0) without a graphical interface for Debian 9, I described in a previous article: DIY call button. Raspberry Pi, MajorDoMo, Freeswitch and Linphonec . There you can find information on installing a USB sound card and sound settings for the Raspberry Pi.



In the terminal, execute the following command:



sudo apt-get install linphone-nogtk
      
      





Installing the console client utility linphonec and linphonecsh is a console program for managing linphonec running in the background in the / usr / bin directory.

Let's make the first run by typing:



 linphonec
      
      





We launch it, the SIP client swears at ALSA, we do not pay attention to it.



We execute the client registration command (Linphonec) on the zadarma server, specify your second number and password:



 register sip:xxxxxx@sip.zadarma.com sip.zadarma.com PASSWORD
      
      





If the input is made correctly, then we see the answer about the successful registration:



image



We exit the client (Ctrl + c, while the client saves the settings to the .linphonerc file of the current user, in my case, the directory / home / pi /. The next time SIP starts, the client is automatically registered on the provider's server.



We record a voice message (for example: “Attention of the system has been rebooted, the status is now in operation”:



 arecord -D plughw:1,0 -f cd /home/pi/alarm_restart.wav
      
      





To launch and control Linphonec in daemon mode, the Linphonecsh utility is used. Initially, on startup, initialization of the linphonecsh -init client is required. When this command is executed, the console client starts up without loading the configuration file.



So that at startup the configuration is read, run with the flag with:



  linphonecsh init -c /home/pi/.linphonerc
      
      





Now we can manage the console client from the terminal.



Linphonecsh Command List
 Usage: linphonecsh <action> [arguments] where action is one of init : spawn a linphonec daemon (first step to make other actions) followed by the arguments sent to linphonec generic : sends a generic command to the running linphonec daemon followed by the generic command surrounded by quotes, for example "call sip:joe@example.net" register : register; arguments are --host <host> --username <username> --password <password> unregister : unregister dial : dial <sip uri or number> status : can be 'status register', 'status autoanswer' or 'status hook' soundcard : can be 'soundcard capture', 'soundcard playback', 'soundcard ring', followed by an optional number representing the index of the soundcard, in which case the soundcard is set instead of just read. exit : make the linphonec daemon to exit.
      
      









Let's try the following commands:



 linphonecsh generic 'soundcard use files'
      
      





 linphonecsh generic 'play alarm.wav'
      
      





 linphonecsh dial sip:@sip.zadarma.com
      
      





Where generic are the teams of the Linphonec program itself. XXXXXX is our SIP number installed on the smartphone.



In this case, a call occurs, when answering - you can hear the playback of the previously recorded file with an accident.



Manually it worked. To integrate with the MajorDoMo-based home automation system, for example, take the emergency of restarting the server.

We write the following line in crontab (when loading the PC, the console client loads:



 crontab -e
      
      





 @reboot sudo -u pi linphonecsh init -c /home/pi/.linphonerc
      
      





Next, go to the web page of the DD system, go to the Control Panel - Scripts - Events - Startup. .







Scripts are implemented in the PHP programming language. There is already one line in the code that, when starting the system, pronounces a phrase with the server IP address.



Add a few more:



 exec ("sudo -u pi linphonecsh generic 'soundcard use files'");
      
      





 exec ("sudo -u pi linphonecsh generic 'play alarm.wav'");
      
      





 exec ("sudo -u pi linphonecsh dial sip:576935@sip.zadarma.com ");
      
      





We install to execute after saving , we save, at the same time there is a call on the mobile phone.



We reboot the MajorDoMo system, through the Service - Reboot / shutdown .

After a reboot, a voice notification arrives on the phone. The same algorithm of actions can be done for another accident critical for you.



Having slightly changed the sequence of actions, you can install your own IP telephony server (Asterisk, Freeswitch, etc.) in your home automation system, connect a VoIP gateway to it and directly access a mobile operator or PSTN. This option is more functional, has more features, but also requires a few large costs, both material and for setting up the system.



All Articles