Pages

Friday, February 19, 2021

Geofencing for Domoticz with ESP8266

For an index to all my stories click here

Imagine getting home from your work. At opening the door some lights will automatically go on and the thermostat is being set to a comfortable 20 degrees. This not a view from the future but something we can build right now using Domoticz and the ESP8266.


My goal is to have at one time my complete house automated. To achieve this I want to make several long term tests. At this moment all the sensors and switches are controlled by Domoticz. I have written several stories about connecting an ESP8266 to Domoticz. Here is the list so far :

- ESP8266 sending data to Domoticz
A story that describes how to set switches on and off and send temperature data to Domoticz using ESP-Basic
- Sending data from ESP to Domoticz to set a lamp on or off written in Arduino IDE
- Sending data from ESP to Domoticz part II to send temperature data to Domoticz written in Arduino IDE
- Sending data from ESP8266 to Domoticz part III building a webpage that has buttons and temperature value and sends that data to Domoticz. Written in Arduino IDE

This is all fun. The programs show how to control lamps and switches in Domoticz and everything is commanded from your phone or computer. But there is not a lot of automation going on.

Geofencing

Let me explain what geofencing is.


Most people will not have a fence around their homes. So what the name refers to is a virtual fence. This fence is maintained by your phone. Most people have the Wifi function on their phone permanent on. When you enter the vicinity of your home your phone will automatically make contact with your router. And when you leave the vicinity of your home the contact between your phone and your router will be broken. There you have your virtual fence.

How can we use this veirtual fence. Well, remember ping ????

Ping.

In may 2019 I wrote a story about Ping on this weblog: http://lucstechblog.blogspot.com/2019/05/ping-ping-anybody-home.html

The story demonstrated how an ESP8266 tested IP numbers of devices to see if they where operational and/or present. The program 'pinged' the IP number of your phone. If the phone was connected to your router a led would be set on, or a notification was put on a webpage.

What we are going to do

What we are going to do in this story is to Ping our phone and depending on our presence send a command to Domoticz. The command will set a switch on or off in Domoticz. That switch will be a reference for a script in Domoticz that will automatically set lights on or off depending on the value of the reference switch.

So in the end when we reach our home the lights will go on automatically. And when we leave they will be put out automatically. You can expand this by setting the thermostat to a certain temperature, starting the coffee machine and open the garage door automatically just to name a few options.

What is needed to make this project work

To get this to work you will need a full working Domoticz setup. Domoticz is a house automation system which is free and Open Source. Domoticz runs on a Raspberry (starting at Raspberry 2). To control your Coco (ClickOnClickOff or KlikAanKlikUit) lamps or other cheap 433Mhz lamps you will need a RFlink. This can be build easily by yourself. However a set of Ikea lamps with a bridge or a Philips Hue setup with a bridge can also be used. And you can make your own smart-lamps with an ESP8266 and connect that with Domoticz.

You can find the Domoticz website here:
https://www.domoticz.com/

The RFLINK website can be found here
http://www.rflink.nl/blog2/

Next to the Domoticz setup you will need an ESP8266. Even the humble ESP8266-01 can be used for this project. Indeed it is the module I am going to use. You can however replace it with any ESP8266 model like the NodeMCU or Wemos D1.

Programming is done in the Arduino IDE (C++).

Hardware setup.

Well not much to it. Just use any ESP8266 module with a power source. You can use a NodeMCU or Wemos D1 with an USB power adapter. I used an ESP-01 with a programming board and an USB power adapter.

Domoticz setup.

You will need at least one lamp attached to Domoticz. As you can see I have multiple lamps attached in my system.




For this project we are going to control the lamp in my mancave. It is the lamp in my mancave with the name "Lucs room lampje".

The first thing we need to do is to add a new switch to Domoticz.

 



On the left side in the drop down menu chose "settings" which is called instellingen in Dutch. 
 



From that drop-down menu choose hardware.
As you can see I already have a Virtual Switch called "Virtueel 1". This is my thermometer which I described in an earlier story which you can re-read here http://lucstechblog.blogspot.com/2020/01/sending-data-from-esp-to-domoticz-part.html We will need to add another virtual switch. Simply call it "Virtueel 2". The type of the Switch is dummy. Set the Timeout to "Not Active". And add the switch.

 

 

The new switch will appear in the list. Click on it and then click on "make virtual sensors".

 

 

