14Chapter 6
Do more with a AVM FRITZ!Box

In this chapter we will send a mail on every call the Fritz!Box will receive with the name of the caller based on the phonebook or we show the number if it is unknown.

  1. We need to install the binding “Fritzbox TR064 Binding”. The previously installed “AVM FRITZ!Box Binding” is only to control the SmartHome devices connected to the FRITZ!Box but don’t tell anything about the FRITZ!Box itself. That’s what the “Fritzbox TR064 Binding” is for. The thing “FritzBox” we have right now just represents the bridge and has no functionality. Just a small remark for the binding “Fritzbox Binding”: This one is outdated and cannot be used on latest Fritzboxes anymore because it is not possible to enable Telnet on these models.

2. Now we need to be sure that the FritzBox allows the connections by applications. Login to your FritzBox and check if the following checkbox is enabled (“Allow access for applications”):

3. In the FritzBox we also need to change the authentication method and create a new user . First we will change the authentication method. In the FritzBox go to “System” => “FRITZ!Box-User” and switch to the second tab “Login at Network”. Change the Authentication Method to the first option (“Login with Username and Password”).

4. Now we will create a new user for OpenHab. Switch back to the first Tab “Users” and click the “New User” button at the right bottom. In the new dialog, enter a username and the password. For the E-Mail-Address field you can enter any dummy value that just need a @ and a dot in the string. At the options below you need the settings as shown below. When you are done, click on “OK”.

5. After the binding is installed and the FritzBox is configured, we change to the “Configuration” => “Bindings” page at OpenHab PaperUI and click the edit-pencil of the FritzboxTR064 Binding.

6. In the new dialog, we need to state the username and the password we configured before in the FritzBox. The URL can be customized if it is required, but the default value should work in a regular home network. Finalize the dialog with the save link at the right bottom.

7. After the binding is configured the configuration via PaperUI is done. This binding cannot manage all the items via PaperUI. This is done manually in config files. Do do so, the easiest way is to use the free tool WinSCP. You can download it here: https://winscp.net/eng/download.php

After you installed WinSCP, start it. You will get a Login Page. Select “New Site” at the top left and select “SCP” as file protocol. Enter the Raspberry Pi IP-Address and the username and password. After that click on the “Advanced-Button”

In the Advanced-Dialog select “SCP/Shell” from the tree and select the option “sudo su -” for the “Shell”-field. After that click “OK”.

Back in the main-login-dialog Click “save” to save the connection for future use. In the field “Site name” you can enter any string you like (i.e. “OpenHab”). When you did this click on the “Login”-button of the Login-Dialog.

8. You may be asked if you trust the remote system. Accept the message and continue. After that you will see a window with two main areas. At the left you see your documents-folder on your local system and on the right you see the home folder of the pi-user on the Raspberry Pi. The text with the arrow below shows the current folder on the remote system.

9. On the right, the Raspberry Pi, doubleclick on the first “..”-Folder twice. The remote folder path should now be “/”. Now doubleclick the folder “etc” and after that the folder “openhab2”. This folder is the root folder for all config file-directories. For the configuration of the FritzBox-Items, we need to change to the “items”-folder. In that folder right click a free area below the “readme.txt” and select “New” => “File…”. Name the file “fritzbox.items”.

10. The file will open. Paste the following text in that file:

//################################## 
//##### FritzBox TR064 Bindings #### 
//################################## 
// String fboxName "FBox Model [%s]" {fritzboxtr064="modelName"}
// String fboxManufacturer "FBox Manufacturer [%s]" {fritzboxtr064="manufacturerName"}
// String fboxSerial "FBox Serial [%s]" {fritzboxtr064="serialNumber"}
// String fboxVersion "FBox Version [%s]" {fritzboxtr064="softwareVersion"}
//## get wan ip if FritzBox establishes the internet connection (e. g. via DSL)
// String fboxWanIP "FBox WAN IP [%s]" {fritzboxtr064="wanip"}
//## get wan ip if FritzBox uses internet connection of external router
// String fboxWanIPExternal "FBox external WAN IP [%s]" {fritzboxtr064="externalWanip"}
// Switch fboxWifi24 "2,4GHz Wifi" {fritzboxtr064="wifi24Switch"}
// Switch fboxWifi50 "5,0GHz Wifi" {fritzboxtr064="wifi50Switch"}
// Switch fboxGuestWifi "Guest Wifi" {fritzboxtr064="wifiGuestSwitch"}
// Contact cFboxMacOnline "Presence (WiFi) [%s]" {fritzboxtr064="maconline:11-11-11-11-11-11" }

