Page 1 of 1

how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Sunday 28 July 2024 0:57
by reza
Hi guys
i read this wiki page (https://www.domoticz.com/wiki/ESP8266_WiFi_module) and i want know how put the domoticz username and password for login in this code ?

Code: Select all

#include <Wire.h>
#include <SparkFun_Si7021_Breakout_Library.h> //https://github.com/sparkfun/Si7021_Breakout/blob/master/Libraries/Arduino/Si7021/src/SparkFun_Si7021_Breakout_Library.h
#include <ESP8266WiFi.h>

// int status = WL_IDLE_STATUS;     // the Wifi radio's status
WiFiClient client;

// domoticz
 const char * domoticz_server = "192.168.1.10"; //Domoticz port
 int port = 8080; //Domoticz port
 int idx = 5; //IDX for this virtual sensor, found in Setup -> Devices

 float humidity = 0;
 float temp = 0;
  
 // Create Instance of HTU21D or SI7021 temp and humidity sensor and MPL3115A2 barrometric sensor
 Weather sensor;
  
 void setup()
 {
     // Initialize the I2C sensors and ping them
     sensor.begin();
 }

 void loop()
 {
    // Get readings from all sensors
    getWeather();
    printInfo();
    delay(60000); // Wait 60 seconds
 }

 void getWeather()
 {
    // Measure Relative Humidity from the HTU21D or Si7021
    humidity = sensor.getRH();
    // Measure Temperature from the HTU21D or Si7021
    temp = sensor.getTemp();
    // Temperature is measured every time RH is requested. It is faster, therefore, to read it from previous RH measurement with getTemp() instead with readTemp()
 }
   
 void printInfo()
 {
    // Domoticz format /json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT

    if (client.connect(domoticz_server,port)) {

        client.print("GET /json.htm?type=command&param=udevice&idx=");
        client.print(idx);
        client.print("&nvalue=0&svalue=");
        client.print(temp);
        client.print(";");
        client.print(humidity);
        client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
        
        client.stop();
     }
  }

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Sunday 28 July 2024 8:02
by Kedi

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Monday 29 July 2024 0:07
by reza
Kedi wrote: Sunday 28 July 2024 8:02 It is all here: https://www.domoticz.com/wiki/Security
thank you for answer
i see it and i know about username and password and the format "https://username:password@IP:PORT/json.htm..."
but i dont know how set this format to a arduino code. where put user and password in the code with what command ?
for example this code is true?

Code: Select all

        
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(username);
        client.print(":");
        client.print(password);
        client.print("@");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Monday 29 July 2024 8:06
by Kedi
I don't think that will work, I think it should go in the authentication header.
I always post my data to a MQTT server like mosquitto.
This way I can use the posted data multiple ways and all arduino ESP8266 and ESP32 are centralized on the MQTT server.
This is a good guide https://randomnerdtutorials.com/esp8266 ... t-arduino/
Instead of the Node-Red example you could use the domoticz settings

And I always post 'raw' data to MQTT and process it with Node-Red towards Domoticz, this way I can add data like uptime, LWT and RSSI (and others) to the message and can process or view those.

And if you want to use client.println then

Code: Select all

client.println("Authorization: Basic YWRtaW46YXJkdWlubwo=");
The characters after "Basic" in that line are the base64 encoded username and password in the format "username:password".

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Monday 29 July 2024 14:04
by reza
Kedi wrote: Monday 29 July 2024 8:06 And if you want to use client.println then

Code: Select all

client.println("Authorization: Basic YWRtaW46YXJkdWlubwo=");
The characters after "Basic" in that line are the base64 encoded username and password in the format "username:password".
thank you. Where is my authorization?

This is true?

Code: Select all

client.println("Authorization: Basic YWRtaW46YXJkdWlubwo=username:password");

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Monday 29 July 2024 15:07
by Kedi
Go to this webiste: https://www.base64encode.org/
put username and password in the form of username:password in the first box
Then click encode
Copy the result in place of the YWRtaW46YXJkdWlubwo=

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Monday 29 July 2024 23:28
by reza
Kedi wrote: Monday 29 July 2024 15:07 Go to this webiste: https://www.base64encode.org/
put username and password in the form of username:password in the first box
Then click encode
Copy the result in place of the YWRtaW46YXJkdWlubwo=
Oh thank you . But more one qustion.
Now this line... put where this code ? After which line?

Code: Select all

    
    if (client.connect(domoticz_server,port)) {
        client.print("GET/json.htmtype=command&param=udevce&idx=");
        client.print(idx);
        client.print("&nvalue=0&svalue=");
        client.print(temp);
        client.print(";");
        client.print(humidity);
        client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
        
        client.stop();
     }
  }
  

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Tuesday 30 July 2024 9:24
by jvdz
Have a look in this function for the espcam project we made:
https://github.com/Hoeby/ESP32-Doorbell ... z.cpp#L167

It shows how to add the header with the authentication info.

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Tuesday 30 July 2024 10:28
by Kedi
reza wrote: Monday 29 July 2024 23:28
Kedi wrote: Monday 29 July 2024 15:07 Go to this webiste: https://www.base64encode.org/
put username and password in the form of username:password in the first box
Then click encode
Copy the result in place of the YWRtaW46YXJkdWlubwo=
Oh thank you . But more one qustion.
Now this line... put where this code ? After which line?

Code: Select all

    
    if (client.connect(domoticz_server,port)) {
        client.print("GET/json.htmtype=command&param=udevce&idx=");
        client.print(idx);
        client.print("&nvalue=0&svalue=");
        client.print(temp);
        client.print(";");
        client.print(humidity);
        client.print(";0"); //Value for HUM_STAT. Can be one of: 0=Normal, 1=Comfortable, 2=Dry, 3=Wet
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print(domoticz_server);
        client.print(":");
        client.println(port);
        client.println("User-Agent: Arduino-ethernet");
        client.println("Connection: close");
        client.println();
        
        client.stop();
     }
  }
  
Before or after the User-Agent line, because it is header information.

@jvdz Your link does not work. This should be the same: https://github.com/Hoeby/ESP32-Doorbell ... moticz.cpp
Base64 coding is here done in the source.

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Tuesday 30 July 2024 11:09
by jvdz
Kedi wrote: Tuesday 30 July 2024 10:28 @jvdz Your link does not work.
Ah right... That repo is in private mode.. fixed the link to the public version.

Re: how use domoticz username and password in esp8266 wifi module use arduino IDE?

Posted: Wednesday 31 July 2024 2:25
by reza
Before or after the User-Agent line, because it is header information
Thank you dear 🙏