Before creating the topic
hi blynkers,
I am trying to turn on and off the fan automatically using the dht11. whenever the value from the sensor exceed a certain value the fan must be turned on, and also in addition to it I need to print the temperature value in the blynk app. The problem is when I combine the blynk code to my code app is not displaying the sensor value. I am using Arduino uno with dht11 via usb to connect with blynk.
#define Gate 9
#define BLYNK_PRINT SwSerial
#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX
#include <BlynkSimpleStream.h>
#include <DHT.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "dfe8ce156c5a4a1990fb55bb91326ded";
#define DHTPIN 2 // What digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
// This function sends Arduino's up time every second to Virtual Pin (5).
// In the app, Widget's reading frequency should be set to PUSH. This means
// that you define how often to send data to Blynk App.
void sendSensor()
{
float h = dht.readHumidity();
float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit
if (isnan(h) || isnan(t)) {
SwSerial.println("Failed to read from DHT sensor!");
return;
}
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, h);
Blynk.virtualWrite(V6, t);
}
void fancontrol()
{ float t = dht.readTemperature();
if(t <26 )
{
analogWrite(Gate,0);
// Serial.println ( " Tempvalue is " ) ;
Serial.print ( t ) ; // Printing the temperature on display.tem
// Serial.println ( " *C " ) ;
Serial.println("FAN OFF");
delay(100);
}
else if(t==26)
{
analogWrite(Gate, 51);
// Serial.println ( " Tempvalue is " ) ;
Serial.print ( t ) ; // Printing the temperature on display.
// Serial.println ( " *C " ) ;
Serial.println("FAN SPEED 20%");
delay(100);
}
else if(t==27)
{
analogWrite(Gate, 100);
// Serial.println ( " Tempvalue is " ) ;
Serial.print ( t ) ; // Printing the temperature on display.
// Serial.println ( " *C " ) ;
Serial.println("FAN SPEED 40%");
delay(100);
}
else if(t==28)
{
analogWrite(Gate, 800);
//Serial.println ( " Tempvalue is " ) ;
Serial.print (t ) ; // Printing the temperature on display.
//Serial.println ( " *C " ) ;
Serial.println("FAN SPEED 60%");
delay(100);
}
else if(t==29)
{
analogWrite(Gate, 800);
//Serial.println ( " Tempvalue is " ) ;
Serial.print ( t ) ; // Printing the temperature on display.
// Serial.println ( " *C " ) ;*/
Serial.println("FAN SPEED 80%");
delay(100);
}
else if(t>29)
{
analogWrite(Gate, 900);
// Serial.println ( " Tempvalue is " ) ;
Serial.print ( t ) ; // Printing the temperature on display.
// Serial.println ( " *C " ) ;
Serial.println("FAN SPEED 100%");
delay(100);
}
delay(3000);}
void setup()
{
pinMode(Gate,OUTPUT);
digitalWrite(Gate,LOW);
analogWrite(Gate, 255);
Serial.begin(9600);
// Debug console
SwSerial.begin(9600);
// Blynk will work through Serial
// Do not read or write this serial manually in your sketch
Serial.begin(9600);
Blynk.begin(Serial, auth);
dht.begin();
// Setup a function to be called every second
timer.setInterval(1000L, sendSensor);
}
void loop()
{
Blynk.run();
timer.run();
fancontrol();
}