Introduction

With the intent to build a small plant, I find the necessity to control it. With the whole functions required from a basic SCADA. I think that, a lot of you, with research intent, school, hobby, need to supervise,

control and of course acquire the data.

Safety and functional needs require:

  • Control of the variables with Hysteresis (value ON != value OFF)
  • Control of the variables with feedback
  • Set the value On or OFF and the feedback "Setpoint" run-time, via PC
  • Actuate the corrections
  • Detect the local actions (such push of buttons, example: a pump will be activated when the tank is full, or when I push a button, because I want empty it, and I must activate the pump also from PC)
  • If something is activated manually, show a warning
  • Program save in DB . Important.

Background

To allow the communication between my PC and my controller, there is the need to map the variables.

With an array we assign automatically a number to each variable. But which variables?. All those we are interested to set or also to read.

Therefore in the variable register take place :

  • Limits and Set-points
  • Status (on/off)
  • Analog and digital reads
  • Each sensor

Architecture

Now we organize the communication.

I need a protocol. Which define the meaning of a 8 byte token, which controller and Pc communicate.

This protocol is included in the class as "Sprotocol".

Using the code

Arduino

SCADA sistems, and SPADA for sure, are massively influenced from the system that monitor. And a certain point you have to recognize the nature of your variable. But is possible define a template: this is a Template of code for Arduino.

#include <EEPROM.h>
 
 //these are the index of the register (an array) of variables. For Example:
enum variables{ SYSTEM=0,
              LIGHT_ON=1, //threshold on
              LIGHT_OFF=2  //threshold off,
              LIGHT_READ=3, //lecture of the analog input, (also get in variable)


              L1=4,//status of the LED (my pump)
              };
#define VARIABLES_COUNT 10
    
              
enum msg_Type // Commands, errors, reports, type etc etc
          {
       
           }; 
  
struct register_record
{
    long value;
    byte warnings;


   //to save some space, the warnings are stored in just one byte. the 8 bytes
   //act like 8 boolean values. So it is possible attach 8 warning with each variable
  //the type of warning depend of the physic natur of the variable 
     


 register_record()
  {
    value=0;
    warnings=0; 
  }
  boolean GetWarning(byte i)
  {
    return (warnings & (1 << i))>0; 
  }
   void SetWarning(byte i)
  {
    warnings= warnings ^ (1 << i) ;
     
  }
}v[VARIABLES_COUNT ]; // declaration is here
 
 
 
 
 //Send the messages
 void SendMessage(byte a, byte *value ,byte msg_type)
 {
     
     Serial.write(cnctBegin);
.... 
);
 } 

In the loop function is possible recognize clearly the flow such in draw:

  • switch input Pc Message, and store it in the register
  • Catch local inputs (local inputs, are build on loco, therefore the code depend from your system, but the input must also be stored in a variable)
  • Read the sensors, level, ecc. and store also in a variable
  • NOW Process the variables, actuate the action, and also store in a variable the action
  • Send Reports(therefore also the values read by the sensors and the local inputs)
 
 //switch the incoming message(if the byte are 8, there is a whole token ready to read)
//For example a token  F0...
Read more »