// Code generated by Arduino IoT Cloud
#include <ModbusRTU.h>
#include <HardwareSerial.h>
#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>
HardwareSerial Serialok(1);
ModbusRTU mb;
const char DEVICE_LOGIN_NAME[]  = "08c027d4-e5a2-4fb2-bd13-2abe71a69553";
const char SSID[]               = "Wokwi-GUEST";
const char PASS[]               = "";
const char DEVICE_KEY[]         = "2xi7vJkkI0CZopWywxoV0UM?Z";
String messg;
String data_send;
String request;
String response;
String mesg1 = "";
bool case1 = false;
bool case2 = false;
bool case3 = false;
bool case4 = false;
bool coil[10];
bool idis[10];
uint16_t hreg[10];
uint16_t ireg[10];
CloudSchedule scheduler;
bool send = false, ok = false;
int listreq = 0, req = 0;
void onSelectChange();
void onMessgChange();
void onSchedulerChange();
void initProperties() {
  ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME);
  ArduinoCloud.setSecretDeviceKey(DEVICE_KEY);
  ArduinoCloud.addProperty(messg, READWRITE, ON_CHANGE, onMessgChange);
  ArduinoCloud.addProperty(request, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(response, READ, ON_CHANGE, NULL);
  ArduinoCloud.addProperty(scheduler, READWRITE, ON_CHANGE, onSchedulerChange);
  ArduinoCloud.addProperty(listreq, READWRITE, ON_CHANGE, onListreqChange);
}
WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
void setup() {
  Serial.begin(9600);
  Serialok.begin(9600, SERIAL_8N1, 4, 2);
  mb.begin(&Serialok);
  mb.master();
  delay(1500);
  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}
void loop() {
  ArduinoCloud.update();
  if (send) {
    String d1 = getValue(data_send, ',', 0);
    String d2 = getValue(data_send, ',', 1);
    String d3 = getValue(data_send, ',', 2);
    String d4 = getValue(data_send, ',', 3);
    uint8_t id_slave = d1.toInt();
    uint8_t func_code = d2.toInt();
    uint16_t address = d3.toInt();
    uint16_t value = d4.toInt();
    switch (func_code) {
      case 1:
        mb.readCoil(id_slave, address, coil, value);
        case1 = true;
        break;
      case 2:
        mb.readIsts(id_slave, address, idis, value);
        case2 = true;
        break;
      case 3:
        mb.readHreg(id_slave, address, hreg, value);
        case3 = true;
        break;
      case 4:
        mb.readIreg(id_slave, address, ireg, value);
        case4 = true;
        break;
      case 5:
        mb.writeCoil(id_slave, address, value > 0);
        break;
      case 6:
        mb.writeHreg(id_slave, address, value);
        break;
    }
    while (mb.slave()) {
      mb.task();
      delay(10);
    }
    if (case1) {
      Serial.print("= ");
      for (int i = 0; i < value; i++) {
        Serial.print(coil[i]);
        mesg1 = mesg1 + String(coil[i]);
      }
      Serial.println();
      messg = "= " + mesg1;
      response = messg;
      case1 = false;
    }
    if (case2) {
      Serial.print("= ");
      for (int i = 0; i < value; i++) {
        Serial.print(idis[i]);
        mesg1 = mesg1 + String(idis[i]);
      }
      Serial.println();
      messg = "= " + mesg1;
      response = messg;
      case2 = false;
    }
    if (case3) {
      Serial.print("= ");
      for (int i = 0; i < (value - 1); i++) {
        Serial.print(hreg[i]);
        Serial.print(',');
        if (i == 0) mesg1 = String(hreg[i]) + ",";
        else mesg1 = mesg1 + String(hreg[i]) + ",";
      }
      Serial.println(hreg[value - 1]);
      messg = "= " + mesg1 + String(hreg[value - 1]);
      response = messg;
      case3 = false;
    }
    if (case4) {
      Serial.print("= ");
      for (int i = 0; i < (value - 1); i++) {
        Serial.print(ireg[i]);
        Serial.print(',');
        if (i == 0) mesg1 = String(ireg[i]) + ",";
        else mesg1 = mesg1 + String(ireg[i]) + ",";
      }
      Serial.println(ireg[value - 1]);
      messg = "= " + mesg1 + String(ireg[value - 1]);
      response = messg;
      case4 = false;
    }
    mesg1 = "";
    send = false;
  }
  if (listreq != req) {
    req = listreq;
    if (listreq == 0) request = "1,1,0,8";
    if (listreq == 1) request = "1,2,0,8";
    if (listreq == 2) request = "1,3,0,1";
    if (listreq == 3) request = "1,4,0,1";
    if (listreq == 4) request = "1,3,0,2";
    if (listreq == 5) request = "1,4,0,2";
    if (listreq == 6) request = "2,1,0,8";
    if (listreq == 7) request = "2,2,0,8";
    if (listreq == 8) request = "2,3,0,1";
    if (listreq == 9) request = "2,4,0,1";
    if (listreq == 10) request = "2,3,0,2";
    if (listreq == 11) request = "2,4,0,2";
    data_send = request;
    send = true;
  }
  if (scheduler.isActive()) {
    if (ok) {
      data_send = request;
      send = true;
      messg = data_send;
      ok = false;
      Serial.println(data_send);
    }
  } else ok = true;
}
void onMessgChange()  {
  data_send = messg;
  send = true;
  Serial.println(data_send);
}
void onListreqChange()  {
}
void onSchedulerChange()  {
}
String getValue(String data, char separator, int index) {
  int found = 0;
  int strIndex[] = { 0, -1 };
  int maxIndex = data.length() - 1;
  for (int i = 0; i <= maxIndex && found <= index; i++) {
    if (data.charAt(i) == separator || i == maxIndex) {
      found++;
      strIndex[0] = strIndex[1] + 1;
      strIndex[1] = (i == maxIndex) ? i + 1 : i;
    }
  }
  return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
4<------ 2------>
Modbus Master
Modbus Slave
TX/RX<---> PC