Friday, November 3, 2023

Domoticz with MicroPython part 1

For an index to all my stories click this text.

I migrated to a new internet provider. That meant reprogramming all my microcontrollers. They all needed to contact my new router and got new IP addresses. It was a hell of a job.

To achieve this I needed physical contact with those micro's to connect them to my PC. But what annoyed me most was that I needed to search all the programs that were on these micro's (yes, I am not the best organised) and recompile them in the Arduino IDE (C++) which is slow.

Interlude.
Actually I did not need to make physical contact with all of my microcontrollers cause some of them are running ESPEasy. I am doing a series on ESPEasy and you should really check it out. ESP8266 and ESP32 microcontrollers that are programmed with ESPEasy can be reprogrammed over wify !!! No need to attach an USB cable. There isn't even much programming at hand with ESPEasy. But that also takes a lot of fun out of puzzling to get things working.


MicroPython is an interpreted language which means that it is slower in processing data. But my projects are not time critical. I have no fast rotating motors that I need to know the rotation speed from etc. I just have temperature sensors, light switches a doorbell etc. Nothing MicroPython is not perfectly capable of handling.
Next to that I just have to plug the microcontroller into my PC through USB and I can download the program from the controller itself. No more searching for the programs anymore !!

So I decided to reprogram the sensors in MicroPython. What made this easy is that all my favorite microcontrollers (ESP8266, ESP32 and Raspberry Pi Pico) are supported by MicroPython.

Domoticz is still my preferred home automation system. So I had to program 4 different ways of communication with Domoticz:

- Send data from a simple switch like a doorcontact, windowcontact, doorbell, PIR, vibration sensor etc. to Domoticz
- Send a signal from Domoticz to a microcontroller to set on/off a relay or a lamp etc.
- Send a value (like thermometer value etc) to Domoticz
- Set a slider in Domoticz and send it's data to a microcontroller for dimming lamps, changing a fans speed etc. etc.

I this and the next stories am going to show you how I did these in MicroPython.

Send a button's data to Domoticz

Look upon the word button in a broad context. It could be a pushbutton connected to your microcontroller, a door contact, a doorbell,  a PIR, a tilt sensor, a vibration sensor, a rain sensor etc. etc. etc.

For this example I am using a Raspberry Pi Pico W with a pushbutton connected to GPIO14.

This is the breadboard layout. Nothing special going on here. Just do not forget to attach a 10K pull-up resistor to the pushbutton.


Step 1: Create a virtual device



In the Setup tab chose Hardware and add a new device. I gave it the name Virtual 1 and choose as type Dummy. Then push the blue Add button



Click now on the newly created Virtual 1 entry and click on Create Virtual Sensors.



I gave my new switch the name MicroPython test switch and chose Switch from the drop down menu as Sensor Type. A message appears that the new sensor is created and can be found in the Devices entry in the Setup tab.



There is no need to look in the Devices entry as the new switch is readily available in the switches section.



Click on the edit button of the switch.



The default icon is a lamp. I changed this into Generic which is a switch..



There are two important things on this page.
First at the top you see the IDX which in this case is 23637. You will need this number for using in the MicroPython program. Your own Domotic will give you of course a different number.
Second (but obvious) you need to press the Save button at the bottom.

That is it. The button is defined.

There is however one extra feature. As soon as you press the save button the Switch Type menu changes from nothing to On/Off.



You can now change this into another type of sensor like you can see in this list. Choosing another type also changes the icon. Leave it for now as it was.

Step 2: the MicroPython program.

First thing is that we need to know how we can send information from MicroPython (or another language) to Domoticz. And we will use the Domoticz API for that.
You can find the Api's at:
https://www.domoticz.com/wiki/Domoticz_API/JSON_URL%27s

On that page look for the Lights and Switches section (section 4) and then look at 4.2 Turn a light/switch on/off

/json.htm?type=command&param=switchlight&idx=99&switchcmd=<On|Off|Toggle|Stop>

This is the general API call for setting a switch on or off. This has to get preceeded by the IP address of Domoticz. In my setup Domoticz has the IP address: http://192.168.178.68:8080

There are three possiblities. We can give the command to set the switch:
- On
- Off
- Toggle


In this example I have attached one button to the Raspberry Pi Pico W. So we use that same button to set the switch on or off. Therefore we use the Toggle command.

The complete API call therefore is:

http://192.168.178.68:8080/json.htm?type=command&param=switchlight&idx=99&switchcmd=Toggle.

Please make sure that you use the right IP number that Domoticz has and to change the IDX into the IDX you saw in Domoticz for this switch.

