Create auto and manual mode to control valves with nodemcu esp8266

Hello I’m new here, and i’m new in arduino project. Need some help with my project please
I’m trying to control my home heating system with NODEMCU esp8266 and local blynk server.
My problem is to code “auto” and “manual” mode change with virtual pin. In manual mode to open all valves and run the pump. In auto mode to control valves and pump from 9 sensors ds18b20.

It’s difficult to give sensible advice without some more information regarding your project.

Basic principals are that when a widget attached to a virtual pin is changed in the app then it triggers the corresponding `BLYNK_WRITE(vPin) callback function only once. You should assign the incoming value from the widget to a global variable.

You then need to use a BlynkTimer to take sensor readings, and compare the results to your defined temperature set point in auto mode.
In manual mode you need to override this automatic logic processing, and the easiest way to do this is via an if statement to check if you are in auto or manual mode.

This is covered briefly here…

Pete.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(4); // digital D2 pin
DallasTemperature sensors(&oneWire);
BlynkTimer timer;


uint8_t sensor1[8] = { 0x28, 0xFF, 0x64, 0x02, 0xEC, 0x68, 0x1B, 0xAC };
uint8_t sensor2[8] = { 0x28, 0xFF, 0x64, 0x02, 0x1E, 0x20, 0x76, 0x1B };
uint8_t sensor3[8] = { 0x28, 0xFF, 0x64, 0x02, 0xED, 0xC0, 0x5D, 0x04 };
uint8_t sensor4[8] = { 0x28, 0xFF, 0x64, 0x02, 0xED, 0x9A, 0xF7, 0x45 };
uint8_t sensor5[8] = { 0x28, 0xFF, 0x64, 0x02, 0x1E, 0x20, 0x76, 0x1C };   /////
uint8_t sensor6[8] = { 0x28, 0xFF, 0x64, 0x02, 0xED, 0xC0, 0x5D, 0x0A };   ////
uint8_t sensor7[8] = { 0x28, 0xFF, 0x64, 0x02, 0xEC, 0x68, 0x1B, 0xAD };   ////
uint8_t sensor8[8] = { 0x28, 0xFF, 0x64, 0x02, 0x1E, 0x20, 0x76, 0x1D };   /////
uint8_t sensor9[8] = { 0x28, 0xFF, 0x64, 0x02, 0xCE, 0x20, 0x76, 0x1D };   /////

  float temp1 = sensors.getTempC(sensor1);  //Сензор парно
  float temp2 = sensors.getTempC(sensor2);  //Сензор външен
  float temp3 = sensors.getTempC(sensor3);  //Сензор бойлер
  float temp4 = sensors.getTempC(sensor4);  //Сензор коридор
  float temp5 = sensors.getTempC(sensor5);  //Сензор детска
  float temp6 = sensors.getTempC(sensor6);  //Сензор кухня
  float temp7 = sensors.getTempC(sensor7);  //Сензор баня
  float temp8 = sensors.getTempC(sensor8);  //Сензор спалня
  float temp9 = sensors.getTempC(sensor9);  //Сензор остъкление  
  
const int pompa = 16; //D0
const int boiler = 5; //D1
const int serpentina = 14; //D5
const int dejurna = 12; //D6
const int ostuklenie = 2; //D4 remote control pompa
const int koridor = 0; //D3 
const int detska = 13; //D7
const int kuhnq = 15; //D8
const int banq = 3; //RX
const int spalnq = 1; //TX

////int ldr = digitalRead (LDR);

// You should get Auth Token in the Blynk App.
char auth[] = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
// Your WiFi credentials.
char ssid[] = "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
char pass[] = "!!!!!!!!!!!!!!!!!!!";
WidgetLCD lcd(V100);

void setup()

{
  pinMode(16, OUTPUT);    // sets the digital pin 16 as output
  pinMode(5, OUTPUT);    // sets the digital pin 5 as output
  pinMode(14, OUTPUT);    // sets the digital pin 14 as output
  pinMode(12, OUTPUT);    // sets the digital pin 12 as output
  pinMode(2, OUTPUT);    // sets the digital pin 2 as input
  pinMode(0, OUTPUT);    // sets the digital pin 16 as output
  pinMode(1, OUTPUT);    // sets the digital pin 5 as output
  pinMode(3, OUTPUT);    // sets the digital pin 14 as output
  pinMode(13, OUTPUT);    // sets the digital pin 12 as output
  pinMode(15, OUTPUT);    // sets the digital pin 2 as input
  pinMode(10, OUTPUT);    // blynk remote control pump
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass, IPAddress(95,43,237,93), 8080 );
  sensors.begin();
// Setup a function to be called every second  
  timer.setInterval(1000L, sendTemps);
}

void sendTemps()

{
 sensors.requestTemperatures();

 temp1 = sensors.getTempC(sensor1);
 temp2 = sensors.getTempC(sensor2);
 temp3 = sensors.getTempC(sensor3);
 temp4 = sensors.getTempC(sensor4);
 temp5 = sensors.getTempC(sensor5);
 temp6 = sensors.getTempC(sensor6);
 temp7 = sensors.getTempC(sensor7);
 temp8 = sensors.getTempC(sensor8);
 temp9 = sensors.getTempC(sensor9);
    
  if (temp1 > temp3) {
   digitalWrite(serpentina , HIGH);
   Serial.print("Серпентината отворена, бойлера изключен: ");
   Blynk.virtualWrite(V13,1 );
    }
    else {
    digitalWrite(serpentina , LOW);
    Serial.println("серпентината затворена");
    Blynk.virtualWrite(V13,0);
    }

  if ((temp1 > 28) && (temp4 < 22)) {
    digitalWrite(koridor , HIGH);
    Blynk.virtualWrite(V14,1);
  }
   else {
    digitalWrite(koridor , LOW);
    Blynk.virtualWrite(V14,0);
  }
  
   if ((temp1 > 28) && (temp5 < 22)) {
    digitalWrite(detska , HIGH);
    Blynk.virtualWrite(V15,1);
   }
      else {
      digitalWrite(detska , LOW);
      Blynk.virtualWrite(V15,0);
    }

   if ((temp1 > 28) && (temp6 < 22)) {
    digitalWrite(kuhnq , HIGH);
    Blynk.virtualWrite(V16,1);
   }
    else {
    digitalWrite(kuhnq, LOW);
    Blynk.virtualWrite(V16,0);
   }

   if ((temp1 > 28) && (temp7 < 22)) {
    digitalWrite(banq , HIGH);
    Blynk.virtualWrite(V17,1);
   }
       else {
    digitalWrite(banq , LOW);
    Blynk.virtualWrite(V17,0);
   }

   if ((temp1 > 28) && (temp8 < 22)) {
    digitalWrite (spalnq , HIGH);
    Blynk.virtualWrite(V18,1);
    }
      else {
      digitalWrite(spalnq , LOW);
      Blynk.virtualWrite(V18,0);
    }
   if ((temp1 > 28) && (temp9 < 15)) {
    digitalWrite(ostuklenie , HIGH);
    Blynk.virtualWrite(V19,1);
   }
     else {
      digitalWrite(ostuklenie , LOW);
      Blynk.virtualWrite(V19,0);
    }

 
 
 Blynk.virtualWrite(V1, temp1);
 Blynk.virtualWrite(V2, temp2);
 Blynk.virtualWrite(V3, temp3);
 Blynk.virtualWrite(V4, temp4);
 Blynk.virtualWrite(V5, temp5);
 Blynk.virtualWrite(V6, temp6);
 Blynk.virtualWrite(V7, temp7);
 Blynk.virtualWrite(V8, temp8); 
 Blynk.virtualWrite(V9, temp9);
}

BLYNK_WRITE(V10)// Executes when the value of virtual pin 10 changes
    {
           int button = param.asInt();
           if(button == 1)   
           {
           digitalWrite( pompa, HIGH);
           Blynk.virtualWrite(V12, 1);
           lcd.clear(); //Use it to clear the LCD Widget
           lcd.print(0, 0, "pompata raboti"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
           lcd.print(0, 1, "ruchno");
           Blynk.syncVirtual(V10);
           }
           
           if (temp1 > 28)  {
           Serial.println("Помпата работи");
           Serial.print("Температура Парно");
           Serial.println(temp1);
           digitalWrite(pompa, HIGH);
           Blynk.virtualWrite(V12, 1);
           Blynk.syncVirtual(V10);
           lcd.clear(); //Use it to clear the LCD Widget
           lcd.print(0, 0, "pompata raboti"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
           lcd.print(0, 1, "avtomatichno");
           }
           
           if (temp1 < 5 ) {
           Serial.println("Помпата работи против замръзване");
           digitalWrite(pompa, HIGH);
           digitalWrite(serpentina, HIGH);
           digitalWrite(koridor, HIGH);
           digitalWrite(banq, HIGH);
           digitalWrite(spalnq, HIGH);
           digitalWrite(kuhnq, HIGH);
           digitalWrite(ostuklenie, HIGH);
           digitalWrite(detska, HIGH);  
           Blynk.virtualWrite(V12, 1);
           Blynk.virtualWrite(V13, 1);
           Blynk.virtualWrite(V14, 1);
           Blynk.virtualWrite(V15, 1);
           Blynk.virtualWrite(V16, 1);
           Blynk.virtualWrite(V17, 1);
           Blynk.virtualWrite(V18, 1);
           Blynk.virtualWrite(V19, 1);
           Blynk.syncVirtual(V10);
           lcd.clear(); //Use it to clear the LCD Widget
           lcd.print(0, 0, "raboti protiv"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
           lcd.print(0, 1, "zamruzvane");
             
           }
           
           if ((temp2 < -5) & (temp1 < 5)) {
           Serial.println("Помпата работи против замръзване");
           Serial.print(temp2);
           digitalWrite(pompa, HIGH);
           digitalWrite(serpentina, HIGH);
           digitalWrite(koridor, HIGH);
           digitalWrite(banq, HIGH);
           digitalWrite(spalnq, HIGH);
           digitalWrite(kuhnq, HIGH);
           digitalWrite(ostuklenie, HIGH);
           digitalWrite(detska, HIGH);  
           Blynk.virtualWrite(V12, 1);
           Blynk.virtualWrite(V13, 1);
           Blynk.virtualWrite(V14, 1);
           Blynk.virtualWrite(V15, 1);
           Blynk.virtualWrite(V16, 1);
           Blynk.virtualWrite(V17, 1);
           Blynk.virtualWrite(V18, 1);
           Blynk.virtualWrite(V19, 1);
           Blynk.syncVirtual(V10);
           lcd.clear(); //Use it to clear the LCD Widget
           lcd.print(0, 0, "raboti protiv"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
           lcd.print(0, 1, "zamruzvane");
           }
          
           else {
           digitalWrite(pompa, LOW);
           Serial.println("Помпата не работи");
           Serial.print("Температура Парно");
           Serial.println(temp1);
           Blynk.virtualWrite(V12, 0);
           Blynk.syncVirtual(V10);
           lcd.clear(); //Use it to clear the LCD Widget
           lcd.print(0, 0, "pompata ne"); // use: (position X: 0-15, position Y: 0-1, "Message you want to print")
           lcd.print(0, 1, "raboti");
           }
    }
BLYNK_WRITE(V11)// Executes when the value of virtual pin 0 changes
           {
           int button1 = param.asInt();
           if(button1 == 1) {
           digitalWrite( boiler, HIGH);
           Blynk.syncVirtual(V11);
           }      
           else {
           digitalWrite(boiler, LOW);
           }
           }
           
BLYNK_CONNECTED()
{
  Blynk.syncVirtual(V10);
  Blynk.syncVirtual(V12);
  Blynk.syncVirtual(V13);
  Blynk.syncVirtual(V14);
  Blynk.syncVirtual(V15);
  Blynk.syncVirtual(V16);
  Blynk.syncVirtual(V17);
  Blynk.syncVirtual(V18);
  Blynk.syncVirtual(V19);
}

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

@vanchi Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Also, I’d suggest that rather just dumping a load of code into a post without any explanation, I’d suggest that you add some narrative to explain why you’ve posted this code, what issues you’re having with it, what widgets you have attached to each of the virtual pins, and what function each of those widgets is supposed to have.

Pete.

This is a nodemcu code that I want to use to control the heating in my house. As in each room there is a ds18b20 sensor and radiator valve. As in every room, it will open when the pump is on and the temperature is below the set point. I also have two 3 separate sensors: one external, one on the water heater and one on the fireplace water jacket.
The idea of the latter is to turn on the pump at +28 degrees or at +5 to prevent freezing. The external sensor turns on the pump at -5 degrees of frost. The third sensor controls a valve on my electric water heater.
There are pins attached to the virtual pins used to monitor valve and pump statuses, except for pin V10 and V11. I want to use the first one to forcibly start the pump and open all the valves, and the second one to turn on the electric water heater.
My question to you is, is there a way to make automatic and manual mode, or will it work anyway.

Yes, but not the way you’ve written the sketch.
As I said initially…

So when your V10 widget changes it will override the sendTemps() logic, but then the next tome that sendTemps() is called it will override the BLYNK_WRITE(V10) logic, switching back to automatic mode.

Your BLYNK_WRITE(V10) code should look something like this…

bool auto_mode;

BLYNK_WRITE(V10)
{
  auto_mode = param.asInt();
}

Then you expand your sendTemps() function to take different actions if sendTemps()==true

Also, i think you’ve chosen the wrong type of board for this project. The NodeMCU really only has 5 useable pins…

As you need at least 11 pins you would be better using an ESP32.

Also, I don’t see the point in defining aliases for your pins…

then not using them…

Pete.

Thank Pete, i wrote my code as i want with many tries and probes :slightly_smiling_face: