Blynk Ethernet ENC28J60 issues with RTC, Email and Terminal widgets

Update 2018-08-31: Scroll down for solution!

Hello,

I’m having some difficulties with three widgets, namely the terminal widget, the rtc widget and the email widget.

Hardware: Arduino UNO with ENC29J60 Ethernet module
App: Android
Server: Blynk Cloud
Library Version: 0.5.3

Terminal widget: As soon as I add the line terminal.flush(); to my code, my Arduino is unable to connect to the Blynk server.

Working Code:


/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

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

char auth[] = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; // Censored

boolean rcswitchstatus1 = false; // Pump 1
boolean rcswitchstatus2 = false; // Computer 1 Bedroom

boolean starttime_sent = false;

int blynkRead1; // Pump 1
int blynkRead2; // Computer 1 Bedroom
int led_status;

unsigned long blink_timer = millis(); 

WidgetTerminal terminal(V0);

RCSwitch mySwitch = RCSwitch();

BLYNK_WRITE(V4){
  blynkRead1 = param.asInt();
}

BLYNK_WRITE(V5){
  blynkRead2 = param.asInt();
}

void switchit(char tristate[20]){
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
}

void status_led(){
  if ((millis() - blink_timer) >= 2000){
    led_status = !digitalRead(9);
    digitalWrite(9, led_status);
    if (led_status == 1){
      Blynk.virtualWrite(V1, 1023);
    }
    else{
      Blynk.virtualWrite(V1, 0);
    }
    blink_timer = millis();
  }  
}

void setup()
{
  // Debug console
  Serial.begin(9600); 
  
  pinMode(9, OUTPUT);
  
  mySwitch.enableTransmit(6);
  mySwitch.setPulseLength(309);
  Blynk.begin(auth);
}

void loop()
{
  Blynk.run();
  status_led();
  if (starttime_sent == false and millis() > 1000){
    terminal.println("System Started");
    starttime_sent = true;
  }
  if (blynkRead1 == 1 && rcswitchstatus1 == false){
    terminal.println("Pump 1 ON");
    switchit("XXXXXXXXXXXX");
    rcswitchstatus1 = true;
  }
  if (blynkRead1 == 0 && rcswitchstatus1 == true){
    terminal.println("Pump 1 OFF");
    switchit("XXXXXXXXXXXX"); // Censored
    rcswitchstatus1 = false;
    
  }
  if (blynkRead2 == 1 && rcswitchstatus2 == false){
    terminal.println("Computer 1 ON");
    switchit("XXXXXXXXXXXX"); // Censored
    rcswitchstatus2 = true;
  }
  if (blynkRead2 == 0 && rcswitchstatus2 == true){
    terminal.println("Computer 1 OFF");
    switchit("XXXXXXXXXXXX"); // Censored
    rcswitchstatus2 = false;
  }
}

Adding terminal.flush to my code:

void loop()
{
  Blynk.run();
  status_led();
  if (starttime_sent == false and millis() > 1000){
    terminal.println("System Started");
    terminal.flush();
    starttime_sent = true;
  }
  if (blynkRead1 == 1 && rcswitchstatus1 == false){
    terminal.println("Pump 1 ON");
    terminal.flush();
    switchit("XXXXXXXXXXXX");
    rcswitchstatus1 = true;
  }
  if (blynkRead1 == 0 && rcswitchstatus1 == true){
    terminal.println("Pump 1 OFF");
    terminal.flush();
    switchit("XXXXXXXXXXX");
    rcswitchstatus1 = false;
    
  }
  if (blynkRead2 == 1 && rcswitchstatus2 == false){
    terminal.println("Computer 1 ON");
    terminal.flush();
    switchit("XXXXXXXXXX");
    rcswitchstatus2 = true;
  }
  if (blynkRead2 == 0 && rcswitchstatus2 == true){
    terminal.println("Computer 1 OFF");
    terminal.flush();
    switchit("XXXXXXXXXX");
    rcswitchstatus2 = false;
  }
}

Serial Output:

[0] Getting IP...
[5334] IP:192.168.178.21
[5335] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on Arduino Uno

  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library

