Cannot connect to Blynk with Ethernet shield 2

Hi,
I have connected a new Ethernet shield 2 to my Arduino Uno. Previously I have run an application successfully with a MKR shield. I copied the example sketch for Ethernet 2 in IDE and made small changes, see below. When starting, connection to Blynk server is unsuccessful:

23:24:53.111 -> [0] Getting IP...
23:24:55.333 -> [2249] IP:192.168.0.29
23:24:55.368 -> [2249] 
23:24:55.368 ->     ___  __          __
23:24:55.403 ->    / _ )/ /_ _____  / /__
23:24:55.438 ->   / _  / / // / _ \/  '_/
23:24:55.472 ->  /____/_/\_, /_//_/_/\_\
23:24:55.472 ->         /___/ v1.1.0 on Arduino Uno
23:24:55.507 -> 
23:24:55.507 ->  #StandWithUkraine    https://bit.ly/swua
23:24:55.576 -> 
23:24:55.576 -> 
23:24:55.576 -> [2415] Connecting to blynk.cloud:80
23:25:27.690 -> [34589] Connecting to blynk.cloud:80
23:25:59.832 -> [66727] Connecting to blynk.cloud:80

I have downloaded Ethernet 2 library as well as Blynk library 1.1.0. I think there might be some problem with authentication since Ethernet board has received an IP address and tries to connect to Blynk server.
I have run other Blynk app from same Arduino board over same internet connection so that should not be the problem.

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Social networks:            http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example shows how to use Arduino Ethernet Shield 2 (W5500)
  to connect your project to Blynk.

  Note: This requires Ethernet2 library
    from http://librarymanager/all#Ethernet2

        Pins 10, 11, 12 and 13 are reserved for Ethernet module.
        DON'T use them in your sketch directly!

  Feel free to apply it to any other example. It's simple!
 *************************************************************/

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

/* Fill-in your Template ID (only if using Blynk.Cloud) */
#define BLYNK_TEMPLATE_ID           "TMPLQsUn1mZj"
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "Dx3UwvsBNKVtv7dFLABC-3Ec0mV3IOGo"


#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = BLYNK_AUTH_TOKEN;


void setup()
{
  // Debug console
  Serial.begin(9600);
  
  Serial.println("Debug_1");
  Blynk.begin(auth);
  Serial.println("Debug_2");
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 80);
  //Blynk.begin(auth, IPAddress(192,168,1,100), 8080);
  // For more options, see Boards_Ethernet/Arduino_Ethernet_Manual example
}

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

Have you tried uninstall and reinstall ?

Hi! I removed 1.0.1 library files so now 1.1.0 i used. But still same connection problem.

Have you tried turning off your firewall ?
Have you tried network reset option ?

Hi, yes. I used same router and setup before. Only difference is that I now use a Ethernet shield 2 instead of an MKR Ethernet shield.

I’d suggest that you try a simple internet connectivity sketch which doesn’t use Blynk.

You may find that you need to specify MAC address, IP address, gateway, DNS etc.

Pete.

Hi,
Good idea! I tried an Ethernet 2 sketch with static IP, MAC etc. and it worked. So I applied the same on my current sketch and I think I got closer:

21:29:44.204 → [9999] Using static IP
21:29:46.185 → [12000] IP:192.168.0.20
21:29:46.220 → [12001]
21:29:46.220 → ___ __ __
21:29:46.254 → / _ )/ /_ _____ / /__
21:29:46.290 → / _ / / // / _ / '/
21:29:46.326 → /
//_, /////_
21:29:46.361 → /
__/ v1.1.0 on Arduino Uno
21:29:46.394 →
21:29:46.394 → #StandWithUkraine StandWithUkraine | #StandWithUkraine banner and related documents
21:29:46.428 →
21:29:46.428 →
21:29:46.428 → [12169] Connecting to blynk-cloud.com:80
21:29:46.703 → [12517] Invalid auth token

I don’t know if I actually get contact and server says that token is invalid or if it is a result of not connecting. I have used the token successfully before.

/*************************************************************

 Styrning av värmepump
 
 *************************************************************/
#define BLYNK_PRINT Serial 

// Blynk-items
#define BLYNK_TEMPLATE_ID           "TMPLQsUn1mZj"
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "Dx3UwvsBNKVtv7dFLIYI-3Ec0mV3IOGo"
    
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0xA8, 0xDC };
IPAddress arduino_ip ( 192,   168,   0,  20);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,   168,   0,   1);
IPAddress subnet_mask(255, 255, 255, 0);

