Page 22 - AutomationNotebook-Issue43-flip
P. 22

Cover Story continued
A PLC Asks, "What is Tomorrow's Temperature?"
Perhaps the most common example of using a PLC to obtain internet data is when a PLC contacts a weather website to determine the forecast local temperature for the next day. The current local tem- perature could also be obtained this way, or via an outdoor temperature sensor connected to the PLC.
The AccuWeather website, for example, offers an API with many details under the “API reference” tab. Users should always consult the target website regarding registration, creating an application, cost and other technical details or restrictions.
In this case, a private user API key is necessary for initiating HTTP communications. This user API key authorizes communications and is similar to
a password.
This API offers many methods of obtaining data. If the PLC, for example, is in a major city, then the temperature can be obtained on that basis in one step. For better accuracy, the user can choose lati- tude/longitude or even the postal ZIP code to look up a location key, which is used to call additional
API methods.
This example uses the latter approach. First, an HTTPCMD instruction is issued to the target web- site, to “get” results for a “postal code search” (Figure 3).
Figure 4: This JSONPARSE block in an AutomationDirect BRX PLC is used so the PLC can drill into the layers of objects within a JSON data payload and extract the desired information.
location. The JSONPARSE instruction allows the PLC to delve into the payload and extract the necessary key (Figure 4).
Once the key is known, it can be used to configure another set of HTTPCMD and JSONPARSE instructions, and then repeated as often as is neces- sary for the temperature value.
In this case, to obtain the temperature fore- cast for the next day, the user would configure the HTTPCMD to “get” the “forecasts/1day”, and then use the JSONPARSE to extract the temperature in degrees. Note that JSON responses are handled as string variables and may require several successive JSONPARSE instructions to drill down through the layers to find the desired data.
Although initial programming requires a few steps, subsequent requests can be generated automatically.
Keeping Control
The ability for a PLC to interrogate the internet for useful data is powerful, but users must provide careful security for any industrial devices or networks capable of contacting the internet. Also, the PLC program must be robustly arranged so that it can withstand any type of internet outage.
The internet offers lots of information, much of which can’t be easily obtained with local sensors and instruments. With the right PLC, some planning, and a little programming, users can enhance their applications by letting their PLCs directly gather information from internet websites.
Go to Article
Editor's Note
New Product Focus
- Headless C-more Remote HMI without attached display
- Continuous Flexing Profinet Cable
- ProductivityOpen Arduino-compatible Controller
- Hammond Industrial Enclosures
Tech Thread
Productivity®Open - Maker In, Industrial Out
Business Notes
Cover Story
Does Your PLC Talk JSON?
What's New
User Solutions
Roller Coaster Overhaul
Student Spotlight
Industrial Automation with a Mission
Break Room
Brain Teasers
Following the API rules, the JSON response will be a data payload containing a “key” value, which in this case is a lookup index for the desired
Figure 3: This HTTPCMD block in an AutomationDirect BRX PLC enables a PLC to initiate a request to an internet website API to obtain data. Automa- tionDirect Do-more Designer Software has the necessary instructions.
Cover Story | Issue 43

   20   21   22   23   24