Closed dhunink closed 7 years ago
Hi! this is not actually a bug. The pson
format does not support setting directly a String
, or converting a value to String
. This class is not a standard class (it applies only to Arduino environment), meanwhile the pson format must be portable to different environments.
You can set the string value passing a standard const char*, that can be obtained directly from the string class, something like:
String yaw = "test";//Or String yaw = "12:45";
void setup(){
thing["yaw"] >> [](pson& out){
out = yaw.c_str();
};
}
Hope it helps!
Anyway, I will try to add this conversions to the Arduino library, to simplify the integration.
Hi again @dhunink ! The latest Library version (2.5.1) Now supports working directly with Strings. Check the release notes here:
https://github.com/thinger-io/Arduino-Library/releases/tag/2.5.1
You will be able to update this library in a few hours through the Arduino IDE
WOW, that's a blazing fast update, thanks so much @alvarolb!
Just for reference I run a quick test. Following is working excellent:
thing["time"] >> [](pson& out)
{
String t = String(hour())+":"+String(minute());
out = t;
};
Directly using like this results in an error tough:
thing["time"] >> [](pson& out)
{
out = String(hour())+":"+String(minute());
};
Error: thinger.io/src/thinger/pson.h:292:27: error: invalid conversion from 'int' to 'const char*' [-fpermissive] }else if(value==1) { ^
Hi, not sure I can fix that, as the String class can be converted to different data types, and the pson type also allows setting different data types, so the compiler does not know which conversion is the correct one. You can force the assignment by casting the whole result as a String, like the following example:
thing["rand"] >> [](pson& out){
out = (String) (String(rand()) + ":" + rand());
};
I have tested also with casting to a String reference, and it seems to work, and in theory, it should be even better.
thing["rand"] >> [](pson& out){
out = (String&) (String(rand()) + ":" + rand());
};
You're so right, it's defintly better!
For further reference for anyone reading this issue, the following code works like a charm:
thing["time"] >> [](pson& out)
{
out = (String&) (String(hour()) + ":" + minute());
};
Result in the dashboard:
With a big thanks to @alvarolb for a quick and excellent update!
Great @dhunink ! Good to know it works! Thanks! 👍
After some hours of struggling to return a String (actually the current Time as HH:MM format, to be used in the text element on the dashboard) I believe I found a bug.
From the docs, the following statement works:
But as soon as yaw is defined as a string, a compilation error occurs
On compiling, the following error occurs:
Am I using this wrong or is this an actual bug? Any thoughts on returning the current time as a human readable text-line are highly appreciated!