Relay, numeric input

Hi, I have some problems with my coding. Until now I managed to do this code which is working mostly ok. Now I want to control relays with IF functions, but it doesn’t work, and I don’t know why. Sorry for ugly code, in future I will fix it also. I thought numeric input didn’t work, but then I tried to see pin V15 and value is correct, if I change it, otherwise is 0. Hope someone can help me with this. Thanks in advance!
Details :
• Arduino +ESP8266 via tx rx + wifi
Android
• Blynk server
• Blynk Library newest


#include <BlynkSimpleShieldEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <DHT.h>
#define ON 1
#define OFF 0

int rele1 = 3;
int rele2 = 4;
int rele3 = 5;
int rele4 = 6;

char auth[] = "";

char ssid[] = "";
char pass[] = "";

#define aref_voltage 3.3

#define THERMISTORPIN A0         
#define THERMISTORNOMINAL 10000  
#define TEMPERATURENOMINAL 25   
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000    
     
   uint16_t samples[NUMSAMPLES];

#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);

#define DHTPIN 7
#define DHTTYPE DHT22 

DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
  float h = dht.readHumidity();
  float t = dht.readTemperature();
// 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();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  
 }




byte TempGretja;
BLYNK_WRITE(V0)
// This function will be calLED every time  Widget
// in Blynk app writes values to the Virtual Pin V2
{
  TempGretja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V11, TempGretja);
  Serial.println(TempGretja);
}

int TempHlajenja;
BLYNK_WRITE(V1)
{
  TempHlajenja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V12, TempHlajenja);
  Serial.println(TempHlajenja);
}
float Toleranca;
BLYNK_WRITE(V2)
{
  Toleranca = param.asFloat();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V13, Toleranca);
  Serial.println(Toleranca);
}




void setup()
{
pinMode (rele1, OUTPUT);
digitalWrite (rele1, HIGH);
pinMode (rele2, OUTPUT);
digitalWrite (rele2, HIGH);
pinMode (rele3, OUTPUT);
digitalWrite (rele3, HIGH);
pinMode (rele4, OUTPUT);
digitalWrite (rele4, HIGH);
 // Debug console
 Serial.begin(9600);

 analogReference(EXTERNAL);
 
 delay(10);
 
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);
 delay(10);

 
 Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);
}







void (* resetFunc) (void) =0; 


BLYNK_WRITE(V10) // Reset
{
if (param.asInt()==1) {
resetFunc();
delay(100);
}
}

void loop(){






 if (t < TempGretja){
    digitalWrite (rele1, LOW);}
 else if (t > TempGretja) {
    digitalWrite (rele1, HIGH);}




  

 uint8_t i;
      float average;
   
     
// take N samples in a row, with a slight delay
   for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
   }
// average all the samples out
   average = 0;
   for (i=0; i< NUMSAMPLES; i++) {
   average += samples[i];
  }
  average /= NUMSAMPLES;
 
   
  // convert the value to resistance
  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V4, steinhart); 

  Blynk.run();
  timer.run();



  delay(1000);
  Blynk.virtualWrite(V15, TempGretja);



}

Unless you provide more info about what controls are attached to which widgets, what’s working, and what you want your if statement to do, it’s very difficult to take a quick look and offer advice.

You won’t get very far with all that stuff in the void loop, especially the delay.

Pete.