[5531] Connecting to blynk-cloud.com:80
[6

Serial output stops here. No connection can be established and as soon as I remove the terminal.flush(); lines again, the code works fine.

Email Widget:

void loop()
{
  Blynk.run();
  status_led();
  if (starttime_sent == false and millis() > 1000){
    terminal.println("System Started");
    Blynk.email("XXXXXXXXXXXX@gmail.com", "Arduino 1 Connected", "Arduino 1 Successfully Connected");
    starttime_sent = true;
  }
  if (blynkRead1 == 1 && rcswitchstatus1 == false){
    terminal.println("Pump 1 ON");
    switchit("XXXXXXXXXXXXXXXXXXXXXX");
    rcswitchstatus1 = true;
  }
  if (blynkRead1 == 0 && rcswitchstatus1 == true){
    terminal.println("Pump 1 OFF");
    switchit("XXXXXXXXXXXXXXXXXXXXXXX");
    rcswitchstatus1 = false;
    
  }
  if (blynkRead2 == 1 && rcswitchstatus2 == false){
    terminal.println("Computer 1 ON");
    switchit("XXXXXXXXXXXXXXXXXXXXXXX");
    rcswitchstatus2 = true;
  }
  if (blynkRead2 == 0 && rcswitchstatus2 == true){
    terminal.println("Computer 1 OFF");
    switchit("XXXXXXXXXXXXXXXXXXXXXXX");
    rcswitchstatus2 = false;
  }
}

Serial Monitor:


[0] Getting IP...
[5334] IP:192.168.178.21
[5335] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on Arduino Uno

  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library

[1] Getting IP...
[5329] IP:192.168.178.21
[5330] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on Arduino Uno

  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library

[1] Getting IP...
[5347] IP:192.168.178.21
[5348] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.3 on Arduino Uno

  Give Blynk a Github star! => https://github.com/blynkkk/blynk-library

[1] Getting IP...

This happens over and over again. After removing the line, the Arduino connects instantly and works like expected.

RTC Widget:

/* Comment this out to disable prints and save space */
//#define BLYNK_PRINT Serial

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

#include <TimeLib.h>
#include <WidgetRTC.h>
BlynkTimer timer;

char auth[] = "XXXXXXXXXXXXXXXXXXXXXX";

boolean rcswitchstatus1 = false; // Pump 1
boolean rcswitchstatus2 = false; // Computer 1 Bedroom

boolean starttime_sent = false;

int blynkRead1; // Pump 1
int blynkRead2; // Computer 1 Bedroom
int led_status;

unsigned long blink_timer = millis(); 

WidgetTerminal terminal(V0);

WidgetRTC rtc;

RCSwitch mySwitch = RCSwitch();

BLYNK_WRITE(V4){
  blynkRead1 = param.asInt();
}

BLYNK_WRITE(V5){
  blynkRead2 = param.asInt();
}

BLYNK_CONNECTED() {
  rtc.begin();
}

void switchit(char tristate[20]){
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
  mySwitch.sendTriState(tristate);
}

void status_led(){
  if ((millis() - blink_timer) >= 2000){
    led_status = !digitalRead(9);
    digitalWrite(9, led_status);
    if (led_status == 1){
      Blynk.virtualWrite(V1, 1023);
    }
    else{
      Blynk.virtualWrite(V1, 0);
    }
    blink_timer = millis();
  }  
}

void setup()
{
  // Debug console
//  Serial.begin(9600); 
  
  pinMode(9, OUTPUT);
  
  mySwitch.enableTransmit(6);
  mySwitch.setPulseLength(309);
  Blynk.begin(auth);
  setSyncInterval(10 * 60);
}

void loop()
{
  Blynk.run();
  status_led();
  if (starttime_sent == false and millis() > 1000){
    terminal.println("System Started");
    starttime_sent = true;
  }
  if (blynkRead1 == 1 && rcswitchstatus1 == false){
    terminal.println("Pump 1 ON");
    switchit("XXXXXXXXXXXX");
    rcswitchstatus1 = true;
  }
  if (blynkRead1 == 0 && rcswitchstatus1 == true){
    terminal.println("Pump 1 OFF");
    switchit("XXXXXXXXXXXXXX");
    rcswitchstatus1 = false;
    
  }
  if (blynkRead2 == 1 && rcswitchstatus2 == false){
    terminal.println("Computer 1 ON");
    switchit("XXXXXXXXXXXXXX");
    rcswitchstatus2 = true;
  }
  if (blynkRead2 == 0 && rcswitchstatus2 == true){
    terminal.println("Computer 1 OFF");
    switchit("XXXXXXXXXXXXXX");
    rcswitchstatus2 = false;
  }
}

Same issue, no connection to Blynk server possible with the minium required lines for the RTC widget. I also tried a version without any other code, which didn’t work without removing the following function from the code, either. This doesn’t fix the issue with my code above, though.

BLYNK_CONNECTED() {
  rtc.begin();
}

I would really appreciate some help from you guys!

I had a quick look on your code and looks to me you’re flooding the system with multiple writes.

Avoid using Blynk tasks inside the void loop() check: Blynk Limitations and Recommendations

Thank you for your reply.

I don’t think this is the issue.

The Blynk commands inside the void loop are only executed when I manually turn on or off a switch on the app, which happens rarely. The functions BLYNK_WRITE(V4) and BLYNK_WRITE(V5) are only called when I change a value on the app by turning a switch on or off. Both functions change the global variables blynkRead1 and blynkRead2, which are used in the void loop. The status LED switches its state every two seconds. If 100 requests per second are allowed, this shouldn’t be an issue, either.

@Dmitriy I would really appreciate if you could take a look at my code. If the mistake isn’t on my side, it could be a bug I’m dealing with.

Nobody who is able to help me?

However when you DO activate a switch, then the associated commands could try to run thousands of times a second… Fix your code as suggested.

EDIT: OK looks like you may have prevented that with a flag… good… but you still have other options without resorting to unproven bug alerts :stuck_out_tongue:

Try Blynk.virtualWrite() commands instead of terminal.println() that way you shouldn’t need any flush command.

Also, as searching this forum would indicate… Arduino Ethernet adapters seem rather flaky at best. Try using the USB link to compare… or a WiFi adapter like ESP-01… or simply switch to standalone ESP8266 Dev board.

I will try using Blynk.virtualWrite() instead.

The ethernet module has been working very reliable and I don’t see how a single email command somewhere at a later stage in the code, where it’s not even executed (for example when it’s only executed when I press a switch and I don’t press a switch), should make it impossible for it to connect if it works reliable for days without it. This screams like a bug to me.

The issue doesn’t happen with the USB link connection. I suspect some sort of conflict with the used ethernet library.

Edit: Using Blynk.virtualWrite() works for the terminal.

I should receive a new W5100 ethernet module soon. I’ll report back if I experience the same issues with it. It would still be nice if somebody from Blynk took a look at this.

Okay, here is the solution:

I considered any possible cause, except the set connection type in the Blynk app. I had been playing around with multiple connection types and must have forgotten to switch it back to Ethernet after using the serial connection for some tests. The fact that all other features worked well made it more difficult to find the mistake. The weird thing is that I still have one W5100 ethernet module running with the wrong setting (serial connection) and it works totally fine.

I take all the blame and you’re totally allowed to make fun of me.

Thank you to everybody who tried to help me! I hope somebody will find this thread helpful.

1 Like