Ethernet shield

Hi everyone, I ask for your help for a project I implemented by building on the DHT22 sensor reading sketch. I used an Arduino mega 2560 with ethernet shield W5100. It happens that if I do not connect the LAN cable to the ethernet shield, Arduino does not start. I guess I connect it, everything works regularly. And so even if, hypothetically, the router freezes or shuts off, it blocks everything.
Do you have suggestions? Thanks in advance

#include <Ethernet.h>
#include <Blynk.h>
#include <BlynkSimpleEthernet.h>
#include <FastIO.h>
#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <Wire.h>
#include <DHT.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>

char auth[] = "ea0dcf471a02475ca22c000f6e3889e6";
IPAddress server_ip (46, 101, 143, 225);

#define BLYNK_PRINT Serial
#define W5100_CS  10
#define SDCARD_CS 4

#define DHTPIN 2          // What digital pin we're connected to
#define DHTPIN1 3          // What digital pin we're connected to
#define ONE_WIRE_BUS 7
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
#define deum 9
#define vlv 8

OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DHT dht(DHTPIN, DHTTYPE);
DHT dht1(DHTPIN1, DHTTYPE);
BlynkTimer timer;
WidgetLED LEDdeum(V3);
WidgetLED LEDvlv(V4);
byte range = 3;
int val = 0;
float h, t, h1, t1, td, td1, s1;
String deu, valve;
byte ultimo_pulsante;
byte page = 0;
byte freccia_su[8] = {
  B00000,
  B00000,
  B00100,
  B01110,
  B11111,
  B00000,
  B00000,
  B00000,
};
byte freccia_giu[8] = {
  B00000,
  B00000,
  B00000,
  B11111,
  B01110,
  B00100,
  B00000,
  B00000,
};
byte grado[8] = {
  B00010,
  B00101,
  B00010,
  B00000,
  B00000,
  B00000,
  B00000,
  B00000,
};


LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

BLYNK_WRITE(V1) {
  val = param.asInt(); // assigning incoming value from pin V1 to a variable
  range = range + val;
  Serial.println("pulsante remoto");

}

// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature();
  h1 = dht1.readHumidity();
  t1 = dht1.readTemperature();
  sensors.requestTemperatures();
  s1 = sensors.getTempCByIndex(0);

  double a = 17.271;
  double b = 237.7;
  double tr = (a * t) / (b + t) + log(h / 100);
  td = (b * tr) / (a - tr);
  double tr1 = (a * t1) / (b + t1) + log(h1 / 100);
  td1 = (b * tr1) / (a - tr1);


  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V0, range);
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  Blynk.virtualWrite(V7, h1);
  Blynk.virtualWrite(V8, t1);
  Blynk.virtualWrite(V9, s1);
  Blynk.virtualWrite(V10, td);
  Blynk.virtualWrite(V11, td1);


}

void setup()
{ lcd.begin(20, 4);
  lcd.clear();
  lcd.createChar(1, freccia_su);
  lcd.createChar(2, freccia_giu);
  lcd.createChar(3, grado);
  lcd.setCursor(0, 0);
  lcd.print("CONTROLLO          ");
  lcd.setCursor(0, 1);
  lcd.print("DEUMIDIFICAZIONE");
  lcd.setCursor(0, 2);
  lcd.print("Premere ENTER      ");
  lcd.setCursor(0, 3);
  lcd.print("per accedere ai menu");
  page = 0;
  Serial.begin(9600);
  pinMode(deum, OUTPUT);
  pinMode(vlv, OUTPUT);
  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card
  //Blynk.begin(auth, server_ip, 8442);
  // You can also specify server:
  //Blynk.begin(auth, "blynk-cloud.com", 8442);
  Blynk.begin(auth, IPAddress(server_ip), 8442);
  dht.begin();
  sensors.begin();
  // Setup a function to be called every second
  //timer.setInterval(500L, sendSensor);
  digitalWrite (deum, HIGH);
  digitalWrite (vlv, LOW);

}

void loop()
{ Serial.println("Inizia");
  Blynk.run();
  //timer.run();
  sendSensor();
  onoff();
  lettura_pulsanti();
  richiama_update();
  if ((page == 4) || (page == 5) || (page == 6) || (page == 7) || (page == 8) || (page == 4)) {
    Serial.println("OK");
    Serial.println("OKK");
    updateLcd();
  }
}

These Arduino Ethernet shields, especially the W5100 ones, are extremely unreliable. If you do a bit of googling you’ll find lots of people who have had similar issues, along with some potential fixes, such as adding a capacitor across the reset switch on the Ethernet shield, or adding a hardware reset routine to do repeated software resets until the shield starts correctly.

Take a look at the main chip on your shield under a magnifier and see if there are any solder bridges. If there are then the chances of getting your shield working reliably is very slim.

I’d reccomended using an ESP8266 based device such as the Wemos D1 Mini and connecting using Wi-Fi rather than the an Arduino and the Ethernet shield. These are much more powerful than the Arduino and are much smaller and can be programmed using the Arduino IDE.

Pete.

@Mic_Putignano take a look at this thread relating to navigating the Blynk blocking routine for a W5100:

Just happened to read the response and was surprised to find the line … I think that was probably you were using the nasty Blynk.begin .

But i thought that was the key statement to get Blynk going … no ?? Maybe i am missing something here…

( Incidentally in the code by the original poster i see way too many things inside the loop() without a trap. Would it not be better if after Blynk.run() he sets up a non blocking delay for about a second and process all those other code inside that ??

Edit : Just read the linked post and the non blocking code therein. Lots of things to learn from there… maybe we can use this Blynk.connect() the way you have done in all instances ??

Yes it’s to get beginners to Blynk to become Blynkers but the blocking routine is generally not used by experienced Blynkers.

And yes the OP’s loop() is not suitable for Blynk and it requires the use of the Blynk Timer (more commonly known as Simple Timer in the Arduino world). Blynk Timer IS Simple Timer and it’s works without a connection to Blynk or the internet.

Ok got it … from today i will not use Blynk.begin()

Trust this will promote from a beginner level to Pro level … LOL .

Going by your posts looks like you have been Bl(i)ynking for long !!

1 Like