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

I am new at coding


#1

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

I am new at coding. I would like to build a simple thermostat with NodeMcu and the DHT22 temperature sensor. I become an error when debuging the code. With wirtual pin V1 I would like to turn the regulation on and off

#define grelci D1, OUTPUT
#define BLYNK_PRINT Serial
  float maxtemp = 27;
int pinValue;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxxxxx";
char pass[] = "xxxxxx";

#define DHTPIN D2          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

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)) {
    Serial.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);
      if (t < maxtemp && pinValue == 1 ){
    digitalWrite(grelci, HIGH);}
    else
    {
      digitalWrite(grelci, LOW);
    }
}
   BLYNK_WRITE(V1)
  {
     int pinValue = param.asInt();
    }

  
  void setup()
 {

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
 }

 // Setup a function to be called every second

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



#2

Change this ^ to a Global variable

Then you could add in a “flag check” with if() command to determine if you want to run the temp scan or not, depending on the value of the Widget on V1.

Eg.

void sendSensor() {
  if (pinValue == 1) {
  // run all your sensor stuff here
  } else {
    // do nothing
  }
}

#3

Thank you for answer. I changed the code, but it still does not work.
This is my new code:

#define grelci D1, OUTPUT
#define BLYNK_PRINT Serial
  float maxtemp = 27;
int pinValue;

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = " xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxxxx";

#define DHTPIN D2          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

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.

   BLYNK_WRITE(V1)
  {
     int pinValue = param.asInt();
    }
void sendSensor()
{
  if (pinValue == 1){
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  if(t < maxtemp){
   digitalWrite(grelci, HIGH);  
  }
  else
  {
    digitalWrite(grelci, LOW);
  }
 } 
}  

void setup()
 {

  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(1000L, sendSensor);
 }

 // Setup a function to be called every second

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

#4

You didn’t properly format your posted code :wink: (So I did it again :frowning: )

Exactly what does not work? Not compiling? loading? running? connecting? showing correct value? showing any value? making you coffee?..


#5

I think that you should define your D1 in the setup func as:

void setup {
...
pinMode(pin, mode);
...
}

and not on top as you did…
I would also move your

if(pinValue==1)

lower… just above the if maxtemp so you would still get the temp readings even if you don’t have the V1 set to on…


#6

Thank you both for suggestions. I solve the problem.


#7

So, for future the readers of this post, what was the solution?


#8

Here is the solution - the working code:

/* Comment this out to disable prints and save space */
#define grelci D1
#define BLYNK_PRINT Serial
 int maxtemp;
int pinValue;
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxxxxxxxx";

#define DHTPIN D2          // What digital pin we're connected to

// Uncomment whatever type you're using!
//#define DHTTYPE DHT11     // DHT 11
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321
//#define DHTTYPE DHT21   // DHT 21, AM2301

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)) {
    Serial.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); 
    
    if(t < maxtemp && pinValue == 1) {
    digitalWrite(grelci, HIGH);
  }else{
    digitalWrite(grelci, LOW);
  }
}
   BLYNK_WRITE(V1)
 {
   pinValue = param.asInt();

   }
 BLYNK_WRITE(V3)
  {
  maxtemp = param.asInt();
  }
 
  
  void setup()
 {
  pinMode(grelci, OUTPUT);
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  dht.begin();
  timer.setInterval(1000L,sendSensor);
 }

 // Setup a function to be called every second

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

#9

I added virtual pin V3 for adjusting the temperature.


#10

If I were you I’d change the “pool” time as per spec, the DHT22 doesn’t like (if I recall correctly) to be “pooled” with a sampling rate higher than 0.5Hz (once every 2seconds) even though it works.