Hi, I’m currently working on a project.
So this project has two mode, auto and manual mode.
Auto mode was fine. But when i go to manual mode, after several tries, blynk disconnected. I have tried used similar code to for LED(replace relay), but it was fine. Help Please.
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 7 // What digital pin we're connected to
#include <SoftwareSerial.h> //Software Serial on Uno
SoftwareSerial EspSerial(2, 3); // RX, TX
#define relay1 11
#define ledred 10
#define ledGreen 12
#define relay2 4
char auth[] = "";
char ssid[] = "";
char pass[] = "";
ESP8266 wifi(&EspSerial);
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
int i=0;
int button2;
SimpleTimer timer;
int selectmode;
long duration;
int distance;
const int trigPin = 9;
const int echoPin = 10;
int lightpin =0;
float Vout;
int Lux;
void senddata()
{
long x = ultrasonic();
int y= lightsensor();
float h = dht.readHumidity();
float t = dht.readTemperature();
// Blynk.virtualWrite(V1, h);
// Blynk.virtualWrite(V2, t);
// Blynk.virtualWrite(V3, x);
// Blynk.virtualWrite(V4, y);
if(h>80)
Blynk.tweet(String("Warning,Current Humidity is ") + h + ",higher than pre-set 80%");
if(t>40)
Blynk.tweet(String("Warning,Current Temperature is ")+ t + ",higher than pre-set 40");
}
long ultrasonic (){
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
delay(10);
return distance;
}
int lightsensor(){
int analoglux=analogRead(lightpin);
Vout = (analoglux* 0.0048828125);
Lux=(2500/Vout-500)/10;
return Lux;
}*/
void Auto(){
//int w=lightsensor();
//if(w >50)
if(i==0&&selectmode==0){
digitalWrite(relay1,HIGH);
delay(5000);
digitalWrite(relay1,LOW);};
/* else{
digitalWrite(relay1,LOW);
}
}*/
}
void Manual(){
if(button2==1&&selectmode==1){
digitalWrite(relay1,HIGH);
}
else{
digitalWrite(relay1,,LOW);
}
}
/*void waterlevel(){
long w=ultrasonic();
if (x >= 50) {
digitalWrite(relay2,HIGH);
} else {
digitalWrite(relay2,LOW);
}
}*/
void setup()
{
pinMode(led, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(ledred, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(lightpin,OUTPUT);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Debug console
Serial.begin(9600);
// Set ESP8266 baud rate
EspSerial.begin(9600);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
dht.begin();
//Setup a function to be called every second
// timer.setInterval(5000, senddata);
// timer.setInterval(100, waterlevel);
}
void loop(){
Blynk.run();
timer.run(); // Initiates SimpleTimer
}
BLYNK_WRITE(V6){
button2 = param.asInt();
}
BLYNK_WRITE(V5){
selectmode = param.asInt();
if(selectmode ==1){
// digitalWrite(ledGreen,LOW);
// digitalWrite(ledred,HIGH);
i=1;
timer.setInterval(1000L, Manual);
}
else{
// digitalWrite(ledred,LOW);
// digitalWrite(ledGreen,HIGH);
i=0; //include led over here red + green
timer.setInterval(15000, Auto);
}
}
BLYNK_CONNECTED(){
Blynk.syncVirtual(V5);
Blynk.syncVirtual(V6);
}