ENC28J60 Ip Loop

Hello, yesterday I want to connect my Tracer Solar Charge Controller to the internet using blynk, I succeded connecting it with an esp8266/esp-01 and a TTL to rs485 converter, but now I want to run it with an ethernet module (ENC28J60) with an Arduino nano so I can put a POE Injector, But I ran with issues that caused the blynk client to loop getting an IP,although it already have an IP,I tried using the blank skecth and it did connect successfully to the server,I have tried setting a static IP with
Blynk.begin(auth, IPAddress(192,168,11,101), 8080,IPAddress(192,168,11,22));
but it just loops again.
The Code :

#define BLYNK_PRINT Serial
#include <ModbusMaster.h>

#include <UIPEthernet.h>
#include <BlynkSimpleUIPEthernet.h>

#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0]))
int timerTask1, timerTask2, timerTask3;
float battChargeCurrent, battDischargeCurrent, battOverallCurrent, battChargePower;
float bvoltage, ctemp, btemp, bremaining, lpower, lcurrent, pvvoltage, pvcurrent, pvpower;
float stats_today_pv_volt_min, stats_today_pv_volt_max;
uint8_t result;
bool rs485DataReceived = true;
bool loadPoweredOn = true;

char auth[] = "ZeAPleo1PlgxD8qMIzysAriIRqjHpB5b";

ModbusMaster node;
SimpleTimer timer;

void preTransmission() {
digitalWrite(3, HIGH);
}

void postTransmission() {
digitalWrite(3, LOW);
}

// A list of the regisities to query in order
typedef void (*RegistryList[])();

RegistryList Registries = {
  AddressRegistry_3100,
  AddressRegistry_3106,
  AddressRegistry_310D,
  AddressRegistry_311A,
  AddressRegistry_331B,
};

// keep log of where we are
uint8_t currentRegistryNumber = 0;

// function to switch to next registry
void nextRegistryNumber() {
  currentRegistryNumber++;
  if (currentRegistryNumber >= ARRAY_SIZE(Registries)) {
    currentRegistryNumber = 0;
  }
}