// # WAN statistics
// String fboxWanAccessType "FBox WAN access type [%s]" {fritzboxtr064="wanWANAccessType"}
// Number fboxWanLayer1UpstreamMaxBitRate "FBox WAN us max bit rate [%s]" {fritzboxtr064="wanLayer1UpstreamMaxBitRate"}
// Number fboxWanLayer1DownstreamMaxBitRate "FBox WAN ds max bit rate [%s]" {fritzboxtr064="wanLayer1DownstreamMaxBitRate"}
// String fboxWanPhysicalLinkStatus "FBox WAN physical link status [%s]" {fritzboxtr064="wanPhysicalLinkStatus"}
// Number fboxWanTotalBytesSent "WAN total bytes sent [%s]" {fritzboxtr064="wanTotalBytesSent"}
// Number fboxWanTotalBytesReceived "WAN total bytes received [%s]" {fritzboxtr064="wanTotalBytesReceived"}

// # DSL statistics
// Contact fboxDslEnable "FBox DSL Enable [%s]" {fritzboxtr064="dslEnable"}
// String fboxDslStatus "FBox DSL Status [%s]" {fritzboxtr064="dslStatus"}
// Number fboxDslUpstreamCurrRate "DSL Upstream Current [%s mbit/s]" {fritzboxtr064="dslUpstreamCurrRate"}
// Number fboxDslDownstreamCurrRate "DSL Downstream Current [%s mbit/s]" {fritzboxtr064="dslDownstreamCurrRate"}
// Number fboxDslUpstreamMaxRate "DSL Upstream Max [%s mbit/s]" {fritzboxtr064="dslUpstreamMaxRate"}
// Number fboxDslDownstreamMaxRate "DSL Downstream Max [%s mbit/s]" {fritzboxtr064="dslDownstreamMaxRate"}
// Number fboxDslUpstreamNoiseMargin "DSL Upstream Noise Margin [%s dB*10]" {fritzboxtr064="dslUpstreamNoiseMargin"}
// Number fboxDslDownstreamNoiseMargin "DSL Downstream Noise Margin [%s dB*10]" {fritzboxtr064="dslDownstreamNoiseMargin"}
// Number fboxDslUpstreamAttenuation "DSL Upstream Attenuation [%s dB*10]" {fritzboxtr064="dslUpstreamAttenuation"}
// Number fboxDslDownstreamAttenuation "DSL Downstream Attenuation [%s dB*10]" {fritzboxtr064="dslDownstreamAttenuation"}
// Number fboxDslFECErrors "DSL FEC Errors [%s]" {fritzboxtr064="dslFECErrors"}
// Number fboxDslHECErrors "DSL HEC Errors [%s]" {fritzboxtr064="dslHECErrors"}
// Number fboxDslCRCErrors "DSL CRC Errors [%s]" {fritzboxtr064="dslCRCErrors"}

// # only when using call monitor
// Switch fboxRinging "Phone ringing [%s]" {fritzboxtr064="callmonitor_ringing" }
// Switch fboxRinging_Out "Phone ringing [%s]" {fritzboxtr064="callmonitor_outgoing" }
// Call fboxIncomingCall "Incoming call: [%1$s to %2$s]" {fritzboxtr064="callmonitor_ringing" } 
// Call fboxOutgoingCall "Outgoing call: [%1$s to %2$s]" {fritzboxtr064="callmonitor_outgoing" }

// # resolve numbers to names based on phonebook
// Call fboxIncomingCallResolved "Incoming call: [%1$s to %2$s]" {fritzboxtr064="callmonitor_ringing:resolveName" }

// # Telephone answering machine (TAM) items
// # Number after tamSwitch is ID of configured TAM, start with 0
// Switch fboxTAM0Switch "Answering machine ID 0" {fritzboxtr064="tamSwitch:0"}
// Number fboxTAM0NewMsg "New Messages TAM 0 [%s]" {fritzboxtr064="tamNewMessages:0"}

// # Missed calls: specify the number of last days which should be searched for missed calls
// Number fboxMissedCalls "Missed Calls [%s]" {fritzboxtr064="missedCallsInDays:5"}

