Blynk server disconnection

DHT sensor is lost or failed ?

I saw that you have redundancy between void ads1115() and other void !

timer.setInterval(5000,ads1115);  



failed…

your code is very poorly structured :wink:
too many redundancies

Alexis, how could I fix it?
… I tried with the varibaili goblali but I can not make it work …
… if I exclude the display on the LCD, for how I thought it works even if it is badly written :slight_smile:

Alexis, so far I’ve made simpler projects with blynk, this is the first time I’ve done a more complex project …
the difficulties I am encountering is the simultaneous display of the data I send to Blynk also on LCDs

I will fix that for you soon, if you can wait till tomorrow
I have to edit your code and manage it :wink:

Alexis, of course I’m waiting!
thank you so much…
I hope to learn a lot from your directions …

1 Like

Salvatore,
Learn that and try it
As you can see, I managed all your sketch
I can’t test it because I’ve no ADS1015,
but , code compiles without any error on my IDE.
waiting for your comments

//--------------------- include librerie utilizzate ----------------------------
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Adafruit_ADS1015.h>
//#include <SimpleTimer.h> not needed , already included with blynk
BlynkTimer timer;
Adafruit_ADS1115 ads;  /* Use this for the 16-bit version */

//--------------------- parametri connessione wi-fi + blynk ----------------------------
char auth[] = "56xxxxxxxxxx95"; // token Serra Domotica

char ssid[] = "TISxxxxxx";
char pass[] = "xxxxxxxx";

//--------------------- parametri LCD ----------------------------
int lcdColumns = 16;              // set the LCD number of columns
int lcdRows = 2;                  // set the LCD number of rows
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);  // set LCD address, number of columns and rows

//--------------------- definizione variabili ---------------------
#define DHT1PIN D7
#define DHT1TYPE DHT22

DHT dht1(DHT1PIN, DHT1TYPE);

float adc0 = ads.readADC_SingleEnded(0); //fotocellula
float adc1 = ads.readADC_SingleEnded(1); //umidita Sx
float adc2 = ads.readADC_SingleEnded(2); //umidita Dx
float adc3 = ads.readADC_SingleEnded(3); //livello serbatorio

float livello, Luminosita , umiditaSx , umiditaDx , DX, SX, LV , H , T, t1, h1 ;

void setup() {
  Serial.begin(115200);             // console debug
  Blynk.begin(auth, ssid, pass);  // avvio connessione

  Wire.begin();
  dht1.begin();                   // set DHT22
  ads.setGain(GAIN_ONE);          // set valore ADS1115 --> 1x gain   +/- 4.096V  1 bit = 2mV      0.125mV
  ads.begin();                    // set Adafruit_ADS1015
  lcd.init();                     // set LCD
  lcd.backlight();                // accensione LCD

  //--------------------- definizione PIN ---------------------
  pinMode(D0, OUTPUT);// rele soglia <25
  //pinMode(D1);// SDA --> LCD + ADS1115
  //pinMode(D2);// SLC --> LCD + ADS1115
  pinMode(D3, OUTPUT);// allarme sensore acqua
  //pinMode(D4);// RELE LAMPADA 1 via Blynk
  //pinMode(D5);// RELE LAMPADA 2 via Blynk
  pinMode(D6, OUTPUT);// rele soglia >25
  //pinMode(D7);// DHT 22
  pinMode(D8, OUTPUT);// inverter BC237
  pinMode(D9, INPUT);// sensore acqua

  //--------------------- assegnazione valore iniziale PIN ---------------------
  digitalWrite(D0, HIGH);
  digitalWrite(D3, LOW);
  digitalWrite(D4, LOW);
  digitalWrite(D5, LOW);
  digitalWrite(D6, LOW);
  digitalWrite(D8, LOW);

  Serial.print ("IP Locale -->");
  Serial.println (WiFi.localIP());
  Serial.print ("Connessione Blynk OK!  ");
  Serial.println (Blynk.connected());
  Serial.println ("");

  //--------------------- TIMERS iniziale ---------------------
  timer.setInterval(500000, LCD); //7x60000 + margin,  time needed to execute all the functions, could be adjusted if you have to change '60000' intervals

}//end setup

