05-08-2023, 08:53 PM
Could someone tell what is wrong in the code? it's not working with a hmi delta dop-107EV, I don't know what could be an error in the code.
#include <WiFi.h>
#include <ETH.h>
#include <ModbusIP_ESP8266.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PCF8574.h>
////////////////////////////////////////////////////////////////////
#define ETH_ADDR 0
#define ETH_POWER_PIN -1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
const int port = 502;
////////////////////////////////////////////////////////////////////
TwoWire I2Cone = TwoWire(0);
//TwoWire I2Ctwo = TwoWire(1);
// Set i2c address
PCF8574 pcf8574_I1(&I2Cone, 0x21, 4, 5);
PCF8574 pcf8574_I2(&I2Cone, 0x22, 4, 5);
PCF8574 pcf8574_R1(&I2Cone, 0x24, 4, 5);
PCF8574 pcf8574_R2(&I2Cone, 0x25, 4, 5);
////////////////////////////////////////////////////////////////////
const int LED_COIL = 100;
const int ledPin = pcf8574_R1.pinMode (P1); //GPIO0
const int TEST_HREG = 100;
////////////////////////////////////////////////////////////////////
IPAddress staticIP(192, 168, 0, 40);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);
////////////////////////////////////////////////////////////////////
ModbusIP mb;
ETH mb;
void setup() {
Serial.begin(115200);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
Serial.begin(115200);
if (WiFi.config(staticIP, gateway, subnet, dns, dns) == false) {
Serial.println("Configuration failed.");
}
////////////////////////////////////////////////////////////////////
// put your setup code here, to run once:
pcf8574_R1.pinMode(P0, OUTPUT);
pcf8574_R1.pinMode(P1, OUTPUT);
pcf8574_R1.pinMode(P2, OUTPUT);
pcf8574_I1.pinMode(P0, INPUT);
pcf8574_I1.pinMode(P1, INPUT);
pcf8574_I1.pinMode(P2, INPUT);
////////////////////////////////////////////////////////////////////
modbus.server(port);
mb.addCoil(LED_COIL);
}
void loop() {
// put your main code here, to run repeatedly:
mb.task();
//Attach ledPin to LED_COIL register
digitalWrite(ledPin, mb.Coil(LED_COIL));
delay(10);
}
#include <WiFi.h>
#include <ETH.h>
#include <ModbusIP_ESP8266.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PCF8574.h>
////////////////////////////////////////////////////////////////////
#define ETH_ADDR 0
#define ETH_POWER_PIN -1
#define ETH_MDC_PIN 23
#define ETH_MDIO_PIN 18
#define ETH_TYPE ETH_PHY_LAN8720
#define ETH_CLK_MODE ETH_CLOCK_GPIO17_OUT
const int port = 502;
////////////////////////////////////////////////////////////////////
TwoWire I2Cone = TwoWire(0);
//TwoWire I2Ctwo = TwoWire(1);
// Set i2c address
PCF8574 pcf8574_I1(&I2Cone, 0x21, 4, 5);
PCF8574 pcf8574_I2(&I2Cone, 0x22, 4, 5);
PCF8574 pcf8574_R1(&I2Cone, 0x24, 4, 5);
PCF8574 pcf8574_R2(&I2Cone, 0x25, 4, 5);
////////////////////////////////////////////////////////////////////
const int LED_COIL = 100;
const int ledPin = pcf8574_R1.pinMode (P1); //GPIO0
const int TEST_HREG = 100;
////////////////////////////////////////////////////////////////////
IPAddress staticIP(192, 168, 0, 40);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress dns(192, 168, 0, 1);
////////////////////////////////////////////////////////////////////
ModbusIP mb;
ETH mb;
void setup() {
Serial.begin(115200);
ETH.begin(ETH_ADDR, ETH_POWER_PIN, ETH_MDC_PIN, ETH_MDIO_PIN, ETH_TYPE, ETH_CLK_MODE);
Serial.begin(115200);
if (WiFi.config(staticIP, gateway, subnet, dns, dns) == false) {
Serial.println("Configuration failed.");
}
////////////////////////////////////////////////////////////////////
// put your setup code here, to run once:
pcf8574_R1.pinMode(P0, OUTPUT);
pcf8574_R1.pinMode(P1, OUTPUT);
pcf8574_R1.pinMode(P2, OUTPUT);
pcf8574_I1.pinMode(P0, INPUT);
pcf8574_I1.pinMode(P1, INPUT);
pcf8574_I1.pinMode(P2, INPUT);
////////////////////////////////////////////////////////////////////
modbus.server(port);
mb.addCoil(LED_COIL);
}
void loop() {
// put your main code here, to run repeatedly:
mb.task();
//Attach ledPin to LED_COIL register
digitalWrite(ledPin, mb.Coil(LED_COIL));
delay(10);
}