Friday, February 7, 2020

ESP8266 sending data to Domoticz

For a complete index to all my stories click this text !!

If you have not heard about Domoticz by now it is time you should.




Domoticz is a complete home automation system that is open source and free for you to download. It runs on Apple, Windows, Linux and last but not least Raspberry. You can find the web-page here: http://www.domoticz.com/

My system is running on a Raspberry 2B with some extra hardware called RF-Link. My RF-Link clone an Arduino Mega with a 433Mhz Transmitter and Receiver. This makes it possible for Domoticz to communicate with Klik-Aan-Klik-Uit and other cheap 433 Mhz switches, doorbells, blinds, weatherstations etc.
Domoticz itself communicates with systems like Philips Hue, Sonos, Anslut (Ikea lights), Mi-Light etc etc etc etc. You can find a complete list of brands and devices on the Domoticz Wiki Page:
https://www.domoticz.com/wiki/Hardware

The Domoticz software is controlled from a webpage that can be accessed by your PC and there are Android App's available to control it from your tablet or phone.

You can even make rules like switch the TV on, close the curtains, switch the home-cinema on and dim the lights. So with one button your set for a move evening.

Summarising: with Domoticz you have a complete system to automate your home which will set you back around 60 euro.

I bought some cheap 433 Mhz switches and automated the lights in my hobby room, living room and with christmass I switch on the christmass-tree and all related lamps from anywhere with the App on my phone.

This is all great but I wanted more.

In all the years I am tinkering I build several projects with led-strips, sensors like a rain-sensor, door and window sensors, a thermometer etc. I want them all to be controlled from Domoticz.

Next to that I want to build some special switches. For example a switch with an LDR that tests the light in a room and automatically puts on the lamps when needed. Or a switch that puts a lamp on when someone enters a room. You can do all this with individual switches. But it is neat to have one central place where it all comes together.

