thinger-io / Arduino-Library

IOTMP Arduino Library for connecting devices to thinger.io #IoT
https://thinger.io
MIT License
114 stars 66 forks source link

Error when using String type #4

Closed dhunink closed 7 years ago

dhunink commented 7 years ago

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:

float yaw = 1;

void setup(){
  thing["yaw"] >> [](pson& out){
      out = yaw;
  };
}

But as soon as yaw is defined as a string, a compilation error occurs

String yaw = "test";//Or String yaw = "12:45";

void setup(){
  thing["yaw"] >> [](pson& out){
      out = yaw;
  };
}

On compiling, the following error occurs:

libraries/thinger.io/src/thinger/pson.h:292:27: error: invalid conversion from 'int' to 'const char*' [-fpermissive]
             }else if(value==1) {

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!

alvarolb commented 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.

alvarolb commented 7 years ago

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

dhunink commented 7 years ago

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) { ^

alvarolb commented 7 years ago

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());
  };
dhunink commented 7 years ago

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: schermafbeelding 2016-10-24 om 21 12 02

With a big thanks to @alvarolb for a quick and excellent update!

alvarolb commented 7 years ago

Great @dhunink ! Good to know it works! Thanks! 👍