Give the switch a name. I gave it the name "Is Luc Aanwezig" meaning "Is Luc present". And choose as type: Switch. Press OK.

Switch over to the Devices Menu in the Settings (right most tab).

 

 

Using the button at the top on the left I chose to display only active (Gebruikt) devices so the list is short.

The second device is the "Virtueel2" device which is the new created switch. Note the IDX in the second column. In this example it is 8500. Remember this as we will need it in the ESP8266 program. No need to write it down as you can come back to this menu anytime.

 



Now move over to the Switches tab. All your switches are visible and you can see the last created one at the bottom being the "Is Luc Aanwezig" switch. Lets give it a different Icon. Next to the log button is a button that allows you to adapt the switch. In Dutch it is called "Aanpassen". Press that button.



Change the switches icon from a lamp to the Phone Icon. And leave the rest of the settings unaltered. Chose "Opslaan" which will be "Save" in the English version.

 

 

The program will automatically jumpo back to the switches tab and you can see that the icon of the last switch has altered in a mobile phone just like we wanted. Now the switch is really representing what it means.

Pressing the little star on the left side of the switch will put it on your dashboard.

 

 

Here you can see my Dashboard with my mancave lamp and my phone switch. The screen of the phone in the switch will be black when it is off and coloured when it is on.

Find Domoticz and your Phone's IP Adress

Next step is to find Domoticz and your Phone's IP adress. Maybe you remember these from previous projects. If you dont you can find them in your router. Open the webpage from your router and look in the manual where to find IP adresses of connected devices.



 

As you can see in my router the Phone has IP adress 192.168.1.67 We will need that IP adress in the ESP8266 program.
Use the same way to get Domoticz IP adress. In my case that is 192.168.1.66

Domoticz API


On the Domoticz website we can find all API calls that we can use to control Domoticz externally. You can find that page here: https://www.domoticz.com/wiki/Domoticz_API/JSON_URL's

Chapter 3.2 tells us how to turn a switch on or off. The Api call for turning a switch on is:

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

And for turning the switch Off the call is:

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

We have to translate that info Arduino code, which is not all to difficult. You will have to fill in the IDX which you found in Domoticz. In the above lines it is 99. In my example it was 8500. You can find it in the Domoticz settings in the devices section as described above.

ESP8266 program

As usual on my weblog I'll give you the full program first and then describe the most important parts.


/*
 * Anybody Home ??? 
 * This program tests an IP number. If that number is active
 * a command is send to Domoticz which sets a switch on.
 * This switch will in Domoticz be used as a reference for
 * someone being present or not. Domoticz will then act on that.
 * Written by Luc Volders
 */

#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <ESP8266HTTPClient.h>

const char* ssid = "XXXXXXXXXXXXXXX"; 
const char* password = "YYYYYYYYYYYYYY";

const IPAddress Lucsphone(192, 168, 1, 67);

const char* host = "192.168.1.66";
const int   port = 8080;

HTTPClient http;

int present = 0;

void setup() 
{
  Serial.begin(115200);
  delay(200);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("-o");
    delay(100);
  }
  Serial.println("-");
}

void loop() 
{  
    String url = "/json.htm?type=command&param=switchlight&idx=";
    if(Ping.ping(Lucsphone) && present == 0) 
    {
        url += String(8500);
        url += "&switchcmd=On";
        Serial.println("on");
        Serial.println(url);
        sendToDomoticz(url);
        present = 1;
    }
    else if(!Ping.ping(Lucsphone) && present == 1)
    {
        url += String(8500);
        url += "&switchcmd=Off";
        Serial.println("off");
        Serial.println(url);
        sendToDomoticz(url);
        present = 0;
    }
    delay(15000);
}
    
