Friday, November 24, 2023

Sinterklaas 2023

For an index to all my stories click this text.

Het is bijna Sinterklaas.
Als je nog een leuk cadeau zoekt voor je partner, kind, neefje etc die interesse heeft in electronica, microcontrollers of programmeren dan heb ik hier twee suggesties voor je.

ESP32 uitgelegd

Dit was het eerste boek dat ik schreef en is nog steeds een bestseller. Ideaal als je met  het Internet Of Things wil beginnen.



Dit boek begint met uitleg over de populaire ESP32 microcontroller en legt de programmeertaal C++ in het kort uit.
Daarna bespreekt elk hoofdtuk een sensor, laat zien hoe je deze aansluit en geeft een kort demo programma dat laat zien wat je ermee kunt doen. Vele sensoren komen aan bod: druktoets, bewegings sensor (PIR), tril sensor, tilt sensor, thermometer (Dallas DS18B20), licht sensor (LDR), aanraak sensor etc. etc.
Er komen ook aansturingen aan bod: leds, motoren, lichtstrips, servo's, relais, en transistoren en H-bruggen om motoren die meer kracht vergen aan te sturen.
En natuurlijk ontbreken displays als oled schermpjes, en TM1637 7-segment modules niet.
Als laatste wordt uitgelegd hoe je de Wifi mogelijkheden van de ESP32 kunt gebruiken. Met IFTTT kun je alarmen naar je telefoon sturen. Thingspeak wordt besproken om gegevens zoals temperatuur in grafiekvorm of wijzerplaat weer te geven. En uiteraard wordt uitgelegd hoe je de ESP32 een webpagina kunt laten maken waarop je gegevens kunt plaatsen, en waar knoppen op staan die acties (zoals een relais aanzetten) kunnen starten.

Het boek ESP32 Uitgelegd is verkrijgbaar bij alle boekhandels, de webshop van mijn drukker en Bol. Maar ook bij elektronica winkels als Otronic en Brokx. Hieronder vindt je een paar links die je naar de pagina's brengen als je er op klikt.

  
 
 

Brokx Electronics


https://www.otronic.nl/nl/




Javascript tips.

Het boek Javascript tips is dit jaar uitgekomen en er zijn er al vele van verkocht. Het boek gaat ervan uit dat je basiskennis over het programmeren in Javascript hebt. Het is dus geen leerboek. 



Wat is het dan wel ?
Het is een boek met meer dan 500 tips die je helpen bij het programmeren in Javascript. Hier is een kleine greep uit wat er allemaal beschreven wordt.
- Hoe reken ik ook al weer uit hoe lang het duurt tot een bepaalde datum
- Hoe verwijder ik leestekens uit een string
- Hoe kopieer ik alle even getallen uit een array naar een nieuw array
- Hoe sorteer ik ook weer een array maar zo dat de volgorde onafhankelijk is van hoofdletters
- Hoe roep ik een functie aan terwijl ik niet weet hoeveel argumenten de functie heeft
- Hoe start ik een functie op vastgestelde tijden
- Hoe test ik of een bepaalde key in een objecvt aanwezig is
- Hoe verander ik teksten, buttons, kleuren etc op een html pagina
- Hoe verwijder ik elementen of voeg ik elementen toe aan een html pagina
- Hoe voorkom ik dan een gebruiker delen van een webpagina kan kopieren.
- Hoe voorkom ik dat een gebruiker een webpagina kan printen.

Het boek Javascript tips is verkrijgbaar bij alle boekhandels, de webschop van mijn drukker en Bol.


https://www.boekenbestellen.nl/


En via Bol


Beide boeken zijn overigens ook in het engels verkrijgbaar bij Amazon. Net zoals mijn engelstalige boeken over de Raspberry Pi Pico.

Klik hier voor de link naar Amazon

 

 

Fijne Sinterklaas

Luc Volders

Friday, November 17, 2023

Domoticz with MicroPython part 4

For an index to all my stories click this text.


This is the fourth story in a series about sending data to, and receiving data from Domoticz with MicroPython.

The reason why I am writing these is because I migrated to a new internet provider. That meant reprogramming all of my microcontrollers. They needed to contact my new router and got new IP addresses. It was a hell of a job.

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.

The first story describes connecting a pushbutton to a Raspberry Pi Pico and sending it's data to Domoticz. You can use this for a myriad of purposes like a doorcontact, a windowcontact, doorbell, PIR, vibration sensor, rain sensor etc. etc. Another purpose is using your Pico with a pushbutton as a remote control for your lamps. You can re-read that story here:
http://lucstechblog.blogspot.com/2023/11/domoticz-with-micropython-part-1.html

The second story describes how to build a switch in Domoticz that will set a led, attached to a microcontroller, on or off. You can re-read that story here:
http://lucstechblog.blogspot.com/2023/11/domoticz-with-micropython-part-2.html

