3
\$\begingroup\$

I got a Licor CO2-Logger (Li-820) for a scientific experiment. The Li-820 outputs an analog signal (voltage) and also offers a serial communication via a RS-232 port. I would like to sniff the serial communication with an Arduino Uno and display the CO2 values on a LCD display such that I can control the analog signal that is logged in a computer system. I want to display both the analog and the digital signal.

I use a RS-232 level shifter to connect the Licor CO2 analyzer to the Arduino Uno, and I can sniff the COM port successfully both with the Arduino serial monitor and a sniffer program. Over the RS-232 port the Li-820 device outputs an XML-like line that is as follows (Line breaks and indentation added for readability):

<li820>
  <data>
    <celltemp>5.1252350e1</celltemp>
    <cellpres>9.7159633e1</cellpres>
    <co2>5.2527637e2</co2>
    <co2abs>7.7893261e-2</co2abs>
    <ivolt>1.1386718e1</ivolt>
    <raw>3950702,3808028</raw>
  </data>
</li820>

I would like to parse that information for the relevant part with the Arduino Uno which is the <co2>5.2527637e2</co2> ("CO2" value) and first output it to the serial monitor. Next, I will display that value on a LCD display. This last step should be a minor problem.

So, how can I parse the information for the relevant bits and then display it to the serial monitor?

I looked into many examples on the Internet. A modified version of the working code from here was the closest I got.

\$\endgroup\$
  • 2
    \$\begingroup\$ This is a software problem and does not belong in this forum. I suspect stackover flow might be a better place to ask the question but am not sure... \$\endgroup\$ – smashtastic Jul 19 '12 at 16:46
  • \$\begingroup\$ I think you find more subject matter experts at stackoverflow, maybe migrate the question to it. \$\endgroup\$ – jippie Jul 19 '12 at 17:56
  • \$\begingroup\$ This question is being worked with on an arduino, this seems like a boundary question but one that fits on either site. If it continues to not get attention I am willing to migrate. \$\endgroup\$ – Kortuk Jul 20 '12 at 4:04
  • \$\begingroup\$ See if arduino gives you a sscanf() which works with floating point types or even scientific notation. If not you will have to use an integer solution with a denominator you can grow. Break the problem down into pieces - finding the right value, then reading it in. \$\endgroup\$ – Chris Stratton Jul 20 '12 at 17:14
2
\$\begingroup\$

I'm assuming that you can already read the string into a buffer so I will start at the buffer. Your code should go something like this (you will need to import the string library):

String wholeString = String(yourLoadedCharBuffer);
int startTag = wholeString.indexOf("<co2>");
int endTag = wholeString.indexOf("</co2>");

String co2FullString = wholeString.substring(startTag+5,endTag-1);

This gives us "5.2527637e2", you add 5 to the start tag because it is 4 characters long and you want to start at the "5", and you subtract 1 from the endtag, because we don't want the first character of it.

int eStart = co2FullString.indexOf("e");
String decimalString = co2FullString.substring(0,eStart-1);
String powerString = co2FullString.substring(estart+1, co2FullString.length-1);

char decimalCharArray[] = decimalString.toCharArray();
char powerCharArray[] = powerString.toCharArray();

float decimal = atof(&decimalCharArray[0]);
float power = atof(&powerCharArray[0]);

float finalCO2Value = pow(decimal,power);
\$\endgroup\$
0
\$\begingroup\$

This may be obvious to you, but the output is being sent in XML format. Getting an XML parsing library for arduino will go a long way to meeting your needs.

\$\endgroup\$
  • 1
    \$\begingroup\$ Technically correct, but probably a bit overweight for the problem at hand where likely only one value is needed. \$\endgroup\$ – Chris Stratton Jul 20 '12 at 17:11
  • \$\begingroup\$ Thanks to all for your answers. It is kind of working now. However, I only get half of the string via the soft serial port. The part with the CO2 concentrations is missing. Any ideas why not the whole string is read in? \$\endgroup\$ – Strohmi Aug 15 '12 at 8:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.