#define W5100_CS 10
#define SDCARD_CS 4

// Uppstartsfördröjning START_DELAY * 1 s
// Ska sättas till 60 sekunder när systemet är live
// Routern verkar hitta koppling efter cirka 35 sekunder
#define START_DELAY 10

//*************************************************************/

// Används för periodisk koll
BlynkTimer periodicTimer;

// Räknare för antalet checkar mot cloud
// Kör varje minut
// Låt dem nollas vid omstart
unsigned int connected = 0;
unsigned int not_connected = 0;

// Kör noll på den initialt, ska sen synkas vid connect
byte currentLevel = 0;

// Räknare för periodisk timer
int periodicCount = 0;

// Antal minuter - periodtid
const int period = 60; 

// Förra samplet, sätt till 11 så att det inte triggar nerstängning
double lastInhouse = 11;   

// Virtuella pinnar
// Sätta temperaturnivå : V0
// Visa temperaturnivå  : V1
// Hall                 : V2
// Utomhus              : V3
// Vardagsrum           : V4
// Uppkoppling stat.    : V5


// Kommer om pinne V0 (slider) ändrats eller om det är synkning. Sätt reläer.
BLYNK_WRITE(V0)
{  
  int tempLevel = param.asInt();
  
  Serial.print("Tog emot och skickade tillbaka nivå:");
  Serial.println(tempLevel);
  
  setLevel(tempLevel);

  Blynk.virtualWrite(V1, tempLevel);

}

// Synka värdet från server
BLYNK_CONNECTED()
{
   Blynk.syncVirtual(V0);
}

// Kör var 10:e sekund   
void timerHit()
{
  //---------------------------------------------------------------------------------
  
  // Kolla om en period gått, om inte, hoppa ur
  periodicCount = periodicCount + 1;
  
  if (periodicCount < period)
  { 
    return;
  }
  //---------------------------------------------------------------------------------

  Serial.println("Kör periodisk funktion");
  
  // Eftersom en period gått, nolla räknaren
  periodicCount = 0;
  
  // Börja om ifall räknarna blivit stora
  if ( (connected > 65000) || (not_connected > 65000) )
  {
    connected = 0;
    not_connected = 0;
  }
  else
  {
  // Kolla om grejerna är uppe
  if (Blynk.connected() == true)
  {
    connected = connected + 1;
  }
  else
  {
    not_connected = not_connected + 1;
  }

  // Undvik nolldivision samt uppdatera procentandel 
  if ( (connected+not_connected) != 0 )
  {
    Blynk.virtualWrite(V5, 100*connected/(connected+not_connected));
    Serial.print("Connected, not connected och andel:");
    Serial.println(connected);
    Serial.println(not_connected);
    Serial.println(100*connected/(connected+not_connected));
  }
  }

  //---------------------------------------------------------------------------------  
  
  // Temperaturer, kör tre mätningar per sensor
  double tempHall = calcTemperature(analogRead(0),analogRead(0),analogRead(0));
  
  double tempVardagsrum = calcTemperature(analogRead(1),analogRead(1),analogRead(1));
  
  double tempUte = calcTemperature(analogRead(2),analogRead(2),analogRead(2));

  double averageTempIn = (tempHall+tempVardagsrum)/2;
   
  // Uppdatera temperaturer till mobil
  Serial.println("Uppdatera temperaturer mot server");
  Serial.println(tempHall);
  Serial.println(tempVardagsrum);
  Serial.println(tempUte);
  
  Blynk.virtualWrite(V2, tempHall);

  Blynk.virtualWrite(V4, tempVardagsrum);

  Blynk.virtualWrite(V3, tempUte);

  Blynk.virtualWrite(V6, averageTempIn);

  /* Om senaste och förra mätningen är för låga -> stäng av
  if ( (lastInhouse < 10) && (averageTempIn < 10) )
  {
    setLevel(4);
    Blynk.virtualWrite(V1, 4); 
    lastInhouse = averageTempIn;
  }*/
     
}

//---------------------------------------------------------------------------------
void timerHitLed()
{
  digitalWrite(5, HIGH);
  delay(40);
  digitalWrite(5, LOW);

  Serial.println("Kör periodisk LED-funktion");
}

