Try PLC controlled with WiFi and RS-485

Try PLC controlled with WiFi and RS-485 © GPL3+

Check the condition of your machine from anywhere.

  • 1,529 views
  • 0 comments
  • 1 respect

Components and supplies

Necessary tools and machines

Apps and online services

About this project

Code

Aside_trArduino
// THIS IS THE "A" SIDE

#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
#include <SPI.h>
#include <WiFiNINA.h>
#define BLYNK_PRINT SerialUSB
#include <BlynkSimpleWifi.h>

// YOUR WIFI
char ssid[] = "traumwelt";        
char pass[] = "traumwelt!!"; 

// BLYNK'S TOKEN
char auth[] = "HQhd9md5NDpnZ9VOlBf7wIy1z7D6C9L7";
WiFiServer server(23);

float temp4; 
int16_t TEMP4;
uint8_t Device4;
int16_t Dev_Valu4;

WidgetBridge bridge1(V0);
BlynkTimer timer;

BLYNK_CONNECTED() 
    {
       bridge1.setAuthToken("P7Gh59GKqz90pdKrrEc4F5ki3BJLupe9");       
    }  
       
void setup() 
{  
  Serial.begin(9600);                             
  WiFi.begin(ssid, pass);                          
  Blynk.begin(auth, ssid, pass);

  
  timer.setInterval(1000L,TempRead);
   
 while(Blynk.connect()==false){    
  }
  
  if (WiFi.status() == WL_NO_MODULE) 
  {
    Serial.println("Communication with WiFi module failed!");
   
    while (true);
  }  
 
  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print("Attempting to start AP ");
    Serial.println(ssid);

    delay(10);
  }
  
   IPAddress localIp(192, 168, 0, 57);
   WiFi.config(localIp);  
   
   server.begin();  
   
   while (!Serial);
    Serial.println("Modbus Temperature Read By Wifi");    // start the Modbus RTU client
    if (!ModbusRTUClient.begin(2400,SERIAL_8N2)) 
    {      
        Serial.println("Failed to start Modbus RTU Client!");
        while (1);
    } 
    printWifiStatus();    
  }
       
void loop () 
{  
   
   Blynk.run();
    timer.run(); 
}

void wifiReadfromhmi()
{
    // RECEIVE DATA BY WIFI
    WiFiClient client = server.available(); 
    while (client.available())
       {            
            char ch = client.read();                       
            if(client.find("A4:")==true)             
              {
              Device4=client.parseInt();             
              Serial.print("Device4:");
              Serial.println(Device4);              
              }               
       }
       comm();
}
void Comm_Dev()
{
  // RECEIVED THE DATA OF TEMPERATURE DEVICE 
  
 if (!ModbusRTUClient.requestFrom(0x04, COILS, 0x0000,1)) 
           {
             Serial.print("failed to read registers! ");
             
             Serial.println(ModbusRTUClient.lastError());
           } 
             else 
                {
                 Dev_Valu4 = ModbusRTUClient.read();                                 
                 Serial.print("Dev_Valu4 :  ");                 
                 Serial.println(Dev_Valu4);                                                                       
                }
 }
 void comm()
 {
  if ( !Dev_Valu4==Device4)
      {
        ModbusRTUClient.coilWrite(0x04,0x0000,Device4);
      }
 }
