Monitoring My Home IP Phone Connection

A couple of years ago, I got fibre to my house and switched to IP telephony for my home number. The primary reason is cost – it is must cheaper that traditional copper. But unfortunately, it also less reliable. With irregular intervals, it simply stops working and we cannot call out and, which is more problematic, others cannot call us. So I thought about how to set up some kind of automatic monitoring, and finally found a solution.

I have a computer at home which is always on, and in my technology archive, I found a classic analogue modem. Problem one was how to connect them, since the modem of course has a serial RS232 port but the computer only has USB ports. That part I solved by buying an adapter/converter (I found this one (EAN 4040849954351) at my local dealer ( It had a 9-pin connector and my modem a 25 pin, but luckily my archive also contained an adapter for that.

When connected to the computer, it showed up as COM3. The next part of the solution was to write a script to send a command to the modem to dial my mobile phone and collect the result. If the IP telephony is down, the modem will not get a dial-tone and answer “NO DIALTONE”. The script, which I wrote in PowerShell, detects this and sends an e-mail in this case. Here it is:

param([string] $logfile)

$phonenumber = "a telephone number"
$comport = "COM3"
$smtphost = ""
$smtpport = 587
$smtpuser = "user@domain"
$smtppassword = "password"
$emailsubject = "Telefonövervakaren"

function InitLog($logfile)
    if ($logfile -eq $null -or $logfile -eq "") 
        $logfile = ($MyInvocation.ScriptName) + ".log"
    Write-Host "Logfile: $logfile"
    if ([System.IO.File]::Exists($logfile)) { Remove-Item $logfile }
    return $logfile

function SendCommand ($port, $cmd, $logfile)
    $cmd = "AT" + $cmd
    Write-Host "> $cmd"
    "> $cmd" >> $logfile
    $response = ""
    for ($i = 0; ($response -eq $null -or $response -eq "" -or $response -eq $cmd) -and ($i -lt 100); $i++)
        Write-Host "." -NoNewline
        Start-Sleep -Seconds 1
        $response = $port.ReadExisting().Trim("`r", "`n");
    Write-Host $response
    $response >> $logfile
    return $response

    $logfile = (InitLog $logfile)
    $port = new-Object System.IO.Ports.SerialPort $comport,9600,None,8,one
    $port.NewLine = "`r"
    $response = (SendCommand $port "DT$phonenumber" $logfile)
    Write-Host $_ -ForegroundColor Red
    $_ >> $logfile
$success = $response -eq "BUSY" -or $response -eq "NO ANSWER" -or $response -eq "NO CARRIER" -or $response -eq "VOICE"
Write-Host "Success: $success"
if (-not $success)
    try {
        Write-Host "Sending e-mail"
        $body = (Get-Content $logfile | Out-String)
        $smtpclient = New-Object Net.Mail.SmtpClient($smtphost, $smtpport) 
        $smtpclient.EnableSsl = $true 
        $smtpclient.Credentials = New-Object System.Net.NetworkCredential($smtpuser, $smtppassword); 
        $smtpclient.Send($smtpuser, $smtpuser, $emailsubject, $body)
        Write-Host $_ -ForegroundColor Red
        Write-Host $_.Exception -ForegroundColor Red
        $_ >> $logfile
        $_.Exception >> $logfile

I saved this script as PhoneMonitor.ps1 and scheduled it using Windows task scheduler. On the action tab, I entered:

  • Program/script: PowerShell.exe
  • Arguments: C:\Users\HTPC\Documents\PhoneMonitor.ps1

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.