Cannot declare strings?

I must be missing something very basic here ?
Using Particle Build, whenever I try and declare a String it does not “highlight” the word String ? Or string ?
And I get an error when I verify.

I can declare integers just fine, int is highlighted, but not Int - which is fine no caps
Also I can declare char just fine, char is automatically highlighted

Looking at Strings in Particle Reference docs: It looks just like Arduino IDE, which works fine.

What am I missing.
thanks much

String, with a capital “S” should work, but it does not get highlighted, even though it shows it blue in the docs. Are you trying this in a .ino file? If not, then you should include “application.h” at the top of your file. What error are you getting?

Yes, writing .ino in Build

I added application.h and the string now appears to be defined.
Strange, I then took application.h out and it still works (like Build remembered it)

Still one problem,

The code below works fine (reads from serial 2 and outputs to serial 0)

#include "Serial2/Serial2.h"
#include "application.h"

String myString = "test string";

// EXAMPLE USAGE
void setup()
{
  Serial.begin(9600);
  Serial2.begin(9600);

}

void loop()
{
  // read from port 2, send to port 0:
  if (Serial2.available())
   { 
     int inByte = Serial2.read();
     int a = inByte;
     Serial.write(a);
     // Serial.write(myString);
          
      }
}

However, if I add the Serial.write(myString); which was commented out I get the error below

serialtest.cpp:22:27: error: no matching function for call to 'USBSerial::write(String&)'
   if (Serial2.available())
                           ^

What hardware (Core or Photon) and what firmware version are you targeting?

Serial.print(myString) should always work, but with a current version Serial.write(myString) should work too - alternatively you could try Serial.write((const char*)myString) or Serial.write(myString.c_str()) too.

1 Like

Core hardware, Serial.print(myString) WORKS…

So looks like a version problem to me…

Thanks loads ALL, back on track…

Randy

1 Like

Hi - I’m having a similar problem. Can’t seem to declare String, IDE does not turn the writing blue. And particle.publish() using my sting will not send back an event.

Does anyone have any more thoughts on this?

Cheers.

Yes

Also

1 Like

Have made sure I have capital ‘S’

I am using an .ino file but tried putting application.h at top anyway - code compiled but did not make any difference.

I’m using an Electron - latest version.

There is no error produced - it just doesn’t seem to recognise ‘String’ declaration…

Then I'm confused, if there's no error, everything should be running. How are you verifying it's not working?

Would you mind giving us a share link from the web IDE so we can have a look at your code, or paste it here?

//#include “application.h”

int pin2 = D2;
int pin7 = D7;
int switchState = 0;

//unsigned long openMillis;
//unsigned long closedMillis;
//unsigned long closedTime;
int openMillis;
int closedMillis;
int closedTime;
int cond;
int Count = 0;

void setup() {
pinMode(pin2,INPUT);
pinMode(pin7,OUTPUT);
//Serial.begin(9600);
}

void loop() {

switchState = digitalRead(pin2);

if (switchState == LOW) {
    openMillis = millis(); 
    digitalWrite(pin7,HIGH);

    if (cond == 1) {
    
    Count = Count+1;
    
    String data = String(Count) + "," + String(closedTime);
    Particle.publish("Time",data);
    
    
    cond = 0;
    }
    
    else{
    // do nothing
    }

    }
    
else{
    digitalWrite(pin7,LOW);
    closedMillis = millis();
    closedTime = closedMillis - openMillis;
    cond = 1;
    
}
delay(1000);
}

In the IDE String is not being declared / highlighted Blue. Code just take a push of a button into Pin3. Logic turns off LED on Pin7. When button is released some data will be published, num times pressed, how long.

As an aside I’m also trying to publish lots of data in one event. Particle.publish() only seems to handle Stings. Welcome any advice on publishing arrays of data.

Well, hence this:

the fact that it's not lighting up blue doesn't mean it's not working. It's just that the syntax highlighting of the Web IDE shows it as being something 'special'.

But other than that, I'm not quite seeing how you determine how it's not working? If it compiles and flashes, it ought to work, save for logical errors.

What doe you expect it to do, and what doesn't it do? And what does it do?

2 Likes

Just sat down to re-try this bit of code. Seems to be working now. Thanks for your reply!