void setup()
{

  // Debug console
  Serial.begin(9600);

  pinMode(2, OUTPUT); // Relä för level 3
  pinMode(3, OUTPUT); // Relä för level 2
  pinMode(4, OUTPUT); // Relä för level 1
  pinMode(5, OUTPUT); // LED

  // Använd inte SD-kort       
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  // Vänta lite, öka chansen att routern vaknat efter strömavbrott
  delay(1000*START_DELAY);
  
  // Authentisera mot cloud
  //Blynk.begin(auth);
  Blynk.begin(auth, "blynk-cloud.com", 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  // Periodisk funktion, kör varje minut för att pusha data
  periodicTimer.setInterval(60000L, timerHit);
  periodicTimer.setInterval(5000L, timerHitLed);
}

void loop()
{
  Blynk.run();
  periodicTimer.run();

}

// Gör om mätvärde till temperatur
double calcTemperature(int reading1, int reading2, int reading3) 
{
  // Medelvärde
  int reading = (reading1+reading2+reading3)/3;
  double Temp;
  Temp = log(10000.0 * ((1024.0 / reading - 1)));
  Temp = 1 /(0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp);
  Temp = Temp - 273.15; // convert from Kelvin to Celsius
  return Temp;
}


void setLevel(byte value)
{
  
// Sätt relä direkt
  if (value==4)
  { 
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==3)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==2)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
  }
  if (value==1)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
  }  
}

" blynk-cloud.com " is the old blynk server, the new blynk server is " blynk.cloud "

Ok, I tried:

Blynk.begin(auth, "blynk.cloud", 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);

but it was unsuccessful, but no token error code though.

21:57:15.126 -> [9999] Using static IP
21:57:17.138 -> [12000] IP:192.168.0.20
21:57:17.172 -> [12001] 
21:57:17.172 ->     ___  __          __
21:57:17.209 ->    / _ )/ /_ _____  / /__
21:57:17.250 ->   / _  / / // / _ \/  '_/
21:57:17.250 ->  /____/_/\_, /_//_/_/\_\
21:57:17.288 ->         /___/ v1.1.0 on Arduino Uno
21:57:17.321 -> 
21:57:17.321 ->  #StandWithUkraine    https://bit.ly/swua
21:57:17.358 -> 
21:57:17.358 -> 
21:57:17.358 -> [12169] Connecting to blynk.cloud:80

So did you try a non-Blynk sketch?

Pete.

Hi,
Yes, I tried an non-Blynk Ethernet 2 sketch where I set static IP, MAC address and DNS. That sketch works. I tried to do the same for Blynk.begin(). I changed “blynk.cloud” to actual IP address like you can see in sketch below. Then it seems to connect to server because BLYNK_CONNECTED is triggered in my device. My timers hit at the right intervals, sketch is running, but nothing seems to be sent to server. :frowning:

23:29:11.645 -> [9999] Using static IP
23:29:13.626 -> [12000] IP:192.168.0.20
23:29:13.660 -> [12001] 
23:29:13.660 ->     ___  __          __
23:29:13.694 ->    / _ )/ /_ _____  / /__
23:29:13.728 ->   / _  / / // / _ \/  '_/
23:29:13.728 ->  /____/_/\_, /_//_/_/\_\
23:29:13.763 ->         /___/ v1.1.0 on Arduino Uno
23:29:13.797 -> 
23:29:13.797 ->  #StandWithUkraine    https://bit.ly/swua
23:29:13.868 -> 
23:29:13.868 -> 
23:29:13.868 -> [12169] Connecting to 46.101.217.214
23:29:14.039 -> [12421] Ready (ping: 42ms).
23:29:14.215 -> Tog emot och skickade tillbaka nivå:3
23:29:19.171 -> Kör periodisk LED-funktion
23:29:19.238 -> [17601] Connecting to 46.101.217.214
23:29:24.191 -> Kör periodisk LED-funktion
23:29:24.225 -> [22602] Connecting to 46.101.217.214
23:29:29.174 -> Kör periodisk LED-funktion
23:29:29.244 -> [27603] Connecting to 46.101.217.214
23:29:34.175 -> Kör periodisk LED-funktion
23:29:34.246 -> [32604] Connecting to 46.101.217.214
23:29:39.170 -> Kör periodisk LED-funktion
...
...
/*************************************************************

 Styrning av värmepump
 
 *************************************************************/
#define BLYNK_PRINT Serial 

