Help needed,
I have adjusted my code to what I think is better for the blynk application but I am having trouble staying online and esp not responding. I have changed the baud rate to 9600 and yesterday the code and baud didnt match and it randomly started working. I’m really confused and have to hand up this project in 2 days.
Today Baud was changed to 9600 and was working but stopped at the next upload with no changes to ESP not responding. Any help really appreciated as my time is running out
#include <util/delay.h>
#include <DHT.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#define pumptime 2500 //Pump on time
//BLYNK- APP and WEB requirements
#define BLYNK_TEMPLATE_ID "TMPL6Pv17xQ0"//
#define BLYNK_TEMPLATE_NAME "Greenhouse"//project name
#define BLYNK_AUTH_TOKEN "5j3T5Wy-3sapGdJpzz3F-T3AfyN-IKEN"
#define BLYNK_PRINT Serial
//Blynk Libraries
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
//BlynkTimer timer;
BlynkTimer timer;
#define INTERVAL 5000L //5 sec timer
// Your WiFi details.
// Set password to "" for open networks.
char ssid[] = "Fridgefi";
char pass[] = "xxxxxxx";
// For arduino Uno
SoftwareSerial EspSerial(2, 3); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
// DHT parameters
#define DHTPIN 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
// Arduino Pins
#define FAN 10 //enable Fan on pin 10
#define BULB 9 //enable Bulb on pin 9
#define PUMP 8 //enable pump on pump 8
#define RedLED1 7 //Red LED for water level
#define GreenLED1 4 //Green LED for water level
#define SoilMoisture A1 //Soil Moisture Sensor
#define WaterLevel A2// WaterLevel Sensor
//Virtual pins FOR BLYNK APP
#define V1_SOIL_MOISTURE V1
#define V2_WATER_LEVEL V2
#define V3_GREEN_LED V3
#define V5_Humidity V5
#define V6_Temperature V6
#define V7_RED_LED V7
#define V8_PUMP V8
#define V9_BULB V9
#define V10_FAN V10
const int SoilDry = 520; //Dry value of soil
const int SoilWet = 260; //Wet value of soil
void getTemp(){
// Read DHT11- Inside Greenhouse
int temperature = dht.readTemperature();// read the temp and assign the value to temperature
int humidity = dht.readHumidity();// read the humidity and assign the value to temperature
Blynk.virtualWrite(V6_Temperature, temperature);
Blynk.virtualWrite(V5_Humidity, humidity);
Serial.print("Temp: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
// Check if reads fails
if (isnan(temperature) || isnan(humidity)) {// if not a number on temp or humidity return the error below
//Serial.println(F("Failed to read from DHT sensor"));
digitalWrite (RedLED1,HIGH);
digitalWrite (GreenLED1,LOW);// turn on red LED for alarm
return;
}
if (temperature < 22) {
digitalWrite(FAN, LOW);//Turn off the fan
digitalWrite(BULB,HIGH);
Blynk.virtualWrite(V9_BULB, HIGH);
Blynk.virtualWrite(V10_FAN, LOW);
}
else if(temperature > 23 ) {
digitalWrite(FAN, HIGH);//Turn on the fan
digitalWrite(BULB, LOW);//Turn off the bulb
Blynk.virtualWrite(V9_BULB, LOW);
Blynk.virtualWrite(V10_FAN, HIGH);
}
}
void checkMoisture(){
int SoilMoisture = analogRead (SoilMoisture);//read value of soil moisture
int WaterLevel = analogRead (WaterLevel); //read value of waterlevel
int SoilMoistpercent; // moisture value in percentage
SoilMoistpercent = map(SoilMoisture,SoilWet,SoilDry,100,0); // To display the soil moisture value in percentage
Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoistpercent);
Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);
Serial.print("WaterLevel: ");
Serial.println(WaterLevel);
Serial.print("SoilMoisture % is : ");
Serial.println(SoilMoistpercent);
if (SoilMoistpercent < 50 && WaterLevel > 350){
digitalWrite (RedLED1,LOW);
digitalWrite (GreenLED1,HIGH);
digitalWrite (PUMP,HIGH);
Blynk.virtualWrite(V7_RED_LED, LOW);
Blynk.virtualWrite(V3_GREEN_LED, HIGH);
Blynk.virtualWrite(V8_PUMP, HIGH);
delay (pumptime);
digitalWrite (PUMP,LOW);
digitalWrite (RedLED1,LOW);
digitalWrite (GreenLED1,HIGH);
Blynk.virtualWrite(V8_PUMP, LOW);
Blynk.virtualWrite(V3_GREEN_LED, HIGH);
Blynk.virtualWrite(V7_RED_LED, LOW);
delay(1000);
}
else if (WaterLevel < 340){
digitalWrite (RedLED1,HIGH); //turn on the red LED
Blynk.virtualWrite(V7_RED_LED, HIGH);//Write to app
digitalWrite (GreenLED1,LOW);//Turn off green led
Blynk.virtualWrite(V3_GREEN_LED, LOW);//write to blynk
digitalWrite (PUMP,LOW); //turn off pump
delay(5000);//5 second delay
}
}
void mytimer(){// write on the timer ticks
Blynk.virtualWrite(V1_SOIL_MOISTURE, SoilMoisture);
Blynk.virtualWrite(V2_WATER_LEVEL, WaterLevel);
Blynk.virtualWrite(V3_GREEN_LED, GreenLED1);
Blynk.virtualWrite(V7_RED_LED, RedLED1);
Blynk.virtualWrite(V8_PUMP, PUMP);
Blynk.virtualWrite(V9_BULB, BULB);
Blynk.virtualWrite(V10_FAN, FAN);
Blynk.syncVirtual(V1, V2, V3, V5, V6, V7, V8, V9, V10);// sync with app
}
void setup() {
// Debug console
Serial.begin(9600);// for Blynk app
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
// Initialize Temp & Humidity Sensor
dht.begin();
//Blynk WIFI
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80); //BLYNK app
//timer.setInterval(INTERVAL, getTemp);
//Enable pinMode
//OUTPUTS//
pinMode (FAN, OUTPUT); //enable pin 10 as output
digitalWrite(FAN, LOW);//initial low
pinMode (BULB, OUTPUT); //enable pin 9 as output
digitalWrite(BULB, LOW);//initial low
pinMode (PUMP, OUTPUT); //enable pin 8 as output
digitalWrite(PUMP, LOW);//initial low
pinMode (RedLED1,OUTPUT);
digitalWrite(RedLED1, LOW);//initial low
pinMode (GreenLED1,OUTPUT);
digitalWrite(GreenLED1, LOW);//initial low
//INPUTS//
pinMode(SoilMoisture, INPUT); //enable pin A1 as input
pinMode(WaterLevel, INPUT);//enable pin A2 as input
//pinMode()
}
void loop() {
Blynk.run();//Runs blynk
getTemp (); //function for temp
checkMoisture();//function for soil moisture
timer.run();//Run the Blynk timer
}