//------------------ visualizzazione LCD ------------
void LCD() {
  lcd.clear();
  lcd.setCursor(2, 0);//colonna e riga del display
  lcd.print("SERVER BLYNK");
  lcd.setCursor(0, 1);
  if (!Blynk.connected()) {
    lcd.print("NON CONNESSO");
  }
  else {
    lcd.setCursor(0, 1);
    if (Blynk.connected()) {
      lcd.print(" CONNESSO");
      timer.setTimeout(60000, Temper_Serra);
    }
  }
}

void Temper_Serra() {

  sensoreDHT();//call sensor

  Serial.print("temperatura lcd : ");  Serial.println(t1);
  Serial.println(" ");

  lcd.clear();
  lcd.setCursor(1, 0);//colonna e riga del display
  lcd.print("Temper.  Serra");
  lcd.setCursor(4, 1); //colonna e riga del display
  lcd.print(t1, 2);
  lcd.setCursor(11, 1); //colonna e riga del display
  lcd.print("%");
  timer.setTimeout(60000, umidita);
}

void umidita() {

  Serial.print("umidita' lcd : ");  Serial.println(h1);
  Serial.println(" ");
  lcd.clear(); lcd.setCursor(2, 0); //colonna e riga del display
  lcd.print("Umidita' Serra");
  lcd.setCursor(4, 1); //colonna e riga del display
  lcd.print(h1, 2);
  lcd.setCursor(11, 1); //colonna e riga del display
  lcd.print("%");
  timer.setTimeout(60000, luminosita);
}

// Fotocellula - Luminosita
void luminosita() {
  Luminosita = adc0;
  adc0 = ads.readADC_SingleEnded(0); //fotocellula

  Serial.print("IN0 Fotocellula: ");   Serial.print(adc0);
  float Luminosita = (adc0);
  Serial.print("\  Luminosita: ");  Serial.println(Luminosita / 32767 * 100);
  Blynk.virtualWrite(V7, (100 - Luminosita / 32767 * 100)); // virtual pin
  Serial.println(" ");

  lcd.clear();
  lcd.setCursor(2, 0);//colonna e riga del display
  lcd.print("Luminosita'");
  lcd.setCursor(4, 1); //colonna e riga del display
  lcd.print(100 - Luminosita / 32767 * 100, 2);
  lcd.setCursor(11, 1); //colonna e riga del display
  lcd.print("%");
  timer.setTimeout(60000, Umidita_SX);
}

// Umidità Sx
void Umidita_SX() {
  umiditaSx = adc1;
  adc1 = ads.readADC_SingleEnded(1); //umidita Sx

  Serial.print("IN1 umiditaSx : "); Serial.print(adc1);
  umiditaSx = (adc1);
  Serial.print("\  Umidita' terreno Sx: ");  Serial.println(umiditaSx / 32767 * 100, 2);
  Blynk.virtualWrite(V5, (100 - umiditaSx / 32767 * 100)); // virtual pin
  SX = (100 - umiditaSx / 32767 * 100);

  Serial.println(" ");
  lcd.clear();
  lcd.setCursor(2, 0);//colonna e riga del display
  lcd.print("Umidita' SX'");
  lcd.setCursor(4, 1); //colonna e riga del display
  lcd.print(100 - umiditaSx / 32767 * 100, 2);
  lcd.setCursor(11, 1); //colonna e riga del display
  lcd.print("%");
  timer.setTimeout(60000, Umidita_DX);
}

// Umidità Dx
void Umidita_DX() {
  umiditaDx = adc2;
  adc2 = ads.readADC_SingleEnded(2); //umidita Dx

  Serial.print("IN2 umiditaDx : "); Serial.print(adc2);
  umiditaDx = (adc2);
  Serial.print("\  Umidita' terreno Dx : ");  Serial.println(umiditaDx / 32767 * 100, 2);
  Blynk.virtualWrite(V6, (100 - umiditaDx / 32767 * 100)); // virtual pin
  Serial.println(" ");
  DX = (100 - umiditaDx / 32767 * 100);

  if ((DX <= 45) || (SX <= 45))
  { digitalWrite(D3, HIGH); //Accendi led
    Blynk.virtualWrite(V3, 255);
  }
  else {
    digitalWrite(D3, LOW); //Spegni led
    Blynk.virtualWrite(V3, 0);
  }

  lcd.clear();
  lcd.setCursor(2, 0);//colonna e riga del display
  lcd.print("Umidita' DX'");
  lcd.setCursor(4, 1); //colonna e riga del display
  lcd.print(100 - umiditaDx / 32767 * 100, 2);
  lcd.setCursor(11, 1); //colonna e riga del display
  lcd.print("%");
  timer.setTimeout(60000, Livello_acqua);
}

