Hello, here is the code I used to connect to the HMI through Modbus RS485 on OPTA. I'm no expert, but it worked perfectly for me.
In addition, I also created an online dashboard on the Arduino platform, in addition to the HMI.
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/ce28a408-ef79-4e2d-a0b6-dea70ad05741
Arduino IoT Cloud Variables description
The following variables are automatically generated and updated when changes are made to the Thing
float i;
float med1;
float med2;
float p;
float p1;
float temperatura;
float temperatura2;
float v;
float voltageA0;
float voltageA1;
CloudLight led;
CloudLight nmax1;
CloudLight nmax2;
CloudLight sinalizacaob1;
CloudLight sinalizacaob2;
int b1;
int b2;
int ctr;
int liga;
Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/
bool estadobotao1 = 1;
bool estadobotao2 = 0;
bool estadobotao3 = 0;
#include <ArduinoModbus.h>
#include <ArduinoRS485.h>
constexpr auto baudrate{ 19200 };
constexpr auto btime{ 1.0f / baudrate };
constexpr auto predl{ btime * 9.6f * 3.5f * 1e6 };
constexpr auto postdl{ btime * 9.6f * 3.5f * 1e6 };
#include "thingProperties.h"
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(115200);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
delay(400);
RS485.setDelays(predl, postdl);
if (!ModbusRTUClient.begin(baudrate, SERIAL_8N2)) {
Serial.println("Erro Modbus");
while (1);
}
analogReadResolution(12);
pinMode(LED_D0, OUTPUT);
pinMode(LED_D1, OUTPUT);
pinMode(LED_D2, OUTPUT);
pinMode(LED_D3, OUTPUT);
pinMode(LED_USER, OUTPUT);
pinMode(BTN_USER, INPUT);
pinMode(LEDR, OUTPUT);
pinMode(D0, OUTPUT);
pinMode(D1, OUTPUT);
pinMode(D2, OUTPUT);
pinMode(D3, OUTPUT);
}
void loop() {
ArduinoCloud.update();
// Your code here
ctr++;
ModbusRTUClient.beginTransmission(2, HOLDING_REGISTERS, 1, 10);
ModbusRTUClient.write(ctr);
ModbusRTUClient.write(2);
ModbusRTUClient.write(3);
ModbusRTUClient.write(v);
ModbusRTUClient.write(p1);
ModbusRTUClient.write(voltageA0);
ModbusRTUClient.write(liga);
ModbusRTUClient.write(b1);
ModbusRTUClient.write(b2);
ModbusRTUClient.write(voltageA1);
auto estadoSaida = ModbusRTUClient.coilRead(2,1);
Serial.println("Conectado com sucesso");
if (!ModbusRTUClient.endTransmission()) {
Serial.print("failed! ");
Serial.println(ModbusRTUClient.lastError());
}
//0x09BA - Active Power Total 32490-32491, IEEE 754 T_Float, x 1W
p = readdata(0x21, 0X9BA);
//0x09C4 - Tensione 32500-32501, IEEE 754 T_Float, x
v = readdata(0x21, 0X9C4);
// 0x9D4 - corrente 32516-32517, IEEE 754 T_Float, x 1A
i = readdata(0x21, 0X9D4);
Serial.println(String(p, 1) + "W " + String(v, 1) + "V " + String(i, 3) + "A ");
delay(400);
p1 = p*10;
int sensorValueA0 = analogRead(A0);
voltageA0 = sensorValueA0 * (3.0 / 4095.0)/ 0.3;
med1 = voltageA0 * 10;
int sensorValueA1 = analogRead(A1);
voltageA1 = sensorValueA1 * (3.0 / 4095.0)/ 0.3;
med2 = voltageA1 * 10;
Serial.print("I1 value: ");
Serial.print(sensorValueA0);
Serial.print(" corresponding to ");
Serial.print(voltageA0, 5); // Print the voltage as a float with 5 decimal digits
Serial.println("Volts");
Serial.print("I2 value: ");
Serial.print(sensorValueA1);
Serial.print(" corresponding to ");
Serial.print(voltageA1, 5); // Print the voltage as a float with 5 decimal digits
Serial.println("Volts");
estadobotao2 = digitalRead(A2);
estadobotao3 = digitalRead(A3);
if(voltageA0 >= 8 || estadobotao2 == HIGH){
digitalWrite(D2, HIGH);
digitalWrite(LED_D2, HIGH);
}else{
digitalWrite(D2, LOW);
digitalWrite(LED_D2, LOW);
}
if(voltageA1 >= 9 || estadobotao3 == HIGH){
digitalWrite(D3, HIGH);
digitalWrite(LED_D3, HIGH);
}else{
digitalWrite(D3, LOW);
digitalWrite(LED_D3, LOW);
}
if(med1 >= 80){
sinalizacaob1 = HIGH;
}else{
sinalizacaob1 = LOW;
}
if(med2 >= 90){
sinalizacaob2 = HIGH;
}else{
sinalizacaob2 = LOW;
}
if(med1 >=100){
nmax1 = HIGH;
}else{
nmax1 = LOW;
}
if(med2 >=100){
nmax2 = HIGH;
}else{
nmax2 = LOW;
}
estadobotao1 = digitalRead(BTN_USER);
if(estadobotao1 == LOW || led == HIGH){
digitalWrite(D1, HIGH);
digitalWrite(LED_D1, HIGH);
}else{
digitalWrite(D1, LOW);
digitalWrite(LED_D1, LOW);
}
if(estadoSaida){
digitalWrite(LED_USER, HIGH);
digitalWrite(LEDR, LOW);
delay(300);
digitalWrite(LED_USER, LOW);
digitalWrite(LEDR, HIGH);
delay(50);
digitalWrite(LED_D0, LOW);
digitalWrite(D0, LOW);
}else{
digitalWrite(LED_D0, HIGH);
digitalWrite(D0, HIGH);
digitalWrite(LED_USER, HIGH);
digitalWrite(LEDR, HIGH);
}
int sensorValueA2 = analogRead(A4);
float voltageA2 = sensorValueA2 * (3.0 / 4095.0)/ 0.3;
temperatura = voltageA2 * 19;
Serial.print("Temperatura: ");
Serial.print(temperatura);
Serial.println(" °C");
// Read the input on analog input I1 corresponding to A0:
int sensorValueA3 = analogRead(A5);
float voltageA3 = sensorValueA3 * (3.0 / 4095.0)/ 0.3;
temperatura2 = voltageA3 * 19;
Serial.print("Temperatura 2: ");
Serial.print(temperatura2);
Serial.println(" °C");
}
float readdata(int addr, int reg) {
float res = 0.0;
if (!ModbusRTUClient.requestFrom(addr, INPUT_REGISTERS, reg, 2)) {
Serial.println("Erro de comunicação");
Serial.println(ModbusRTUClient.lastError());
} else {
uint16_t word1 = ModbusRTUClient.read();
uint16_t word2 = ModbusRTUClient.read();
uint32_t parz = word1 << 16 | word2;
res = *(float *)&parz;
}
return res;
}
/*
Since Ctr is READ_WRITE variable, onCtrChange() is
executed every time a new value is received from IoT Cloud.
*/
void onCtrChange() {
// Add your code here to act upon Ctr change
}