Http requests to a server using an Arduino based PLC

Working with external servers using Industrial Arduino Solutions
January 24, 2019 by
Http requests to a server using an Arduino based PLC
Boot & Work Corp. S.L., Support Team

Introduction

In this post, we will show you how to perform HTTP requests with the POST method to send information to an external server.

The information is received from analog data of a potentiometer filtered by software. 
If you want to know more about analog data filtering do not hesitate to follow this post!

Requirements

Ethernet PLC or 20 I/Os PLC:
Ethernet PLC >>>     20 I/Os PLC >>>

ArduinoHTTPClient library:
Download ArduinoHTTPClient library on Github >>>

Industrial Shields Power supply:
Industrial Shields Accessories >>>

Industrial Shields boards:
Install Industrial Shields boards >>>


Connections

To know the part related to the connection of the potentiometer, the Analog data filtering and also the SD card datalog, follow this blog post .

The Ethernet PLC will act as a client making write requests to the server to send the analog data received periodically. This will be connected via Ethernet to a Router with access to the server where the received data is written.

So that, at the Hardware level we will only require an ethernet connection between the Arduino-based PLC and the server.


Software

This sketch creates a client in our PLC based on Arduino responsible for making POST requests to the URL path composed of server IP + "/ analog-value": 192.168.1.209:1880/analog-value.

The values that we will show in the server will be the filtered analog results received from a potentiometer connected to the Arduino based PLC to be able to test the global operation of this test. 

The post requests that the server waits follow this structure: {"value": data}.
 

/*

 Copyright (c) 2018 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 <ArduinoHttpClient.h>
#include <Ethernet.h>
#include <SD.h>
#include <Filter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
IPAddress server(192,168,1,209); 

int port = 1880;

Sd2Card card;
SdVolume volume;

AnalogFilter<5, 2> aFilter;

EthernetClient ether;
HttpClient client = HttpClient(ether,server,port);
String response;


void setup() {
  Serial.begin(9600);
  // Ethernet.begin(mac,ip,dnsServer,gateway,subnet);
  Ethernet.begin(mac);
  Serial.print("IP: ");
  Serial.println(Ethernet.localIP());
  if (!SD.begin(53)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");
}


void loop() {
  int data = analogRead(I0_12);
   int filtered = aFilter.update(data);
  static uint32_t lastPrint = millis();
  if (millis() - lastPrint > 3000) {
    Serial.println(filtered);
    lastPrint = millis();
    logToSd(filtered);
    String postData = "{\"value\":" + String(filtered) + "}";

    client.beginRequest();
    client.post("/analog-value");
    client.sendHeader("Content-Type", "application/json");
    client.sendHeader("Content-Length", postData.length());
    client.beginBody();
    client.print(postData);
    client.endRequest();

    int statusCode = client.responseStatusCode();
    client.stop();

    Serial.println(statusCode);   //Prints the HTTP status response - 200 if OK
    Serial.println("Data to the Server");
    Serial.println("Wait one seconds");
   }
}

void logToSd(int value){
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
 

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(value);
    dataFile.close();
  }
  
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
}

HTTP requests to a server using an Arduino Based PLC


 
 


See also

​Search in our Blog

Http requests to a server using an Arduino based PLC
Boot & Work Corp. S.L., Support Team January 24, 2019

Looking for your ideal Programmable Logic Controller?

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

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


Industrial PLC comparison >>>