Does not show the values in my blynk app

Hello could you help me show the issue in this project the blynk does not show the values in my blynk app. Please help me thank you

Source code:

#define BLYNK_TEMPLATE_NAME "*****"
#define BLYNK_AUTH_TOKEN "*****"
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <RTClib.h>
#include <DHT.h>

// Your WiFi credentials.
char auth[] = "usBPpIFvpjSjO3K3rbizTQx6SqvZ-WU3";
char ssid[] = "Paulene";
char pass[] = "four_eyes";

// Define pins for sensors
#define PH_SENSOR_PIN A0
#define IR_SENSOR_PIN 5
#define TOUCH_SENSOR_PIN 4
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define FAN_PIN 9

// DHT and RTC Sensor
#define DHTPIN 6
#define DHTTYPE DHT22
RTC_DS3231 rtc;
DHT dht(DHTPIN, DHTTYPE);

// Adafruit PWM Servo Driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servo channel definitions
#define WATER_SERVO_CHANNEL 0
#define FOOD_SERVO_CHANNEL 2
#define SANITATION_SERVO_CHANNEL1 4
#define SANITATION_SERVO_CHANNEL2 6
#define DOOR_SERVO_CHANNEL1 8
#define DOOR_SERVO_CHANNEL2 10
#define DOOR_SERVO_CHANNEL3 12

// Software Serial for ESP8266
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

// Routine times set by user
int waterHours[3] = {-1, -1, -1};
int waterMinutes[3] = {-1, -1, -1};
int foodHours[3] = {-1, -1, -1};
int foodMinutes[3] = {-1, -1, -1};
int sanitationHours[3] = {-1, -1, -1};
int sanitationMinutes[3] = {-1, -1, -1};

// Water calibration
float calibration_value = 21.34 + 0.7;
unsigned long int avgValue;
int buf[10], temp;

// Function to convert degrees to pulse length
int convertDegToPulse(int deg) {
  int pulse = map(deg, 0, 180, 150, 600); // 150-600 corresponds to the 1-2ms pulse width
  return pulse;
}

// Functions for servo actions
void moveServo(int channel, int position) {
  int pulse = convertDegToPulse(position);
  pwm.setPWM(channel, 0, pulse);
}

void dipSensor() {
  moveServo(WATER_SERVO_CHANNEL, 90); // dip the pH sensor
  delay(5000);                         // Allow time for the sensor to stabilize in water
  moveServo(WATER_SERVO_CHANNEL, 0);
}

