//C:\modpoll\win>modpoll -r 1 -c 10 -m tcp 192.168.1.167
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <esp_system.h>

const int ONE_WIRE_BUS = 14; // GPIO2

OneWire oneWire(ONE_WIRE_BUS); //installeren onewire bus
DallasTemperature sensors(&oneWire);//installeren temperatuur sensor

int var1 = 2;


const char* ssid = "Wokwi-GUEST";//SSID
const char* password = "";//SSID password
int ModbusTCP_port = 502;

//////// Required for Modbus TCP / IP /// Requerido para Modbus TCP/IP /////////
#define maxInputRegister 20
#define maxHoldingRegister 20

#define MB_FC_NONE 0
#define MB_FC_READ_REGISTERS 3 //implemented
#define MB_FC_WRITE_REGISTER 6 //implemented
#define MB_FC_WRITE_MULTIPLE_REGISTERS 16 //implemented
//
// MODBUS Error Codes
//
#define MB_EC_NONE 0
#define MB_EC_ILLEGAL_FUNCTION 1
#define MB_EC_ILLEGAL_DATA_ADDRESS 2
#define MB_EC_ILLEGAL_DATA_VALUE 3
#define MB_EC_SLAVE_DEVICE_FAILURE 4
//
// MODBUS MBAP offsets
//
#define MB_TCP_TID 0
#define MB_TCP_PID 2
#define MB_TCP_LEN 4
#define MB_TCP_UID 6
#define MB_TCP_FUNC 7
#define MB_TCP_REGISTER_START 8
#define MB_TCP_REGISTER_NUMBER 10

byte ByteArray[260];
int MBHoldingRegister[maxHoldingRegister];

//////////////////////////////////////////////////////////////////////////

WiFiServer MBServer(ModbusTCP_port);

// Set your Static IP address
IPAddress local_IP(192, 168, 1, 50);
// Set your Gateway IP address
IPAddress gateway(192, 168, 1, 1);

IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8);   // optional
IPAddress secondaryDNS(8, 8, 4, 4); // optional

void setup() {

 pinMode(13, OUTPUT);
 
  // Seriële monitor starten
  Serial.begin(9600);

  // Verbinding maken met WiFi
  WiFi.begin(ssid, password);

  // Wachten tot WiFi is verbonden
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Verbinden met WiFi...");
  }

  // Statische IP configuratie toepassen
  if (!WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS)) {
    Serial.println("Fout bij het configureren van een statisch IP-adres.");
  }

  // Verbinding opnieuw tot stand brengen met de nieuwe configuratie
  WiFi.disconnect();
  WiFi.begin(ssid, password);
  // Wachten tot WiFi opnieuw is verbonden
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Opnieuw verbinden met WiFi...");
  }

  // Verbinding geslaagd
  Serial.println("Verbonden met WiFi");
  Serial.print("IP-adres: ");
  Serial.println(WiFi.localIP());

 MBServer.begin();
 Serial.println("Connected ");
 Serial.print("ESP8266 Slave Modbus TCP/IP ");
 Serial.print(WiFi.localIP());
 Serial.print(":");
 Serial.println(String(ModbusTCP_port));
 Serial.println("Modbus TCP/IP Online");
}



void loop() {

 // Check if a client has connected // Modbus TCP/IP
 WiFiClient client = MBServer.available();
 if (!client) {
 return;
 }
 

 boolean flagClientConnected = 0;
 byte byteFN = MB_FC_NONE;
 int Start;
 int WordDataLength;
 int ByteDataLength;
 int MessageLength;
 
 // Modbus TCP/IP
 while (client.connected()) {
 
 if(client.available())
 {
 flagClientConnected = 1;
 int i = 0;
 while(client.available())
 {
 ByteArray[i] = client.read();
 i++;
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 }

 client.flush();


//aanvragen temperatuur
sensors.requestTemperatures();

// Lees de temperatuurwaarde en sla deze op in de variabele 'temperatuur'
float temperatuur = sensors.getTempCByIndex(0);
int temp2 = (temperatuur*100);

///// code here --- codigo aqui

 ///////// Holding Register [0] A [9] = 10 Holding Registers Escritura
 ///////// Holding Register [0] A [9] = 10 Holding Registers Writing
 
 MBHoldingRegister[0] = random(0,99);
 MBHoldingRegister[1] = 1;
 MBHoldingRegister[2] = var1;
 MBHoldingRegister[3] = temp2;
 MBHoldingRegister[4] = random(0,12);
 MBHoldingRegister[5] = random(0,12);
 MBHoldingRegister[6] = random(0,12);
 MBHoldingRegister[7] = random(0,12);
 MBHoldingRegister[8] = random(0,12);
 MBHoldingRegister[9] = random(0,12);

 


 ///////// Holding Register [10] A [19] = 10 Holding Registers Lectura
 ///// Holding Register [10] A [19] = 10 Holding Registers Reading
 
 int Temporal[10];
 
 Temporal[0] = MBHoldingRegister[10];
 Temporal[1] = MBHoldingRegister[11];
 Temporal[2] = MBHoldingRegister[12];
 Temporal[3] = MBHoldingRegister[13];
 Temporal[4] = MBHoldingRegister[14];
 Temporal[5] = MBHoldingRegister[15];
 Temporal[6] = MBHoldingRegister[16];
 Temporal[7] = MBHoldingRegister[17];
 Temporal[8] = MBHoldingRegister[18];
 Temporal[9] = MBHoldingRegister[19];

 /// Enable Output 14
 digitalWrite(14, MBHoldingRegister[14] );


 //// debug
/*
 for (int i = 0; i < 10; i++) {

 Serial.print("  [W");
 Serial.print(i);
 Serial.print("] : ");
 Serial.print(MBHoldingRegister[i]);

 Serial.print("  [R");
 Serial.print(i);
 Serial.print("] : ");
 Serial.print(Temporal[i]);
 
 }
 Serial.println("");
*/
Serial.println("R/W:ok");


//// end code - fin 
 

 //// rutine Modbus TCP
 byteFN = ByteArray[MB_TCP_FUNC];
 Start = word(ByteArray[MB_TCP_REGISTER_START],ByteArray[MB_TCP_REGISTER_START+1]);
 WordDataLength = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);
 }
 
 // Handle request

 switch(byteFN) {
 case MB_FC_NONE:
 break;
 
 case MB_FC_READ_REGISTERS: // 03 Read Holding Registers
 ByteDataLength = WordDataLength * 2;
 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
 ByteArray[8] = ByteDataLength; //Number of bytes after this one (or number of bytes of data).
 for(int i = 0; i < WordDataLength; i++)
 {
 ByteArray[ 9 + i * 2] = highByte(MBHoldingRegister[Start + i]);
 ByteArray[10 + i * 2] = lowByte(MBHoldingRegister[Start + i]);
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 }
 MessageLength = ByteDataLength + 9;
 client.write((const uint8_t *)ByteArray,MessageLength);
 
 byteFN = MB_FC_NONE;
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 break;
 
 
 case MB_FC_WRITE_REGISTER: // 06 Write Holding Register
 MBHoldingRegister[Start] = word(ByteArray[MB_TCP_REGISTER_NUMBER],ByteArray[MB_TCP_REGISTER_NUMBER+1]);
 ByteArray[5] = 6; //Number of bytes after this one.
 MessageLength = 12;
 client.write((const uint8_t *)ByteArray,MessageLength);
 byteFN = MB_FC_NONE;
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 break;
 
 case MB_FC_WRITE_MULTIPLE_REGISTERS: //16 Write Holding Registers
 ByteDataLength = WordDataLength * 2;
 ByteArray[5] = ByteDataLength + 3; //Number of bytes after this one.
 for(int i = 0; i < WordDataLength; i++)
 {
 MBHoldingRegister[Start + i] = word(ByteArray[ 13 + i * 2],ByteArray[14 + i * 2]);
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 }
 MessageLength = 12;
 client.write((const uint8_t *)ByteArray,MessageLength); 
 byteFN = MB_FC_NONE;
 yield();  // Voeg hier yield() toe om te voorkomen dat de loop vastloopt
 break;
 }
 }
if (!client.connected()) {
    // Acties om uit te voeren wanneer de client de verbinding verbreekt
    Serial.println("Client disconnected");
    esp_restart();
}

}