The third story was about sending data to Domoticz again. The first story did that also but there I send just the ON or OFF state of a switch. The third story showed how to send values to Domoticz like the values of a temperature sensor, a light sensor etc. etc. You can read that story here:
http://lucstechblog.blogspot.com/2023/11/domoticz-with-micropython-part-3.html

This story covers how to send values from Domoticz to a microcontroller. This can be used to dim a lamp, dim a led strip, control a motor speed, control a servo position etc. etc. etc.

Breadboard setup

Actually it is the same setup from the second story. Just a Raspberry Pi Pico with a led.



Nothing special here. The led is connected to GPIO15 Through a current delimiting resistor of 220 Ohm.

Please note that you can also use an ESP8266 or an ESP32 in stead of the Pico. Just alter the pin number where the led is attached to in the program.

What we are going to do.

We are going to send values from a dimmer in Domoticz to the microcontroller. The value of the dimmer position determines how bright the led will glow.

Just like in the previous stories: this is just a demo. Replace the led with the proper electronics and you can use this for loads of purposes.

Creating a dimmer in Domoticz.



In Domoticz choose setup and then the Hardware entry.

In my case there was a Virtual 1 entry (which is my real mancave thermometer). If you do not have virtual entries you can call it whatever you like. But for the sake of this story call it Virtual 2 and choose as type Dummy.

Don't forget to press the Add button.



A new entry will become available in your hardware list with the name Virtual 2 which you gave it. Now click on Create Virtual Sensors.



A pop-up window opens where you can define your new virtual sensor. I gave it the name MicroPython-test-dimmer and choose as type Switch.

Press the OK button and Domoticz will tell you that the sensor can be found in the devices tab. But don't bother.



The just created switch is immediately visible in the Switches tab. As you can see I already had a lot of switches defined. The new switch looks like a lamp. But we want it to be a dimmer. Press on the edit button in the switch.



In the drop-down menu from the Switch Type choose dimmer.



The Icon changes in the picture of a small dimmed lamp. That is the default icon for a dimmer which is ok. You can choose another icon if you like.

Press the Save button and you are done for now.



And this is how your dimmer will look now in the Switches tab, or on your Dashboard.

Sending values over HTTP

The second article in this series showed how to send an ON or OFF command from Domoticz. That could be done in the switches settings with On Action and Off Action. You can read that here: http://lucstechblog.blogspot.com/2023/11/domoticz-with-micropython-part-2.html

However that method doesn't work for sending values. We need to take a different path for that.



Click on the Setup tab, then choose More Options and from there Data push and HTTP



In this window we need to change several parameters.



The first thing we can alter is Target Type. That is set as Global variable and DO NOT change this. A Global variable is a variable that can be used throughout all parts of Domoticz.

Next we need to choose the device we want to send data. From the Device Name drop-down menu therefore choose MicroPython-test-dimmer.



Set the value to send to: Level.

The other choise (Status) only sends on and off. We want to send a value.



The last thing to change in this Edit Link part of the screen is the Target Variable. I chose as name MP-Dimval. But feel free to choose any name you like.

Then press the Add button.
A pop-up screen arises that shows that the Http link is saved.



And as you can see at the top of the screen a new entry is shown.



Now let's have a look at the right side of this screen.
At the top it gives us all available data which we can send.

%v : Value
%t0 : Timestamp (epoc time localtime)
%t1 : Timestamp (epoc ms localtime)
%t2 : Timestamp (epoc time UTC)
%t3 : Timestamp (epoc ms UTC)
%t4 : Timestamp : "2015-01-29T21:50:44Z
%D : Target Device id
%V : Target Variable
%u : Unit
%n : Device name
%T0 : Type
%T1 : SubType
%h : hostname
%idx : 'Original device' id (idx)


As you can see there are many things we can send to our microcontroller.

In this demo we are going to make things easy and we are only going to send the level (value) of the dimmer.
For this we need to use %v in our HTTP command.

But first we now need to know where to send that command. And for that we need the IP address of the microcontroller.

MicroPython program to get IP address

This is just a small program to obtain the IP address that the microcontroller (Raspberry pi pico W) is going to get from the router.

import network

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)

Nothing unfamiliar here. We have used this part in our previous programs so look in the previous stories for a detailed description of this.



And this is what you will see in Thonny's shell.
Undoubtedly you will get a different IP address. But this is what we need.


Back to the HTTP screen.

Things are getting a bit complicated now.

In a real-life situation we are not going to send out just one command. We will be sending multiple HTTP commands to one microcontroller. But we will also be sending commands to several controllers. Therefore you need to choose wisely what command you are going to send to avoid confusion later on.

So the first part of our HTTP command is the IP number.
Next there is a / to separate the IP number from the actual command we are sending.
And then there is the command itself.

-dimval-%v-end

This is the command I used.
I start with -dimval- to show it is the dimmers value. Then %v which is the representation of the actual value that is going to be send. And I end with -end.
The %v is what we found we needed in the list at the top of this screen. %v is replaced by the real value by Domoticz.

http://192.168.178.193/-dimval-%v-end