void sendToDomoticz(String url){
  Serial.print("Connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}

I will explain the program in detail.


#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>
#include <ESP8266HTTPClient.h>

This is obvious. The required libraries are loaded. They should all be available in the Araduino IDE. If you did not build the project I described about the PING function it is time to re-read that story now and install the PING library. You can find the story here:
https://lucstechblog.blogspot.com/2019/05/ping-ping-anybody-home.html

If you just need to install the ESP8266 Ping library you can find it here:
https://github.com/dancol90/ESP8266Ping

const char* ssid = "XXXXXXXXXXXXXXXXXX"; 
const char* password = "YYYYYYYYYYYYYYYYYYY";

const IPAddress Lucsphone(192, 168, 1, 67);

Fill in your own Routers credentials at the XXXXXXX's and YYYYYYYYYY's. And fill in your phone's IP adress which you have looked up in your router as described above.

const char* host = "192.168.1.66";
const int   port = 8080;

Replace the host IP number by the number that Domoticz has in your router. The port is always 8080.

The setup has no magic in it. A variable with the name present is defined. Next the Serial Monitor is activated and the display shows a line being printed as lomg as the ESP8266 is not connected to the router. Nothing special here

 

String url = "/json.htm?type=command&param=switchlight&idx=";

The loop starts with putting the first part of the Domoticz API call in a string with the name url.

    if(Ping.ping(Lucsphone) && present == 0) 
    {
        url += String(8500);
        url += "&switchcmd=On";
        Serial.println("on");
        Serial.println(url);
        sendToDomoticz(url);
        present = 1;
    }

The PING library is executed with the IP adress of your mobile phone. If the PING part is true AND the present variable is 0 then the switch is off and should be set ON.
The IDX and the command ON are added to the url and printed in the Serial Monitor as a visual check. Then the sendToDomoticz(url); routine is called and the present variable is set to 1.

At the next test, after 15 seconds determined by the delay(15000) command, the Ping will be true and the present variable will be 1 and therefore the command will not be executed again.

    else if(!Ping.ping(Lucsphone) && present == 1)
    {
        url += String(8500);
        url += "&switchcmd=Off";
        Serial.println("off");
        Serial.println(url);
        sendToDomoticz(url);
        present = 0;
    }

This does the opposite. If PING is not true (!Ping) you will be out. So if present is 1 the switch is on and should be set off.

    delay(15000);

The delay makes sure that the loop is executed once every 15 seconds. Adjust this tou your own needs. In my case it takes about 15 seconds to open my front door, put my coat in the closet and walk to my mancave. By then the lamp will be on. Your mileage may vary.

void sendToDomoticz(String url){
  Serial.print("Connecting to ");
  Serial.println(host);
  Serial.print("Requesting URL: ");
  Serial.println(url);
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
        Serial.println("Domoticz response "); 
        Serial.println(payload);
      }
    }
  Serial.println("closing connection");
  http.end();
}

This is the routine that sends the url which is build in the loop to Domoticz by http commands. The serial port prints Domoticz IP adress and the url string which was build in the loop. Next using the host and port variables the url is send to Domoticz which switches the lamp ON or OFF. Domoticz sends an acknowledgement back and that is printed in the Serial Monitor.

Thats all.

Upload the program to the ESP8266 and reset the ESP. In the serial monitor you can see that the ESP8266 contacts the router and then the program starts pinging your phone.

First test

This is a simple test to see if everything in Domoticz works as expected.

Get your phone and activate wifi. 

 

 

The Domoticz switch should be ON or set ON which is indicated by the blue screen of the Icon.

Now turn your Phone's wifi off.

 

 

In a few seconds the icon will change in a black screen.

Now we know that the new build switch works and reacts to the wifi settings in our phone. Cool !!!

So now we have a switch in Domoticz that will be set ON as soon as we enter our home and have the wifi in our phone activated. As soon as we leave out home the connection from the phone to the router will be lost and the switch will be set off.

Cool !!! Great !!!
But hey wait. We want the lamp in the mancave to react. Or to set a thermostat or open and close the garagedoor electrically or........ Thats right. Move on to the next step.

Automating Domoticz

We now have a working switch in Domoticz that reacts on our presence. The next step is to use that switch as a trigger to have Domoticz automate things for us.

In my example I have a lamp in my mancave which I want to turn on automatically when I enter my home, and turn off when I leave.

 

 

In Domoticz got to the Settings tab (on the right side), choose more options and happenings. In Dutch it is called "Meer opties" and "Gebeurtenissen".
 

 

We are presented with an empty screen in which we can graphically program Domoticz to automate functions. This way of programming is called Blockly in Domoticz and is a bit like Mit's App Inventor for building programs for your Android Phone.

You can build all kinds of routines. This example we make a routine which is called "presence". Fill this name in at the top right line where it says "Event name"

 


At the control section on the left choose the lower IF-DO block and drag it to the main screen.

 


Click on the blue gear and add the else-if block to the if block by dragging it to the if block.

 

 



You should now have an IF-DO-ELSE IF-DO Block
 



Move to the Logic controls and choose the first one with the "=" sign. Drag that to the screen.
 

 

