In compliance with high industry standards

Browse our Blog. You will find multiple applications, solutions, code examples. Navigate using the tag cloud or search using specific criteria

Arduino Web Server Implementation for Industrial Automation Applications

How to implement an Arduino Web Server

Introduction Arduino web server


In this blog is showed how to implement an Arduino project Web Server implementation on Arduino automation based PLC’s.  The requirement for this example is just an Ardunio based PLC programming with an Ethernet connection (Arduino web server ethernet shield)

 .

Requirements to implement an Arduino Web Server


Ethernet PLC Arduino:          MDuino Family Products

Ethernet2 Library:             Arduino Ethernet2.h library


Configuration


M-Duino requires a power supply to make work the Ethernet shield and Ethernet wire. 

You must select the IP and Mac direction. In this example are: IP = 192.168.1.219 and Mac = 0xDE, 0xAB, 0xBE, 0x15, 0x00, 0x01.

This Web server is a simple Arduino Web Server that allows toggling the M-Duino Pin Q0.0. Using port TCP 80 and HTTP protocol our server web will be listening through this port waiting for a client. Once there is a request from the client the function togglePin() is called and Q0.0 is toggled. Otherwise, the server will show an appropriate error message to the client. 

Once you upload the sketch, open the Serial Monitor on Arduino IDE to know if the setUp has succeeded:

Serial Monitor on Arduino IDE setUp

How to connect to the server? Just open your browser and type the server IP. In this example is 192.168.1.219.
See next arduino web server image:

How to connect to Web Server

Next you can click on LOW to toggle the Q0.0 Pin to HIGH:

How to connect to Web Server

Software


The code for implement this Industrial Shields Arduino web server HTML CSS is shown below:

/*
   Copyright (c) 2017 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
//If using V7 versions you have to include instead the following library: #include <Ethernet.h> 
#include <Ethernet2.h> #define TCP_PORT 80 // Digital output int pin = Q0_0; int value = LOW; int _numDigitalOutputs = 1; uint8_t _mac[] = {0xDE, 0xAB, 0xBE, 0x15, 0x00, 0x01}; byte IP[] = {192, 168, 1, 219}; EthernetServer _server(TCP_PORT); //////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(9600L); Serial.println("ISWeb started"); pinMode(pin, OUTPUT); digitalWrite(pin, value); Ethernet.begin(_mac, IP); Serial.print("IP address: "); Serial.println(Ethernet.localIP()); _server.begin(); Serial.print("Listening on port "); Serial.println(TCP_PORT); } //////////////////////////////////////////////////////////////////////////////////////////////////// void loop() { EthernetClient client = _server.available(); if (client) { String requestType = client.readStringUntil(' '); String requestUrl = client.readStringUntil(' '); String requestVersion = client.readStringUntil('\n'); String response; if (requestType == "GET") { Serial.println("process GET"); Serial.print("Request URL: "); Serial.println(requestUrl); if (requestUrl == "/") { response = createResponse(); } else if (requestUrl.startsWith("/toggle")) { togglePin(); response = createResponse(); } else { response = create404(); } } else { response = create404(); } sendResponse(response, client); client.stop(); } } //////////////////////////////////////////////////////////////////////////////////////////////////// String createResponse() { String content = "<!DOCTYPE html>" "<html>" "<head>" "<title>ISWeb</title>" "</head>" "<body>" "Arduino web server IS<br>"; content += "Q0.0: <a href=\"/toggle\">"; content += value == HIGH ? "HIGH" : "LOW"; // if value = HIGH print HIGH, else print LOW content += "</a>"; content += "</body>" "</html>"; return createResponse(200, content); } //////////////////////////////////////////////////////////////////////////////////////////////////// String create404() { return createResponse(404, "404 Not Found"); } //////////////////////////////////////////////////////////////////////////////////////////////////// String createResponse(int statusCode, const String &content) { String response; switch (statusCode) { case 200: response = "HTTP/1.1 200 OK\r\n"; break; case 404: response = "HTTP/1.1 404 Not found\r\n"; break; default: response = "HTTP/1.1 500 Internal server error\r\n"; break; } response += "Server: ISWeb\r\n" "Content-Type: text/html\r\n" "Connection: closed\r\n" "Content-Length: "; response.concat(content.length()); response += "\r\n" "\r\n"; response += content; return response; } //////////////////////////////////////////////////////////////////////////////////////////////////// void sendResponse(const String &response, EthernetClient &client) { client.print(response); } //////////////////////////////////////////////////////////////////////////////////////////////////// void togglePin() { if (value == HIGH) { value = LOW; } else { value = HIGH; } digitalWrite(pin, value); }

 

See also:

Ethernet Test on M-Duino PLUS versions

How to reset a PLC using the Ethernet shield

How to connect Arduino based PLC with Siemens PLC with Ethernet


 

Looking for your ideal PLC?

Take a look at this product comparison with other industrial controllers Arduino.

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC controller comparison >>

Do you want more information?

Just fill the form!

Tell me more!