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

HTTP Server in a M-Duino

Introduction


In this post is shown how to create a HTTP Server in a M-Duino PLC in order to control some Outputs from a Website. 

HTTP (Hyper Text Transfer Protocol) is the protocol to transfer information between websites. The protocol specifies how the information must be request and how the responses are formed. HTTP Server is the implementation of that protocol in a piece of software. 

In short terms, every time you write some address on the browser, for example www.industrialshields.com, and press Enter, the browser is connecting to the HTTP Server behind that address, the server will interpret the main request and then provide the principal webpage.


Requeriments


M-Duino family PLC.

Industrial Shields boards for Ethernet and pins reference.

HTTP Server library.

 

Arduino - HTTP Server


HTTP-Server library allows you to receive a request from the HTML webpage of the browser and to send a response.  The answer of the server is the HTML file of the path that the user has requested to see. For example, when you search, www.industrialshields.com in the browser and click Enter, automaticaly you send a request to the server to see the main page. Usually, the route of this page is a forward slash "/". In this example, the main page or root, is the route "/". So, writing 10.1.1.2 or 10.1.1.2/ in the navigation toolbar of the browser, the server respons with the HTML file of the main page. When we write some information in the configuration section and we click the button Submit, the request route is "/configurations", the server answer saving the data and sending the HTML file with of a confirm message.   

To make it easier, in hte http-server library we have implied a function that sends all the information saved in a SD file. This function is called sendStream(file, type file). file is the opened SD file and type file is the file type, in this case html. 

That's the code with some information on the side in Bold Font.


/*
   Copyright (c) 2019 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/>.
 */

#ifdef MDUINO_PLUS #include <Ethernet2.h> #else #include <Ethernet.h> #endif #include <SD.h> #include <HttpServer.h> #define SD_SS_PIN 53 byte mac[] = {0xAF, 0xBE, 0xCD, 0xDC, 0xEB, 0xFA}; IPAddress ip(10, 1, 1, 2); HttpServer http; File htmlFile; void setup() { Serial.begin(9600UL); Ethernet.begin(mac, ip); http.begin(); //Start HTTP Server Serial.println("HTTP Server Started"); Serial.print("IP Server: "); Serial.println(Ethernet.localIP()); Serial.println("********************************"); if (!SD.begin(SD_SS_PIN)){ Serial.println("Initialization failed!"); while(1){} } //OUTPUT PINS CONFIGURATION pinMode(Q0_0, OUTPUT); pinMode(Q0_1, OUTPUT); } void loop() { http.update(); //Constantly updating the server, waiting for a request } void httpServerEvent(const HttpRequest &req, HttpResponse &res) {//When we receive a request from the browser, this function is automatically executed.
  //req: variable with request information
  //res: variable to send information
String pagina,str; str=req.body;
//Display the request type (GET or POST), the route, query string in case of GET request and body. Serial.print("method: "); Serial.println(req.method); Serial.print("route: "); Serial.println(req.route); Serial.print("query string: "); Serial.println(req.queryString); Serial.print("body: "); Serial.println(str); Serial.println("--------------------------------");
//Filter by route if(req.route == "/"){//Homepage route serverRoot(res); } else if (req.route == "/configurations"){ String name, type; FormString formString(str); name = formString.getValue("name"); type = formString.getValue("type");
//See variables values Serial.print("var1: "); Serial.println(name); Serial.print("var2: "); Serial.println(type); serverConfigurations(res); } else if (req.route == "/actions/q0_0/on"){ digitalWrite(Q0_0, HIGH); serverActivationQ0_0(res); } else if (req.route == "/actions/q0_0/off"){ digitalWrite(Q0_0, LOW); serverDeactivationQ0_0(res); } /*else if (req.route == "/actions/r0_1/on"){ digitalWrite(R1, HIGH); serverActivationR0_1(res); } else if (req.route == "/actions/r0_1/off"){ digitalWrite(R1, LOW); serverDeactivationR0_1(res); }*/ else { res.send("Not Found", "text/plain", 404, "Not Found"); } } void serverRoot(HttpResponse &res){
//Response: Homepage HTML htmlFile = SD.open("root.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file root"); } htmlFile.close(); } void serverConfigurations(HttpResponse &res){
//Response: HTML with confirmation message of information received. htmlFile = SD.open("config.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file configurations"); } htmlFile.close(); } void serverActivationQ0_0(HttpResponse &res){
//Response: HTML with confirmation message of Q0.0 activated htmlFile = SD.open("act_q0_0.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file activation_q0_0"); } htmlFile.close(); } void serverDeactivationQ0_0(HttpResponse &res){
//Response: HTML with confirmation message of Q0.0 deactivated htmlFile = SD.open("dact_q0_0.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file Deactivation_q0_0"); } htmlFile.close(); } void serverActivationR0_1(HttpResponse &res){
//Response: HTML with confirmation message of R0.1 activated htmlFile = SD.open("act_r0_1.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file activation_r0_1"); } htmlFile.close(); } void serverDeactivationR0_1(HttpResponse &res){
//Response: HTML with confirmation message of R0.1 activated htmlFile = SD.open("dact_r0_1.txt"); if (htmlFile){ res.sendStream(htmlFile, F("text/html")); } else { Serial.println("Error opening file Deactivation_r0_1"); } htmlFile.close(); }


Website


Homepage. Simple interface. Just to check that the system works okay. 




In the next video you can see how it works. When we send some information or we enable/disable some Output, the HTTP Server respons with a confirm message. 

Note: To check that the two actions work correctly you need a PLC with analog and relay output. It means M-Duino 19R, 38R, 59R or 50RRA.  


 
 

Do you want more information?

Open Source technology allows you to develop your installations.

Just fill the form and we will contact you as soon as we can.

Send