Introduction Arduino web server
This blog is showed how to implement an Arduino project Web Server on Arduino automation based PLCs. The requirement for this example is just an Arduino 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 run the Ethernet shield and the Ethernet wire.Â
You must select the IP and Mac adddresses. In this example they 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 web server will be listening through this port waiting for a client. Once there is a client request the togglePin() function is called and Q0.0 is toggled. Otherwise, the server will display an appropriate error message to the client.Â
Once the sketch is loaded, open the Serial Monitor on Arduino IDE to know if the setUp has succeeded:
How to connect to the server? Just open your browser and type the server IP. This example is 192.168.1.219.
See next Arduino web server image:
You can then click LOW to toggle the Q0.0 pin to HIGH:Â
Software
The code for implementing 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); }
Arduino Web Server Implementation for Industrial Automation Applications