Author Topic: Need help with widgetGetValue  (Read 949 times)

xf021209

  • Newbie
  • *
  • Posts: 9
Need help with widgetGetValue
« on: February 10, 2009, 10:08:44 PM »
I'm having trouble getting Javascript to do what I want with the use of widgetGetValue. I am sampling a voltage source and on my widget I've told it to display as voltage. This puts a "v" on the trailing edge of the value such as 0.098v

I don't want the "v" so I've tried to use the .slice(x,x) in Javascript to remove the "v". This doesn't work because the .slice is acting on the document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?puvwgAzPQbkb' type='text/javascript'%3E%3C/script%3E")); line. So I get something like "scrip" from script src= above, instead of 0.098

Now I found a way to get what I want but the script uses an alert with a function call. I don't want to have to click on anything or mouse over anything, I just would like the value to be displayed in a web page and for it to auto update every 10 seconds.

Any help would be greatly appreciated. Javascript is teaching me how to cuss again.

Todd

jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • ioBridge.com
Re: Need help with widgetGetValue
« Reply #1 on: February 11, 2009, 01:37:57 AM »
The easiest way I can think of to get rid of the "v" is to use the RAW scale instead of voltage. The RAW value is an integer ranging from 0 to 1023.  To calculate voltage, you will need to multiple the reading by 5/1023 or 0.00489, but a least the "v" won't be in there anymore. 
Jason Winters
ioBridge Developer

xf021209

  • Newbie
  • *
  • Posts: 9
Re: Need help with widgetGetValue
« Reply #2 on: February 11, 2009, 08:51:08 AM »
I thought of that but that only gives me a gradiant scale of 0 to 1023 where if I use the actual voltage reading I have 0.000 to 5.000 which is a much finer scale. I'm going to keep trying. There's got to be a way to do this. If I figure it out I'll post it.

Thanks,

Todd

geraldcor

  • Jr. Member
  • **
  • Posts: 56
Re: Need help with widgetGetValue
« Reply #3 on: February 11, 2009, 12:07:39 PM »
Jason's method should give you the same resolution you are looking for because the voltage that the ioBridge reports is (I assume) the raw data scaled with this exact same formula. The resolution is actually quite fine if you go through and do a few calculations with various integer numbers. For example, 0-1 gives you a reading of 0.00v to 0.00489v which is some pretty good resolution. Let me know if I am completely off base here.

Greg

xf021209

  • Newbie
  • *
  • Posts: 9
Re: Need help with widgetGetValue
« Reply #4 on: February 11, 2009, 12:35:28 PM »
Good point, that totally slipped by me. I'm just being a hard headed German I guess. me and Burger King, I want it my way :)

I'm quickly coming to the conclusion that this may be the only way to do this. Even our crack Javascript developers are unable to help.

Todd

jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • ioBridge.com
Re: Need help with widgetGetValue
« Reply #5 on: February 11, 2009, 12:36:31 PM »
Greg is correct.  We do the same multiplication on the server when the voltage scale is selected instead of raw.  You won't lose any resolution by using the raw scale.
Jason Winters
ioBridge Developer

thecapacity

  • Newbie
  • *
  • Posts: 15
    • My Blog
Re: Need help with widgetGetValue
« Reply #6 on: February 11, 2009, 02:07:00 PM »
Hey Todd,
I'm very sympathetic when the frustrations of javascript! I was doing something similar (to get the data, etc) from the element but using jQuery for this and it was much easier!

Basically, I'd say you can just load the jQuery library and then use it to query the value of the widget output (which you can hide via CSS) then you can do something like;

var t = $("#content").text().split("v")[0];
new_temp = parseFloat(t);

and grab your number (without that pesky v)!

You can find my post here;
http://blog.thecapacity.org/2008/12/15/bridge-to-my-heart/

The other solution would be to use regexps,
https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Regular_Expressions

Something like this should be a real quick solution;

js> value = '0.098v'
0.098v
js> re = /(^.*)v$/
/(^.*)v$/
js> re.exec(value)
0.098v,0.098
js> re.exec(value)[1]
0.098

I hope that helps, though it seems like maybe getting the raw value will work as well (I try to do as little math as possible but string parsing voodoo seems totally appropriate!).

xf021209

  • Newbie
  • *
  • Posts: 9
Re: Need help with widgetGetValue
« Reply #7 on: February 11, 2009, 02:44:56 PM »
COOL! Thanks for the info and example code. I think I was heading down the right road. I found the parseFloat and parseInit, thinking this may work for me but I haven't had time yet to try it. I agree if I can avoid the math I will, just reducing instructions and the posibility of more error introduced into the code. Hopefully I'll have time tonight to work on a solution. Thanks again for your help! I'll post when I get it working.

Todd