Browsed by
Category: General

How to create a web server to read a SD card in our PLC

How to create a web server to read a SD card in our PLC

This is an example it is explain how to read a SD card into the PLC through Ethernet connection. In this post we will see how create a server that when there is a request through a browser, you can see the directories and the files of the SD card in your browser.

The PLC has to be connected the Ethernet connection and the SD card correctly, check this post how to do it.

Once it is all connected correctly the next step is make the right configuration of the Ethernet port and create the server:

 

/*
   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 <MDUINO.h>
#include <SPI.h>
#include <SD.h>
#include <Ethernet.h>


#define SD_SS_PIN 3


File root;

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80);

/////////////////////////////////////////////////////////////////////////////////
void setup() {
	Serial.begin(115200L);

  // Init Ethernet and TCP server
  Serial.println("Starting Ethernet ...");
  Ethernet.begin(mac);
  server.begin();

  // Init SD card
  Serial.println("Starting SD card ...");
  if (!SD.begin(SD_SS_PIN)) {
    Serial.println("initialization failed!");
    return;
  }

  Serial.print("IP address: ");
  Serial.println(Ethernet.localIP());
}

///////////////////////////////////////////////////////////////////////////////
void loop() {
  EthernetClient client = server.available();
  if (client) {
    char lastC = 0;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if ((c == '\n') && (lastC == '\n')) {
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          client.println("Connection: close");
          client.println();
          client.println("SD card files:");

          root = SD.open("/");
          printDirectory(root, client);

          break;
        }
        if (c != '\r') {
          lastC = c;
        }
      }
    }
    delay(50);
    client.stop();
  }
}

////////////////////////////////////////////////////////////////////////////////
void printDirectory(File dir, Stream &stream) {
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) {
      // no more files
      break;
    }

    stream.print(entry.name());
    if (entry.isDirectory()) {
      stream.println("/");
    } else {
      // files have sizes, directories do not
      stream.print(" [");
      stream.print(entry.size(), DEC);
      stream.println("]");
    }
    entry.close();
  }
}

See also:

How to connect a SD card to a PLC, How to connect the Ethernet port using W5100 library

 

How to connect a SD card to a PLC

How to connect a SD card to a PLC

 

In this example we will see how to connect a SD card to each of our PLCs.

Hardware required:

  • One of our PLCs
  • A SD card

How to connect the SD card to our PLC?

First we start with the connections; as follows you have a table and circuit diagram where you can see how to connect the SD card to the PLC.

 

 

 

PLC SD CARD
MOSI MOSI
SCK SCK
PIN3 CS
CD
MISO DO
GND

 

Next there is an example code to how to configured our PLC to read the SD card.        

Read More Read More

Control of soil humidity in an irrigation installation

Control of soil humidity in an irrigation installation

This newsletter is really interesting because control soil humidity in irrigation installations makes saving water, time and money.  Continue reading… sch0   Hardware

Hardware selection is very important when you try to develop projects where sensors, actuators and PLC´s must talk each other. For this project, with chosen the following equipment:

  • PLC: Any PLC from IndustrialShields is a good option. We have chosen the M-Duino PLC Arduino 21 I/Os Analog/Digital because we think it has different communication options to be communicated with other systems (Ethernet, RS232, I2C…).
  • Panel PC: For this project, we are going to visualize and control our installation with the HummTouch 10.1” Linux panel pc.
  • Humidity sensor: Probably, when you try to develop an application like we are trying to, the most important selection is the sensor itself. There are a lot of humidity sensors available around the market completely compatible with Arduino based hardware. We´ve chosen the SHT10 sensor from Adafruit because it includes a temperature sensor as well, it comes with an intermetal mesh encasing, which is weatherproof and because it is designed to be submersible in water.
  •  Others: A relay to activate the irrigation system, a 24Vdc power supply, a good enclosure to protect the equipment and cables are important things to keep in mind too.

Software

To develop the project, the first thing to do is to ensure that we have the proper software and libraries. As we are going to use the SHT10 sensor, we will use the SHT1x library that you can download in the following link: https://github.com/practicalarduino/SHT1x. SHT10 uses a two wire communication interface similar to I2C. As usual, we will use the Arduino IDE to develop the software. You can download it here: http://www.arduino.cc/en/Main/Software. You can see the code in the following file: 2014111_arduino_code   Key benefits   There are a lot of benefits to implement a humidity control in an irrigation system. Let´s see the most important:

  • Saves Time: Automation does the job for you, so you can go on holidays knowing that your flowers will be maintained when you come back.
  •  Saves Money: No more water wasted, automation guarantees efficiency: water is used when is needed.
  •  Improves Growth: An irrigiation system guarantees flowers waterd with proper amount of water. In a reasonable time, you will have a greener garden.

  Contact us!

Do you have a process to be automated? Contact Industrialshields to buy the proper hardware and Opiron to implement the entire solution.

How to read an Analog Input

How to read an Analog Input

You can read the data of an Analog Input through:

...
int myAnalog;
...
void setup() {}

void loop() {
   myAnalog = analogRead(A0);
}

With this piece of code you’ll read the voltage of a sensor. As our analog input works between 0-10V, the read value (myAnalog) will be a value between 0-1023.

If the value of sensor is (3V), myAnalog value is 3*1023/10 = 307.

How to test / read data from console / RS232

How to test / read data from console / RS232

RS232 commnication is not necessary, but is very useful. You can write some code and you want to switch on a led. The easier way to know what is happening if the led isn’t turning on, is through RS232, and watching what are the values of the vars,…
 
So, if you have purchased an Ardbox, you’ll need to initialize communications through:

  Serial.begin(9600);
  while (!Serial) {}  

and after that, you’ll be able to send any value to the RS232 port, in order to show it, through the Arduino IDE console, minicom,…, or any other Serial Console that you have in your Laptop.

  Serial.println("MyVar value");

If you have an M-Duino, you don’t need the

  while (!Serial) {}  

line.

How to Modbus-Ethernet

How to Modbus-Ethernet

  • Modbus is a Serial communications protocol and it is used for communicating with several industrial devices. It may run over RS485, Ethernet, or whatever hardware interface.
  • If we have an ethernet connection between a lot of devices, we can use this hardware connection (eth), and over it, we can use our own protocol (custom protocol,…), or for instance, Modbus protocol. The way is the same as using RS485, but hardware connections will become much easier, the speed will be higher, and the possible frame errors will be controlled by TCP/IP layers.
  • So, if you know Modbus, you have an easier way to implement it!.
HVAC System activated through an SCADA

HVAC System activated through an SCADA

Introduction

As a continuation of the Air-conditioning system post, in today´s newsletter Opiron suggests to expand the HVAC system controlled through the Touchberry Pi Panel PC and Ardbox Relay from IndustrialShields. As the last post, the suggested SCADA that will run on the Touchberry Pi is the following: myscadatechnologies.com PanelPcSummarizing the previous post, we designed a system that was able to activate the air conditioning with a relay. In order to expand the system, we will include the following sensors to have a complete automatic HVAC system:

  • Inside temperature.
  • Outside temperature.
  • PID development in order to control the inside temperature (the relay will be controlled by the PID loop).

Additionally, the air conditioning system will be integrated into a complete home automation solution where the Scada will give us access to a light system control, open and close windows, and security cameras. Software and code: Inputs and outputs definition

What are inputs and outputs from the coding point of view? As inputs we will consider the inside and outside temperature. As output, the relay that activates the air conditioning that we already programmed in the last post. As mentioned in the last post, the SCADA is owned by myscadatechnologies.com. Its main advantages are that is Open Source based, and very easy to be used. We will use I2C to communicate the SCADA and the Ardbox PLC. The code to be uploaded is the next one: 2014111_arduino_code SCADA design: Control Screen

Once we have already defined PLC programming, it is time to define the screen we will make the interaction between man and machine (HMI). Designing SCADAs really forces us to simplify and decide what’s most important- what needs to stay on the screen. As we want to implement an easy to use temperature control screen, we will show a thermometer with the actual temperature inside, and 4 big windows to see the basic features of the system. The first window with the temperatures inside and outside, at right, the system status window to have an easy access to alarms and other parameters, and below the temperature PID´s,  one for the day and the other one for the night. At the left of the screen we will have a menu to access to the other parts of the system (lighting, temperatures, windows, cameras and service). image12 Key Benefits

The main benefits of the this suggested solution are:

  • Automatic temperature regulation: The relay is now controlled by the PID!
  • Scalable system: The purposed system permits to connect multiple devices to the Touchberry Pi.
  • Open Source based: This feature means big flexibility, big availability of resources on the internet in case of failures or change implementations, etc.
  • Remote control: Control climate conditions through an HMI panel.
Contact us!

Do you have a process to be automated? Contact Industrialshields to buy the proper hardware and Opiron to implement the entire solution.