Benefits of connecting a PLC to the internet
Programmable logic controllers (PLCs) are now gaining information technology (IT) abilities to interact directly with the internet, creating new automation and information possibilities.
For several decades, the defining characteristics of programmable logic controllers (PLCs) centered on solving industrial automation logic problems reliably and quickly. Those fundamentals remain in effect today, but that doesn’t mean end users should just focus on these features. PLCs have gained many advanced abilities, some of which can help solve end user problems or enhance applications.
PLCs have traditionally been constrained by the information available to them from wired input/output (I/O) points and networked smart devices. Yet a wealth of information is available over the internet, which end users take advantage of with web browsers and apps. Many websites offer a means to programmatically deliver this information, which is how PLCs can obtain the data and put it into action.
Why would a PLC need to reach out to the internet, however? One reason is many common applications need to know the current or forecasted weather. For example, an irrigation or cooling tower automation system could obtain live and predicted local values for temperatures and precipitation to adjust operation.
Another example is a solar installation acquiring azimuth and altitude information from an appropriate website, and using it to optimize solar panel alignment. Information obtained from websites may not be needed for direct control, but it may be useful for operators. A wire production facility could grab the daily copper commodity price from a stock exchange website to help display the near-real-time profit margin on a human-machine interface (HMI).
Web browser basics
What happens when a browser is used to navigate the internet? First, the string typed into the browser address bar to reach a certain website is called a uniform resource locator (URL) and points to a specific domain and webpage. This string may be manually prefixed with “https://” or the browser will likely add this text if omitted. Secure sites will indicate “https://”.
HTTP defines the communications structure for how applications will communicate over the world wide web (www), specifically how messages are formatted, transmitted, acted upon and responded to.
Typing a URL into a browser generates an HTTP request to a web server, and the HTTP response is used to populate the browser display. The HTTP request and response are lightweight text files that are human-readable and easy for software to create and parse.
That’s how a human uses a browser to interact with internet websites, but the process is a little different for PLCs.
Many websites offer API definitions, which can be used by PLCs to interchange certain data. Of more interest to the PLC community are sites offering the kind of hard data likely to be useful for automated machines, equipment and systems. Of course, a PLC must have outbound internet access to connect with sites offering APIs. Information may include current and future weather conditions, the position of the sun, the current price of commodities or energy, or many other possible items of interest.
An internet search will indicate which sites offer APIs for accessing the desired information. Some APIs allow free access while other limit users to a certain number of calls per day. Providers with more valuable information may charge a subscription fee.
When a PLC or other software initiates an HTTP request following the defined format, a website will return a JSON-formatted response. The user can parse this response to obtain the specific data.
When embarking on a project to link a PLC with a website HMI or attempting any form of low-level Ethernet communications, there are some helpful tools a programmer should have. One is the open-source Wireshark, which will expose the HTTP request and response packets. Another is a text editor to facilitate viewing JSON responses. Any text editor will do, but there are many free editors that make the job much easier by providing enhanced displays of JSON formatted content.
Working with JSON files
JSON responses are received as a compact ASCII string, which is not easy for humans to read in raw format (Figure 1). Viewing the JSON string with a compatible text editor reveals the layered and nested nature of information objects within the JSON file (Figure 2).
The information of interest is often nested several layers down and must be systematically parsed and extracted. In the sample figure, it is necessary to drill through Layer 0, then into the “Temperature” object in Layer 1, then into the “Imperial” object in Layer 2, and finally obtain the “Value” of 44 for temperature in Fahrenheit.
Every PLC is different, but any PLC will need to support a few common basic features, such as HTTP requests and parsing of JSON responses.
First, the PLC device must have an Ethernet port configured on the plant network with the applicable default gateway, domain name system (DNS), IP address and subnet mask. Also, the Ethernet port used for this purpose must be able to connect to the internet over the site network.
Second, the PLC device must be configurable to act as a transmission control protocol (TCP) client. This amounts to opening a logical channel within the PLC so HTTP instructions can initiate internet website API requests out over the Ethernet port.
Third, the PLC programming must have an HTTP instruction used to define the target API server name (the URL) and other details (Figure 3). This configuration will include the request string and where the response information should be placed.
Finally, the PLC programming software must have a JSON-aware instruction that can parse through the layers of objects to obtain the necessary data (Figure 4). Due to the nested layers within JSON, this parsing operation may take several steps.
Checking the temperature via the internet
One common application providing a good proof-of-concept is using a PLC to contact a weather website to determine the current or forecast local temperature. In this example, a PLC has been configured with designer software featuring the necessary instructions – HTTPCMD and JSONPARSE.
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. As the application is created, a private user API key will be issued, which is necessary for initiating HTTP communications. This user API key is a little like a password authorizing communications.
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. For this example, we will use the latter approach.
Searching the locations portion of the reference reveals the “get” method of “postal code search,” displaying all valid parameters and allowing the user to build a valid HTTP request string. This tool also can be used to execute the request and view how the response message and the payload, the JSON data, will look. Following the API rules, the “key” value contains the desired location key.
Next, searching the “current conditions” portion of the reference shows the “get” method of “current conditions.” If this method is executed using the user API key and the new location key, then another response is obtained containing the temperature.
During development, the AccuWeather website can be used in this way to explore communication options. In actual runtime, the PLC will be configured to issue HTTP commands with the proper request strings. As each response is received, the PLC needs to place the data in string variables and then issue successive JSON parse instructions to drill down to the desired data.
Playing it safe
Enabling a PLC to connect with the internet opens a world of possibilities, but also some concerns. Users must ensure network security is preserved any time an industrial device can connect to the internet. Also, the final program must be robust enough to withstand any sort of internet outage.
For the sample application, the end user might be better served by installing a local temperature transmitter. However, the internet offers plenty of useful information, which can’t be instrumented locally. With a little thought and some programming effort, users can take advantage of PLC-based HTTP and JSON instructions to add a new level of functionality to applications.
Bill Dehner, technical marketing manager, AutomationDirect. Edited by Chris Vavra, production editor, Control Engineering, CFE Media, email@example.com.
Keywords: Programmable logic controllers, PLCs, JSON
Programmable logic controllers (PLCs) can be used to connect to the internet to gather useful real-time information for the user.
Connecting the PLC to the internet requires a fundamental understanding of how the internet operates and shares information.
The internet can add value, but users connecting PLCs to the internet should be aware of cybersecurity issues.
How could connecting a PLC to the internet help your company?