void waterRoutine() {
  dipSensor();
  delay(5000); // Allow time for the sensor to stabilize in water (adjust as needed)

  // Read pH value
  avgValue = 0;
  for (int i = 0; i < 10; i++) { // Get 10 sample values from the sensor for smoothing
    buf[i] = analogRead(PH_SENSOR_PIN);
    delay(10);
  }

  for (int i = 0; i < 9; i++) { // Sort the analog values from small to large
    for (int j = i + 1; j < 10; j++) {
      if (buf[i] > buf[j]) {
        temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }

  for (int i = 2; i < 8; i++) avgValue += buf[i]; // Take the average value of 6 center samples

  float phValue = (float)avgValue * 5.0 / 1024 / 6; // Convert the analog value into millivolts
  phValue = -5.70 * phValue + calibration_value;    // Convert the millivolts into pH value

  // Serial.print("pH Value: ");
  // Serial.println(phValue, 2); // Print pH value with 2 decimal places

  String phStatus;
  // Determine acidity level
  if (phValue < 6.9) {
    Serial.println("Acidic");
  } else if (phValue > 7) {
    Serial.println("Neutral");
  } else if (phValue > 7.1) {
    Serial.println("Alkaline");
  } else{
  
  }

  // Create a single string for pH value and status
String phValueAndStatus = String(phValue, 2) + " " + phStatus;

// Print to Serial (optional)
Serial.println(phValueAndStatus);

// Send pH value and status to Blynk
Blynk.virtualWrite(V4, phValueAndStatus);
}

void checkFoodStatus() {
  int irSensorValue = digitalRead(IR_SENSOR_PIN);
  
  // IR sensor logic (adjust as per your sensor's behavior)
  if (irSensorValue == LOW) {
    Blynk.virtualWrite(V8, "Empty");  // Send status to Blynk
  } else {
    Blynk.virtualWrite(V8, "Available");  // Send status to Blynk
  }
}

void readTouchSensor() {
  int touchValue = digitalRead(TOUCH_SENSOR_PIN);

  // Check if touch sensor is triggered (adjust condition based on sensor behavior)
  if (touchValue == HIGH) {
    foodRoutine();  // Call foodRoutine if touch sensor is triggered
    delay(1000);    // Add a delay to prevent rapid triggering
  }
}

void foodRoutine() {
  moveServo(FOOD_SERVO_CHANNEL, 180); // dispense food
  delay(1000);
  moveServo(FOOD_SERVO_CHANNEL, 0);
}

void sanitationRoutine() {
  moveServo(SANITATION_SERVO_CHANNEL1, 180);
  moveServo(SANITATION_SERVO_CHANNEL2, 180);
  delay(1000);
  moveServo(SANITATION_SERVO_CHANNEL1, 0);
  moveServo(SANITATION_SERVO_CHANNEL2, 0);
}

void doorRoutine() {
  moveServo(DOOR_SERVO_CHANNEL1, 90);
  moveServo(DOOR_SERVO_CHANNEL2, 90);
  moveServo(DOOR_SERVO_CHANNEL3, 90);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Open");

  delay(1000);

  moveServo(DOOR_SERVO_CHANNEL1, 0);
  moveServo(DOOR_SERVO_CHANNEL2, 0);
  moveServo(DOOR_SERVO_CHANNEL3, 0);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Close");
}

void fanControl(float temp) {
  if (temp > 30.0) {
    digitalWrite(FAN_PIN, HIGH);
  } else {
    digitalWrite(FAN_PIN, LOW);
  }
}

void checkTime() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  for (int i = 0; i < 3; i++) {
    if (hour == waterHours[i] && minute == waterMinutes[i]) {
      waterRoutine();
    }

    if (hour == foodHours[i] && minute == foodMinutes[i]) {
      foodRoutine();
    }

    if (hour == sanitationHours[i] && minute == sanitationMinutes[i]) {
      sanitationRoutine();
    }
  }
}

// Blynk handlers to set routine times
BLYNK_WRITE(V1) {
  String time = param.asStr();
  waterHours[0] = time.substring(0, 2).toInt();
  waterMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V2) {
  String time = param.asStr();
  waterHours[1] = time.substring(0, 2).toInt();
  waterMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V3) {
  String time = param.asStr();
  waterHours[2] = time.substring(0, 2).toInt();
  waterMinutes[2] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V5) {
  String time = param.asStr();
  foodHours[0] = time.substring(0, 2).toInt();
  foodMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V6) {
  String time = param.asStr();
  foodHours[1] = time.substring(0, 2).toInt();
  foodMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V7) {
  String time = param.asStr();
  foodHours[2] = time.substring(0, 2).toInt();
  foodMinutes[2] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V9) {
  String time = param.asStr();
  sanitationHours[0] = time.substring(0, 2).toInt();
  sanitationMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V10) {
  String time = param.asStr();
  sanitationHours[1] = time.substring(0, 2).toInt();
  sanitationMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V11) {
  String time = param.asStr();
  sanitationHours[2] = time.substring(0, 2).toInt();
  sanitationMinutes[2] = time.substring(3, 5).toInt();
}

// Blynk Write Handlers for Servo
BLYNK_WRITE(V12) {
  int value = param.asInt();
  if (value == 1) {
    sanitationRoutine();
  }
}

BLYNK_WRITE(V13) {
  int value = param.asInt();
  if (value == 1) {
    doorRoutine();
  }
}

BLYNK_WRITE(V17) {
  int value = param.asInt();
  digitalWrite(FAN_PIN, value);
}

void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz

  Wire.begin();
  
  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(FAN_PIN, OUTPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
  dht.begin();
  rtc.begin();
}

void loop() {
  Blynk.run();

  double humidity = dht.readHumidity();
  double temp = dht.readTemperature();

  // Send temperature and humidity data to Blynk
  Blynk.virtualWrite(V15, temp);
  Blynk.virtualWrite(V16, humidity);

  checkTime();
  checkFoodStatus(); 
  readTouchSensor();
  fanControl(temp);

  // Read distance from ultrasonic sensor
  long duration, distance;
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  
  if (distance < 10) { // cat is detected
    doorRoutine();
  }
}```

@paupau2 Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Also, it is VERY IMPORTANT that you take the device running this code offline immediately. It contains Blynk.virtualWrites in the void loop and in functions called directly from the void loop. This will flood the Blynk servers.

Pete.

1 Like

Done, why is the value not showing to my widget box?

TBH, your code is a total mess, and you break the golden rule of Blynk, which is no Blynk.virtualWrites in the void loop.

In fact, your void loop should just contain this…

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

and you should use BlynkTimer to call your functions at the required frequency.
Read this for more info…

Until you’ve restructured your code in that way, and posted your updated code and serial monitor results (and you should include enough serial print statements to allow your variables to be tracked) then you won’t be able to progress any further with using Blynk.

Pete.

1 Like

Here is the updated code is this ok I cannot upload it because I have some error about program storage.

Global variables use 2319 bytes (113%) of dynamic memory, leaving -271 bytes for local variables. Maximum is 2048 bytes.
Sketch too big; see https://support.arduino.cc/hc/en-us/articles/360013825179 for tips on reducing it.
text section exceeds available space in board

Compilation error: text section exceeds available space in board```

```#define BLYNK_TEMPLATE_ID "*****"
#define BLYNK_TEMPLATE_NAME "*****"
#define BLYNK_AUTH_TOKEN "****"
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <RTClib.h>
#include <DHT.h>

// Your WiFi credentials.
char auth[] = "usBPpIFvpjSjO3K3rbizTQx6SqvZ-WU3";
char ssid[] = "Paulene";
char pass[] = "four_eyes";

BlynkTimer timer; // Announcing the timer

// Define pins for sensors
#define PH_SENSOR_PIN A0
#define IR_SENSOR_PIN 5
#define TOUCH_SENSOR_PIN 4
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define FAN_PIN 9

// DHT and RTC Sensor
#define DHTPIN 6
#define DHTTYPE DHT22
RTC_DS3231 rtc;
DHT dht(DHTPIN, DHTTYPE);

// Adafruit PWM Servo Driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servo channel definitions
#define WATER_SERVO_CHANNEL 0
#define FOOD_SERVO_CHANNEL 2
#define SANITATION_SERVO_CHANNEL1 4
#define SANITATION_SERVO_CHANNEL2 6
#define DOOR_SERVO_CHANNEL1 8
#define DOOR_SERVO_CHANNEL2 10
#define DOOR_SERVO_CHANNEL3 12

// Software Serial for ESP8266
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

// Routine times set by user
int waterHours[3] = {-1, -1, -1};
int waterMinutes[3] = {-1, -1, -1};
int foodHours[3] = {-1, -1, -1};
int foodMinutes[3] = {-1, -1, -1};
int sanitationHours[3] = {-1, -1, -1};
int sanitationMinutes[3] = {-1, -1, -1};

/*              SERVO MOTOR DEGREE            */ 
// Function to convert degrees to pulse length
int convertDegToPulse(int deg) {
  int pulse = map(deg, 0, 180, 150, 600); // 150-600 corresponds to the 1-2ms pulse width
  return pulse;
}

// Functions for servo actions
void moveServo(int channel, int position) {
  int pulse = convertDegToPulse(position);
  pwm.setPWM(channel, 0, pulse);
}

/*              TIME INPUT            */
void checkTime() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  Serial.print("Current time: ");
  Serial.print(hour);
  Serial.print(":");
  Serial.println(minute);

  for (int i = 0; i < 3; i++) {
    if (hour == waterHours[i] && minute == waterMinutes[i]) {
      Serial.print("Water routine scheduled at ");
      Serial.print(waterHours[i]);
      Serial.print(":");
      Serial.println(waterMinutes[i]);
      waterRoutine();
    }

    if (hour == foodHours[i] && minute == foodMinutes[i]) {
      Serial.print("Food routine scheduled at ");
      Serial.print(foodHours[i]);
      Serial.print(":");
      Serial.println(foodMinutes[i]);
      foodRoutine();
    }

    if (hour == sanitationHours[i] && minute == sanitationMinutes[i]) {
      Serial.print("Sanitation routine scheduled at ");
      Serial.print(sanitationHours[i]);
      Serial.print(":");
      Serial.println(sanitationMinutes[i]);
      sanitationRoutine();
    }
  }
}

/*              WATER DISPENSER            */
// Water calibration
float calibration_value = 21.34 + 0.7;
unsigned long int avgValue;
int buf[10], temp;

void dipSensor() {
  moveServo(WATER_SERVO_CHANNEL, 90); // dip the pH sensor
  delay(5000);                         // Allow time for the sensor to stabilize in water
  moveServo(WATER_SERVO_CHANNEL, 0);
}

void waterRoutine() {
  Serial.println("Starting water routine");
  dipSensor();
  delay(5000); // Allow time for the sensor to stabilize in water (adjust as needed)

  // Read pH value
  avgValue = 0;
  for (int i = 0; i < 10; i++) { // Get 10 sample values from the sensor for smoothing
    buf[i] = analogRead(PH_SENSOR_PIN);
    delay(10);
  }

  for (int i = 0; i < 9; i++) { // Sort the analog values from small to large
    for (int j = i + 1; j < 10; j++) {
      if (buf[i] > buf[j]) {
        temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }

  for (int i = 2; i < 8; i++) avgValue += buf[i]; // Take the average value of 6 center samples

  float phValue = (float)avgValue * 5.0 / 1024 / 6; // Convert the analog value into millivolts
  phValue = -5.70 * phValue + calibration_value;    // Convert the millivolts into pH value

  String phStatus;
  // Determine acidity level
  if (phValue < 6.9) {
    phStatus = "Acidic";
  } else if (phValue > 7.1) {
    phStatus = "Alkaline";
  } else {
    phStatus = "Neutral";
  }

  // Create a single string for pH value and status
  String phValueAndStatus = String(phValue, 2) + " " + phStatus;

  // Print to Serial (optional)
  Serial.print("pH Value: ");
  Serial.print(phValueAndStatus);

  // Send pH value and status to Blynk
  Blynk.virtualWrite(V4, phValueAndStatus);
}

// Blynk handlers to set routine times
BLYNK_WRITE(V1) {
  String time = param.asStr();
  waterHours[0] = time.substring(0, 2).toInt();
  waterMinutes[0] = time.substring(3, 5).toInt();
  Serial.print("Water routine time 1 set to ");
  Serial.print(waterHours[0]);
  Serial.print(":");
  Serial.println(waterMinutes[0]);
}

BLYNK_WRITE(V2) {
  String time = param.asStr();
  waterHours[1] = time.substring(0, 2).toInt();
  waterMinutes[1] = time.substring(3, 5).toInt();
  Serial.print("Water routine time 2 set to ");
  Serial.print(waterHours[1]);
  Serial.print(":");
  Serial.println(waterMinutes[1]);
}

BLYNK_WRITE(V3) {
  String time = param.asStr();
  waterHours[2] = time.substring(0, 2).toInt();
  waterMinutes[2] = time.substring(3, 5).toInt();
  Serial.print("Water routine time 3 set to ");
  Serial.print(waterHours[2]);
  Serial.print(":");
  Serial.println(waterMinutes[2]);
}

/*              FOOD DISPENSER            */
void checkFoodStatus() {
  int irSensorValue = digitalRead(IR_SENSOR_PIN);
  Serial.print("IR Sensor Value: ");
  Serial.println(irSensorValue);

  // IR sensor logic (adjust as per your sensor's behavior)
  if (irSensorValue == LOW) {
    Blynk.virtualWrite(V8, "Empty");  // Send status to Blynk
    Serial.println("Food Status: Empty");
  } else {
    Blynk.virtualWrite(V8, "Available");  // Send status to Blynk
    Serial.println("Food Status: Available");
  }
}

void readTouchSensor() {
  int touchValue = digitalRead(TOUCH_SENSOR_PIN);
  Serial.print("Touch Sensor Value: ");
  Serial.println(touchValue);

  // Check if touch sensor is triggered (adjust condition based on sensor behavior)
  if (touchValue == HIGH) {
    Serial.println("Touch sensor triggered, executing food routine.");
    foodRoutine();  // Call foodRoutine if touch sensor is triggered
    delay(1000);    // Add a delay to prevent rapid triggering
  }
}

void foodRoutine() {
  moveServo(FOOD_SERVO_CHANNEL, 180); // dispense food
  delay(1000);
  moveServo(FOOD_SERVO_CHANNEL, 0);
}

BLYNK_WRITE(V5) {
  String time = param.asStr();
  foodHours[0] = time.substring(0, 2).toInt();
  foodMinutes[0] = time.substring(3, 5).toInt();
  Serial.print("Food routine time 1 set to ");
  Serial.print(foodHours[0]);
  Serial.print(":");
  Serial.println(foodMinutes[0]);
}

BLYNK_WRITE(V6) {
  String time = param.asStr();
  foodHours[1] = time.substring(0, 2).toInt();
  foodMinutes[1] = time.substring(3, 5).toInt();
  Serial.print("Food routine time 2 set to ");
  Serial.print(foodHours[1]);
  Serial.print(":");
  Serial.println(foodMinutes[1]);
}

BLYNK_WRITE(V7) {
  String time = param.asStr();
  foodHours[2] = time.substring(0, 2).toInt();
  foodMinutes[2] = time.substring(3, 5).toInt();
  Serial.print("Food routine time 3 set to ");
  Serial.print(foodHours[2]);
  Serial.print(":");
  Serial.println(foodMinutes[2]);
}

/*              SANITATION MANAGEMENT            */
void sanitationRoutine() {
  Serial.println("Starting sanitation routine");
  moveServo(SANITATION_SERVO_CHANNEL1, 180);
  moveServo(SANITATION_SERVO_CHANNEL2, 180);
  delay(1000);
  moveServo(SANITATION_SERVO_CHANNEL1, 0);
  moveServo(SANITATION_SERVO_CHANNEL2, 0);
}

BLYNK_WRITE(V9) {
  String time = param.asStr();
  sanitationHours[0] = time.substring(0, 2).toInt();
  sanitationMinutes[0] = time.substring(3, 5).toInt();
  Serial.print("Sanitation routine time 1 set to ");
  Serial.print(sanitationHours[0]);
  Serial.print(":");
  Serial.println(sanitationMinutes[0]);
}

BLYNK_WRITE(V10) {
  String time = param.asStr();
  sanitationHours[1] = time.substring(0, 2).toInt();
  sanitationMinutes[1] = time.substring(3, 5).toInt();
  Serial.print("Sanitation routine time 2 set to ");
  Serial.print(sanitationHours[1]);
  Serial.print(":");
  Serial.println(sanitationMinutes[1]);
}

BLYNK_WRITE(V11) {
  String time = param.asStr();
  sanitationHours[2] = time.substring(0, 2).toInt();
  sanitationMinutes[2] = time.substring(3, 5).toInt();
  Serial.print("Sanitation routine time 3 set to ");
  Serial.print(sanitationHours[2]);
  Serial.print(":");
  Serial.println(sanitationMinutes[2]);
}

// Blynk Write Handlers for Servo
BLYNK_WRITE(V12) {
  int value = param.asInt();
  if (value == 1) {
    Serial.println("Sanitation button pressed, executing sanitation routine.");
    sanitationRoutine();
  }
}

/*              DOOR MANAGEMENT            */
void doorRoutine() {
  Serial.println("Starting door routine");
  moveServo(DOOR_SERVO_CHANNEL1, 90);
  moveServo(DOOR_SERVO_CHANNEL2, 90);
  moveServo(DOOR_SERVO_CHANNEL3, 90);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Open");

  delay(1000);

  moveServo(DOOR_SERVO_CHANNEL1, 0);
  moveServo(DOOR_SERVO_CHANNEL2, 0);
  moveServo(DOOR_SERVO_CHANNEL3, 0);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Close");
}

BLYNK_WRITE(V13) {
  int value = param.asInt();
  if (value == 1) {
    Serial.println("Door button pressed, executing door routine.");
    doorRoutine();
  }
}

/*              FAN CONTROL            */
void fanControl(float temp) {
  Serial.print("Current temperature: ");
  Serial.println(temp);
  if (temp > 30.0) {
    Serial.println("Temperature above 30.0, turning fan on.");
    digitalWrite(FAN_PIN, HIGH);
  } else {
    Serial.println("Temperature below 30.0, turning fan off.");
    digitalWrite(FAN_PIN, LOW);
  }
}

BLYNK_WRITE(V17) {
  int value = param.asInt();
  digitalWrite(FAN_PIN, value);
  Serial.print("Fan status set to ");
  Serial.println(value);
}


void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz

  Wire.begin();

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(FAN_PIN, OUTPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
  dht.begin();
  rtc.begin();

  // Setup timers
  timer.setInterval(1000L, checkTime);            // Check time every second
  timer.setInterval(2000L, checkFoodStatus);      // Check food status every 2 seconds
  timer.setInterval(2000L, readTouchSensor);      // Read touch sensor every 2 seconds
  timer.setInterval(5000L, []() {                 // Read DHT sensor every 5 seconds
    double humidity = dht.readHumidity();
    double temp = dht.readTemperature();
    Blynk.virtualWrite(V15, temp);
    Blynk.virtualWrite(V16, humidity);
    Serial.print("Temperature: ");
    Serial.println(temp);
    Serial.print("Humidity: ");
    Serial.println(humidity);
    fanControl(temp);
  });
  timer.setInterval(1000L, []() {                 // Read ultrasonic sensor every second
    long duration, distance;
    digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
    delayMicroseconds(2);
    digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
    delayMicroseconds(10);
    digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
    duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
    distance = (duration / 2) / 29.1;
    Serial.print("Ultrasonic Sensor Distance: ");
    Serial.println(distance);
    if (distance < 10) { // cat is detected
      Serial.println("Cat detected by ultrasonic sensor, executing door routine.");
      doorRoutine();
    }
  });
}

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

When you use triple backticks they need to be on a separate line.

The stuff you’re doing with these timers is very weird…

The error message regarding space is because of the board you’re using, which is presumably an Uno.
You could swap to a Mega, but TBH you’d be far better using an ESP32, and it would be cheaper too.

Pete.

1 Like

How bout this?

void readDHTSensor() {
  double humidity = dht.readHumidity();
  double temp = dht.readTemperature();
  Blynk.virtualWrite(V15, temp);
  Blynk.virtualWrite(V16, humidity);
  Serial.print("Temperature: ");
  Serial.println(temp);
  Serial.print("Humidity: ");
  Serial.println(humidity);
  fanControl(temp);
}

void readUltrasonicSensor() {
  long duration, distance;
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("Ultrasonic Sensor Distance: ");
  Serial.println(distance);
  if (distance < 10) { // cat is detected
    Serial.println("Cat detected by ultrasonic sensor, executing door routine.");
    doorRoutine();
  }
}

void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz

  Wire.begin();

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(FAN_PIN, OUTPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
  dht.begin();
  rtc.begin();

  timer.setInterval(1000L, checkTime);            // Check time every second
  timer.setInterval(2000L, checkFoodStatus);      // Check food status every 2 seconds
  timer.setInterval(2000L, readTouchSensor);      // Read touch sensor every 2 seconds
  timer.setInterval(5000L, readDHTSensor);        // Read DHT sensor every 5 seconds
  timer.setInterval(1000L, readUltrasonicSensor); // Read ultrasonic sensor every second
}

Pete.

1 Like

Here is this ok?

void readDHTSensor() {
  double humidity = dht.readHumidity();
  double temp = dht.readTemperature();
  Blynk.virtualWrite(V15, temp);
  Blynk.virtualWrite(V16, humidity);
  Serial.print("Temperature: ");
  Serial.println(temp);
  Serial.print("Humidity: ");
  Serial.println(humidity);
  fanControl(temp);
}

void readUltrasonicSensor() {
  long duration, distance;
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;
  Serial.print("Ultrasonic Sensor Distance: ");
  Serial.println(distance);
  if (distance < 10) { // cat is detected
    Serial.println("Cat detected by ultrasonic sensor, executing door routine.");
    doorRoutine();
  }
}

void setup() {
  Serial.begin(9600);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);

  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz

  Wire.begin();

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(FAN_PIN, OUTPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
  dht.begin();
  rtc.begin();

  timer.setInterval(1000L, checkTime);            // Check time every second
  timer.setInterval(2000L, checkFoodStatus);      // Check food status every 2 seconds
  timer.setInterval(2000L, readTouchSensor);      // Read touch sensor every 2 seconds
  timer.setInterval(5000L, readDHTSensor);        // Read DHT sensor every 5 seconds
  timer.setInterval(1000L, readUltrasonicSensor); // Read ultrasonic sensor every second
}

That looks more sensible, although I’m not sure you really need to check your temperature/humidity that frequently, especially when yoire using such an inaccurate sensor as the DHT range.

Pete.

Why when I upload the code the serial monitor shows “esp is not responding” yesterday its ok then now its not responding im using arduino UNO and Esp01 for my wifi to access to blynk

How did you get to the point where you were able to upload the code. Hast time you said that it was too big for your Uno.

Presumably you’ve made some change to either your sketch or your hardware that has broken your setup, but as you haven’t posted your full updated sketch, or provided any details of hardware changes you’ve made it’s difficult to point you in the right direction.

Maybe start by looking for hardware problems. Are you using a breadboard for your ESP-01 ?

Pete.

Yes im using breadboard for my esp01 and I remove some serial print to lessen the memory of my code thats why its ok now also the hardware is still the same

RX > 2
TX > 3
VCC > 3.3V
EN > 3.3V using breadboard
GND > GND

Presumably that’s not what you had before, because you’re connecting Rx to Rx and Tx to Tx like that.

If you have more than about 80% memory usage on the Uno then you’ll probably run into problems.

If you want to save memory with the serial prints then Google how to use the “F macro”.

Pete.

1 Like

Its ok now but its still no data showing on my blynk app

Without sensible serial print statements and seeing the results that these produce then it’s difficult to know what to suggest.
Also, understanding how your datastreams are configured will be necessary.

Pete.

1 Like

why when I upload this code its showing in my blynk app but when my original code its not showing

My Sample Code that work:

#define BLYNK_TEMPLATE_ID "*****"
#define BLYNK_TEMPLATE_NAME "****"
#define BLYNK_AUTH_TOKEN "****"
#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// Your WiFi credentials.
char auth[] = "***";
char ssid[] = "Paulene";
char pass[] = "four_eyes";

// Software Serial for ESP8266
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

BlynkTimer timer;

void setup() {
  // Debug console
  Serial.begin(115200);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  // Attempt to connect to Blynk
  Serial.println("Connecting to Blynk...");
  Blynk.begin(auth, wifi, ssid, pass);

  // Verify connection
  if (Blynk.connected()) {
    Serial.println("Blynk connected");
  } else {
    Serial.println("Failed to connect to Blynk");
  }

  // Simple timer to print a message to the serial monitor every second
  timer.setInterval(1000L, []() {
    Serial.println("Running...");
    Blynk.virtualWrite(V0, "Hello, Blynk!");
  });
}

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

My Original Sketch:

#define BLYNK_TEMPLATE_ID "******"
#define BLYNK_TEMPLATE_NAME "****"
#define BLYNK_AUTH_TOKEN "***"
#define BLYNK_PRINT Serial

#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <RTClib.h>
#include <DHT.h>

// Your WiFi credentials.
char auth[] = "****";
char ssid[] = "Paulene";
char pass[] = "four_eyes";

BlynkTimer timer; // Announcing the timer

// Define pins for sensors
#define PH_SENSOR_PIN A0
#define IR_SENSOR_PIN 5
#define TOUCH_SENSOR_PIN 4
#define ULTRASONIC_TRIG_PIN 8
#define ULTRASONIC_ECHO_PIN 7
#define FAN_PIN 9

// DHT and RTC Sensor
#define DHTPIN 6
#define DHTTYPE DHT22
RTC_DS3231 rtc;
DHT dht(DHTPIN, DHTTYPE);

// Adafruit PWM Servo Driver
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();

// Servo channel definitions
#define WATER_SERVO_CHANNEL 0
#define FOOD_SERVO_CHANNEL 2
#define SANITATION_SERVO_CHANNEL1 4
#define SANITATION_SERVO_CHANNEL2 6
#define DOOR_SERVO_CHANNEL1 8
#define DOOR_SERVO_CHANNEL2 10
#define DOOR_SERVO_CHANNEL3 12

// Software Serial for ESP8266
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

// Routine times set by user
int waterHours[3] = {-1, -1, -1};
int waterMinutes[3] = {-1, -1, -1};
int foodHours[3] = {-1, -1, -1};
int foodMinutes[3] = {-1, -1, -1};
int sanitationHours[3] = {-1, -1, -1};
int sanitationMinutes[3] = {-1, -1, -1};

/*              SERVO MOTOR DEGREE            */ 
// Function to convert degrees to pulse length
int convertDegToPulse(int deg) {
  int pulse = map(deg, 0, 180, 150, 600); // 150-600 corresponds to the 1-2ms pulse width
  return pulse;
}

// Functions for servo actions
void moveServo(int channel, int position) {
  int pulse = convertDegToPulse(position);
  pwm.setPWM(channel, 0, pulse);
}

/*              TIME INPUT            */
void checkTime() {
  DateTime now = rtc.now();
  int hour = now.hour();
  int minute = now.minute();

  for (int i = 0; i < 3; i++) {
    if (hour == waterHours[i] && minute == waterMinutes[i]) {
      waterRoutine();
    }

    if (hour == foodHours[i] && minute == foodMinutes[i]) {
      foodRoutine();
    }

    if (hour == sanitationHours[i] && minute == sanitationMinutes[i]) {
      sanitationRoutine();
    }
  }
}

/*              WATER DISPENSER            */
// Water calibration
float calibration_value = 21.34 + 0.7;
unsigned long int avgValue;
int buf[10], temp;

void dipSensor() {
  moveServo(WATER_SERVO_CHANNEL, 90); // dip the pH sensor
  delay(5000);                         // Allow time for the sensor to stabilize in water
  moveServo(WATER_SERVO_CHANNEL, 0);
}

void waterRoutine() {
  Serial.println(F("Starting water routine"));
  dipSensor();
  delay(5000); // Allow time for the sensor to stabilize in water (adjust as needed)

  // Read pH value
  avgValue = 0;
  for (int i = 0; i < 10; i++) { // Get 10 sample values from the sensor for smoothing
    buf[i] = analogRead(PH_SENSOR_PIN);
    delay(10);
  }

  for (int i = 0; i < 9; i++) { // Sort the analog values from small to large
    for (int j = i + 1; j < 10; j++) {
      if (buf[i] > buf[j]) {
        temp = buf[i];
        buf[i] = buf[j];
        buf[j] = temp;
      }
    }
  }

  for (int i = 2; i < 8; i++) avgValue += buf[i]; // Take the average value of 6 center samples

  float phValue = (float)avgValue * 5.0 / 1024 / 6; // Convert the analog value into millivolts
  phValue = -5.70 * phValue + calibration_value;    // Convert the millivolts into pH value

  String phStatus;
  // Determine acidity level
  if (phValue < 6.9) {
    phStatus = "Acidic";
  } else if (phValue > 7.1) {
    phStatus = "Alkaline";
  } else {
    phStatus = "Neutral";
  }

  // Create a single string for pH value and status
  String phValueAndStatus = String(phValue, 2) + " " + phStatus;

  // Print to Serial (optional)
  Serial.print(F("pH Value: "));
  Serial.println(phValueAndStatus);

  // Send pH value and status to Blynk
  Blynk.virtualWrite(V4, phValueAndStatus);
}

// Blynk handlers to set routine times
BLYNK_WRITE(V1) {
  String time = param.asStr();
  waterHours[0] = time.substring(0, 2).toInt();
  waterMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V2) {
  String time = param.asStr();
  waterHours[1] = time.substring(0, 2).toInt();
  waterMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V3) {
  String time = param.asStr();
  waterHours[2] = time.substring(0, 2).toInt();
  waterMinutes[2] = time.substring(3, 5).toInt();
}

/*              FOOD DISPENSER            */
void checkFoodStatus() {
  int irSensorValue = digitalRead(IR_SENSOR_PIN);

  // IR sensor logic (adjust as per your sensor's behavior)
  if (irSensorValue == LOW) {
    Blynk.virtualWrite(V8, "Empty");  // Send status to Blynk
    Serial.println(F("Food Status: Empty"));
  } else {
    Blynk.virtualWrite(V8, "Available");  // Send status to Blynk
    Serial.println(F("Food Status: Available"));
  }
}

void readTouchSensor() {
  int touchValue = digitalRead(TOUCH_SENSOR_PIN);
  Serial.print(F("Touch Sensor Value: "));
  Serial.println(touchValue);

  // Check if touch sensor is triggered (adjust condition based on sensor behavior)
  if (touchValue == HIGH) {
    Serial.println(F("Touch sensor triggered, executing food routine."));
    foodRoutine();  // Call foodRoutine if touch sensor is triggered
    delay(1000);    // Add a delay to prevent rapid triggering
  }
}

void foodRoutine() {
  moveServo(FOOD_SERVO_CHANNEL, 180); // dispense food
  delay(1000);
  moveServo(FOOD_SERVO_CHANNEL, 0);
}

BLYNK_WRITE(V5) {
  String time = param.asStr();
  foodHours[0] = time.substring(0, 2).toInt();
  foodMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V6) {
  String time = param.asStr();
  foodHours[1] = time.substring(0, 2).toInt();
  foodMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V7) {
  String time = param.asStr();
  foodHours[2] = time.substring(0, 2).toInt();
  foodMinutes[2] = time.substring(3, 5).toInt();
}

/*              SANITATION MANAGEMENT            */
void sanitationRoutine() {
  Serial.println(F("Starting sanitation routine"));
  moveServo(SANITATION_SERVO_CHANNEL1, 180);
  moveServo(SANITATION_SERVO_CHANNEL2, 180);
  delay(1000);
  moveServo(SANITATION_SERVO_CHANNEL1, 0);
  moveServo(SANITATION_SERVO_CHANNEL2, 0);
}

BLYNK_WRITE(V9) {
  String time = param.asStr();
  sanitationHours[0] = time.substring(0, 2).toInt();
  sanitationMinutes[0] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V10) {
  String time = param.asStr();
  sanitationHours[1] = time.substring(0, 2).toInt();
  sanitationMinutes[1] = time.substring(3, 5).toInt();
}

BLYNK_WRITE(V11) {
  String time = param.asStr();
  sanitationHours[2] = time.substring(0, 2).toInt();
  sanitationMinutes[2] = time.substring(3, 5).toInt();
}

// Blynk Write Handlers for Servo
BLYNK_WRITE(V12) {
  int value = param.asInt();
  if (value == 1) {
    Serial.println(F("Sanitation button pressed, executing sanitation routine."));
    sanitationRoutine();
  }
}

/*              DOOR MANAGEMENT            */
void doorRoutine() {
  Serial.println(F("Starting door routine"));
  moveServo(DOOR_SERVO_CHANNEL1, 90);
  moveServo(DOOR_SERVO_CHANNEL2, 90);
  moveServo(DOOR_SERVO_CHANNEL3, 90);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Open");

  delay(1000);

  moveServo(DOOR_SERVO_CHANNEL1, 0);
  moveServo(DOOR_SERVO_CHANNEL2, 0);
  moveServo(DOOR_SERVO_CHANNEL3, 0);
  // Send door status to Blynk
  Blynk.virtualWrite(V14, "Close");
}

BLYNK_WRITE(V13) {
  int value = param.asInt();
  if (value == 1) {
    Serial.println(F("Door button pressed, executing door routine."));
    doorRoutine();
  }
}

/*              FAN CONTROL            */
void fanControl(float temp) {
  if (temp > 30.0) {
    Serial.println(F("Temperature above 30.0, turning fan on."));
    digitalWrite(FAN_PIN, HIGH);
  } else {
    Serial.println(F("Temperature below 30.0, turning fan off."));
    digitalWrite(FAN_PIN, LOW);
  }
}

BLYNK_WRITE(V17) {
  int value = param.asInt();
  digitalWrite(FAN_PIN, value);
  Serial.print(F("Fan status set to "));
  Serial.println(value);
}

void readDHTSensor() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Serial.print("Temperature: ");
  Serial.println(t);
  Serial.print("Humidity: ");
  Serial.println(h);
  
  Blynk.virtualWrite(V15, t);
  Blynk.virtualWrite(V16, h);
  fanControl(t);
}

void readUltrasonicSensor() {
  long duration, distance;
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(ULTRASONIC_TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
  duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH);
  distance = (duration / 2) / 29.1;

  if (distance < 10) { // cat is detected
    Serial.println(F("Cat detected by ultrasonic sensor, executing door routine."));
    doorRoutine();
  }
}

void setup() {
  Serial.begin(115200);
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  // Debug Blynk connection
  Serial.println("Connecting to Blynk...");
  Blynk.begin(auth, wifi, ssid, pass);
  
  if (Blynk.connected()) {
    Serial.println("Blynk connected");
  } else {
    Serial.println("Failed to connect to Blynk");
  }
  pwm.begin();
  pwm.setPWMFreq(60);  // Analog servos run at ~60 Hz

  Wire.begin();

  if (!rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }

  if (rtc.lostPower()) {
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }

  pinMode(FAN_PIN, OUTPUT);
  pinMode(ULTRASONIC_TRIG_PIN, OUTPUT);
  pinMode(ULTRASONIC_ECHO_PIN, INPUT);
  pinMode(IR_SENSOR_PIN, INPUT);
  pinMode(TOUCH_SENSOR_PIN, INPUT);
  dht.begin();
  rtc.begin();

  timer.setInterval(1000L, checkTime);            // Check time every second
  timer.setInterval(2000L, checkFoodStatus);      // Check food status every 2 seconds
  timer.setInterval(2000L, readTouchSensor);      // Read touch sensor every 2 seconds
  timer.setInterval(5000L, readDHTSensor);        // Read DHT sensor every 5 seconds
  timer.setInterval(1000L, readUltrasonicSensor); // Read ultrasonic sensor every second
}

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

Pete.

instead of continuously asking for help, why don’t you start some debugging by added pieces to the working code until you find the culprit. You would learn a lot :wink:

1 Like

Hello when I add adafruit PWM servo driver that have delays the code cannot show the BLYNK CONNECTED only WIFI CONNECTED what should I do?