When i comment out some codes (either LCD, or ESP-01 or all sensors), then it works fine. separately every single functionality works fine. When i put all of them together, the code gets stuck in blynk.begin() function.
#define BLYNK_TEMPLATE_ID "TMPL6q522FnRw"
#define BLYNK_TEMPLATE_NAME "Faw"
#define BLYNK_AUTH_TOKEN "UZMxeaRLAumm1VVfdGGhtNiTmGOz8OJe"
#define BLYNK_PRINT Serial
#define ESP8266_BAUD 38400
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <Servo.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <LiquidCrystal_I2C.h>
#define RX_PIN 2
#define TX_PIN 3
#define TEMP_PIN 4
#define BUTTON_PIN 5
#define SERVO_PIN 6
#define TRIG_PIN 9
#define ECHO_PIN 10
#define PH_PIN A0
#define GREEN_LED_PIN 11
#define RED_LED_PIN 13
#define sec 1000
char ssid[] = "Look Ma, No Wires";
//char ssid[] = "Fawzan";
char pass[] = "APD2F2206";
//char pass[] = "1223334444";
Servo myservo;
OneWire oneWire(TEMP_PIN);
DallasTemperature sensors(&oneWire);
SoftwareSerial EspSerial(RX_PIN, TX_PIN);
ESP8266 wifi(&EspSerial);
LiquidCrystal_I2C lcd(0x27, 20, 4);
bool blynkManualFeed, blynkAutoFeed;
int blynkRotation, blynkFeedingFreq;
String lastTimeFed_Str;
unsigned long lastTimeFed = 0;
unsigned long previousMillisFood = 0;
int temp_L, temp_H;
unsigned long lastSensorUpdated = 0;
unsigned long previousMillisSensor = 0;
BLYNK_WRITE(V0)
{
int switch_value = param.asInt();
if (switch_value == 1)
{
blynkManualFeed = true;
}
else
{
blynkManualFeed = false;
}
}
BLYNK_WRITE(V1)
{
int switch_value = param.asInt();
if (switch_value == 1)
{
blynkAutoFeed = true;
lcd.setCursor(0, 2);
lcd.print("Auto Feed: ON");
}
else
{
blynkAutoFeed = false;
lcd.setCursor(0, 2);
lcd.print("Auto Feed: OFF");
}
}
BLYNK_WRITE(V2)
{
blynkFeedingFreq = param.asInt();
}
BLYNK_WRITE(V3)
{
blynkRotation = param.asInt();
}
BLYNK_WRITE(V10)
{
temp_L = param.asInt();
}
BLYNK_WRITE(V12)
{
temp_H = param.asInt();
}
void setup()
{
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Starting.... ");
Serial.begin(115200);
Serial.println("Setup");
Serial.println("0");
myservo.attach(SERVO_PIN);
myservo.write(0);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
sensors.begin();
EspSerial.begin(ESP8266_BAUD);
Serial.println("1");
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
Serial.println("2");
temp_L = 25;
Blynk.virtualWrite(V10, temp_L);
temp_H = 35;
Blynk.virtualWrite(V12, temp_H);
blynkAutoFeed = false;
Blynk.virtualWrite(V0, 0);
blynkAutoFeed = false;
Blynk.virtualWrite(V1, 0);
blynkFeedingFreq = 4;
Blynk.virtualWrite(V2, blynkFeedingFreq);
blynkRotation = 3;
Blynk.virtualWrite(V2, blynkRotation);
Blynk.virtualWrite(V8, 1);
Blynk.virtualWrite(V9, 0);
Blynk.virtualWrite(V11, "Starting....");
Blynk.virtualWrite(V15, "");
}
void loop()
{
lcd.setCursor(0, 0);
lcd.print("Started ");
Blynk.run();
lastTimeFed = millis() - previousMillisFood;
unsigned long hours = lastTimeFed / 3600000; // 1 hour = 3600000 milliseconds
unsigned long minutes = (lastTimeFed % 3600000) / 60000; // 1 minute = 60000 milliseconds
if (digitalRead(BUTTON_PIN) == 1 || blynkManualFeed == true || (blynkAutoFeed == true && hours >= blynkFeedingFreq)) // if the button is pressed or the switch is ON
{
rotateServo(blynkRotation);
if (blynkManualFeed == true)
{
Blynk.virtualWrite(V0, 0);
blynkManualFeed = false;
}
previousMillisFood = millis();
}
lastTimeFed_Str = String(hours) + "h" + String(minutes) + "m";
if (previousMillisFood != 0)
{
Blynk.virtualWrite(V4, lastTimeFed_Str);
}
lcd.setCursor(0, 1); // move cursor the first row
lcd.print("Last Fed: " + String(lastTimeFed_Str) + " ago");
lastSensorUpdated = millis() - previousMillisSensor;
if (lastSensorUpdated >= 1 * sec)
{
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
Serial.println(temp);
Blynk.virtualWrite(V5, temp);
int d = getFoodAmount();
Blynk.virtualWrite(V7, d);
Serial.println("D %: " + String(d));
if (temp < temp_L || temp > temp_H)
{
Blynk.virtualWrite(V15, "Critical Temp");
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
Blynk.virtualWrite(V8, 0);
Blynk.virtualWrite(V9, 1);
Blynk.virtualWrite(V11, "Warning!");
}
else
{
Blynk.virtualWrite(V11, "All Good!");
Blynk.virtualWrite(V15, "Normal Temp");
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
Blynk.virtualWrite(V8, 1);
Blynk.virtualWrite(V9, 0);
}
lcd.setCursor(0, 0); // move cursor the first row
lcd.print("Temp:" + String(temp) + "C pH:14.0");
previousMillisSensor = millis();
}
}
int getFoodAmount()
{
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
float duration = pulseIn(ECHO_PIN, HIGH);
float distance = duration * 0.034 / 2;
Serial.println("Distance: " + String(distance));
int distancePercentage = round((12 - distance) * 100 / 12);
return distancePercentage;
}
void rotateServo(int n)
{
for (int i = 0; i < n; i++)
{
if (myservo.read() == 0)
{
myservo.write(180);
}
else if (myservo.read() == 180)
{
myservo.write(0);
}
delay(1 * sec);
Serial.println(i);
}
}
Sketch uses 22900 bytes (70%) of program storage space. Maximum is 32256 bytes.
Global variables use 1243 bytes (60%) of dynamic memory, leaving 805 bytes for local variables. Maximum is 2048 bytes.