// Blynk-items
#define BLYNK_TEMPLATE_ID           "TMPLQsUn1mZj"
#define BLYNK_DEVICE_NAME           "Test"
#define BLYNK_AUTH_TOKEN            "Dx3UwvsBNKVtv7dFLIYI-3Ec0mV3IOGo"
    
#include <SPI.h>
#include <Ethernet2.h>
#include <BlynkSimpleEthernet2.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0xA8, 0xDC };
IPAddress arduino_ip ( 192,   168,   0,  20);
IPAddress dns_ip     (  8,   8,   8,   8);
IPAddress gateway_ip ( 192,   168,   0,   1);
IPAddress subnet_mask(255, 255, 255, 0);
IPAddress ip_s (46,101,217,214);

#define W5100_CS 10
#define SDCARD_CS 4

// Uppstartsfördröjning START_DELAY * 1 s
// Ska sättas till 60 sekunder när systemet är live
// Routern verkar hitta koppling efter cirka 35 sekunder
#define START_DELAY 10

//*************************************************************/

// Används för periodisk koll
BlynkTimer periodicTimer;

// Räknare för antalet checkar mot cloud
// Kör varje minut
// Låt dem nollas vid omstart
unsigned int connected = 0;
unsigned int not_connected = 0;

// Kör noll på den initialt, ska sen synkas vid connect
byte currentLevel = 0;

// Räknare för periodisk timer
int periodicCount = 0;

// Antal minuter - periodtid
const int period = 60; 

// Förra samplet, sätt till 11 så att det inte triggar nerstängning
double lastInhouse = 11;   

// Virtuella pinnar
// Sätta temperaturnivå : V0
// Visa temperaturnivå  : V1
// Hall                 : V2
// Utomhus              : V3
// Vardagsrum           : V4
// Uppkoppling stat.    : V5


// Kommer om pinne V0 (slider) ändrats eller om det är synkning. Sätt reläer.
BLYNK_WRITE(V0)
{  
  int tempLevel = param.asInt();
  
  Serial.print("Tog emot och skickade tillbaka nivå:");
  Serial.println(tempLevel);
  
  setLevel(tempLevel);

  Blynk.virtualWrite(V1, tempLevel);

}

// Synka värdet från server
BLYNK_CONNECTED()
{
   Blynk.syncVirtual(V0);
}

// Kör var 10:e sekund   
void timerHit()
{
  //---------------------------------------------------------------------------------
  
  // Kolla om en period gått, om inte, hoppa ur
  periodicCount = periodicCount + 1;
  
  if (periodicCount < period)
  { 
    return;
  }
  //---------------------------------------------------------------------------------

  Serial.println("Kör periodisk funktion");
  
  // Eftersom en period gått, nolla räknaren
  periodicCount = 0;
  
  // Börja om ifall räknarna blivit stora
  if ( (connected > 65000) || (not_connected > 65000) )
  {
    connected = 0;
    not_connected = 0;
  }
  else
  {
  // Kolla om grejerna är uppe
  if (Blynk.connected() == true)
  {
    connected = connected + 1;
  }
  else
  {
    not_connected = not_connected + 1;
  }

  // Undvik nolldivision samt uppdatera procentandel 
  if ( (connected+not_connected) != 0 )
  {
    Blynk.virtualWrite(V5, 100*connected/(connected+not_connected));
    Serial.print("Connected, not connected och andel:");
    Serial.println(connected);
    Serial.println(not_connected);
    Serial.println(100*connected/(connected+not_connected));
  }
  }

  //---------------------------------------------------------------------------------  
  
  // Temperaturer, kör tre mätningar per sensor
  double tempHall = calcTemperature(analogRead(0),analogRead(0),analogRead(0));
  
  double tempVardagsrum = calcTemperature(analogRead(1),analogRead(1),analogRead(1));
  
  double tempUte = calcTemperature(analogRead(2),analogRead(2),analogRead(2));

  double averageTempIn = (tempHall+tempVardagsrum)/2;
   
  // Uppdatera temperaturer till mobil
  Serial.println("Uppdatera temperaturer mot server");
  Serial.println(tempHall);
  Serial.println(tempVardagsrum);
  Serial.println(tempUte);
  
  Blynk.virtualWrite(V2, tempHall);

  Blynk.virtualWrite(V4, tempVardagsrum);

  Blynk.virtualWrite(V3, tempUte);

  Blynk.virtualWrite(V6, averageTempIn);

  /* Om senaste och förra mätningen är för låga -> stäng av
  if ( (lastInhouse < 10) && (averageTempIn < 10) )
  {
    setLevel(4);
    Blynk.virtualWrite(V1, 4); 
    lastInhouse = averageTempIn;
  }*/
     
}

//---------------------------------------------------------------------------------
void timerHitLed()
{
  digitalWrite(5, HIGH);
  delay(40);
  digitalWrite(5, LOW);

  Serial.println("Kör periodisk LED-funktion");
}

void setup()
{

  // Debug console
  Serial.begin(9600);

  pinMode(2, OUTPUT); // Relä för level 3
  pinMode(3, OUTPUT); // Relä för level 2
  pinMode(4, OUTPUT); // Relä för level 1
  pinMode(5, OUTPUT); // LED

  // Använd inte SD-kort       
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  // Vänta lite, öka chansen att routern vaknat efter strömavbrott
  delay(1000*START_DELAY);
  
  // Authentisera mot cloud
  //Blynk.begin(auth);
  
  Blynk.begin(auth, ip_s, 80, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
  // Periodisk funktion, kör varje minut för att pusha data
  periodicTimer.setInterval(60000L, timerHit);
  periodicTimer.setInterval(5000L, timerHitLed);
}

void loop()
{
  Blynk.run();
  periodicTimer.run();

}

// Gör om mätvärde till temperatur
double calcTemperature(int reading1, int reading2, int reading3) 
{
  // Medelvärde
  int reading = (reading1+reading2+reading3)/3;
  double Temp;
  Temp = log(10000.0 * ((1024.0 / reading - 1)));
  Temp = 1 /(0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp)) * Temp);
  Temp = Temp - 273.15; // convert from Kelvin to Celsius
  return Temp;
}


void setLevel(byte value)
{
  
// Sätt relä direkt
  if (value==4)
  { 
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==3)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,LOW);
    digitalWrite(4,LOW);
  }
  if (value==2)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,LOW);
  }
  if (value==1)
  {
    digitalWrite(2,HIGH);
    digitalWrite(3,HIGH);
    digitalWrite(4,HIGH);
  }  
}  

Try blynk.cloud with the region short before it, like in my case fra.blynk.cloud. You can find it in the rightbottom of the webconsule.

Hi,
I got the same result, when pinging fra1.blynk.cloud I get same IP as for blynk.cloud. When I use the actual IP address I get connected and then disconnected. Is there some debug to see what happens on Blynk server side? Or is it time to throw Ethernet shield 2 away and go back to Mkr Ethernet shield?

Br Björn

I wonder if this is the correct parameter sequence?
If you look at BlynkEthernet.h (which is included in BlynkSimpleEthernet2.h) then it contains this line…

Ethernet.begin(SelectMacAddress(auth, mac), local, dns, gateway, subnet);

What happens if you use your parameters in this sequence?

Pete.

I can try but not sure how my values map to those given in your example :grinning:

Which fields are you unsure about?

Pete.

I got the impression that the line you pasted is “inside” the function, see below. I am a bit confused since there is an example in IDE that is for W5500 Ethernet shield 2, but it does not work. Somewhere I read that BlynkSimpleEthernet2 and Ethernet2 are outdated since Ethernet shield 2 is supported by the “regular” BlynkSimpleEthernet. But running that I get connected on first attempt, then thrown out, I assume. I wonder what happens on Blynk server side.

// DHCP with domain
    void begin( const char* auth,
                const char* domain = BLYNK_DEFAULT_DOMAIN,
                uint16_t port      = BLYNK_SERVER_PORT,
                const byte mac[]   = NULL)
    {
        BLYNK_LOG1(BLYNK_F("Getting IP..."));
        if (!Ethernet.begin(SelectMacAddress(auth, mac))) {
            BLYNK_FATAL(BLYNK_F("DHCP Failed!"));
        }

//Björn

I would have thought that this…

should be this:

Blynk.begin(auth, arduino_mac, arduino_ip, dns_ip, gateway_ip, subnet_mask);

but I may be wrong, and I don’t have the hardware to test it.

Pete.

I tested it, compiles but does not work. Periodic attempts to connect but device is still offline.

//Björn

Try adding some code to report on the MAC address, IP Address etc as provided by the Ethernet library and see if the values they return make sense.

Pete.