I am reading a serial value using port Serial1. The basics of the code is that there is a device connected to using Serial1. I send it a signal to it and it responds with a string. I want to read the string and publish it.
My code is below and I have been struggling for a few days now. I am getting a NULL in my device console. Any ideas would be of great help.
String PH;
String ist;
String sensorstring = "";
String inputstring = "";
float value;
void setup() {
Serial1.begin(9600);
Spark.variable("PH", PH);
inputstring.reserve(10);
}
void loop() {
Serial1.print(inputstring); //Command to get value
Serial1.print('\r'); //Command to get value
if (Serial1.available() > 0) { //Device responded
char inchar = (char)Serial1.read(); //get the char we just received
sensorstring += inchar; //add the char to the var called sensorstring
if (inchar == '\r') { //if the incoming character is a <CR> end of message
value = sensorstring.toFloat(); //Random attempt
PH = String(sensorstring); //Name input string PH
Particle.publish("PH", sensorstring); //Publish
}
}
delay(1500);
}
The main point here is that you are only reading one byte every 1.5sec.
You should read all the data youâve got available.
Something like this
while (Serial1.available() > 0) { //Device responded
char inchar = (char)Serial1.read(); //get the char we just received
if (inchar == '\r') { //if the incoming character is a <CR> end of message
value = sensorstring.toFloat(); //Random attempt
PH = String(sensorstring); //Name input string PH
Particle.publish("PH", sensorstring); //Publish
sensorstring = "";
}
else {
sensorstring += inchar; //add the char to the var called sensorstring
}
}
BTW: Is inputstring supposed to be an empty string?
1 - The initial string is empty. The manufacturer sent me code that is used on arduinos. I am modifying it to be used with the photon. It also left me a little confused.
2 - Maybe I miss understood the while clause in C/C++, I am more use to other programming languages. While youâre inside the âwhile clauseâ doesnât the code loop from the while to the bracket and not all the way around?
I tried it out the code and still got Nulls. In addition, I have copied my code and put it into an Arduino Uno and ran it. Only difference was that I had to add a few lines of code to use pins 2 and 3 as my âSerial1â.
Thank for the idea. Unfortunately, I do not have access to transmitting device and can not force it to send an X. I will try doing this with an Arduino. Basically, I send the Arduino a string using the command prompt, Arduino then sends it via a second Serial port.
Wouldnât it be possible to transmit the string via the photon usb to computer? Photon Serial document shows that you can use the USB as a serial but I have never gotten it to work. If you know any good tutorial that could help me set up the USB serial let me know. I have checked in the past with no luck.
everything that's enclosed be the matching pair of curly braces { ... } will be executed over and over as long (while) the condition (enclosed by the parentheses ()) is true.
So as long there are unread bytes in the buffer, all that byte reading and checking will be done over and over.
You could add some debug statements to see what you do get from the sender
while (Serial1.available() > 0) { //Device responded
char inchar = (char)Serial1.read(); //get the char we just received
Serial.write(inchar);
if (inchar == '\r') { //if the incoming character is a <CR> end of message
Serial.println("\n\rFound a CR!");
value = sensorstring.toFloat(); //Random attempt
PH = String(sensorstring); //Name input string PH
Particle.publish("PH", sensorstring); //Publish
sensorstring = "";
}
else {
sensorstring += inchar; //add the char to the var called sensorstring
}
}
Serial.printlnf("\n\rReceived string so far: '%s'", (const char*)sensorstring);
This way you can check via a serial terminal program and the USB serial interface of the device, what raw data you receive
Thank for the fast response. For the serial terminal program, where can I find this program? Or can I use the Arduino built in serial communication port.
Photon Serial document shows that you can use the USB as a serial but I have never gotten it to work. If you know any good tutorial that could help me set up the USB serial let me know. I have checked in the past with no luck.
I got it to work. Iâm not entirely sure what exactly changed. I spoke with the manufacturers they suggested instead of sending a blank try to send an âRâ. This assured that the info sent was strictly the numerical value I needed and not a string with large amounts of info.