How to implement an Arduino web server

How to implement an Arduino web server

In this blog is showed how to implement an Arduino web server on Arduino based PLC’s. 

The requirement for this example is just an Ardunio based PLC with Ethernet connection as   M-Duino.  

M-Duino require 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 allow to toggle 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 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:

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

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

The code for implement this Industrial Shields server 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/>.
*/

#include <Ethernet>

#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:

HOW TO PROGRAM ARDUINO BASED PLC THROUGH ETHERNET

HOW CONNECT ARDUINO BASED PLC WITH SIEMENS PLC EQUIPPED WITH ETHERNET PORT,

RS-485 COMMUNICATION EXAMPLE USING OUR LIBRARIES

HOW TO CREATE A WEB SERVER TO READ A SD CARD WITH OUR PLC’S

 

 

Comments are closed.