So this is our complete HTTP command we are going to send. Please make sure to make no typo's in this as otherwise the program running on your Micro will not react.

You can make changes in this, however make sure to adjust the MicroPython program accordingly.



And here is how you should fill this in.

Leave the rest as is. We are going to send an HTTP Get command. Make sure Active is activated as the command will otherwise not be send.

Press Save.
Don't forget to press save otherwise your work was in vain.

The microPython dimmer program

The dimmer program is actually a webserver framework that does not produce a webpage but just uses the framework to receive data from an external source. In this case from Domoticz.

As usual I start with the complete program and will then look at some details.

import network
import socket
import time

from machine import Pin

led = machine.PWM(machine.Pin(15))
led.freq(1000)

ssid = "YOUR_ROUTER_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)

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

# Listen for connections
while True:        

        cl, addr = s.accept()
        print('client connected from', addr)
        request = cl.recv(1024)
        print(request)

        request = str(request)
        
        dimbeg = request.find('-dimval')
        dimend = request.find('-end')
        print("=================================")
        print(dimbeg)      
        print(dimend)
        start = (request[dimbeg:dimend])
        valdim = start.split("val-")[1]
        print(valdim)
        valdim = int(valdim)
        if (valdim <4):
            valdim = 0
            
        led = machine.PWM(machine.Pin(15))
        led.freq(1000)
        led.duty_u16(655*valdim)
        # response = html % stateis

        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')
        #cl.send(response)
        cl.close()

Let us look at some details.

import network
import socket
import time

from machine import Pin

led = machine.PWM(machine.Pin(15))
led.freq(1000)

The necessary libraries are imported which are all available with your standard MicroPython distribution.
Then GPIO15 is defined as a PWM pin. This makes it possible to dim the led.
The PWM frequency is set to 1000.

If you have any trouble with this part I recommend reading my book on the Raspberry Pi Pico W on which you will find a link at the bottom of this page.

In the next lines do not forget to replace YOUR_ROUTER_NAME and PASSWORD with your routers credentials.

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

A network socket is opened and the program starts listening for incoming commands on this socket.

while True:        
    # try:
        cl, addr = s.accept()
        print('client connected from', addr)
        request = cl.recv(1024)
        print(request)

In the loop the program waits till it receives some data. That received data is stored in the variable request.
For our convenience the received data is printed in Thonny's shell.



And this is what the received data looks like. What we now need to do is to distill the actual data we need.

        request = str(request)
        
        dimbeg = request.find('-dimval')
        dimend = request.find('-end')
        print("=================================")
        print(dimbeg)      
        print(dimend)

First we make a string from the received data.
Then we search for the first position of the word -dimval and the first position of the word -end.

b'GET /-dimval-46-end

This is the first thing we have received and the information we need.
As you can see -dimval is at the seventh position in this string and -end at the 17th position. Remember counting starts at 0.
And the program indeed printed 7 and 17 in the shell.

start = (request[dimbeg:dimend])

The text in request starting at dimbeg and ending at dimend is stored in the variable start.
The variable start therefore contains: -dimval-46

We have to split that string up to get the value (in this case 46).

        valdim = start.split("val-")[1]
        print(valdim)
        valdim = int(valdim)

Therefore -dimval-46 is split at the letters "val-" in a list (kind of array), with two values: ["-dimval-"] and ["46"]
We need the second element, which is [1]

For our convenience we print the value in the Shell so we can see that we got the real thing.
The received value is a string and the last command in this part converts that into an integer value.

        if (valdim <4):
            valdim = 0

The program tests if the received value is less then 4. If that is the case valdim is set to 0 which sets the led off.

        led = machine.PWM(machine.Pin(15))
        led.freq(1000)
        led.duty_u16(655*valdim)

And this is the part where we actually dim the led to the received value.
The lowest brightness is 5 and the highest 65536.
The dimmer in Domoticz has a scale of 1 to 100. So we have to divide 65536 by 100 to get the dimming steps.
Next we multiply that step value by valdim which is the received number.
And that is the dim level to set the led's duty cycle.

That's it.

If you have any problems with this MicroPython program I really recommend my book on the Raspberry Pi Pico W. In this book the the webserver and several other internet capabilities are explained netx to the use of several sensors and actuators. You can find a link to this book at the bottom of this page.

Using the dimmer.

Well the only thing you have to do is to move the slider. When you release your mouse button the led on the microcontroller will almost immediately get dimmed to the value you choose.

Real world applications.

Using a dimmer is more complicated than just adding a relay to your microcontroller. There are however battery fed lightstrips that can be used with this setup.
You'll need a transistor and a resistor for powering the strip. You can find an example here:

https://lucstechblog.blogspot.com/2016/05/esp8266-controlled-ledstrip.html

As stated before you can use the dimmer to set the position of a servo. So how about a Domoticz controlled tea-timer:

https://lucstechblog.blogspot.com/2018/08/teatimer.html

Till next time.
Have fun


Luc Volders