0

I have the below code to read a light sensor, convert to lux, concat with "lux." and send it to my SmartThings cloud. Ultimately I want a value sent to SmartThings formatted like lux.110

void checkLux() { 
  float logLux = analogRead(lightPIN) * logRange / rawRange;
  int luxValue = pow(10, logLux); 
  String statusUpdate = "lux." + luxValue; 
  Serial.println(statusUpdate);
  smartthing.send(statusUpdate); 
  delay(1000);
}

This above code spits out some weird combination of characters to the serial monitor and doesnt print lux. or the luxvalue. If I add this line String luxString = "lux."; and modify the line below, it all works great. Any thoughts on why I need to declare this string separately. According to the documentation either should work fine.

Also if there are any suggestions on variable savings within this block of code. I am not that great at it yet.

2
  • You can use concat function. statusUpdate.concat(luxValue) Doesnt that solve your problem? Commented Oct 26, 2014 at 19:57
  • @ahaltindis That's an answer, put it in the answers section. Commented Oct 26, 2014 at 23:18

1 Answer 1

3

As Arduino just uses C++ most C++ functions will also work so dont just limit yourself to Arduino's reference pages.
Apparently the String constructor doesn't support numbers, you must convert them using the String() function first as seen here.
Alternatively I think you can append a string like this:

String statusUpdate = "lux.";
statusUpdate += luxValue;

as seen here,
which is the same as using String's concat function.

statusUpdate.concat(luxValue);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.