The newest PLCs can communicate directly with internet websites, creating new automation possibilities.
By Eric Witcher, AutomationDirect
The July 2019 issue of Control Engineering magazine featured a cover story describing how modern industrial controllers are now able to interact directly with internet resources. Titled Benefits of Connecting a PLC to the Internet, this article shows how users can enhance their automation systems to obtain live data directly from the internet. The following sections are adapted from that story.
Programmable logic controllers (PLCs) have long been the preferred technology for reliably and quickly executing the control logic necessary for performing industrial automation. PLCs originally worked just with their local hardwired input/output (I/O) connections, and eventually gained networking capabilities so they could communicate with human-machine interfaces (HMIs) or other smart devices.
But even more is possible. End users are familiar with the wealth of information available from various websites, viewable using browsers or dedicated apps. What if there was a way for PLCs to connect to the internet and obtain this type of helpful data? The latest generation of AutomationDirect Do-more PLCs (BRX PLCs) includes the PLC instructions and features to accomplish this task.
For example, irrigation, cooling tower, and HVAC applications could use the current and forecasted precipitation, humidity, and temperature conditions found online to fine-tune operation without needing operator interaction. A solar installation could determine the suns azimuth and altitude for any known location and time. Information obtained from the internet may not be necessary for live control but could help operators in other ways, such as an HMI displaying live commodity prices.
Many useful websites already provide a programmatic way for users to obtain raw data. This is called an application programming interface (API). PLCs with the right instructions can be configured to communicate with these kinds of internet resources using the API definition. The following PLC tools make this possible:
- Instructions for accessing hypertext transfer protocol (HTTP)
Using these instructions, PLCs can initiate HTTP communications with various websites and receive JSON data from them. The received JSON data must then be parsed to extract the desired information.
Most people have used a web browser to navigate the internet. They are familiar with typing a website name into the browser address bar, sometimes preceded by “http://” (or “https://” for a secure site). A website address is formally known as uniform resource locator (URL), and this points to a specific domain and webpage. The HTTP protocol defines how applications can communicate over the world wide web (www). Specifically, HTTP describes how messages are formatted, transmitted, and handled.
When a user types a URL into a browser, an HTTP request is generated to the target web server, and the corresponding HTTP response is used to populate the browser display. Both HTTP requests and responses are lightweight text files, which are easy for software to create and parse.
PLCs can interact with the internet in a similar manner, but with a few key differences.
APIs in Action
While many websites offer all sorts of API definitions, the PLC community is probably most interested in those websites offering hard data useful for automated machines, equipment, and systems. To obtain this type of information, a PLC needs outbound internet access so it can connect to the desired websites.
Research performed on the internet by searching for “website APIs” and similar terms will identify various websites and the APIs they offer. Some APIs offer free access, or at least a limited number of free connections per day. Other websites, often with more valuable information, may require registration and charge a usage or subscription fee.
Programmers should have a few software tools available as they begin their first project to connect a PLC with an internet website, including the open-source Wireshark packet monitoring software and a JSON-aware text editor.
HTTP requests to website APIs will return responses in JSON format, which is not easily human-readable in raw format (Figure 1). However, JSON has a well-defined system of layers and nesting, and those information objects are readily viewed with a compatible text editor (Figure 2).
The information a user would want is often nested several layers deep within the JSON file, which must be progressively parsed so it can be retrieved. In the Figure 2 example, the temperature of 44 degrees Fahrenheit resides in Layer 3, underneath Layer 2 “Imperial” and
Layer 1 “Temperature”.
AutomationDirect BRX PLCs incorporate four key features to make communication with internet websites possible:
- An Ethernet port configured on the plant network, with proper privileges allowing the PLC to connect to the internet
- The ability to be configured as a transmission control protocol (TCP) client, so the PLC can open a logical channel to initiate HTTP instructions
- An HTTP command instruction, so request strings can be sent to the API of a target URL
- A JSON parsing instruction, so that the response data payload can be processed
For AutomationDirect, the specific program instructions are HTTPCMD and JSONPARSE. Following is an application example demonstrating the necessary steps.
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 temperature 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 latitude/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 website, to “get” results for a “postal code search”
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 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 necessary for the temperature value.
In this case, to obtain the temperature forecast 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.
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.