I am new at coding

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


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
  }
}

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

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?..

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…

Thank you both for suggestions. I solve the problem.

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

1 Like

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

I added virtual pin V3 for adjusting the temperature.

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.

Guys, I’m gonna use your tips and solutions. I’m working on software documentation where I try to cover most problems that I found by myself or which others encountered and come out with the solution. Maybe one day my documentation will become a book about programming or will be part of another one’s book, in the support section. I’m working with DOCSIE, and if you guys will someday have to manage a lot of text, which will be embedded in a website, feel free to use it too. A great app for managing text.