Friday, May 26, 2023

Controlling Domoticz with Telegram

For an index to all my stories click this text

Telegram keeps interesting me and the more I work with it the more possibilities I see.
The first story in this series showed how to install Telegram and make a bot to communicate with your ESP. You can re-read that story here. http://lucstechblog.blogspot.com/2023/01/telegram-messenger-for-iot.html

For testing purposes working on a big screen is more convenient. So the second story showed how to get Telegram working on your PC. Read that story here: http://lucstechblog.blogspot.com/2023/02/convenient-working-with-telegram.html

The third story showed how to send sensor data from your ESP to Telegram. Re-read that story here. http://lucstechblog.blogspot.com/2023/02/request-esp-sensor-data-with-telegram.html

The fourth story showed how to connect Octoprint to Telegram. That way you can get an alert with a picture if your 3D printer has finished. You can read that story here: http://lucstechblog.blogspot.com/2023/03/octoprint-notifications-to-telegram.html

The fifth story showed how your ESP could send alerts to Telegram. This way you can get an alert on your smart-phone or tablet whenever a temperature gets critical or a door is opened etc. Read that story here. http://lucstechblog.blogspot.com/2023/03/esp-sending-alert-to-telegram.html

There was just one thing I wanted and that is a connection between Telegram and my home automation system: Domoticz.

Domoticz to Telegram.

Actually Domoticz has a connection to Telegram.

In Domoticz you can set notifications.



Go to the last tab on your screen and choose settings (called Instellingen in Dutch).



In the settings menu choose Notifications. Here you can find the settings for all kind of notifiers. Just scroll all the way down.



There it is. Click on active (Actief) so it is checked and fill in your Telegram token which you got when you made your Telegram bot. If you not know how to get that token re-read the first story in this series.
http://lucstechblog.blogspot.com/2023/01/telegram-messenger-for-iot.html

next to the token you also need the Chat ID. You can get that with the IDBot. You can read how to get the Chat ID in this story http://lucstechblog.blogspot.com/2023/03/esp-sending-alert-to-telegram.html

To copy these two codes it is more convenient to work with Telegram on your PC's screen. Read how to do that here.
http://lucstechblog.blogspot.com/2023/02/convenient-working-with-telegram.html

Now press the TEST button to see if you filled in the right codes. If you filled in every thing right you would get a message on your phone in the Telegram app saying "Domoticz test message".

Now switch over to the Switches tab in Domoticz.



As you can see each switch has a Notifications button. Click on that button.



Here you can see that I activated a notification when the lamp is set ON and a notififaction when the lamp is put OFF. I also added my own text in the "Aangepast bericht" field so I would get a meaningfull text in Telegram.
In the list it shows that I only have a notification for Telegram.

That is all. Or isn't it..........

How about the other way round ???

I not only wanted to get a notification by Telegram when a lamp is switched on or off but I also wanted to set the lamp on or off with my Telegram bot. There is no solution from Domoticz for that.
We can send from Domoticz to Telegram but we can not send commands from Telegram to Domoticz.......

ESP to the rescue !!!

So I had this weird idea.

I could send commands from Telegram to my ESP, and I could send data from my ESP to Telegram. But I also can send commands from my ESP to Domoticz as these stories showed:

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

So why not combine these. The Idea is as folows:

Telegram sends to an ESP ==> The ESP sends to Domoticz.

Well this is a nasty solution, but as long as there is nothing else that can solve my problem this is the way it is done. I use the ESP as a bridge between Telegram and Domoticz.

ESP to Domoticz

A fast recap of the program that makes the ESP send commands to Domoticz.

First you need the ID of the switch you are going to set ON and OFF. Next you need to send a command do Domoticz that incorporates that ID.

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

As how to do this is a story in itself I am not going to tell you here. Just re-read the story in the following link and all will be clear.

Breadboard

For completeness I show you the breadboard setup here. It is the same setup as from the previous story which you can re-read here.



The button is attached with a 10K pull-up resistor to D14, The LDR with a 10K pull-doen resistor to pin D32 and the Dallas DS18b20with a 4.7 pull-up resistor to pin D23. Nothing we have not discussed before.

The ESP bridge program

What I am going to do is to combine the program from the Telegram to ESP stories with the ESP to Domoticz stories. All these are referenced above.

/*
Adapted by:               Luc Volders
                          http://lucstechblog.blogspot.com/
*/

#include <Arduino.h>
#include "AsyncTelegram.h"

//#include <WiFi.h>
#include <HTTPClient.h>

#include <DallasTemperature.h>
#include <OneWire.h>

# define ONE_WIRE_BUS 23
OneWire oneWire(ONE_WIRE_BUS);

AsyncTelegram myBot;

DallasTemperature dallas(&oneWire);

float temp = 0;
int button = 34;
String tosend = "";
String text;


const char* ssid = "ROUTERS NAME";
const char* pass = "PASSWORD";
const char* token = "TELEGRAM TOKEN";   

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

int led = LED_BUILTIN; 


void sendToDomoticz(String url)
{
  HTTPClient http;
  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();
}

