Sample Entry - Get worldwide weather data into your strategy


#1

Hi OptoFans,

Here’s a subroutine that might be helpful to you if you want your strategy to have current weather information, from nearly anywhere on the planet. This could be displayed in PAC Display or used in logic (for example when deciding to turn on sprinklers for the lawn or not).

This weather-grabbing example requires 9.1 or newer PAC Control and firmware (uses new-in-9.1 HTTP commands).

Here’s how to use it:

Download the attached .zip file and extract the contents to a known location.

Double-click in your strategy tree on “Subroutines Included” and browse to the GetWeatherData.isb file included in the attached zip.

Find a “Station ID” in the location of interest by visiting Weather Underground here:
http://www.wunderground.com/weatherstation/ListStations.asp

For example, the Station ID for one weather station here in Temecula is “KCATEMEC4” (Roripaugh Hills station).

Your call to the new command/subroutine will look something like this:


And the data returned in the string table you pass should look something like this:

What you’re seeing here is:
[0] = location info
[1] = temperature (degrees F)
[2] = relative_humidity
[3] = wind direction
[4] = wind speed (mph)
[5] = pressure in inches
[6] = precipitation today (inches)

Neat-o!

See comments in the subroutine itself for ways you might modify this to better suit your needs.

Code on.

-OptoMary

P.S. (7-31-2012) This newer version (attached v1.2) includes the fix I describe in a later post, and more error reporting.
WeatherExample.v.1.2.zip (5.21 KB)


Greatest Hits - and mini-lessons
#2

Having trouble openning zip file??
Thanks,
Tom


#3

I got that zip to Tom, anyone else having trouble w/zip attachments in OptoForums? If so, what browser are you using?
Thanks,
-OptoMary


#4

I am only able to retrieve the first field (location info). Any suggestion what I should check for or what I am doing wrong?

Thanks,
Tim


#5

Hmm, not sure. Here’s a couple of questions for you:

  1. Does it work w/the station ID I gave as an example? (KCATEMEC4)

  2. What station ID were you using? (I’ll try it here and see if I see anything different.)

  3. Are you getting something non-zero back from the “Sub Status” parameter? If so, what did you get?

Thanks,
-OptoMary


#6

I get -20 in the “Sub Status” parameter. What does that mean?

Thanks,
VThomas


#7

VThomas: Thanks for trying this out. The -20 = “Device busy. May be in use by another user or another application.” Could the that weather server/station is busy? Can you try hitting it directly from a browser? For example, this link will show you the XML the subroutine is parsing one of the Temecula stations I give in the example. You can change the last piece of the URL to match the station you’re interested in: http://www.wunderground.com/weatherstation/WXCurrentObXML.asp?ID=KCATEMEC4

Tim: I just found a little bug that could chop off data like you mention for certain weather stations. KCATEMEC4 wasn’t one of them – I apologize for missing this! I’ll share an update shortly…


#8

Hi Tim,
Sorry for the trouble, thanks for trying this. I think I have a fix:

In Block 5, the yellow one called “Create temp file to store receive data”:
Line 11:

nCommStatus = TransmitStrTable(-1, 0, stDstStrTblBody, chTempFile);

Should be replaced with:

 
nIndex = 0;
while ( (GetStringLength(stDstStrTblBody[nIndex]) > 0) and
    (nIndex < GetLengthOfTable(stDstStrTblBody)) and 
    (nCommStatus == 0) )// loop while something in the table to write
  nCommStatus = TransmitString(stDstStrTblBody[nIndex],chTempFile);
  nIndex = nIndex + 1;
wend

Could you let me know if that fixes it for you?
Thanks,
-OptoMary


#9

I can hit it directly with no problem. Its any station that I try, even Temecula’s.


#10

What firmware version & hardware are you using?


#11

OptoMary: All is working now and I did not do any changes. When I had the problem, it was doing it in all (5) stations I tried. This is very helpfull for our application because our system in affected by barometric pressure, now I can make minor adjustments accordingly.

Thank You.


#12

Hi Tim,

Excellent! I’m glad it’s working and helpful for you. I’d recommend changing that one line anyway (the TransmitStrTable becomes multiple calls to TransmitString) because that subtle bug could come and go or not depending on, well, the weather!

I’m thinking I’ll write up a longer explanation of what’s going on here since the overall technique of using the HttpGet call then parsing what’s returned could be useful more generally for other information found out the web!

-OptoMary


#13

Fantastic. I can get the weather info from any station.


#14

[INDENT]Try using IE9 (or using some other browser like Firefox). Were you using Internet Explorer version 8 when you had this trouble? That seems to be he common thread for those who’ve had trouble downloading/opening the attached zip archives.
Thanks,
-OptoMary [/INDENT]


#15

Hello I am trying to get this to work for me but am getting an error in PAC Control when I run the subroutine.
The description of the error is GetWeatherData subroutine: Error calling HttpGet, returned: 0, HttpStatus: 403.
I’m pretty sure this is due to my proxy server but am not sure on how to get the subroutine to authenticate with it. Is there a way to write a script to authenticate with a proxy automatically before trying to get the weather data?


#16

Hi All,

Currently there’s an issue running the Http commands w/SoftPAC & PAC Sim (possibly other PACs, depending on the circumstances).

Here’s another way of doing something very similar:
http://www.opto22.com/community/showthread.php?t=393&p=1441&viewfull=1#post1441

Happy Surfing!
-OptoMary


#17

Hi OptoMary, I keep getting an “error -50 HttpStatus:0” Any Ideas what I am doing wrong?


#18

Hi insmike5,

Welcome to the OptoForums! For those HTTP commands I use in the sub, and just pass back, (from the command reference):[INDENT] -50 = Open connection timeout. Could not establish connection within the timeout period. Make sure that the controller is configured with a DNS and Gateway for the network.
[/INDENT]Do you have your DNS/Gateway set up on the PAC? (User PAC Manager to check/set those values.)

If you really have hit the timeout value (more likely if you have a very remote connection, like through a satellite) you might want to use this method of grabbing web server info, which gives you more control over the communication, including the timeout.

-OptoMary


#19

I am having the same trouble. Did you get a resolution for this? When I use my local weather station, it fills only the top line. When I use the KCATEMEC4 I get the location info only as well.
Thanks


#20

What version of firmware do you have? There’s a bug in R9.4a which will break the response-parsing part of that logic…