I’m sorry that I forgot that.
So, I’m using:
DHT22 on pin D7,
analog 10k thermistor on A0,
relays (digital LOW is ON) from pins D3-D6,
esp8266 on tx rx,
-virtual pins:
V0 - wanted temperature when incubation is going on (set with numeric input (where is 42 on picture),
V1 - cooling temperature, when there is no incubation (set with numeric input (where is 15 on picture),
V2 - tolerance (for set temperature (both incubation and cooling)
V3 - time of incubation (don’t have it yet, I want to set time like 4 hours - at that time will be temperature at wanted incubation temperature, after that, temperature will be like V1)
V4 - water temperature (for later function)
V5 - moisture (for later function)
V6 - current temperature (of incubator)
V7 - segment switch (manual control of heating/ cooling) and also start button
V10 - reset button
V15 - only for testing (incubation temperature)

Relay problems example:
I can’t get pin D3 low - (with that relay1 high) using if functions.

Also I’m working on rewriting all code.

Thanks for help!

In the declarations section of your code (at the top) you declare these two float variables and initialise them with the current snapshot readings of temperature and humidity:

  float h = dht.readHumidity();
  float t = dht.readTemperature();

In your void sendSensor function you then re-declare these variables, creating local versions that are only visible inside the scope of that function:

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  
}

So, you’re taking temp and humidity readings and sending them to Blynk, but the values of t and h that are being used in your void loop are the ones that you initialised at boot time, and will never change until you reboot the MCU
Your if statement in the void loop is using this initial snapshot value of t to evaluate whether or not to execute the rest of the cvode in the if statement.

The simple answer is to remove the float declarations from your void sendSensor function like this:

void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  
}

The better solution would be to have a “take readings and do the appropriate actions” function which takes a reading from your sensor and then evaluates what to do with it, using a series of if statements (or possibly a select case statement) before updating Blynk with the values and relay statuses.

Pete.

Thanks for help! I just needed that. For now will be good the easy way, but soon I will rewrite all code to more readable and better functioning.
Now I managed with help mostly everything, but I would like to input time for countdown with something like Time Input in HH:mm, instead of slider.
I have also problems with timer counting down, because time goes too slow, in two seconds it takes off only about one. I realize that it maybe has to do with the delay in loop. How can I fix that?

I used this timer code: [SOLVED] How to countdown?

#include <SimpleTimer.h>
#include <BlynkSimpleShieldEsp8266.h>
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <DHT.h>


int rele1 = 3;
int rele2 = 4;
int rele3 = 5;
int rele4 = 6;

char auth[] = "xxxxxxxxxxxxxxxx";
char ssid[] = "xxxx";
char pass[] = "xxx";

SimpleTimer timer;
int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;


#define aref_voltage 3.3

#define THERMISTORPIN A0         
#define THERMISTORNOMINAL 10000  
#define TEMPERATURENOMINAL 25   
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000    
     
   uint16_t samples[NUMSAMPLES];

#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);

#define DHTPIN 7
#define DHTTYPE DHT22 

DHT dht(DHTPIN, DHTTYPE);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
  
 }



byte TempGretja;
BLYNK_WRITE(V0)
// This function will be calLED every time  Widget
// in Blynk app writes values to the Virtual Pin V2
{
  TempGretja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V11, TempGretja);
  Serial.println(TempGretja);
}

int TempHlajenja;
BLYNK_WRITE(V1)
{
  TempHlajenja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V12, TempHlajenja);
  Serial.println(TempHlajenja);
}
float Toleranca;
BLYNK_WRITE(V2)
{
  Toleranca = param.asFloat();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V13, Toleranca);
  Serial.println(Toleranca);
}

void setup(){
  
pinMode (rele1, OUTPUT);
digitalWrite (rele1, HIGH);
pinMode (rele2, OUTPUT);
digitalWrite (rele2, HIGH);
pinMode (rele3, OUTPUT);
digitalWrite (rele3, HIGH);
pinMode (rele4, OUTPUT);
digitalWrite (rele4, HIGH);
 // Debug console
 Serial.begin(9600);

 analogReference(EXTERNAL);
 
 
 // Set ESP8266 baud rate
 Serial.begin(ESP8266_BAUD);


 
 Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);


  
  while (Blynk.connect() == false) {}

  CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
  timer.disable(CountdownTimer); // disable it on boot

}


void (* resetFunc) (void) =0; 


BLYNK_WRITE(V10) // Reset
{
if (param.asInt()==1) {
resetFunc();
delay(100);
}
}

void loop(){

  
  TempVode();
 if (t < TempGretja + Toleranca){
    digitalWrite (rele1, LOW);}
 else if (t > TempGretja + Toleranca) {
    digitalWrite (rele1, HIGH);}

  
  Blynk.run();
  timer.run();

  Blynk.virtualWrite(V15, TempGretja);

  delay(1000);

}


//Functions
//funkcije za timer

