Page 1 of 1

json Text sensor

Posted: Friday 10 November 2023 22:16
by DomoticaRob
In Domoticz I have made a Text sensor and in a ESP8266 I have a function that must update the text.

In the Wiki it says:
/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEXT

IDX = id of your device (This number can be found in the devices tab in the column "IDX")
TEXT = Text you want to display

If I place the next code in my ESP8266 it works:

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Relais%201%20is%20uit.";
              http.setAuthorization("My InlogName for Domoticz", "My PassWord for Domoticz");
              sendDomoticz(url);    
Now I want to make a long text out of different variables. String variables, Integer variables, Long variables etc.

For instance:

Variable1 = "Het weer is"
Variable2 = "super"
Variable3 = "."
Variable4 = "De temperatuur ="
Variable5 = 21,5
Variable6 = "'C"
Variable7 = "en de luchtvochtigheid ="
Variable8 = 65,40
Variable9 = %

I want in my Text sensor the text shown as:

Het weer is super.
De temperatuur = 21,5 'C en de luchtvochtigheid = 65,40%.



How do I do that?

In dzVents I can do it but I want to do it from my ESP8266 module.

Re: json Text sensor

Posted: Friday 10 November 2023 22:50
by waltervl
Check the documentation of your esp8266 programming language how to create strings of multiple values.

Re: json Text sensor

Posted: Saturday 11 November 2023 13:29
by DomoticaRob
I do programm the ESP8266 with the Ardiuino IDE.

So, I think it is C?

And in the C documentation I get different solutions.

When I use these I get error messages as:

Compilation error: cannot convert 'String' to 'const char*' in assignment

It would be helpfull if I get an example of the Domoticz json syntaxt wit a variable for the TEXT part at the end of the line.

Re: json Text sensor

Posted: Saturday 11 November 2023 14:19
by heggink
Not sure I fully understand but let's try anyway: normally, you need to use some urlencode function to convert special characters. '\n' is a newline which you should be able to insert in the text string. You can also do things manually but then need to know how to translate. A space character would be %20 and so on...

Re: json Text sensor

Posted: Saturday 11 November 2023 14:56
by Jieffe
Have you tried converting your "String" to a "const char*" ?

Code: Select all

String str = "My_String";
const char* c = str.c_str();

Re: json Text sensor

Posted: Saturday 11 November 2023 22:58
by DomoticaRob
Hello Jieffe,

I do understand this.

But how do I put this const char* c into the TEXT part of the json syntax?

url = "/json.htm?type=command&param=udevice&idx=IDX&nvalue=0&svalue=TEXT";

If I do the next line:

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=c";

I get:

json.jpg
json.jpg (49 KiB) Viewed 1576 times

Re: json Text sensor

Posted: Sunday 12 November 2023 8:12
by BartSr
I would post your question on the arduino forum.

Re: json Text sensor

Posted: Sunday 12 November 2023 8:51
by Kedi
Just use VariableResult = Variable1+"%20"+Variable2+"%20"; etc....
And add the VariableResult to the end of the url string like

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue="+VariableResult;

Re: json Text sensor

Posted: Sunday 12 November 2023 10:39
by Jieffe
I would construct the whole URL as a String and then convert it as a const char*

Something like that :

Code: Select all

int IDX_variable = 83;
int NVALUE_variable = 0;
float TEMP_variable = 20.50;
float HUMD_variable = 60;

String tmp = "/json.htm?type=command&param=udevice&idx=";  

tmp += IDX_variable; 
tmp += "&nvalue="; 
tmp += NVALUE_variable;
tmp += "&svalue='Het weer is super. De temperatuur = ";
tmp += TEMP_variable;
tmp += "°C en de luchtvochtigheid = ";
tmp += HUMD_variable;
tmp += " %'";

const char* url = tmp.c_str();
The result is : /json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Het weer is super. De temperatuur = 20.50 °C en de luchtvochtigheid = 60.00 %'

Re: json Text sensor

Posted: Sunday 12 November 2023 11:30
by DomoticaRob
A thousand apologies.
Sorry Sorry Sorry.


I had defined the 'url' as const char* instead of a string.

Now 'url' is a string and it works.


Now placing a new line in the text.

I use "\n".

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Voorbeeldtekst\nRegel2\nRegel3";
This produces a new line with Serial.print(url); of the Arduino edi (monitor), which is good.
But this also splits my URL string and gives me a httpCode = : 400 after a URL request. This should be httpCode = : 200.

Code: Select all

connecting to 192.168.178.200
poort 8080
Requesting URL: /json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue=Voorbeeldtekst
Regel2
Regel3
httpCode = : 400
So I am almost at the ponit where I want to be. Only new lines in my Domoticz Text Device and not in my json string.

Re: json Text sensor

Posted: Sunday 12 November 2023 11:49
by Kedi
Try using %0A for newline and %0D for carriage return.

Re: json Text sensor

Posted: Sunday 12 November 2023 12:00
by Jieffe
Try enclosing the text you want to show in Domoticz with single quotes, i.e :

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Voorbeeldtekst\nRegel2\nRegel3'";

Re: json Text sensor

Posted: Sunday 12 November 2023 13:58
by DomoticaRob

Code: Select all

url = "/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue='Voorbeeldtekst\nRegel2\nRegel3'";
Unfortanataly this is not working.

Thanks for the tip.

=================================================================================

Try using %0A for newline and %0D for carriage return.

This is working.

Super!


Muchas gracias everybody for all thoughts.