Did you know...Importing Data from a Website?
In the olden days, SCADA networks used specialized communication standards and each computer existed as it's own standalone environment. Consequently, SCADA networks existed as a separate entity from the business networks and a de facto "Air Gap" kept SCADA environments far from the internet.
Then manufacturers starting using Ethernet for everything and since the blue wire was already connecting all the equipment together anyway, why not connect the SCADA network to the business network and the internet? Well there are a lot of reasons actually, however when done correctly there are some very nice benefits to being able to access the web from your SCADA environment.
One such benefit is the ability to Import Data from a website. Below is a step-by-step tutorial demonstrating how to import weather information from the website "Weather Underground" into the Ignition HMI. This should be equally doable from iFix or FactoryTalk, both of which use a variant of VBA, or Wonderware, which uses Quickscript, however today I'm only focusing on Ignition.
These steps have been tested using Ignition 7.9 that runs on python 2.5, however this should also work on earlier versions using python 2.4
You should only need the file named json.py, though I recommend extracting all three .py files just to be safe and placing them in Ignition's Python library folder.Additional information on using third party python libraries can be found in this knowledge base article
To get an data from weather underground, you must get an API key. The API key will be used to validate your attempts to access the weather data from their website. Weather Underground offers a free developer's license that, at the time of this write up, provides for up to 10 queries a minute or 500 queries in a day. At 1 query every hour, you could implement this in 20 unique projects without breaking your limits. Paid subscription plans exist if you need more, or you could just setup each client with their own API and query every 5 minutes without breaking your limit. To setup an free developer's account:
Open a web browser and go to http://www.wunderground.com/.
At the bottom of the web page, click the link labeled 'Weather API for developers' in the footer area
Enter your name, e-mail address and password in the 'Create free account' form. Tap the 'Sign Up' button.
Click the link in the confirmation e-mail to confirm your account. Then register on the provider web site with your login and password.
Tap the 'Explore My Options' button under the 'API Home' tab.
Select the plan that fits your needs ('Stratus' is the free plan) and tap the 'Purchase Key' button (this is the free option.
Enter your information, N/A can be entered if you don't have a website
Check 'I agree to the Terms of Service' flag and tap the 'Purchase Key' button.
Copy the personal API key for use in the next step.'
Use the following script as a template, replace [YOUR_API_KEY] with your API key from weather underground , [TWO_LETTER_STATE_ABBREVIATION] with your state ID (i.e. TX for Texas), and [NAME_OF_CITY] (i.e. Dallas). Additional variables are listed after the code section.
#Import necessary libraries
#replace these variables with your specific details
apiKey = [YOUR_API_KEY]
weatherState = [TWO_LETTER_STATE_ABBREVIATION]
weatherCity = [NAME_OF_CITY]
#create URL string
urlStr = "http://api.wunderground.com/api/%s/geolookup/conditions/q/%s/%s.json" %(apiKey, weatherState, weatherCity)
#Get data on the selected city
f = urllib2.urlopen(urlStr)
json_string = f.read()
#parse as fully formatted json, it is important to use the .strip() function
parsed_json = system.util.jsonDecode(json_string.strip())
#example to find city, temperature (as Fahrenheit), wind speed, and wind direction
city = parsed_json['location']['city']
temp = parsed_json['current_observation']['temp_f']
windSpeed = parsed_json['current_observation']['wind_mph']
windDir = parsed_json['current_observation']['wind_dir']
#Will print the following - Folsom weather: 59.8 °F, wind 5 MPH from the East
print (city + " weather: " + str(temp) + " °F, wind "\
+ str(windSpeed) + " MPH from the " + windDir)
System.util.jsonDecode creates a pyObject, presumably it is a 2 dimensional list and I will refer to it as such, but there might be more, check the weather underground API for more detailed information. All elements listed in this document can be accessed by referencing [Element1_ID][Element2_ID]
The first element of the list will either be ['location'] or ['current_observation']. All the items in the ['location'] list will give information about the selected city, items in the ['current_observation'] will give you weather details.
Here are some of the available elements for the ['location'] list are (and an example of what they'd display):
Available elements for the ['current_observation'] list are (and an example of what they'd display):
The above script will print the requested information to the console tool in the Ignition Designer, however using Ignition's builtin funcitons, this data can be written to memory or PLC tags, displayed on screen, or historized.
All logos are the property of their respective owners and only used for their descriptive purposes under fair use guidelines. Their inclusion in this post are not implying their endorsement of Bennett Automation Solutions or any of its employees.