void setup() 
{
  // initialize the Serial
  Serial.begin(115200);
  Serial.println("Starting TelegramBot...");

  WiFi.setAutoConnect(true);   
  WiFi.mode(WIFI_STA);
  
  WiFi.begin(ssid, pass);
  delay(500);
  while (WiFi.status() != WL_CONNECTED) 
  {
  Serial.print('.');
  delay(500);
  }

  myBot.setUpdateTime(1000);
  myBot.setTelegramToken(token);
  
  Serial.print("\nTest Telegram connection... ");
  myBot.begin() ? Serial.println("OK") : Serial.println("Not OK");

  dallas.begin();

  pinMode(led, OUTPUT);
  digitalWrite(led, LOW); //<== high for esp8266

  pinMode(14, INPUT);
}

void loop() 
{
TBMessage msg;

  if (myBot.getNewMessage(msg)) 
  {
    if (msg.text.equalsIgnoreCase("Lon")) 
    {      
      digitalWrite(led, HIGH); //<== low for esp8266                         
      myBot.sendMessage(msg, "Mancave light is now ON"); 
        String url = "/json.htm?type=command&param=switchlight&idx=";
        url += String(3);
        url += "&switchcmd=On";
        sendToDomoticz(url);     
    }
    
    else if (msg.text.equalsIgnoreCase("Loff")) 
    {       
      digitalWrite(led, LOW); //<== high for ESP8266                        
      myBot.sendMessage(msg, "Mancave light is now OFF");
        String url = "/json.htm?type=command&param=switchlight&idx=";
        url += String(3);
        url += "&switchcmd=Off";
        sendToDomoticz(url);    
    }
    
    else if (msg.text.equalsIgnoreCase("Temp")) 
    {       
      dallas.requestTemperatures();
      Serial.print("Temperature is: ");
      temp=dallas.getTempCByIndex(0); 
      Serial.println(String(temp)); 
      tosend = "The temperature is now : ";
      tosend += String(temp);                 
      myBot.sendMessage(msg, tosend);      
    }


    else if (msg.text.equalsIgnoreCase("LDR")) 
    {       
      Serial.print("The LDR's value is: ");
      Serial.println(analogRead(15)); 
      tosend = "The LDR's value is : ";
      tosend += String(analogRead(32));                 
      myBot.sendMessage(msg, tosend);      
    }

    else 
    {                                                  
      // generate the message for the sender
      String reply;
      reply = "Welcome ESP8266 number 1\n" ;
      reply += "You can use these commands :\n" ;
      reply += "============================\n\n" ;
      reply += ". Mancave Light on = Lon\n";
      reply += ". Mancave Light off = Loff\n\n";
      reply += ". What is the temperature = Temp\n";
      reply += ". The light value = LDR\n";
      myBot.sendMessage(msg, reply);
    }
  }
    if ((digitalRead(14))==0)
    {
    Serial.println("Button PRESSED");
    
    HTTPClient http;
    text = "https://api.telegram.org";
    text += "/bot";
    text += "TELEGRAM TOKEN"
    text += "/sendMessage?chat_id=CHATID";
    text += "&text=Button pressed";
    http.begin(text);
    int httpCode = http.GET(); //Send the request
    if (httpCode > 0) 
      { //Check the returning code
      String payload = http.getString(); //Get the request response payload
      Serial.println(payload); //Print the response payload
      }
      http.end(); //Close connection
    }
}



As this really is a mix of previous stories I will not go into details here although there is one important thing I will show you. And beware that this is the ESP32 version. Adapting for the ESP8266 should be easy if you followed the previous stories in this series.

#include <Arduino.h>
#include "AsyncTelegram.h"

#include <HTTPClient.h>


The two libraries AsyncTelegram.h and HTTPClient.h are not exactly friends of eachother. So to make sure they do not byte eachother and crash the ESP32 we only call the HTTPClient.h library when it is needed.


