Thermostat Relay Control and How to Organize Blynk Code on a Yun?

Hey all!

I’m making a climate monitoring/thermostat-controlled relay application (with an HTU21D sensor) with an arduino yun and I have a couple questions pertaining to my code.

  1. On organization… I’m not sure where to correctly place my Blynk.virtualWrites so that I have constantly-updating values in my Blynk app. It seems to only work if they are in the void loop portion, but I’m not sure if this is correct code writing.

  2. In my BLYNK_WRITE portion, I want a relay to turn on or off if my actual temperature value is different from my slider value (aka my thermostat setting). At the moment, the relay is turning on if i physically move the slider above the temperature value and it turns off if i physically move the slider below the value, but the relay does not come on if the sensor temperature goes from above the slider value to below the slider value on its own . Additionally, I’m only getting One single push notification. I want this temperature check/relay control to loop every minute and I’m not really sure how to do that.

Here’s my code: I obviously have a few things repeated way too many times, because I’m not really sure how to properly format things.

#include <Bridge.h>
#include <BlynkSimpleYun.h>
#include <Wire.h>
#include "SparkFunHTU21D.h"

//create instance of HTU
HTU21D myClimate;

int relay1 = 5;

//Blynk token
char auth[] = "derpityderp";

void setup()

//Incoming temperature slider value on virtual pin 2
  float actualTemperature = myClimate.readTemperature();
  int tempSetting = param.asInt();

  if (actualTemperature < tempSetting) {
    digitalWrite(5, HIGH);
    Blynk.notify("your temperature is too low"); 
    } else {

void loop()
  float actualTemperature = myClimate.readTemperature();
  float actualHumidity = myClimate.readHumidity();



The timer is easy. There is an examle in the library called pushdata (or pushdata n request) which has a timer in it that executes a function every x milliseconds.

I think you should use blynk_read to make your slider do something. Because than it executes when it reads the value you set. Have you tried that?

I’m still not quite sure how BLYNK_READ would work with the slider, but I think I have the timer figured out.