Glenn

Who is interested in an arduino openplc modbus to mqtt converter?

interested
100%
not interested
0%
2 votes This poll has been closed.
If someone is interested in some custom arduino code for the arduino mega to work as generic modbus tcp device.

Requirements:
-Master with openplc runtime
-Slave (arduino mega with ethernet shield)
-Arduino libraries (Ethernet.h and ArduinoModbus.h)

Slave settings in openplc runtime:

Slave settings.jpg 
Arduino code:

#include <Ethernet.h>
#include <ArduinoModbus.h>

int client_id = 1; //specify client id here
const int ledPin = LED_BUILTIN;
const int input_1Pin = 3; //define input pin
bool input_state = true; //previous input state

EthernetServer EthServer(502);
ModbusTCPServer modbusTCPServer;

void setup() {
  // Ethernet Settings
  byte mac[] = { 0x4E, 0xA0, 0xBE, 0x3F, 0xFE, 0x0F };  // Define MAc address
  byte ip[] = { 192, 168, 1, 88 };                      // Define IP address
  byte subnet[] = { 255, 255, 255, 0 };                 // Define SubNEt mask

  // initialize the ethernet device
  Ethernet.begin(mac, ip, subnet);                      // Assign MAC, IP, and subnet mask
  Serial.begin(9600);

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println("Ethernet cable is not connected.");
  }
  
  EthServer.begin();          // start listening for clients
  modbusTCPServer.begin(client_id);    // start listening for clients
  
  // configure IO
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  pinMode(input_1Pin, INPUT_PULLUP);
  
  // Define registers:
  modbusTCPServer.configureCoils(0, 8); //(address, size) (%QX100.0-100.7)
  modbusTCPServer.configureDiscreteInputs(0, 8); //       (%IX100.0-100.7)
  modbusTCPServer.configureInputRegisters(0,8); //        (%IW100.0-100.7)
  modbusTCPServer.configureHoldingRegisters(0,8); //      (%QW100.0-100.7)
  
  Serial.print("Modbus server address=");
  Serial.println(Ethernet.localIP());
  Serial.print("\n");
}

void loop() {
  // Modbus server accept incoming connections
  EthernetClient client = EthServer.available();
  if (client.connected()) {
    modbusTCPServer.accept(client);
    // poll for Modbus TCP requests, while client connected
    modbusTCPServer.poll();
    Serial.print("poll");
  }

  //----------------------------------Call custom functions here-----------------------------------------------
  read_input1();
  updateLED();
}

void updateLED() {
  // read the current value of the coil
  int coilValue = modbusTCPServer.coilRead(0);
  if (coilValue) {
    // coil value set, turn LED on
    digitalWrite(ledPin, HIGH);
  } else {
    // coild value clear, turn LED off
    digitalWrite(ledPin, LOW);
  }
}

void read_input1(){
  bool input_1 = digitalRead(input_1Pin);
  
  if(input_1 == false && input_state == true){
    modbusTCPServer.discreteInputWrite(0, 1);
    Serial.println("Input == High");
    input_state = false;
  }else if(input_1 == true && input_state == false){
    modbusTCPServer.discreteInputWrite(0, 0);
    Serial.println("Input == Low");
    input_state = true;
  }
}


Link to arduino modbus instruction set: https://github.com/arduino-libraries/ArduinoModbus/blob/master/docs/api.md
Reply 1 0
Seafoxc
Thanks for sharing! 
I have no Idea what I'm doing...
Reply 0 0
Reply