void sendToDomoticz(String url)
{
  HTTPClient http;
  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 place where this happens. Here is the HTTPClient library activated and at the end de-activated with http.end(). The text this routine is sending is build in the loop.

In the loop() we had the Lon and Loff routines that in the original program activated the build-in led of the ESP. This is now changed into building the strings that are needed to set the lamp in Domoticz On or Off.

At the end of the loop there is another HTTPClient(0 routine that sends an alert to Telegram when the button is pressed.

Telegram Bot

And here you can see what will happen in the Telegram Bot screen.



Here is the start menu again. We discussed all the features in the previous entries in this series so let's concentrate on the Lon (Lamp ON) and Loff (Lamp Off) menu entries.



If you type the command Lon in the Telegram bot the build-in led of the ESP will go on, the command to set the lamp on will be send to Domoticz, Telegram will mention that the mancave light is set On and Domoticz confirms that by sending "De lamp gaat aan" to Telegram.



And the lamp in Domoticz goes On.



Typing the command Loff will do the reverse. The build-in lamp will go off, a command to set the lamp off is send to Domoticz, Telegram will mention that the mancave light has been set Off and Domoticz sends a confirmation saying "De lamp gaat uit"



Goal Achieved !!!
Now I can control my whole home automation from Telegram.

Til next time
Have fun

Luc Volders












Friday, May 19, 2023

Delft Maker Fair 2023

For an index to all my stories click this text

Last weekend I attended the Maker Fair at the Technical University campus in Delft.

For those who are not familiar with the term Maker Fair, a small explanation.
There is a maqgazine called Make. The Magzine covers all kinds of technical fields. Not only electronics but also mechanics, robotics, aerospace etc. etc. etc.
And worldwide they encourage people to organise technical fairs.
And so each year there are some fairs in the Netherlands. I have attended (during several years) Maker Fairs in the city's of Groningen, Eindhoven and now Delft.

Each time I am pleasantly surprised about the creativity and ingenuity that are on display.



There were several stands with robots. There was a Boston Dynamics robot dog look alike. Only smaller. There were robots playing football. A robot that did acrobatics (with the aid of a human) etc.



Many 3D printers were on display but also people who made add-ons. One was printing Delft Blue Tiles. You could have a line drawing of yourself printed. But something really impressive as an add-on for most commercial available printers (Creality etc) that can print ceramics and wood-pulp etc. It worked with a syringe. Open source of course.



Delft University was showing a hydrogin powered boat with which they were participating in the world-wide racing contest in Monaco this year.



And of course there were several work-shops where you could learn the basics of programming and interfacing Arduino's. Raspberry's and Raspberry Pico's.

The weather was great, not a cloud in sight. So a lot was done outdoors.

It is great to see how much fun people have in building all kinds of projects. And it is great to see how kids get interested in technology this way.

If there ever is a Maker Fair in your neighbourhood, do attend it. It is really fun.

Till next time
Have fun


Luc Volders





Friday, May 5, 2023

Javascript tips (Nederlandse versie) is nu verkrijgbaar

For an index to all my stories click this text

This text is in Dutch as it is a shameless plug for my latest book called Javascript tips. You'll have to wait a few weeks as the English version is now getting edited.



Javascript is waarschijnlijk de meest gebruikte programmeertaal ter wereld. Je gebruikt het zelf ongetwijfeld elke dag aangezien Javascript in elke web-browser (Chrome, Edge, Firefox, Opera etc.) is ingebouwd. Zo vlug als je op een webpagina op een knop drukt, of een veld invoert komt Javascript om de hoek kijken.

Maar buiten dat wordt Javascript ook steeds belangrijker bij het weken met ESP8266, ESP32 en Raspberry Pi Pico W microcontrollers. Kijk maar eens naar mijn artikel dat een webpagina bouwt met daarop een knop en een indicator die de status van een led aangeeft:

http://lucstechblog.blogspot.com/2019/08/esp-webserver-tutorial-part-3-button.html


Of het artikel over het maken van een webpagina met lijndiagrammen en gauges:

http://lucstechblog.blogspot.com/2019/11/gauges-and-linecharts-in-javascript.html

En er komen nog een aantal artikelen over Javascript op dit weblog. Je kunt nl met Javascript ook heel eenvoudig app's voor je telefoon maken !!!

Bij het schrijven van deze artikelen en het ontwikkelen van appjes voor mijn telefoon liep ik steeds tegen kleine probleempjes aan die met enkele regels programmeren op te lossen waren. Die heb ik verzameld en zo ontstond mijn nieuwe boek.

Dit boek is niet bedoeld om je te leren hoe je in Javascript kunt programmeren. Het biedt echter wel oplossingen voor een breed scala aan kleine problemen die zich vaak voordoen tijdens het programmeren in Javascript. Met meer dan 500 tips in het boek, kun je snel en gemakkelijk oplossingen vinden voor problemen zoals het sorteren van een array op basis van de lengte van de elementen, het berekenen van de datum over 10 dagen, het toevoegen van een rij aan een HTML-tabel en het bepalen van welke dag 1 januari 1999 was.

Voor al deze veelvoorkomende problemen biedt het boek duidelijke oplossingen die worden gedemonstreerd met voorbeelden en vaak met uitleg over hoe je tot de oplossing kunt komen. Als er meerdere oplossingen zijn voor hetzelfde probleem, worden deze getoond, zodat je de methode kunt kiezen die het beste bij je eigen programmeerstijl past.

Of je nu een beginnende of gevorderde Javascript programmeur bent, dit boek kan je tijdens je programmeersessies helpen bij het vinden van oplossingen voor kleine problemen.

De meer dan 500 tips staan op maar liefst 400 pagina's en de tips zijn onderverdeeld per onderwerp:

Getallen
String
Datum
Array
Functie
Communicatie
Objects
Diverse
Html


Het ISBN nummer is: 9789464811285

Javascript tips is nu verkrijgbaar via:

https://www.boekenbestellen.nl/boek/javascript-tips/58638

UPDATE:

NU ook verkrijgbaar bij Bol, Bruna en andere boekhandels:

https://www.bruna.nl/boeken/javascript-tips-9789464811285

https://www.bol.com/nl/nl/p/javascript-tips/9300000150210654/?bltgh=hWZSkDHpvoAq9kfTRIBHFQ.2_13.16.ProductImage

Veel plezier bij het programmeren.

Luc Volders