void setup()
{
  Serial.begin(9600);            
  Blynk.begin(auth, IPAddress(192,168,11,101), 8080);
  
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  
  node.begin(1, Serial);
  node.preTransmission(preTransmission);
  node.postTransmission(postTransmission);
  
  timerTask1 = timer.setInterval(10000, executeCurrentRegistryFunction);
  timerTask2 = timer.setInterval(10000, nextRegistryNumber);
  timerTask3 = timer.setInterval(1000, uploadToBlynk);
}

  void uploadToBlynk() {
//    Blynk.virtualWrite(V1,pvpower);
//    Blynk.virtualWrite(V2,pvcurrent);
//    Blynk.virtualWrite(V3,pvvoltage);
//    Blynk.virtualWrite(V4,lcurrent);
//    Blynk.virtualWrite(V5,lpower);
//    Blynk.virtualWrite(V6,btemp);
//    Blynk.virtualWrite(V7,bvoltage);
//    Blynk.virtualWrite(V8,bremaining);
//    Blynk.virtualWrite(V9,ctemp);
//    Blynk.virtualWrite(V10,battChargeCurrent);
//    Blynk.virtualWrite(V11,battChargePower);
//    Blynk.virtualWrite(V12,battOverallCurrent);
//    Blynk.virtualWrite(V14,loadPoweredOn);
  }
 
  // exec a function of registry read (cycles between different addresses)
  void executeCurrentRegistryFunction() {
    Registries[currentRegistryNumber]();
  }
  
  uint8_t setOutputLoadPower(uint8_t state) {
    delay(10);
    result = node.writeSingleCoil(0x0006, state);

    if (result == node.ku8MBSuccess) {
      node.getResponseBuffer(0x00);
    }
    return result;
  }
  BLYNK_WRITE(vPIN_LOAD_ENABLED) {
    uint8_t newState = (uint8_t)param.asInt();
    result = setOutputLoadPower(newState);
    readOutputLoadState();
    result &= checkLoadCoilState();
    uploadToBlynk();
  }
  
  uint8_t readOutputLoadState() {
    delay(10);
    result = node.readHoldingRegisters(0x903D, 1);
    if (result == node.ku8MBSuccess) {
      loadPoweredOn = (node.getResponseBuffer(0x00) & 0x02) > 0;
    }
    return result;
  }
  
  uint8_t checkLoadCoilState() {
    delay(10);
    result = node.readCoils(0x0006, 1);
    if (result == node.ku8MBSuccess) {
      loadPoweredOn = (node.getResponseBuffer(0x00) > 0);
    }
    return result;
 }
 
  void AddressRegistry_3100() {
    result = node.readInputRegisters(0x3100, 6);
    if (result == node.ku8MBSuccess) {      
      pvvoltage = node.getResponseBuffer(0x00) / 100.0f;
      pvcurrent = node.getResponseBuffer(0x01) / 100.0f;
      pvpower = (node.getResponseBuffer(0x02) | node.getResponseBuffer(0x03) << 16) / 100.0f;    
      bvoltage = node.getResponseBuffer(0x04) / 100.0f;     
      battChargeCurrent = node.getResponseBuffer(0x05) / 100.0f;
    }
  }
  
  void AddressRegistry_3106()
  {
    result = node.readInputRegisters(0x3106, 2);
    if (result == node.ku8MBSuccess) {
      battChargePower = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16)  / 100.0f;
    }
  }
  
  void AddressRegistry_310D() 
  {
    result = node.readInputRegisters(0x310D, 3);
    if (result == node.ku8MBSuccess) {
      lcurrent = node.getResponseBuffer(0x00) / 100.0f;
      lpower = (node.getResponseBuffer(0x01) | node.getResponseBuffer(0x02) << 16) / 100.0f;
    } else {
      rs485DataReceived = false;
    }    
  } 

  void AddressRegistry_311A() {
    result = node.readInputRegisters(0x311A, 2);
    if (result == node.ku8MBSuccess) {    
      bremaining = node.getResponseBuffer(0x00) / 1.0f;
      btemp = node.getResponseBuffer(0x01) / 100.0f;
    } else {
      rs485DataReceived = false;
    }
  }

  void AddressRegistry_331B() {
    result = node.readInputRegisters(0x331B, 2);
    if (result == node.ku8MBSuccess) {
      battOverallCurrent = (node.getResponseBuffer(0x00) | node.getResponseBuffer(0x01) << 16) / 100.0f;
    } else {
      rs485DataReceived = false;
    }
  }


void loop()
{
  Blynk.run();
  timer.run();
}

and this is the serial monitor Output:

[1252] IP:192.168.11.23
[1253] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[1] Getting IP...
[5262] IP:192.168.11.23
[5263] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[1] Getting IP...
[5263] IP:192.168.11.23
[5263] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[1] Getting IP...
[1254] IP:192.168.11.23
[1255] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[1] Getting IP...
[5262] IP:192.168.11.23
[5263] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[1] Getting IP...
[5263] IP:192.168.11.23
[5264] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 ⸮⸮⸮Getting IP...
[1254] IP:192.168.11.23
[1255] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /_[0] Getting IP...
[5263] IP:192.168.11.23
[5263] 

How much free memory did you have when you uploaded the sketch?

Pete.

Without the Blynk_PRINT Serial it occupies 97% of storage space and 95% of dynamic memory…

a running sketch doesn’t need more flash, but definitely needs some RAM for stack.

you can tweak some settings of the UIPEthernet library or try my EthernetENC library and tweak that, but I think you will have to use a different MCU for this project.

the Wiki of EtnernetENC describes the settings. most of them apply for UIPEthernet too

for EthernetENC use

#include <EthernetENC.h>
#include <BlynkSimpleEthernet.h>
1 Like

:scream:

@Juraj Thank you for the library,i tweaked some of the settings and remove some of the code to free up more space and now the arduino successfully connects to my router,but it still have problems with connecting it to my blynk local server,the output of the serial monitor just says

[1061] Connecting to 192.168.11.101
[25004] Connecting to 192.168.11.101
[45004] Connecting to 192.168.11.101
[65004] Connecting to 192.168.11.101

i have double checked the IP address of the local server and it matches in the code.

firewall on server?

I don’t think so, I have 5 other esp8266 that is connected to the server, and it ran just fine on startup. although last time I checked on the serial monitor the Arduino did connect to the local server with 3 ping, but on my blynk app the device is still offline, it went online just for a couple of seconds…