void CountdownTimerFunction() {
  CountdownRemain--; // remove 1 every second
  CountdownShowFormatted(CountdownRemain);
  if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
    timer.disable(CountdownTimer); // if 0 stop timer
    Blynk.virtualWrite(V7, LOW); // reset START/STOP button status
    Blynk.virtualWrite(17, "TIMER COMPLETE");
    Blynk.virtualWrite(18, 255); // LED for timer completed
    Blynk.virtualWrite(19, 0); // Timer LED status light off
  } else {
    Blynk.virtualWrite(18, 0); // LED for timer completed
  }
}

// Button Widget (Switch Type): Start/Pause Timer
BLYNK_WRITE(V7) {
  if (param.asInt()) {
    if (CountdownRemain) { // check if there is a time set or not
      timer.enable(CountdownTimer);
      Blynk.virtualWrite(19, 255); // Timer LED status light on
    } else {
      Blynk.virtualWrite(V7, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
      Blynk.virtualWrite(17, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
    }
  } else {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(19, 0); // Timer LED status light off
  }
}

// Button Widget (Momentary): Reset Timer
BLYNK_WRITE(20) {
  CountdownRemain = CountdownRemainReset; // reset to original start time
}

// Slider Widget (60-180): Set Timer (mins)
BLYNK_WRITE(21) {
  if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
    Blynk.virtualWrite(21, param.asInt() ); // if running, refuse to let use change slider
  } else {
    CountdownRemainReset = param.asInt() * 60 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
    CountdownRemain = param.asInt() * 60;
    CountdownShowFormatted(CountdownRemain);
  }
}

void CountdownShowFormatted(int seconds) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = seconds; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  Blynk.virtualWrite(17, days + hours_o + hours + mins_o + mins + secs_o + secs);

}





  void TempVode() 
  {
     uint8_t i;
      float average;
   
     
// take N samples in a row, with a slight delay
   for (i=0; i< NUMSAMPLES; i++) {
   samples[i] = analogRead(THERMISTORPIN);
   delay(10);
   }
// average all the samples out
   average = 0;
   for (i=0; i< NUMSAMPLES; i++) {
   average += samples[i];
  }
  average /= NUMSAMPLES;

  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  Blynk.virtualWrite(V4, steinhart);
   }

You need to move all of that stuff out of your void loop, and stop using delays, before you start adding more functionality. You’ll start to get disconnections and unusual results with your current setup.

Pete.

Now I tried to get as much as possible out of loop, as I understand it is better to use functions so I did it.
Also I got rid of all delays, and replaced them with millis replacement. But now I have problem, because timer runs too fast and everything else also runs too fast. Also I would like to set slider for timer much bigger (in numbers), as I would like to have time from 0 minutes to 2 days. I don’t find in code what to change for that, but in app i did it, but now if I make time larger than I think 10 hours, it gives out weird output.

My code now is this:


#include <SimpleTimer.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <ESP8266_Lib.h>
#include <DHT.h>
#define BLYNK_PRINT Serial

int rele1 = 3;
int rele2 = 4;
int rele3 = 5;
int rele4 = 6;



int period = 1000;
int period1 = 10;
int period2 = 100;
unsigned long time_now = 0;






char auth[] = "xxxxxxx";

char ssid[] = "xxxxxx";
char pass[] = "xxxx";



SimpleTimer timer;

int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;




#define aref_voltage 3.3

#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000

uint16_t samples[NUMSAMPLES];

#define ESP8266_BAUD 115200
ESP8266 wifi(&Serial);

#define DHTPIN 7
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

float h = dht.readHumidity();
float t = dht.readTemperature();
void sendSensor()
{
  h = dht.readHumidity();
  t = dht.readTemperature();

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);

}



byte TempGretja;
BLYNK_WRITE(V0)
// This function will be calLED every time  Widget
// in Blynk app writes values to the Virtual Pin V2
{
  TempGretja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V11, TempGretja);
  Serial.println(TempGretja);
}

int TempHlajenja;
BLYNK_WRITE(V1)
{
  TempHlajenja = param.asInt();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V12, TempHlajenja);
  Serial.println(TempHlajenja);
}
float Toleranca;
BLYNK_WRITE(V2)
{
  Toleranca = param.asFloat();                            // Assigning incoming value from pin V0 to variable
  Blynk.virtualWrite(V13, Toleranca);
  Serial.println(Toleranca);
}


