parsing a string and setting variable equal to a specific char

Hi,

I have a board that is transmitting a packet of data as '12345678901234'

I have example code below on another board which receives data from a radio.

void loop(void)
{
  // Receive message
  
  e = sx1272.receivePacketTimeout(10000);
  e = sx1272.getRSSIpacket();
  
  
  Serial.print(F("Receive packet timeout, state "));
  Serial.println(e, DEC);
}

Here is the serial output:

Starting 'getPacket'

Packet correctly received

Packet received:

Destination: 8
Source: 3
Packet number: 225
Packet length: 19
Data: 12345678901234
Retry number: 0

My question is how can I set a variable, called ID, equal to the 5th character, i.e. 5?

Is Data a string, should I parse it?

Thanks

You're going to have to post all of your code, I mean you have a serial print in your code and yet I don't see that line anywhere in your output. Also I cannot see what you've got the rather ambiguous variable e set as, or what type it is either.

Hi tammytam,

Thanks for your reply.

The function getRSSIpacket() is a function that comes with the library with the radio. This is example code that comes with the library. e is set to the output of the function, which is printed on the serial output. e is an int. I've posted all of the code at the bottom of this message.

The serial output is, I posted is from the line

 Serial.println(e, DEC);

I'm not sure what is the purpose of the line:

Serial.print(F("Receive packet timeout, state "));

I was thinking that I would have to parse the output of the serial output to the get the line 'data' as a char and then parse this line and set the 5 th char and set this equal to ID (which is an int)? FYI, I need to set all the numbers equal to variables but I'm just practicing.

Kind regards
C

/*
 *  Semtech SX1272 module managing with Arduino
 *
 *  Copyright (C) 2014 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 2.1 of the License, or
 *  (at your option) any later version.

 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:		1.0
 *  Design:		David Gascón
 *  Implementation:	Covadonga Albiñana
 */
 
// Include the SX1272 and SPI library: 
#include "SX1272.h"
#include <SPI.h>

int e;

void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
  
  // Print a start message
  Serial.println("SX1272 module and Arduino: receive packets without ACK");
  
  // Power ON the module
  sx1272.ON();
  
  // Set transmission mode and print the result
  e = sx1272.setMode(1);
  Serial.println(e, DEC);
  
  // Select frequency channel
  e = sx1272.setChannel(CH_16_868);
  Serial.println("Setting Channel: state ");
  Serial.println(e, DEC);
  
  // Select output power (Max, High or Low)
  e = sx1272.setPower('L');
  Serial.println("Setting Power: state ");
  Serial.println(e);
  
  // Set the node address and print the result
  e = sx1272.setNodeAddress(8);
  Serial.println(e, DEC);
  
  // Print a success message
  Serial.print("SX1272 successfully configured ");
}

void loop(void)
{
  // Receive message
  
  e = sx1272.receivePacketTimeout(10000);
  e = sx1272.getRSSIpacket();
  
  Serial.print(F("Receive packet timeout, state "));
  Serial.println(e, DEC);
}

Don't believe you are currently doing anything with the recieved message.

 sx1272.packet_received.length;  // This is the length of recieved message
 sx1272.packet_received.data;    // This is the actual data

This feels sloppy, but to set a variable to the 5th character in data stream:

char id = -1;  

if( sx1272.packet_received.length > 4 )
{
  id = sx1272.packet_received.data[4]; // 5th character stored in id variable.
}

Hi tammytam,

Thanks, that is very helpful.

One last question - where did you find the packet_received.length and packet_received.data properties? How did you know they exist?

Thanks

Google'd the header, then dug a little deeper and found this (Amazing what you can find ;)):

examples and data for your module

Hi,

So id is char.

What if I want to set it to a range of data.

I tried [4:5] but it throws an error:

char[2] id; 

if( sx1272.packet_received.length > 4 )
{
 id = sx1272.packet_received.data[4:5]; 
}

Thanks
C

What are you trying to achieve?

Do you want individual values in each element of your array?

Or do you want a combined value?

Do you want ascii value or do you want a numerical value?

Hi,

The data packet is 012345678901234

I want to set the Id equal to the 4th and 5th values.

So id = '45'

Sorry for not explaining it well. Just not sure how to access a range of values.

If you're going to be doing this for large ranges, there are much better ways of doing it, but for a simple solution for your 2 character range:

int id; 

if( sx1272.packet_received.length > 5 )
{
	char tmp_id[3];
	tmp_id[0] = sx1272.packet_received.data[4]; 
	tmp_id[1] = sx1272.packet_received.data[5]; 
	tmp_id[2] = 0;
	id = atoi(tmp_id);
}

Hi tammytat,

So I have a have a data packet coming in as '012345678901234' and I am assigning parts of it to 3 variables - id, vbat and temperature.

The variables id and vbat are being correctly assigned to the right numbers in the packet. But for some reason temperature is being assigned '0189674312' instead of '01'

You'll see in the definition below that the variables id and temperature are both an array of 2 chars.

I've posted the serial output below the code.

Would you know why this is happening?

Thanks
C

char id[2];
char vbat;
char temperature[2];

void setup()
{
//setup stuff
}

void loop(void)
{
  // Receive message
  
  e = sx1272.receivePacketTimeout(10000);
  e = sx1272.getRSSIpacket();
  
  Serial.println(e, DEC);
  
 if( sx1272.packet_received.length < 14 )
  {
    Serial.println("Missing data");
  }
  else
  {
    
    id[0] = sx1272.packet_received.data[0]; 
    id[1] = sx1272.packet_received.data[1];  
    vbat = sx1272.packet_received.data[4]; 
    temperature[0] = sx1272.packet_received.data[9]; 
    temperature[1] = sx1272.packet_received.data[10]; 
  }
  
  Serial.print("ID:");
  Serial.println(id);
  Serial.print("vbat:");
  Serial.println(vbat);
  Serial.print("temperature:");
  Serial.println(temperature);
 
}
Starting 'getPacket'
## Packet correctly received in LoRa mode ##
## Packet received:
Destination: 8
Source: 3
Packet number: 123
Packet length: 19
Data: 12345678901234
Retry number: 0


ID:12
vbat:7
temperature:0189674312

I think you need to make your arrays 1 larger to hold a NULL termination so when you try to print them it knows where to stop.

Try
char temperature[3];

and then below the line

temperature[1] = sx1272.packet_received.data[10];

add

temperature[2] = '\0';

Would also try a similar thing to your ID one.

But I could be totally wrong too

You probably can capture all the data received as a String, then use the String functions to capture the desired section of data. You might also use the below textfinder application.

http://playground.arduino.cc/Code/TextFinder

Or you could keep it lean and mean, and stick with C style strings ( would be my choice ;)).

As for the temperature problem, I think frostline is on the money, need an extra element and null it out, its your null terminator.

Thank you all for your help. Yes - the null character was the issue.

Kind regards