Prop floats and integers to RPi to txt file
Jonathan
Posts: 1,023
Hi All,
I have gotten my RPi up and running and talking to my Prop via a pair of xbees. I can send a series of ASCII bytes from the pi to the prop and receive a series of ASCII bytes back that contain a temperature reading from a DS18B20 that the prop is reading. Since the DS object returns a floating point value for the I have been rounding it into an integer (f.fround) then using a DIG command converting it to ASCII bytes. I am using a small python program (my first!) that I wrote to query the prop, get the data and then both display it and write it to a file. Ultimately this file will be read and published to a website via the Pi so I can check out things like my hot tub temp. at work. Very important stuff!
Is there a way to send floats (and integers for that matter) without converting it to ASCII? I have been reading up Python all day but can't seem to figure it out. There surely must be a way.
I'm also trying to figure out how to break down a string into a comma delimited array and writing that to a file. I can write the string in but need to insert the commas. I think the bytearray function is what I need for this.
Thanks for any help or examples of similar projects!
Jonathan
I have gotten my RPi up and running and talking to my Prop via a pair of xbees. I can send a series of ASCII bytes from the pi to the prop and receive a series of ASCII bytes back that contain a temperature reading from a DS18B20 that the prop is reading. Since the DS object returns a floating point value for the I have been rounding it into an integer (f.fround) then using a DIG command converting it to ASCII bytes. I am using a small python program (my first!) that I wrote to query the prop, get the data and then both display it and write it to a file. Ultimately this file will be read and published to a website via the Pi so I can check out things like my hot tub temp. at work. Very important stuff!
Is there a way to send floats (and integers for that matter) without converting it to ASCII? I have been reading up Python all day but can't seem to figure it out. There surely must be a way.
I'm also trying to figure out how to break down a string into a comma delimited array and writing that to a file. I can write the string in but need to insert the commas. I think the bytearray function is what I need for this.
Thanks for any help or examples of similar projects!
Jonathan
Comments
There is a way but it's messy. If you're not using ASCII characters then you can't have control characters since what you think is a control character could be a valid data point.
You either need to always send the same number of bytes with each transmission or indicate how many bytes are being sent in the packet's header. There are a variety of protocols which do this. The communication protocol used by Dynamixel servos have a field indicating the message size. Modbus also has the number of bytes (IIRC, it could be the number of words) as part of the packet header.
While dealing with raw data can be a pain, it's a good thing to know how to do. Sending data as ASCII characters can easily double the bytes sent compared with sending raw data.
The hassle of converting data to and from ASCII characters is minor compared with the hassle of sending raw data values. I only bother with raw data when sending ASCII characters is too slow or when required by the devices with which I'm interfacing.
I can stick to ASCII I guess. I won't be sending huge gobs of data and the system is not time sensitive at all. I have a number of prop based controllers (garden timer, rural well and water system, hot tub) that I am tying together with xbees and using the Pi to query the subsystems and then publish to the web.
I'll keep pounding on figuiring out the comma delimited ASCII file writing. I have the correct data in the file, just need to figure out how to insert the commas. Also how to take a float and convert it to an integer while keeping what is to the right of the decimal point.
Thanks!
Jonathan
The object "StrFmt" I mentioned earlier will do both of these things.
There are several ways to deal with the floating point number. One way would be to multiply it by an appropriate value prior to rounding the number. If, for example, you multiplied the number by 1000, you could then use the "decf" (IIRC) "fdec" method and indicate the decimal position as "3" in the appropriate parameter (which is the "dp" parameter).
The object also makes it easy to add a character to a string. IIRC, It's the "ch" method.
At least this how to do it in Spin. I don't know Python well enough to help.
Edit: I just checked the "StrFmt.spin" object and made a couple of corrections.
Great! Very helpful. And duh, of course multiply it! I'll go check out the StrFmt object. I could of course add the commas on the prop side, but that would mean even more bytes sent.
Jonathan
There is a libxbee that should work on your Pi (it works great on my x86 linux and it's open source so I don't see why it wouldn't work) and a Spin library for the propeller that can both do either AT or API mode.
libxbee
Spin XBee driver
my version of the Spin XBee driver that can be used with variants of 4PortFullDuplexSerial