11. This text contains all supported channels of the FritzBoxTR064 binding. All of the have the prefix //. That means they are commented out (=disabled) right now but with this big blog we have a blueprint for all functionalities the binding has.

Our goal will be to get a mail when we receive a phone call. So we need the channels that will inform us about incomming calls and the resolving of the number by phonebook. That means we need three channels for this. Remove the // that are in front of the following lines in the area “only when using call monitor”:

Switch fboxRinging "Phone ringing [%s]" {fritzboxtr064="callmonitor_ringing" }
Call fboxIncomingCall "Incoming call: [%1$s to %2$s]" {fritzboxtr064="callmonitor_ringing" } 
Call fboxIncomingCallResolved "Incoming call: [%1$s to %2$s]" {fritzboxtr064="callmonitor_ringing:resolveName" } 

12. The first line is an indicator if the phone is ringing right now. It is a switch and can have the values “ON”, “OFF” and “NULL”.
The second line indicates an incoming call by number and the third one is the same but resolves the number to name.

To save the file, click the disk symbol at the top right. If you are asked for a password, enter the password of the “pi”-user.

13. Now you should see the items folder that contains the fritzbox.items and the readme.txt.

14. OpenHab now knows the items configured in the items file. This items are not visible in the PaperUI!

15. In the next step we need to teach OpenHab how to send mails. To do so, we change back to the PaperUI. To to the Add-ons page, be sure the “Actions”-Tab is selected and search for “Mail”. Click on install next to the “Mail Action” entry.

16. Now the Mail Action is installed. There is no configuration that can be done in the Paper UI. This is again done via config-file using WinSCP. Start WinSCP and connect to the session we created above (if you closed it). Change to the folder /etc/openhab2 and there into the directory “services”. You should see a file called “mail.cfg”.

17. Open the file with a doubleclick. You will see the configfile for E-Mail. Now you need to know the Mailserver-configuration of the Mailserver or -provider you like to use. I use GMX to send the mails but you can use any provider. For GMX the settings are like below. Pay attention that you have to remove the hash in front of the lines you change. For most other providers they should be the same except the hostname and your username, password and from-address. If you are using a local E-Mail-Client for your mails, you can lookup the data there or you can simply google for “SMTP <Providername>”.

18. Save the file by click at the disk-symbol. Now OpenHab knows our E-Mail-Configuration.

19. Finally we now have all prerequisites to create the first rule for our OpenHab that will send a Mail with the information about the caller. In the WinSCP session change to the /etc/openhab2/rules directory. Create a new file called “fritzbox.rules”.

20. Paste the following text into the file

rule "Phone is ringing"
when
   // fboxRinging is a switch item which switches to ON if call is detected
   Item fboxRinging changed from OFF to ON or 
   Item fboxRinging changed from NULL to ON
then
   // Log an info to the OpenHab log
   logInfo("Incomming call", "Generating caller name message...")

   // Create the call objects 
   val incCall = fboxIncomingCall.state as StringListType
   val incCallResolved = fboxIncomingCallResolved.state as StringListType

   // Create variables that contain the number/name of the caller
   val callerNumber = incCall.getValue(1)
   val callerName = incCallResolved.getValue(1)

   // Send mail
   if (callerName == "Name not found") {
      sendMail("mymail@myserver.de", "Call from " + callerNumber, "Incoming Call")
   }
   else {
      sendMail("mymail@myserver.de", "Call from " + callerName + "(" + callerNumber + ")", "Incoming Call") 
   }
end

Replace the two red parts with your personal email-address.

21. This script will send a mail to your mail address. If the calling number is known because it is in your phonebook, the name is also shown.

22. Call yourself and check if it works. You should get a new mail containing the configured information.

23. Troubleshooting: If everything works, you are done. Congratulations! If not go to the HabPanel and select our System-Dashboard where we can see our logs. In the log-area enter “call” in the search-field to see just our call logs. You should see something like this.

If this is the case, your FritzBox and the binding is configured properly. If not, there might be the problem. Search for “error” in the Searchbox. You may see the problem there.

If the FritzBox seems to be OK, maybe the mail was not send. You should find the problem for invalid mail configuration if you search for “mail” in the searchbox. In the screenshot below the mailserver is wrong.

If there is also nothing and everything seems to be OK, check the rules file. You can edit the rule file (i.e. add a space at the end) and save it. The log will show errors if there are some. Be patient! You have to scroll down to the end to see the most recent logs!