void TempRead()
   {         
     wifiReadfromhmi();                                
    if (!ModbusRTUClient.requestFrom(0x04, INPUT_REGISTERS, 0x03E8, 1)) 
           {
             Serial.print("failed to read registers! ");
             Blynk.virtualWrite(V4,"failed to read registers!");
             Serial.println(ModbusRTUClient.lastError());
           } 
             else 
                {
                 TEMP4 = ModbusRTUClient.read();
                 temp4 = (TEMP4/10+TEMP4%10*0.1);                
                 Serial.print("TEMP4 :  ");                 
                 Serial.println(temp4);                                 
                 bridge1.virtualWrite(V8, TEMP4);                      
                 Blynk.virtualWrite(V4,TEMP4);                                                        
                } 
                             
        Comm_Dev();
              
         
     }
 
 void printWifiStatus() 
 {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Bside_TRArduino
// THIS IS THE "B" SIDE

#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
#include <SPI.h>
#include <WiFiNINA.h>
#define BLYNK_PRINT SerialUSB
#include <BlynkSimpleWifi.h>

BlynkTimer timer;

// YOUR WIFI
char ssid[] = "traumwelt";      
char pass[] = "traumwelt!!"; 
char auth[] = "P7GH59GKqz90pdKrrEc4F5ki3BJLupe9";
          
int status = WL_IDLE_STATUS;
char server[] = "192.168.0.57";
WiFiClient client;

// VARIABLB IN THH B SIDE
int16_t Aside_TEMP4;
int16_t Bside_TEMP4;
int8_t HMIDATA4;

/* 1. HoldingRegister ADDRESS FROM THE PLC TO THE ARDUINO
   2. PLEASE MAKE IT SAME WRITING ADDRESS IN THE YOUR PLC */
    
const int HmiData4 = 0x0004;


/* 1. InputRegister ADDRESS FROM THE ARDUINO TO THE PLC
   2. PLEASE MAKE IT SAME READING ADDRESS IN THE YOUR PLC */
   
const int Plc_TEMP4 = 0x0003;

// RECIEVED THE DATA OF TEMPERATURE AT "A" SIDE FROM THE BLYNK SERVER.
 
BLYNK_WRITE(V8)
  {
     Aside_TEMP4 = param.asInt();         
  } 

void setup() 
{  
  Serial.begin(9600);                             
  WiFi.begin(ssid, pass);                          
  Blynk.begin(auth, ssid, pass);
  
  printWifiStatus(); 
  
  timer.setInterval(1000L,Read_Write_Data );

  while(Blynk.connect()==false){    
  }
 
  if (WiFi.status() == WL_NO_MODULE) 
  {
    Serial.println("Communication with WiFi module failed!");
   
    while (true);
  }   
 
  while (WiFi.status() != WL_CONNECTED) 
  {
    Serial.print(".");
    delay(500);
  }      
     if(!ModbusRTUServer.begin(10,2400,SERIAL_8N2))
     {
    Serial.println("Failed to start Modbus RTU Server!");
    while (1);
     }
       
   /* 1. I SET IT AT THE INPUT REGISTER ADDRESS IN ARDUINO 
      2. I SET IT AT THE HOLDING REGISTER ADDRESS IN ARDUINO */
      
     ModbusRTUServer.configureInputRegisters(0x0003,1);
     ModbusRTUServer.configureHoldingRegisters(0x0004,1);        
  }
  
void loop () 
{    
   Blynk.run();
   timer.run();
} 
    
 void Read_Write_Data() 
 {
     Read_By_PLC();
     Read_From_Plc();

     /* 1. PUT THE VARIABLE VALUE RECEIVED FROM BLYNK'S SERVER AT THE inputRegister ADDRESS
        2. THE ENTERED VALUE IS SENDED TO PLC */     
        
     ModbusRTUServer.inputRegisterWrite(Plc_TEMP4, Aside_TEMP4);  
     Bside_TEMP4 = ModbusRTUServer.inputRegisterRead(Plc_TEMP4);
     Serial.println("Aside_TEMP4");
     Serial.println(Aside_TEMP4);
     Serial.println("Bside_TEMP4");
     Serial.println(Bside_TEMP4); 
     
 }
     
void Read_From_Plc()
    {
      /* 1.SENDS THE VARIABLE TO "MKR WIFI 1010 SHIELD" OF DEVICE SIDE BY WIFI
    
        2.SENDS DATA THROUHG THE BLYNK SERVER. */   
     
     if (client.connect(server,23)) 
     {
      client.print("HMIDATA4:");
      client.println(HMIDATA4);           
     }                   
     Blynk.virtualWrite(V12,HMIDATA4);
     
         
 }
 
 
 void Read_By_PLC()
 {    
      /* SENDS DATA AT CLIENT'S (YOUR PLC) REQUEST */
      ModbusRTUServer.poll();            
      ModbusRTUClient.inputRegisterRead(Plc_TEMP4); 
      HMIDATA4 = ModbusRTUServer.holdingRegisterRead(HmiData4);      
      Serial.print("HMIDATA4:");
      Serial.println(HMIDATA4);     
     
  }

 
 void printWifiStatus() 
 {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

Schematics

Temperature Controller Manual
PLC MANUAL

Comments

Similar projects you might like

RGB Lighting Reactive to Music and WiFi Controlled

Project tutorial by Philipp Jurasic

  • 6,122 views
  • 2 comments
  • 28 respects

Wifi Controlled Car with Central Hub

Project showcase by Cooper and Vaughn

  • 4,467 views
  • 0 comments
  • 12 respects

Arduino-ESP WiFi Integration

Project in progress by Turai Botond

  • 18,262 views
  • 11 comments
  • 61 respects

Arduino Controlled Smart Hydroponic Modular System

Project in progress by Luis Antonio Martin Nuez

  • 49,252 views
  • 14 comments
  • 159 respects

Gestured Controlled Smart Home

Project in progress by Joaquim Silveira

  • 24,073 views
  • 4 comments
  • 41 respects
Add projectSign up / Login