// Livello serbatoio
void Livello_acqua() {
  LV = (100 - adc3 / 32767 * 100, 2);
  adc3 = ads.readADC_SingleEnded(3); //livello serbatorio

  Serial.print("IN3 livello acqua: "); Serial.print(adc3);
  livello = (adc3);
  Serial.print("\  Livello acqua : ");  Serial.println(livello / 32767 * 100, 2);
  Blynk.virtualWrite(V8, (100 - livello / 32767 * 100)); // virtual pin
  Serial.println(" ");
  LV = ((100 - livello / 32767 * 100));
  if (LV <= 30)
  {
    digitalWrite(D3, HIGH); //Accendi led
    Blynk.virtualWrite(V4, 255);
  }
  else {
    digitalWrite(D3, LOW); //Spegni led
    Blynk.virtualWrite(V4, 0);
  }
  Serial.print("IN3 livello acqua: "); Serial.print(adc3);
  livello = (adc3);
  Serial.print("\  Livello acqua : ");  Serial.println(livello / 32767 * 100, 2);
  Blynk.virtualWrite(V8, (100 - livello / 32767 * 100)); // virtual pin
  Serial.println(" ");
  LV = ((100 - livello / 32767 * 100));

  if (LV <= 30)
  {
    digitalWrite(D3, HIGH); //Accendi led
    Blynk.virtualWrite(V4, 255);
    lcd.clear();
    lcd.setCursor(4, 0);//colonna e riga del display
    lcd.print("ALLARME");
    lcd.setCursor(0, 1); //colonna e riga del display
    lcd.print("SERBATOIO VUOTO");
  }
  else {
    digitalWrite(D3, LOW); //Spegni led
    Blynk.virtualWrite(V4, 0);

    lcd.clear();
    lcd.setCursor(2, 0);//colonna e riga del display
    lcd.print("Livello acqua");
    lcd.setCursor(5, 1); //colonna e riga del display
    lcd.print(LV, 2);
    lcd.setCursor(11, 1); //colonna e riga del display
    lcd.print("%");
  }
}

void sensoreDHT() {
  Serial.print ("IP Locale -->");
  Serial.println (WiFi.localIP());
  Serial.print ("Connessione Blynk OK!  ");
  Serial.println (Blynk.connected());
  Serial.println ("");

  t1 = dht1.readTemperature();
  h1 = dht1.readHumidity();
  t1 = ((int) (t1 * 10) / 10.0);
  h1 = ((int) (h1 * 10) / 10.0);

  Blynk.virtualWrite(10, t1); // virtual pin
  Blynk.virtualWrite(11, h1); // virtual pin

  // Check if any reads failed and exit early (to try again).
  if (isnan(t1) || isnan(h1)) {
    Serial.println("Failed to read from DHT #1");
  }
  else {
    Serial.print("Temperatura interna: ");
    Serial.print(t1);
    Serial.print ("");
    Serial.println(" gradi /"), (h1);
    Serial.print("Umidita' interna: ");
    Serial.print(h1);
    Serial.println(" %");
    Serial.println ("");
  }

  if (t1 < 21)
  {
    Blynk.virtualWrite(V1, 255);
    digitalWrite(D0, LOW); //accende alimenatore cella Peltier
  }
  else
  {
    Blynk.virtualWrite(V1, 0);
    digitalWrite(D0, HIGH); //spegne alimenatore cella Peltier

  }
  if (t1 > 25)
  {
    Blynk.virtualWrite(V2, 255);
    digitalWrite(D0, LOW);  //accende alimenatore cella Peltier
    digitalWrite(D8, LOW);  //inverter cella Peltier
  }
  else
  {
    Blynk.virtualWrite(V2, 0);
    digitalWrite(D0, HIGH); //spegne alimenatore cella Peltier
    digitalWrite(D8, HIGH); //spegne inverter cella Peltier
  }
}

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

Alexis, thank you so much!

Your code works, do not disconnect from Blynk and display the data on the LCD, I have only readjusted the intervals of the various functions.

I think the main mistake I made was to keep the LCD display separate from the various functions. I have to understand better the functioning of the Timer which is still unclear to me.

I found that once the cycle is over, the DHT sensor data is lost (I am attaching screenshots of the serial monitor), which are restored shortly thereafter.
thank you so much.

1 Like