//Functions
//funkcije za timer

void CountdownTimerFunction() {
  CountdownRemain--; // remove 1 every second
  CountdownShowFormatted(CountdownRemain);
  if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
    timer.disable(CountdownTimer); // if 0 stop timer
    Blynk.virtualWrite(V7, LOW); // reset START/STOP button status
    Blynk.virtualWrite(17, "TIMER COMPLETE");
  }
}




// Button Widget (Switch Type): Start/Pause Timer
BLYNK_WRITE(V7) {
  if (param.asInt()) {
    if (CountdownRemain) { // check if there is a time set or not
      timer.enable(CountdownTimer);
      Blynk.virtualWrite(19, 255); // Timer LED status light on
    } else {
      Blynk.virtualWrite(V7, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
      Blynk.virtualWrite(17, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
    }
  } else {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(19, 0); // Timer LED status light off
  }
}

// Button Widget (Momentary): Reset Timer
BLYNK_WRITE(20) {
  CountdownRemain = CountdownRemainReset; // reset to original start time
}

// Slider Widget (60-180): Set Timer (mins)
BLYNK_WRITE(21) {
  if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
    Blynk.virtualWrite(21, param.asInt() ); // if running, refuse to let use change slider
  } else {
    CountdownRemainReset = param.asInt() * 60 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
    CountdownRemain = param.asInt() * 60;
    CountdownShowFormatted(CountdownRemain);
  }
}

void CountdownShowFormatted(int seconds) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = seconds; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  Blynk.virtualWrite(17, days + hours_o + hours + mins_o + mins + secs_o + secs);

}


void setup() {

  pinMode (rele1, OUTPUT);
  digitalWrite (rele1, HIGH);
  pinMode (rele2, OUTPUT);
  digitalWrite (rele2, HIGH);
  pinMode (rele3, OUTPUT);
  digitalWrite (rele3, HIGH);
  pinMode (rele4, OUTPUT);
  digitalWrite (rele4, HIGH);
  // Debug console
  Serial.begin(9600);

  analogReference(EXTERNAL);


  // Set ESP8266 baud rate
  Serial.begin(ESP8266_BAUD);



  Blynk.begin(auth, wifi, ssid, pass);

  dht.begin();

  // Setup a function to be called every second
  timer.setInterval(1000L, sendSensor);



  while (Blynk.connect() == false) {}

  CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
  timer.disable(CountdownTimer); // disable it on boot

}








void (* resetFunc) (void) = 0;


BLYNK_WRITE(V10) // Reset
{
  if (param.asInt() == 1) {
    resetFunc();
    if (millis() > time_now + period2) {
      time_now = millis();

    };
  }
}
void TempVode()
{
  uint8_t i;
  float average;



  // take N samples in a row, with a slight delay
  for (i = 0; i < NUMSAMPLES; i++) {
    samples[i] = analogRead(THERMISTORPIN);
    if (millis() > time_now + period1) {
      time_now = millis();
    };
  }
  // average all the samples out
  average = 0;
  for (i = 0; i < NUMSAMPLES; i++) {
    average += samples[i];
  }
  average /= NUMSAMPLES;

  average = 1023 / average - 1;
  average = SERIESRESISTOR / average;

  float steinhart;
  steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
  steinhart = log(steinhart);                  // ln(R/Ro)
  steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
  steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
  steinhart = 1.0 / steinhart;                 // Invert
  steinhart -= 273.15;                         // convert to C
  Blynk.virtualWrite(V4, steinhart);
}






void loop() {



  TempVode();
  CountdownTimerFunction();

  if (t < TempGretja + Toleranca) {
    digitalWrite (rele1, LOW);
  }
  else if (t > TempGretja + Toleranca) {
    digitalWrite (rele1, HIGH);
  }


  Blynk.run();
  timer.run();

  Blynk.virtualWrite(V15, TempGretja);

  if (millis() > time_now + period) {
    time_now = millis();

  };

}
  

You should read this, then try again…
http://help.blynk.cc/getting-started-library-auth-token-code-examples/blynk-basics/keep-your-void-loop-clean

Pete.