Over at the Devices choose "Is Luc Aanwezig" and put that in the comperator. Then move back to the Logic section and Drag "ON" into the comperator. Put the block into the IF section.

Back to the Logic section and choose another comparation.
From the devices section add "Lucs Room lampje" and from the Logic section add "ON".

 

 

The first IF block should now look as the above picture. This translates as:
If "Is Luc Aanwezig" is "On"
Do set "Lucs room lampje" to "On"

Now build the Off block likewise.


 

Your complete program should now look as above.

On the right side in the screen set the event to Active and save it.

That's it.

All Done.

Everything should work now as planned. If you put the Wifi on your phone OFF the ESP will notice and send command to Domoticz to set the switch Off and that triggers the program to set the lamp off. When you set the wifi on again the route will be reversed and the lamp will go on.

Why the switch in Domoticz

In a previous story on this weblog you have seen how to set the lamp in Domoticz direct on or off. No intermediate switch needed. So why build such a switch in Domoticz.

Well this way you can alter the automation process in Domoticz to your liking. You can expand the if-else to have multiple lamps going on and off, switch fans on or off, set a temperature in your thermostat and everything else you can imagine with Domoticz.

It is also possible to use a timestamp in Domoticz. That way the lamp will not go on during day-time. Just use your imagination.

If we would set these things from within the ESP you would have to re-program the ESP each time you wanted a change. Re-programming Domoticz is much easier.

ESP expansion

In this example we test for the presence of one phone. You can easily alter the ESP8266 program to test for multiple phones like those of your partner and kids etc. You would then add multiple switches in Domoticz and have it react different on the presence of your partner or kids or whatever.
Next to that if you use a NodeMCU or Wemos D1 instead of an ESP-01 you can have the ESP do multiple tasks. You could for example attach a relay to the ESP which makes it possible to switch a lamp on or off without using Domoticz and at the same time send commands to Domoticz to automate other lamps etc.

This was a fun project for me to devellop and build. I hove you will find it usefull and find many purposes for this Geofencing trick.

Till next time
Have fun

Luc

Saturday, February 13, 2021

Raspberry Pico reset button

For an index to all my stories click this text

If there is something the Raspberry Pico lacks it is a reset button.

Sometimes your program goes berserk and you need a means to stop it. Pulling the plug is one way. Having a reset button is another. But the Pico has none. So lets add it.

Two ways to reset

There are two ways to reset your Pico and I am going to show both.



Above you can see a setup with two buttons and a led. The led is connected to the power rails with a current delimiting resistor and it is ON as long there is power on the power rails.

The button on the left is connected to GND and to pin 37 on the Pico which is the 3V3 EN pin.
The button on the right is connected to GND and to pin 30 on the Pico which is the RUN pin.

Now lets enter the next Python program:

import utime

while True:
    print('running')
    utime.sleep(1)


Cal it main.py and save it to your Pico.

Run the program and press the button on the right side.
The program will stop and you can edit it. However the led stays on.

Run the program again and now press the button on the left.
The program will stop and the led goes OFF for as long as you press the button.

So there we have it. Two ways to reset the Pico.
Even a program that runs as main stops and we can edit it.

The difference is that by using the button on the left the power is cut and your sensors will get reset too. The Raspberry Pi foundation mentions that this is not recommended and might harm your Pico. I have been using this extensively the last weeks and did not encounter any problem. Be warned anyway.

Which one to use depends on various factors and on which sensors you are using. So choose the one that is best for your project.

Till next time
Have fun

Luc Volders


Friday, February 5, 2021

Raspberry Pico a first look

For an index to all my stories click this text

I bet you heard by now there is a new kid in town: the Rapsberry Pico

For those who already have one or have been reading about it: see you next time. For those who have no clue what it is a bout: read on.

The Raspberry Pi company is well-known for its small Linux computers. I have used them myself in several projects on this weblog. I made an internet radio with it. My home automation system Domoticz runs on a Raspberry Pi and I did some experiments with the Raspberry camera. You can find these stories by clicking on the top red text on this page that brings you to the index.

However the Raspberry Pico is something completely different. It is not a computer but it is a microcontroller just like the Arduino or the ESP8266 and ESP32. You can use it instead of an Arduino and in certain circumstances also as a replacement for an ESP.

There are a few differences with the establishment of the microcontrollers.
- The Raspberry Pico has no wifi or other wireless communication.
- The Pico can be programmed in C and in Python.

