Comparing Numeric Input with Variable or Vritual Pin (data from DS18B20 temperature sensor)

Hi,

I’m newbby with Arduino and Blynk. I’m triying to do an aquatic cooler wih a function that could turn on when the temperature of the water is less than a seteable parameter (It’s a Numeric Input).

I tried to use two functions but without lucky. :confused: Please help me.

• NodeMCU with WiFi
• Android version 10
• Blynk server
• Blynk Library v0.6.1

#include <Blynk.h>
#include <OneWire.h>
#include <SimpleTimer.h>
#include <ESP8266WiFi.h>
#include <DallasTemperature.h>
#include <BlynkSimpleEsp8266.h>

BlynkTimer timer; // Declaración del timer

OneWire oneWire(4);
DallasTemperature sensors(&oneWire);

char auth[] = "***";
const char ssid[] = "***";
const char pass[] = "***";

const int pinRele = 14;

String str;
String str1;

int temperature;  
         
void setup(){
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);

  sensors.begin();                        // Starts the DS18B20 sensor(s).
  sensors.setResolution(10);              

  timer.setInterval(2000L, sendTemps);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.
  timer.setInterval(2000L, enfriar);    // Temperature sensor read interval. 2000 (ms) = 2 seconds.

  pinMode(4, INPUT);

  pinMode(14, OUTPUT);
  digitalWrite(14, HIGH); // relay is HIGH triggering
  
}

void sendTemps(){
  sensors.requestTemperatures();                  // Polls the sensors.
  temperature = sensors.getTempCByIndex(0);   // Stores temperature. Change to getTempCByIndex(0) for celcius.
  Blynk.virtualWrite(V0, temperature);         // Send temperature to Blynk app virtual pin 1.

}

BLYNK_CONNECTED(){
  // relay is HIGH triggering   
  if (digitalRead(14) == 0) Blynk.virtualWrite(V6, digitalRead(14));
  else Blynk.syncVirtual(V6);
}

//Test 1
BLYNK_WRITE(V24){
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
  if(pinValue > temperature){
    digitalWrite(pinRele, HIGH);
  }else{
    // do nothing   
  }
}

//Test 2
void enfriar(){
  // Encender enfriador
  while( digitalRead(V24) > V0){
    digitalWrite( pinRele, HIGH);
  }
}

void loop(){
  Blynk.run();        // Ejecuta la magia de Blynk
  timer.run();        // Ejecuta el timer cada seg
  
}

Thanks,

Gonzalo

Your BLYNK_WRITE(V24) function needs to be assigning the temperature setpoint to a global variable (not local as it is at the moment) and nothing more.

Your enfriar() function shouldn’t be using a while statement.
Instead it should have an if statement which compares the temperature to the setpoint and takes the appropriate action (turns a relay on else turns it off for example).

Pete.

In this case I would suggest you should get your project working as you like on ARDUINO only first . . . then when it is doing what you want, only then add the Blynk code

This will make your troubleshooting much simpler given you are new to this.
billd

Really thank you Pete, your hel was very usefull!