Reading external serial and publishing [Solved]

Hello,

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);
}

Thank you for your help.

are you sure you are getting the carriage return?

try transmitting with a hard char like ‘X’ and see what happens… something like this…

String sensorstring = "";

void setup() 
{
    Serial1.begin(9600);
}

void loop() 
{
  if (Serial1.available()) 
  {
    char inchar = Serial.read();
    if (inchar == 'X') 
    {
	Serial1.println("got message");
        Serial1.println(sensorstring.toFloat());
        sensorstring = "";
    }
    else
    {
      sensorstring += inchar;
    } 
  }
}

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?

Hello Scruff,

Thanks for the response. Two things:

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”.

Hello Bulldog,

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.

Thanks again for the suggestion

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

What OS are you on?
If it's Windows before 10, you'll need these drivers
https://s3.amazonaws.com/spark-website/Particle.zip

Hello ScruffR,

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.

Thank You
Kalty Vazquez

You didn't answer my question

And I guess you also haven't tried the drivers, have you?

Yes, you should be able to use the Arduino Serial Monitor.

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.

2 Likes

Thanks @ScruffR @BulldogLowell for the help!

1 Like