Here is the obvious difference with a controller from the ESP series: no wifi. So no internet communications and no web-pages running on the controller. In this view it is more as an Arduino.

But then the specs are impressive:

- Dual Core Arm Cortex MO processor running up to 133 Mhz
- 264Kb Ram
- 2 Mb Flash
- 26 Multifunction IO pins
- 3 Analog input pins
- 2 UART, 2 SPI controllers, 2 I2C controllers, 16 PWM channels
- 1 USB port with Host and Device support.
- Build in temperature sensor
- 8 Programmable I/O (PIO) state machines

You get all this for about 4 euro !!!
So that's an European product running about 10 times the speed of an Arduino and with loads of IO ports for a ridiculous price.



Above you can see from left to right the ESP8266 (Wemos D1 version) the ESP32 (Devkit 1 version) and the new Pico. The Pico is as tall as the ESP32 but not as wide.



The small width makes it possible to fit the Pico seemless on a breadboard. So no hassle like the ESP32 Devkit has.

Software ??

The Pico can run C and Python programs at the moment. Unfortunately not the Arduino C++ version. Arduino corporation is at this moment developing some boards useing the same processor as the Pico and have annlounced that the Arduino IDE will be developed for the Pico. Just a matter of time.

At this moment I am focussing on Python.
I already did several projects with Python on the 'normal' Raspberries like the internet radio on the humble Raspberry Zero: https://lucstechblog.blogspot.com/2018/10/raspberry-pi-internet-radio-part-3.html
Next to that I must be proud about Python as it is invented by a fellow Dutchman: Guido van Rossum

There are two variations of Python available for the Pico: MicroPython and CircuitPython both have their pro's and cons. At this moment I stick to MicroPython as that is supported by the Raspberry foundation. However I will look into Circuitpython in the next weeks as there might be some interesting libraries available only for MicroPython. By the way, CircuitPython is a fork of MicroPython and supported by Adafruit.

The Pico can be programmed from The Raspberry Pi computers, Linux machines, Apple Mac's and Windows.
For programming with Python the Thonny IDE is recommended and has a special connection to the Pico. Thonny is devellopped at the university of Tartu in Estonia and supported by the Raspberry Pi foundation.

Thonny can be downloaded here: https://thonny.org/

I am not delving to deep into this at the moment as there is very good documentation available on the Raspberry Pi's website: https://www.raspberrypi.org/documentation/pico/getting-started/

I was just amazed how simple it was to get the Pico up and running. So I'll show how to do that briefly here. This is Windows based.

After I downloaded Thonny on my PC it looked like this.



The arrow points to the version of Python you are currently using. When I started it pointed at Python 3.7.9 which is installed on my PC.
Now plug in the Raspberry Pico in a USB port on your computer and wait a few seconds.



Thonny just recognised the Raspberry Pico.


Click on the Raspberry Pico line



A new small window opens. The Pico is already recognised so press the boot-selector on the Pico, hold it and click on Install.

import machine
import utime
led = machine.Pin(25, machine.Pin.OUT)
while True:
    led.value(1)
    utime.sleep(.5)
    led.value(0)
    utime.sleep(.5)

Write the above program in Thonny, save it as hello.py, and press the small green run button and your on-board led should start blinking.

Done !!!

My first program running on the Raspberry Pico in no time.

The program which you just named is saved on the Pico itself in its flash memory. Save it again with the name main.py and you can attach the Pico to a USB power supply or powerbank and it runs automatically. Just like any program saved to an Arduino or ESP would run.



A disadvantage is that the pin layout is only printed on the backside of the Pico. Therefore the pin-numbers are not easily recognisable when the Pico is put on a breadboard.



A nice feature is that the IO pins have rounded connectors and the GND pins have square ends. And they are put at opposite sides on the development board.



To solder the headers on easily, first plug them into a breadboard at the right distance.



Next put the Pico on the headers and solder them.



And here is my first working project. A thermometer with a TM1637 display. Installing the software, soldering the headers and getting my first project up and running is all done in a few hours.

Concluding

Getting the Raspberry Pico up and running is a piece of cake and getting my forst test project up and running was equally easy.
Check the Rapsberry Pico out. It is worth the few euro's and for the price a great competition to the Arduino's. It even is a good replacement for the ESP's as long as you do not need Wifi.

Til next time
Have fun

Luc Volders