NEXTuino HMI with Modbus

Intro

This tutorial demonstrates connecting a NEXTuino HMI (Beijer panel with iX software) to NEXTuino via Modbus. The iX Developer software runs on Windows and programs iX operator panels.

IMPORTANT: Please select the proper target board in Tools > Board > NEXTuino RISE/MAXI/MEGA before uploading.

MODBUS TCP/IP

Hardware Required

  • NEXTuino PEAK/MEGA
  • 24V DC Power supply
  • NEXTuino HMI panel
  • 2x Ethernet cables
  • Router

HMI Setup

  1. Download the NEXTuino HMI TCP/IP iX example and open in iX software
  2. Open Tags to configure application addresses
  3. In Controllers tab, choose MODICON > Modbus Master — activate Modbus_TCP controller
  4. In Settings: set communication mode to Ethernet TCP/IP and set the NEXTuino IP address
  5. Get the IP address by running the Arduino IDE example File > Examples > Ethernet > DhcpAddressPrinter

Code (TCP/IP)

Install the Mudbus library (copy the Modbus folder to Arduino/libraries).

#include <NEXTuino.h>
#include <SPI.h>
#include <Ethernet.h>
#include "Mudbus.h"

Mudbus Mb;

int D0 = NEXTuino_D0;
int D1 = NEXTuino_D1;
int R0 = NEXTuino_R0;
int A0_ = NEXTuino_A0;

void setup() {
  uint8_t mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
  uint8_t ip[] = { 192, 168, 0, 103 }; // CHANGE to your NEXTuino IP
  uint8_t gateway[] = { 192, 168, 0, 254 };
  uint8_t subnet[] = { 255, 255, 255, 0 };
  Ethernet.begin(mac, ip, gateway, subnet);

  delay(5000);
  Serial.begin(9600);
  Serial.println("Started");

  pinMode(D0, OUTPUT);
  pinMode(D1, INPUT);
  pinMode(R0, OUTPUT);
  pinMode(A0_, INPUT);
}

void loop() {
  Mb.Run();
  analogWrite(D0, Mb.R[0]);
  digitalWrite(R0, Mb.R[2]);
  Mb.R[1] = digitalRead(D1);
  Mb.R[3] = analogRead(A0_);
}

MODBUS RTU via RS485

Hardware Required

  • NEXTuino PEAK/MEGA
  • 24V DC Power supply
  • NEXTuino HMI panel
  • 3 wires or RS-485 serial port adapter

HMI Setup

  1. Open the iX example, select Modbus_RTU controller
  2. In Settings: set communication mode to Serial, COM2 port (RS-485)
  3. Match baud rate between NEXTuino and HMI

Note: The HMI communicates with NEXTuino slave on station 5 (5:40001 addresses holding register 40001 in station 5).

Code (RTU Slave)

#include <NEXTuino.h>
#include "ModbusRtu.h"

#define SlaveModbusAdd 5
#define RS485Serial    3

Modbus NEXTuinoModbusSlave(SlaveModbusAdd, RS485Serial, 0);
uint16_t ModbusSlaveRegisters[8];

int D0 = NEXTuino_D0;
int D1 = NEXTuino_D1;
int R0 = NEXTuino_R0;
int A0_ = NEXTuino_A0;

void setup() {
  delay(5000);
  Serial.begin(9600);
  Serial.println("Started");

  pinMode(D0, OUTPUT);
  pinMode(D1, INPUT);
  pinMode(R0, OUTPUT);
  pinMode(A0_, INPUT);

  NEXTuinoModbusSlave.begin(19200);
}

void loop() {
  NEXTuinoModbusSlave.poll(ModbusSlaveRegisters, 8);

  analogWrite(D0, ModbusSlaveRegisters[0]);
  digitalWrite(R0, ModbusSlaveRegisters[2]);
  ModbusSlaveRegisters[1] = digitalRead(D1);
  ModbusSlaveRegisters[3] = analogRead(A0_);
}