Device appears offline

Our project consists of an automatic irrigation system that includes a humidity and temperature sensor, a brightness sensor and a servo motor that simulates the action of a sprinkler that waters the plants and sends the data to our blink application via the ESP8266. There is also a display that shows the current temperature, humidity and luminosity. The sensors, display and servo work perfectly and are able to collect the data but we can’t get the device on the web to collect the data and we think it’s because it appears offline, as the ESP8266 also seems to be working fine.

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

#define BLYNK_TEMPLATE_NAME "Sistema de riego automático"
#define BLYNK_AUTH_TOKEN "JMCU0KtPUynumt4Bm-umGScGy5oqlPxr"

// Definir pines
#define DHTPIN 2     // Pin del sensor DHT11
#define DHTTYPE DHT11
#define LDRPIN A0    // Pin del sensor de luminosidad
#define SERVOPIN 9   // Pin del servomotor

// #include <LiquidCrystal.h>
#include <DHT.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Redmi";
char pass[] = "holasoyantonio";

// Inicializar pantalla LCD (sin I2C)
LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display

#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

DHT dht(DHTPIN, DHTTYPE);
Servo aspersor;

unsigned long previousMillis = 0;  // Para controlar el tiempo de espera
const long interval = 2000;        // Intervalo de 2 segundos

// Umbrales de riego
const float tempThreshold = 30.0;
const float humidityThreshold = 40.0;
const int lightThreshold = 300;

// Variables para Blynk y temporizadores
BlynkTimer timer;
bool autoIrrigation = true;  // Control de riego automático

void setup() {
  // Debug console
  Serial.begin(9600);
  delay(10);

  lcd.init();
  lcd.begin(16, 2);
  lcd.backlight();

  EspSerial.println("AT+CWMODE=1");
  if(EspSerial.find("OK"))
      Serial.println("ESP8266 en modo Estacion");

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);

  // Iniciar el sensor DHT11 y el servomotor
  dht.begin();
  aspersor.attach(SERVOPIN);
  aspersor.write(0);  // Servomotor en posición inicial (aspersor apagado)

  // Mensaje de inicialización en la pantalla LCD
  lcd.setCursor(0, 0);
  lcd.print("Iniciando...");
  delay(2000);
  lcd.clear();

  // Configurar temporizadores para actualizar los sensores y la pantalla cada 2 segundos
  timer.setInterval(2000L, readSensors);
}

// Función para leer los sensores y controlar el riego
void readSensors() {
  // Leer datos de temperatura y humedad
  float temperature = dht.readTemperature();
  float humidity = dht.readHumidity();
  
  // Leer el nivel de luminosidad
  int lightLevel = analogRead(LDRPIN);

  // Mostrar datos en la pantalla LCD
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("T:");
  lcd.print(temperature);
  lcd.print("C H:");
  lcd.print(humidity);
  lcd.print("%");
  
  lcd.setCursor(0, 1);
  lcd.print("Luz:");
  lcd.print(lightLevel);

  // Enviar datos a Blynk
  Blynk.virtualWrite(V0, temperature);  // Enviar temperatura a Blynk (Virtual Pin V0)
  Blynk.virtualWrite(V1, humidity);     // Enviar humedad a Blynk (Virtual Pin V1)
  Blynk.virtualWrite(V2, lightLevel);   // Enviar luminosidad a Blynk (Virtual Pin V2)

  // Control del riego automático
  if (autoIrrigation && temperature > tempThreshold && humidity < humidityThreshold && lightLevel < lightThreshold) {
    startIrrigation();
  } else {
    stopIrrigation();
  }
}

// Función para iniciar el riego
void startIrrigation() {
  aspersor.write(90);  // Activar riego
  lcd.setCursor(12, 1);
  lcd.print("R: ON");
  Blynk.virtualWrite(V3, "Riego activado");  // Notificación en Blynk
}

// Función para detener el riego
void stopIrrigation() {
  aspersor.write(0);  // Detener riego
  lcd.setCursor(12, 1);
  lcd.print("R: OFF");
  Blynk.virtualWrite(V3, "Riego desactivado");  // Notificación en Blynk
}

void loop() {
  Blynk.run();
  timer.run();     // Ejecutar el temporizador
}

This is what we have:
Uploading: WhatsApp Image 2024-10-15 at 17.20.24.jpeg…
Uploading: WhatsApp Image 2024-10-15 at 17.19.13.jpeg…

Your image uploads were unsuccessful.

Are you using an Arduino Mega?

What does your serial monitor show? (Post the text, with triple backticks, not a screenshot).

Pete.

Hi Pete, thank you for your reply.
Yes, we are using Arduino Mega 2560 and the photos are those:

[5190] 
    _  _          _
   / _ )/ /_ __  / /_
  / _  / / // / _ \/  '_/
 /_//\, /////\_\
        /_/ v0.5.0 on Arduino Mega

[5777] Connecting to Redmi
[8823] AT version:1.2.0.0(Jul  1 2016 20:04:45)
SDK version:1.5.4.1(39cb9a32)
v1.0.0
Mar 11 2018 18:27:31
OK
[13933] +CIFSR:STAIP,"192.168.43.2"
+CIFSR:STAMAC,"ec:fa:bc:c1:41:5d"
[13941] Connected to WiFi
[24473] Ready (ping: 29ms).
[42680] Ready (ping: 89ms).
[59488] Ready (ping: 88ms).
[76279] Ready (ping: 88ms).
[92439] Ready (ping: 89ms).

You should be using Blynk library version 1.3.2

Pete.

Hi Pete, thank you very much for your help. We have changed the library and also added a 9V power supply and the site is now ‘online’. Everything is working perfectly. Thanks again!

1 Like