What does? Show the code you used to print the length. By the way, I just tested, and it is ok to compare 2 strings with “==”. That’s my Objective-C background showing.
My mistake, I changed the code back to “input += (char) Serial1.read();” instead of “input = Serial1.readStringUntil(’\n’);”. I was testing something out.
I get back the number 14 each time, one must be the line return maybe. Same code as I last posted in full but changed the “Serial.println(input);” to “Serial.println(input.length());” like you suggested.
Nano:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("IFTTT-HUE-OFF");
delay(1000);
}
Photon:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
String input = "";
if (Serial1.available() > 0){
input = Serial1.readStringUntil('\n');
delay(5);
Serial.println(input.length());
}
if (input == "A") {
Serial.println("Hue On Sent");
Particle.publish("Hue", "ON");
}
if (input == "IFTTT-HUE-OFF") {
Serial.println("Hue Off Sent");
Particle.publish("Hue", "OFF");
}
}
It seems that something is getting added to the string you’re sending. To see what it is, try this,
if (Serial1.available() > 0){
input = Serial1.readStringUntil('\n');
delay(5);
input.getBytes(buff, 14);
for (int i = 0; i<14; i++) {
Serial.printf("%d ", buff[i]);
}
At the top of your file put the following,
byte buff[14];
Send “IFTTT-HUE-OFF”, and report what gets printed out
Ok, after testing on the Photon, I think your problem is with using Serial.println when sending the data from the Nano. You should be using Serial.write. Change the sending code to this,
void loop() {
Serial.write("IFTTT-HUE-OFF#");
delay(1000);
}
And then replace ‘\n’ with ‘#’ as the argument to readStringUntil in your Photon.
This seemed to have worked as a work around. if anyone else knows of a full working code without having to use the “#” please let me know
The thing you need to be aware here is that when you send your data with Serial.println()
your will not only transmit the string you see, but also the CR (0x0D) and LF (0x0A) characters.
So if you test for equality with "IFTTT-HUE-OFF"
but have read with Serial1.readStringUntil('\n');
you will get "IFTTT-HUE-OFF\r"
.
Consequently a check if (input == "IFTTT-HUE-OFF")
will fail, but a check if (input.startsWith("IFTTT-HUE-OFF"))
should succeede.
But such code will always be susceptible to timing issues and buffer artefacts, that’s why you should use some kind of protocol to mark the beginning and end of a transmission and possibly also check the integrity of the data in between.
Thank-you for this, this is very useful. I shall try it out!