As an add-on for Domoticz someone started a project called ESP-Easy ( https://www.letscontrolit.com/ ) that makes it easy to attach sensors to an ESP8266 and connect that to Domoticz. A really neat project so check it out.

However I want to do things myself and presumably you to otherwise you would stop reading from here.

So what I am going to show you here is how a button pressed on the ESP8266 sends a command to Domoticz which then sends a command to a 433Mhz light switch. Great for making remote controls for Domoticz. And next I am going to make a temperature sensor that sends it's data to Domoticz. 


Intermezzo

I am moving away from ESP-Basic. It is no longer maintained nor updated. And there is no ESP32 version available. So I am building most of my new projects in the Arduino IDE (C++). I must admit that writing and develloping programs in ESP-Basic is faster as writing and debugging in C++. There are however a lot of nice sensors and add-ons available now which are not supported in ESP-Basic. So this story will be one of the last ones in which the program is in ESP-Basic.

ESP on screen button as remote for Domoticz

First thing we are going to build is an ESP8266 that creates a webpage on which a button can be clicked that sends information to Domoticz. In my setup I have a 433 Mhz lightswitch that controls a lamp in my hobbyroom which I am going to control this way.









Start with looking in Domoticz at the devices list for the switch you want to control. I wanted to control "Lucs room lampje" which has IDX number 3. We need that number to put in our program.

To control this switch we need to call a Domoticz API. You can find a list off all Domoticz API's here: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's

On this page look for the entry : Turn a light/switch on
There you will find the required API call:

/json.htm?type=command&param=switchlight&idx=99&switchcmd=On

So what we need to do is to build a webpage with a button on it and when that button is pressed this code is send to Domoticz. And that is easily done with ESP-Basic.

For those of you who have no experience with ESP-Basic I urge you to read this tutorial which will have you up and running in no time:
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

And here is the program.



cls
wprint |<h1 style="text-align:center;">Luc Volders</br>Domoticz</br>Control|
wprint "<br/><br/>"

button "Lamp on", [domoticzon]
wprint "<br/><br/>"
button "Lamp off", [domoticzoff]

wait

[domoticzon]
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=On"
ret = wget(query,8080)
wait

[domoticzoff]
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=Off"
ret = wget(query,8080)
wait

Let's have a look at this.

The program starts with putting some text on the screen and two buttons for ON and OFF.

When one of the buttons is pressed the program jumps to the accompanying routine wich calls the API. As described above the API looks like this:

/json.htm?type=command&param=switchlight&idx=99&switchcmd=On

and that is translated in ESP-Basic in this:

query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=On"
ret = wget(query,8080)

query is the variable in which the data is collected that is going to be send. 192.168.1.66 is the IP number of my Domoticz setup. You have to replce that with your own IP. The rest of the API call is just copied except the IDX number which I altered in the number of my switch: 3

The wget statement sends the API call and puts the answer in the ret variable. You do not need to add this line but if you do not any info that Domoticz send back is displayed on the screen which makes a mess of your screen.

I do not think that it can be programmed in any other language for the ESP more easier.



And this is the result on your computer / phone / tablet's screen.

The buttons Lamp on and Lamp off switches lucs room lampje on or of and that can bee seen in Domoticz and off course by just looking at the lamp.





Temperature Sensor

I also wanted to send some sensor data to Domoticz. So I attached a Dallas DS18B20 temperature sensor to the ESP8266 (NodeMCU board) to see if I could get some readings.

To setup a switch or sensor in Domotics that is not physical attached but gets it's data through wifi you will have to define a virtual switch.


First in Domoticz open the devices tab and create a virtual switch. Give it a name. I choose Virtueel 3 and choose as type Dummy.


Now choose create virtual sensor I gave it as name Test temperature and as sensor type Temperature


At this moment the sensor is already visible in the Temperature Tab of Domoticz although it does not have received any value and therefore the temperature will be 0



Go back to the devices menu (Apparaten in Dutch) and there you can find Virtueel 3 and as you can see that the ID number is 4592

Next step is to build the hardware


My test setup is just a DS18B20 with a pull-up resistor of 4.7K attached to GPIO2 (D4) of a NodeMCU board.

In ESP-Basic I wrote a small test pogram to see if the sensor worked.


cls
wprint |<h1 style="text-align:center;">Luc Volders</br>Domoticz</br>Thermometer|
wprint "<br/><br/>"
wprint "The temperature is now "& temp(0)

wait

Nothing special. The program just reads temp(0) which is the DS18b20 and puts the value on the screen.

Back to the Domoticz API page on the web: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s#Create_a_virtual_sensor
Here we can read how the API call looks for reading a sensor.

/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP

IDX = id of your device (This number can be found in the devices tab in the column "IDX")
TEMP = Temperature

So we just need to implement that in our ESP-Basic program. Here you go:


cls

timer 1000 * 60 * 5, [domoticztemp]

wprint |<h1 style="text-align:center;">Luc Volders</br>Domoticz</br>Thermometer|
wprint "<br/><br/></h1>"
wprint "The temperature is now "
textbox temptest
wait


[domoticztemp]
temptest = temp(0)
temptest = (int(temptest * 10))/10
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=udevice&idx=4592&svalue=" & temptest
ret = wget(query,8080)
wait

Again very easy to implement.

A timer is setup that every 5 minutes calls the [domoticztemp] routine.

Timer 1000 * 60 * 5 = every 5 minutes

The formula is simple. ESP's timer works in miliseconds. So 1000 = 1 second (1000 miliseconds). 60 Seconds in a minute and that times 5 minutes.

Why the 5 minute routine ? Well Domoticz just displays the sensors reading once in the 5 minutes on it's screen.


And there you go: the temperature is displayed in Domoticz.

Last step: Combine button and Temperature

A NodeMCU or a Wemos D1 have many I/O ports. Why not use them ? So let's combine the button and the temperature programs and include a physical button as well.


Again the hardware setup is easy. I took the temperature setup and added a button that is connected with a pull-up resistor (10k) to D3 of the NodeMCU board.


cls
wprint |<h1 style="text-align:center;">Luc Volders</br>Domoticz</br>Control<br> And Thermometer|
wprint "<br/><br/>"

but=0

button "Lamp on", [domoticzon]
wprint "<br/><br/>"
button "Lamp off", [domoticzoff]
wprint "<br/><br/>"
wprint "The temperature is now "
textbox temptest

interrupt d3, [change]
timer 1000 * 60 * 5, [domoticztemp]

wait

[domoticzon]
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=On"
ret = wget(query,8080)
wait

[domoticzoff]
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=Off"
ret = wget(query,8080)
wait

[change]

if but = 0 then
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=On"
ret = wget(query,8080)

else 
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=switchlight&idx=3&switchcmd=Off"
ret = wget(query,8080)

endif

but = 1 - but
interrupt d3, [change]
wait

[domoticztemp]
temptest = temp(0)
temptest = (int(temptest * 10))/10
query = "192.168.1.66/json.htm?type=command&"
query = query & "param=udevice&idx=4592&svalue=" & temptest
ret = wget(query,8080)
wait


No surprises in the software either. An interrupt tests wether the button has been pressed and jumps to the [change] routine.

but = 1 - but

This line toggles the but value between 0 and 1 for setting the switch ON or OFF.

The rest is the same as in the previous programs.


This is how it looks on the screen of your computer / phone / tablet.

Hey, what's that Luc's Room Temperatuur thing ???


Well remember my "Oh no, not another wifi thermometer." entry in this weblog. If not you can re-read that story here: http://lucstechblog.blogspot.nl/2017/11/oh-no-not-another-wifi-thermometer.html


As you might remember this thermometer not only set's its data on the screen of a webpage but it also sends it's data to Thingspeak. To refresh your memory you can re-read the weblog entry here:
http://lucstechblog.blogspot.nl/2017/11/thingspeak.html

Well that thermometer has been sending it's data also for a long time now to my Domoticz setup.

Here is the altered code for that Thermometer.


tel=0
Timer 5000, [start]

wprint "<!DOCTYPE html>" 
wprint "<html> <body>"
wprint |<body style="background-color:greenyellow;">|
wprint |<H1><span style="color: red;">|
wprint " A thermometer"
wprint "</H1>"
wprint "</span>"
wprint |<H2><span style="color: blue;">|
wprint "Temperature is now "
textbox test

a = "background-color:greenyellow;"
a = a & "display:block;width:80px;"
a = a & "border-style: hidden;"
a = a & "font-size: 22px;"
a = a & "font-weight: bold;"
a = a & "color: fuchsia ;"
cssid htmlid(), a
wprint "</span>"
wprint "</H2>"
Wait

[start]

test = temp(0)
test = ((int(test*10))/10)
tel=tel+1
if tel = 360 then
  tel = 0
  gosub [thing]
endif
wait

[thing]
sendthing = str(test)
SENDTS("MY-API-KEY", "1", sendthing)

'and to Domoticz
domoticz = "192.168.1.66 /json.htm?type=command&param=udevice&idx=2662&svalue=" & test
wget(domoticz,8080)

wait

As you can see the temperature is not only put on a webpage accessible from your computer/phone/tablet but also send to Thingspeak like described in this story:
http://lucstechblog.blogspot.nl/2017/11/thingspeak.html

And now it also sends the data to Domoticz, using the same code as described above with just another IDX.


The chart shows you that the thermometer has been working continuously from july 2017. This shows how reliable the ESP8266 and ESP-Basic is.

As stated in the beginning of this story: I am moving away from ESP-Basic to the Arduino IDE (C++). So in upcoming stories I'll be showing you how to achieve this in C++. So stay tuned.


Till then
have fun

Luc Volders