Post a new topicPost a reply Page 1 of 1   [ 10 posts ]
Author Message
 Post subject: Formatting Data Sprintf function
PostPosted: Fri Jun 29, 2012 12:04 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Hi, hope someone can tell me where I'm going wrong here. It's probably something very simple that I have/haven't done:

I'm trying to send the wind speed from the agriculture board over XBee. I get the value back from the sensor into a float variable called "value_anemometer". I've checked that this is giving me a good reading.

My problem comes when I try and pack it into the data char array in the function below.

sprintf(data,"-mac:%s%s -x:%d,y:%d,z:%d -temp:%d -bat: %d%c -speed: %f%c%c",macHigh,macLow,ACC.getX(),ACC.getY(),ACC.getZ(),(int)RTC.getTemperature(),PWR.getBatteryLevel(),'%',value_anemometer,'\r','\n');

When I view the frames over XBee I always get "-speed: ?" instead of the actual value. When I force it to be an INT datatype and use the %d in the sprintf function I get the value but obviously as an integer.

I've tested this function in Visual Studio and it works okay there. Any ideas??

Thanks

Richard


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Fri Jun 29, 2012 8:31 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi RichD,

sprintf doesn't handle well the floating point variables, you'll have to copy this value into a string using function float2String before sending it, something like this:

Code:
char data_ane[7];
Utils.float2String(value_anemometer, data_ane, 2);

sprintf(data,"-mac:%s%s -x:%d,y:%d,z:%d -temp:%d -bat: %d%c -speed: %s%c%c",macHigh,macLow,ACC.getX(),ACC.getY(),ACC.getZ(),(int)RTC.getTemperature(),PWR.getBatteryLevel(),'%',data_ane,'\r','\n');


Regards.


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Tue Jul 03, 2012 3:17 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Great. That sorted it.

Thanks for your help.

Richard


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Fri Jul 06, 2012 6:13 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Hi libelium-dev

Hoping you, or someone else, can help me once more.

I had my data formatted correctly and it was transmitting to meshlium. I've just added another two sensors to my waspmote; UV and soil moisture. For some reason my sprintf function is not behaving as I'd like. When I look at the frame in the serial monitor the lines do not break and the new values I have entered are garbage characters.

This is my code:
Code:
sprintf(data,"-mac:%s%s -x:%d,y:%d,z:%d -temp:%d -bat:%d%c -rain:%s -speed:%s -vane:%d -sun:%s -soil:%u%c%c",macHigh,macLow,ACC.getX(),ACC.getY(),ACC.getZ(),(int)RTC.getTemperature(),PWR.getBatteryLevel(),'%',data_plum,data_ane,(int)value_direction,data_radi,value_soil,'\r','\n');


The two extra sensors are appended to the end; "data_radi" is a string and "value_soil" is a Uint.

I don't think it is anything to do with the new sensors as further test have shown that simply adding existing sensor values again to the end of the sprintf gives the same results.

Is there a maximum frame length that you can send?

Any help is appreciated.

Richard


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Fri Jul 06, 2012 9:47 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi RichD,

yes, the maximum data you can send depends on the kind of module you have, for example, 100 bits for 802.15.4 without encryption (including header). You can find this information on the networking guides.

Relating your problem, could you post the lines where you read the sensors and convert the value, and also what you received in your gateway?

Regards.


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Mon Jul 09, 2012 12:44 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Hi there. Thanks for your reply. I am using the 802.15.4 XBee module.

Here is the code where I read the sensors and convert the values:

Code:
  // Setup sensor board
  SensorAgr.setBoardMode(SENS_ON);
  delay(100);
 
  // Take pluviometer reading
  value_pluviometer = SensorAgr.readValue(SENS_AGR_PLUVIOMETER);
 
  // Take anemometer reading
  SensorAgr.setSensorMode(SENS_ON, SENS_AGR_ANEMOMETER);
  delay(100);
  value_anemometer = SensorAgr.readValue(SENS_AGR_ANEMOMETER);
  SensorAgr.setSensorMode(SENS_OFF, SENS_AGR_ANEMOMETER);
 
  // Take wind vane reading
  SensorAgr.setSensorMode(SENS_ON, SENS_AGR_VANE);
  delay(100);
  value_windvane = SensorAgr.readValue(SENS_AGR_VANE);
  value_direction = SensorAgr.vane_direction;
  SensorAgr.setSensorMode(SENS_OFF, SENS_AGR_VANE);

  // Take UV reading
  SensorAgr.setSensorMode(SENS_ON, SENS_AGR_RADIATION);
  delay(100);
  value_radiation = SensorAgr.readValue(SENS_AGR_RADIATION);
  value_SU100 = value_radiation / 0.00015;
  SensorAgr.setSensorMode(SENS_OFF, SENS_AGR_RADIATION);

  // Take soil moisture reading
  SensorAgr.setSensorMode(SENS_ON, SENS_AGR_WATERMARK_1);
  delay(100);
  freq=SensorAgr.readValue(SENS_AGR_WATERMARK_1);
  SensorAgr.setSensorMode(SENS_OFF, SENS_AGR_WATERMARK_1);
 
  // Convert freq reading from soil to centibar
  value_soil = (150940 - 19.74*freq)/(2.8875*freq - 137.5);
 
  // Convert float values to string
  Utils.float2String(value_pluviometer, data_plum, 2);
  Utils.float2String(value_anemometer, data_ane, 2);
  Utils.float2String(value_radiation, data_radi, 4);


This is the output that I get for two frames:

https://docs.google.com/open?id=0BzqSAjIfmUPoMnJrc0VVczBxS28

Hopefully you can see the picture.

Thanks for your help


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Mon Jul 09, 2012 2:22 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Hi again,

I changed the frame I was sending to replace the mac address with a 3 digit ID. This has helped as I am once again getting individual frames. So I guess I was breaching a the maximum count previously.

However when my frame is still not correct as an 'r' character is being inserted at the last set of data in the frame.

Here is my code:
Code:
sprintf(data,"-id:%s -x:%d,y:%d,z:%d -temp:%d -bat:%d%c -rain:%s -speed:%s -vane:%d -uv:%s -soil:%s%c%c",id,ACC.getX(),ACC.getY(),ACC.getZ(),(int)RTC.getTemperature(),PWR.getBatteryLevel(),'%',data_plum,data_ane,(int)value_direction,data_radi,data_soil,'\r','\n');


And this is the frame that gets received at the Meshlium:

r#-id:001 -x:2,y:3,z:1021 -temp:25 -bat:70% -rain:0.00 -speed:0.51 -vane:8192 -uv:-2.74 -soil:0.r22

See how in the last block of data I get the 'r' character in my soil moisture value.

Any ideas?

Thanks


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Mon Jul 09, 2012 9:50 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi RichD,

I can't see anything wrong in your code (though composition of data_soil is still missing, but I guess you are doing it the same way as data_radi and data_ane). Anyway, I think the problem is that your frame is still too long, since it is of 100 bytes and the header must be included. To verify this you may try by changing the order of the parameters and see if the error is still in the same data.

Regards.


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Tue Jul 10, 2012 1:24 am 

Joined: Tue Jun 12, 2012 5:37 am
Posts: 12
Hi. Thanks for your reply.

I have changed the MAX_DATA constant in WaspXBeeConstants.h from 100 to 150. I'm still getting the same result with the 'r' character being inserted in the last data block.

Is there any value I need to increase? Also, how long is the header?

p.s. I have got around this problem for the time being by reducing the number of characters I'm sending in frame, however I may need to add additional sensors when this gets installed, so I'd still like to know how to send more than 100bytes.

Thanks

Richard


Top
 Profile  
 
 Post subject: Re: Formatting Data Sprintf function
PostPosted: Tue Jul 10, 2012 11:53 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7440
Hi RichD,

increasing MAX_DATA won't solve the problem, since this 100 bytes is a limit imposed by the XBee hardware, by doing this the data will be fragmented by the Waspmote API and sent in different packets, so you will be receiving information in differents messages.

Regarding the header, it is dependent on the kind of XBee module and its configuration (API mode, encryption...). you may find this information in the corresponding networking guides.

Regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 10 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use