To send data from MicroPython to Domoticz we will need the urequests library which is supplied with the MicroPython distribution. So here is the full program:

import gc
import machine
import network
import urequests as requests
import time

ssid = "YOUR-ROUTERS-NAME"
pw = "PASSWORD"

# wifi connection
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(ssid, pw)

print('Waiting for connection.',end="")
while wifi.isconnected() == False:
    time.sleep(1)
    print('', end='.')
print("")

ip = wifi.ifconfig()[0]

print("Connected with IP adress : "+ip)

button = machine.Pin(14, machine.Pin.IN)

while True:
    if button.value() == 0:
        print("Button pressed")
        requests.get("http://192.168.178.68:8080/json.htm?type=command&param=switchlight&idx=23637&switchcmd=Toggle")
        gc.collect()



And that is all.
Don't forget to replace "YOUR-ROUTERS-NAME" and "PASSWORD" with your routers credentials.

The program is really simple.
First the libraries are loaded. We need the time, machine, network, urequests and gc libraries. All supplied when you installed MicroPython.

The program defines a button and then makes contact with your router. When the contact is established the Pico's IP address is printed in Thonny's shell.

The real magic happens in the while True: loop.
The program constantly checks if the button is pressed. When that happens the request is send. A confirmation that the button was pressed is printed in Thonny's shell. The program then sends the request and finally performs a garbage collection.

Do not omit the garbage collection. It frees up memory and using requests quickly clogs up your Pico's memory. So the garbage collection is really needed.



This is what will be shown in Thonny's shell.



Step 3: alter the switch appearance

Suppose you are going to use this as an alarm for a door or window. You would put a magnet on the window and a reed contact on the window frame. The reed contact acts as a switch.

In Domoticz press the "edit" button in your switch settings.



Alter the Switch Type in Door Contact and the icon will change.



 

Pressing the button on your Pico now simulates opening and closing the reed contact and this will result in changing the switches appearance on your Domoticz screen.

Step 4: Using Events

Having a switch or contact changing on your screen is fun but not really practical.

In Domoticz's setup menu you can look at More Options and then Events.
Using the events you can write a script to test the switch and act on it's condition by setting an alarm on, sending a notification to your phone etc. etc.

Next time we are going the other way round.
The next story tells how to attach a led to your Pico (or other controller) and use a switch on Domoticz screen to set that led on or off.

Step 5: Use this to control your existing lamps

There is yet another purpose for this.

I have several outlet sockets that are controlled through 433Mhz. A famous brand here in the Netherlands is KlikAanKlikUit (KAKU). But you can also find them unbranded and cheap at thrift stores en DIY stores. Just play safe and make sure they comply to the local electricity rules.



One of these lights is in my mancave and called Mancave lamp



Press on the Edit button of that lamp and you get the settings menu. We are not going to change anything. But we need thye first information that is displayed:

Idx : 3

That is the ID of this lamp in Domoticz.

And we can use that to set this lamp on or off from our Pico with MicroPython. We use the same program and only have to alter the request line:

requests.get("http://192.168.178.68:8080/json.htm?type=command&param=switchlight&idx=3&switchcmd=Toggle"



And there you go. I can control this lamp now from the 433Mhz remote control, from the Pico and from Domoticz.

The fun part is that the MicroPython program uses "Toggle" to control the lamp. This means that if Domoticz sets the lamp on the Pico sets it off and the other way round.

Expansions.

Nobody holds you from sending multiple requests from MicroPython. So you can control several lamps and switches with one button attached to the Pico.

You can, of course, also attach multiple pushbuttons buttons to your Pico and use it as a remote control for controlling several lamps individually.

And you could have multiple Pico's with pushbuttons controlling the same lamp or outlet socket or whatever.

Just the Pico ?

Hell no. MicroPython works equally well on the ESP8266 and on the ESP32. You just have to adjust the pin numbers where the pushbuttons are attached to.

Arduino language  (C++) ????

Yes you can do the same with the Arduino language. The code is although a lot more complicated. I did a story on how to do this in Arduino some time ago. You can find it here:

http://lucstechblog.blogspot.com/2020/01/sending-data-from-esp-to-domoticz.html

MicroPython ???

Micropython is a great language and easy to learn. A lot easier to learn as C++ (Arduino language). If you have no experience with MicroPython and want to learn, I can recommend my books on MicroPython with the Raspberry Pi Pico or Pico W for which you can find a link below this story.

Also check out my website that has lots of tips for programming in MicroPython:


https://micropython-tips.weebly.com/

Till next time
Have fun.


Luc Volders