Friday, January 22, 2021

Control Domoticz with your own APP

For an index to all my stories click this text

 
As you might know by now I am using the Domoticz Software for my home automation. For switching the lights in my home I am using 433Mhz lightswitches from KlikAanKlikUit and mostly unknown brands from the dollar shop. These are controlled by their own Remote Controls AND by Domoticz.


Domoticz is great. It runs on a Raspberry and can be controlled from a Dashboard that is accessible from the Raspberry itself and/or remote from a PC. Next to that you can still use the original Remote controls that are supplied with the wireless light switches. There also is an App for Android and IOS so you even check the status of several appliances and regulate your lights from your work or holliday. Check it out: https://www.domoticz.com/

And herein lies the (small) problem is was facing.
I have several lamps that are controlled by remote control in my living room. However most remote controls are only capable to switch 4 appliences. And I have more. This a problem for my girlfriend who has to use multiple remote controls just to switch lamps on or off. As she is computer illiterate I wanted a simple way to control these lamps. So why not use the Domoticz app. Well I do not want her to accidentally fiddle with the settings. So that is out of the question.

MIT's App Inventor to the rescue !!

So why not make my own APP for Domoticz. It's not that complicated. I used the unsurpassed Mit's App Inventor to build this APP for Android. You can find APP-Inventor here: http://appinventor.mit.edu/explore/



Above is my layout. I am, so this app also is, Dutch. However you can easily exchange the names of the buttons to your own liking.

I am not going to give you a full tutorial on App-Inventor here as there are many tutorials to be found on the internet and there is even a complete course App-Devellopment at EDX : https://www.edx.org/course?search_query=app+inventor

So I'll give you the outline here on how I build my App.


In the Designer window at the top put a label. I gave it the name woonkamer which means living room. However give it any name you like.

Next put a table arrangement on the screen. In this we are going to put our buttons with some spacing between them. So I made it 3 columns wide and 16 rows high. You can expand it as much as you like but then make sure to make the screen scrollable.

In each left column I put an ON (green colored) button and on the right side an OFF (red colored) button. In the middle of the first column I put a label in which I only put a space. This makes sure that there is some horizontal space between the buttons.
Below each row I put a label. This makes sure there is some space vertically between the buttons.

My screen background is pinkish you can alter that to your own liking in the Screen properties.

On the bottom of the screen there is one hidden component called Web1. You can find that in the connectivity tab on the left side of App-Inventors designer screen. This component makes sure we can communicatie over the internet.

The last 2 ON and OFF buttons are called "Niet in gebruik" and are for future expansion. I'll be using them for the lights of my Christmass Tree.

That's about it for the designer screen.

Now let's have a look at the program.

Blocks

We have 14 buttons in total. Seven ON buttons and 7 OFF buttons. Basically they are the same and the only difference is the ID-number that Domoticz has given the Switch and wether it is ON or OFF.


Now this how the blocks for BUTTON 1 and BUTTON 2 look. The first one sets the light "Vensterbank" ON and the second puts it off.

The set WEB1 command is not completely shown in the above screendump. The actual text is:

http://XXX.XXX.XXX.XXX:8081/json.htm?username=YYYYY=&password=ZZZZZ=&type=command&param=switchlight&idx=1446&switchcmd=On

As you can see we have to fill in several parameters. The command needs to know the IP adress where to send the command to. The username and password of your Domoticz system.

IP Adress.

There are two different IP adresses that you can fill in here. The first is your IP adress from within your own network. Your local IP adress. You'll use that one if you are not going to use this APP from outside your home.
If you are going to use the APP from within your own network you can leave phrases for the username and password out.

You can find your local IP adress within your router.



I have a Zyxell router and when I open it's webpage I can see all my devices. At the top are the Wifi devices. At the bottom the wired devices and there you can see Domotcz. Clicking on it will reveal it's IP adress.

If you are using the APP from outside your house you will need to fill in the external IP adress.

You can find your IP adress on this website: http://www.whatsmyip.org/

It will display the following information:



Next to that you will have to open a port in your router which allows the APP to communicate over the internet to Domoticz.

Dig into the setting pages from your router.
Look at something called port-forwarding.

There you can open a new port for the outside world.

In my router I have to give it a name. Next I give it the IP adress of Domoticz and Domoticz port number which is 8080.
Next I instruct it to send all communication from router port no 8081 to the Domoticz port.



The router will most likely ask for a starting port number and an ending port number for your local and global ports. In both cases use the same. So start local 8080, end local 8080, start global 8081 and end global 8081. You can use many numbers as a port number but there are some restrictions. So delve into that by checkin information on the internet about this. This method allows you to open many specific ports for all kinds of projects. We've done that before on this weblog to open up communication to an ESP from anywhere in the world: https://lucstechblog.blogspot.com/2017/05/google-home-and-esp8266.html


Next step is to fill in your username and password.
First you need to set them in the Domoticz setting page. My Domoticz system is in Dutch but look under settings and where I pointed the arrows to.

When you have set a username and password we need to fill these in in the App's blocks.
You can not just fill in the name and password you just typed into Domoticz. Domoticz has a safety feature and that is that it uses base 64 Encode.

So you'll have to encode your username and password with base 64.
You can do that using this link: https://codebeautify.org/base64-encode
Check this site. it has many usefull features and conversions.

Almost there.

The last thing we need to know is the ID of the switch we are going to control.




In Domoticz go to the settings page and look for Apparatus. Now choose only the used apparatus. And there we have all our switches with their IDX.

All data available

Now we have all data available we can fill it in into the WEB 1 Command:

http://XXX.XXX.XXX.XXX:8081/json.htm?username=YYYYY=&password=ZZZZZ=&type=command&param=switchlight&idx=1446&switchcmd=On

The above command is for Button 1 (ON) (Vensterbank with ID 1446) and the next is almost the same for Button 2 (OFF)

http://XXX.XXX.XXX.XXX:8081/json.htm?username=YYYYY=&password=ZZZZZ=&type=command&param=switchlight&idx=1446&switchcmd=Off

Repeat this for all your buttons and replace the ID numbers with the ones you find in your Domoticz settings.

Done !!
Your app is finished. Test it with App Inventors AI companion and make sure all functions work as they should.

Now transfer you APP to your Phone/Tablet and start controlling your home from anywhere in the world with an easy app.

Till next time
Have fun

Luc Volders