BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

How to connect blynk codes

#1

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();
  }
0 Likes

#2

Proper way to format posted code…

Blynk%20-%20FTFC

0 Likes

#3

You have a lot of delays in your code… that will block all processing, including Blynk, and cause disconnections.

You already seem to have a basic understanding of timers (sort of :stuck_out_tongue: ) However, you have a timer running a function every second, but then a 3+ second delay inside that function :crazy_face:, so I recommend a little more study on how timers work.

There are lots of examples and user made code with temperature controlled fans floating around this forum that will give you something to go through and study to learn how you can make yours even better.

0 Likes

#4

Thank you Gunner, so will it be okay if I remove all the delays inside the fancontrol function and instead of that I will call the function after every second with the help of timer.setinterval ? will it work now?

0 Likes

#5

I don’t know :stuck_out_tongue: But it is a good start.

0 Likes

#6

You seem to be doing debug prints to both your software serial and hardware serial ports. This won’t work, as one needs to be dedicated to your Blynk connection (probably your hardware serial port).

Pete.

0 Likes

#7

Good catch @PeteKnight

0 Likes

#8

Thank you PeteKnight for the suggestion, the problem is sloved now program works fine and the updated code is


#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);
     
          }
    
    else if(t==26)
    {
      analogWrite(Gate, 51);
    
    }
    
     else if(t==27)
    {
      analogWrite(Gate, 100);
      
    }
    
     else if(t==28)
    {
      analogWrite(Gate, 800);
       
    }
    
    else if(t==29)
    {
      analogWrite(Gate, 900);
      
    }
     else if(t>29)
    {
      analogWrite(Gate, 1000);
   
    } 
    
 }

void setup()
{
  pinMode(Gate,OUTPUT);
  digitalWrite(Gate,LOW);
  analogWrite(Gate, 0);
 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);
  timer.setInterval(1000L, fancontrol);
  
}

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

#9

These are still trying to run at the exact same time, every second… if that is intentional, then combine them into ONE timed function, else stagger then in non-intersecting timeframes

0 Likes

#10

Also, if you declare global variables, then you do not need to keep pulling the same value twice.

https://www.arduino.cc/reference/en/language/variables/variable-scope--qualifiers/scope/

0 Likes

#11

Thank you gunner for the update global declaration didn’t strike at the moment .:sweat_smile: . I still on have one question is it possible to display the current state of the led through blynk? I mean whether it is on/off state?

0 Likes

#12

What LED?

You can read the state of any digital or analog pin and show the value on a Blynk display… simplest way is like this…

Blynk.virtualWrite(vPin, digitalRead(pin));  // view the state of a Digital GPIO pin

Blynk.virtualWrite(vPin, analogRead(pin));  // view the state of a Analog pin

https://www.arduino.cc/reference/en/

0 Likes