Blynk write function

Thank you everyone helping me. This is a code that can be work successfully by control the pin 8 with a blynk switch. I just hide those #define in hi.h. However, I would like to change a little bit to the code by adding a if statement. “If (temp > 25) then pin 8 on else off” But I not sure where I can add it.

#define BLYNK_TEMPLATE_ID           ""
#define BLYNK_DEVICE_NAME           ""
#define BLYNK_AUTH_TOKEN            ""
#include "BlynkSimpleShieldEsp8266.h"
#include "ESP8266_Lib.h"
#include "DHT.h"

#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>

SoftwareSerial EspSerial(2, 3);
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
char ssid[] = "***";
char pass[] = "***";
char auth[] = BLYNK_AUTH_TOKEN;
#define DHTPIN 10

#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);
BLYNK_WRITE(V0)
{
    if (param.asInt() == 1 )
    {
        digitalWrite(8,1);

    }
    if (param.asInt() == 0)
    {
        digitalWrite(8,0);

    }

}
BlynkTimer timer;

void sendSensor()
{
    Blynk.virtualWrite(V5, dht.readHumidity());
    Blynk.virtualWrite(V6, dht.readTemperature());
}


void setup() {
  // Debug console
  Serial.begin(9600);
  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode(8,OUTPUT);
  timer.setInterval(1000L, sendSensor);
  dht.begin();
}



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

I have tried to add it in

void setup(){
...
dht.begin();
if(dht.readTemperature()>25)
{
digitalWrite(8,1);
}else{
digitalWrite(8,1);
}

but this seems violate the BLYNK_WRITE(V0) function and it cannot run the switch. then i tried to add a or and and statement in the BLYNK_WRITE(V0)

BLYNK_WRITE(V0)
{
    if (param.asInt() == 1|| dht.readTemperature()>25)
    {
        digitalWrite(8,1);

    }
    if (param.asInt() == 0||dht.readTemperature()<25)
    {
        digitalWrite(8,0);

    }

}

this seems useless as the temperature is higher and nth change. only the switch can change its on off.
for and statement

BLYNK_WRITE(V0)
{
    if (param.asInt() == 1&& dht.readTemperature()>25)
    {
        digitalWrite(8,1);

    }
    if (param.asInt() == 0)
    {
        digitalWrite(8,0);

    }

}

this can be run when i switch on it and temperature is higher than 25 but when it is lower, the switch still on unless i switch it off. if I switch it on & temp<25 it wont on. that is not what i want. how can i do it? thank you

What EXACTLY are you trying to achieve?

Do you want the device to automatically activate a relay attached to a GPIO pin if the temperature is above a certain level?
If so, at what point do you want top de-activate that relay - say when the temperature falls below a different pre-set level?

Is the switch widget in the Blynk app meant to act as an override?
If so, is that for a timed period, or until some other criteria is met?

If this is a Blynk IoT sketch then I’d recommend moving the Template ID etc back in to the main sketch, and simply replacing the sensitive information with the word “REDACTED” when you post it to the forum. That way, Blynk will connect correctly and forum members have visibility of your method, but not your personal data.

I’d also recommend removing all of this from your sketch…

It’s poor programming, especially with Blynk and when you are already using BlynkTimer, and it’s simply going to draw off-topic comments from forum members.

Pete.

Thank you for ur advice. I have changed the original coding.
The things that i would like to do is. when I switch it on, it on. when i switch it off, it off. when temp higher than 25, it on. when temp lower than 25 it off. they are running separately. it would be great if the temperature statement can change the switch status too.

So what if the switch widget is off, but the temperature is higher than 25° ?
Which takes priority, the widget switch or the sensor?

Or, are you looking for an auto/manual option where the temperature sensor controls the relay in Auto mode and the switch widget controls the relay in Manual mode?

You need to be clear about your requirements before you start coding.

Pete.

oh your suggestion really show me a whole new world.
is it ok for me to this kind of things?

Switch(auto/manual)
if auto
then temp>25 on
then temp < 25 off
if manual
then use blynk write function

can i do sth like this??

if yes, is it sth like this?

BLYNK_WRITE(V1)//auto/manual
{
  if(param.asInt() == 1) //auto
  {
    if(dht.readTemperature()>25)
    {
     digitalWrite(8,1);
    }
    else if
    {
    digitalWrite(8,0);
    }
  }if(param.asInt() == 0)//manual
  {
   BLYNK_WRITE(V0)
   {
    if (param.asInt() == 1 )
    {
        digitalWrite(8,1);

    }
    if (param.asInt() == 0)
    {
        digitalWrite(8,0);

    }

}

You can’t put a BLYNK_WRITE within a BLYNK_WRITE.

Instead, you need to use global variables and set them within the BLYNK_WRITE functions then use these variables elsewhere within your sketch.

Pete.

This might help you

@John93
please tell me if i am understand it right.
it will be sth like this

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)//auto
  {
    auto_mode();
  }
  if (param.asInt() == 0)//manual
  {
    manual_mode();
  }
}
void manual_mode()
{
  BLYNK_WRITE(V0) // Executes when the value of virtual pin 0 changes
  {
    if (param.asInt() == 1)
    {
      // execute this code if the switch widget is now ON
      //Fan_on();
      digitalWrite(8, 1);
    }
    if (param.asInt() == 0)
    {
      // execute this code if the switch widget is now OFF
      //Fan_off();
      digitalWrite(8, 0);
    }
  }
}
void auto_mode()
{
  if(dht.readTemperature() >=25)
  {
    digitalWrite(8, 1);
  }
  else
  {
    digitalWrite(8, 0);
  }
}

when i tried to do sth like this, it said

Arduino: 1.8.15 (Windows 10), Board: "Arduino Uno"

C:\Users\user\Documents\Arduino\From_Community\From_Community.ino: In function 'void manual_mode()':

From_Community:54:3: error: a function-definition is not allowed here before '{' token

   {

   ^
a function-definition is not allowed here before '{' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

i think Blynk write cannot be inside any {} then how can i call it?

i am not sure global variable can work in blynk write function. please tell me if i am wrong. i remember one time i set sth like this. and it cannot control. is it due to the float??

float a = 0;
BLYNK_WRITE(V0){
    a == param.asInt();
    triggle();
}
void triggle()
{
if(a==1)
  {
  digitalWrite(8,1);
  }
}

Of course you can use global variables in BLYNK_WRITE functions.

I’m not sure what you’re asking here, but you should either be doing…

int a = 0;

BLYNK_WRITE(V0)
{
    a = param.asInt();
    triggle();
}

or

float a = 0;

BLYNK_WRITE(V0)
{
    a = param.asFloat();
    trigglle();
}

If you try to use. double equals == then you are doing a comparison rather than assigning the value from the virtual pin to the global variable…

BLYNK_WRITE(vPin) is a callback function which will trigger whenever the value of the virtual pin changes.
It cannot go inside another function, whether this is a regular function like void manual_mode() or another BLYNK_WRITE() function.

Instead, you have to use the BLYNK_WRITE() functions to assign values to global variables then test the value of these variables within your regular functions.

Pete.

thank you for telling me the way to set the global variable. thanks a lot

I am not sure if I can understand it right. would you mind to give me an example? especially when I need to use a switch to control the pin after choosing the manual mode. Am I understand it correctly?

float manual = 0;
BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)//auto
  {
    manual = 0;
    auto();
  }
  if (param.asInt() == 0)//manual
  {
    manual = 1;
    
  }   
}
BLYNK_WRITE(V0)//manual
{
  if (manual == 1 && param.asInt() == 1)
  {
    digitalWrite(8, 1);
  }
  if (manual == 1 &&param.asInt() == 0)//manual
  {
    digitalWrite(8, 0);
  }
}

i have make some changed to the latest code, sorry.

Why do you insist on declaring your global variable as a float type, then assigning an integer variable type to it?

I think you need to take a few steps back from the project, and rather than focussing the coding (which you’re obostruggling with) focus instead on defining what it is that this project is intended to achieve.

I asked you this question before…

My guess (and it is only a guess because you haven’t yet provided a functional specification) is that you need a regular function (not a BLYNK_WRITE callback) which is called using a timer - say once every 5 seconds.
This function will take a reading from your sensor and display it in the Blynk app, then do different things based on whether you are in manual or automatic mode.
If this is the case, then that function needs to know various things…
Is the system in automatic mode?
If yes then how does the actual temperature compare to the target temperature?
If no then is the manual switch set to on or off?

These automatic mode (yes/no) and manual switch (on/off) values would be global variables set via BLHNK_WRITE functions.

So, start by writing your functional specification rather than getting bogged down in the code

Pete.

i am not that sure about this part.
and i hope the following explanation can help you.
when connected to blynk server, it default to be manual mode
if manual mode
then i can control the relay with switch v0
if i switch v1 to auto
the relay will on and off based of the temp read from dht22.

In order to do it. In my opinion, I need a global variable.

int manual_mode = 1 // in manual mode default

then I use a switch v1 to change its status.

BLYNK_WRITE(V1)// mode switch
{
  if (param.asInt() == 1)//auto
  {
    manual_mode = 0;
    auto_mode();
  }
  if (param.asInt() == 0)//manual
  {
    manual_mode = 1;
    
  }   
}

if in manual mode

BLYNK_WRITE(V0)//manual
{
  if (manual_mode == 1 && param.asInt() == 1)
  {
    digitalWrite(8, 1);
  }
  if (manual_mode == 1 && param.asInt() == 0)
  {
    digitalWrite(8, 0);
  }
}

if auto

void auto_mode()
{
  while (manual_mode == 0)
  {
    if (dht.readTemperature() >= 24)
    {
      digitalWrite(8, 1);
    }
    else
    {
      digitalWrite(8, 0);
    }
  }
}

read dht data to blynk and display it

void sendSensor()
{
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  Serial.println(dht.readHumidity());
  Serial.println(t);
  Blynk.virtualWrite(V5, dht.readHumidity());
  Blynk.virtualWrite(V6, t);
}

in setup

void setup()
{

  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(8, OUTPUT);

  dht.begin();

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

my whole code

#define BLYNK_TEMPLATE_ID "***";
#define BLYNK_DEVICE_NAME "";
#define BLYNK_AUTH_TOKEN "";

// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include <DHT.h>

char auth[] = BLYNK_AUTH_TOKEN;

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";

int manual_mode = 1;
//Blynk.virtualWrite(V1, 1)

// or Software Serial on Uno, Nano...
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600 //I have changed the baud rate of my 8266 to 9600

ESP8266 wifi(&EspSerial);

#define DHTPIN 10          // What digital pin we're connected to

// Uncomment whatever type you're using!
#define DHTTYPE DHT22   // DHT 22, AM2302, AM2321

DHT dht(DHTPIN, DHTTYPE);

BlynkTimer timer;

BLYNK_WRITE(V1)
{
  if (param.asInt() == 1)//auto
  {
    manual_mode = 0;
    auto_mode();
  }
  if (param.asInt() == 0)//manual
  {
    manual_mode = 1;
  }
}

BLYNK_WRITE(V0) // Executes when the value of virtual pin 0 changes
{
  if (manual_mode == 1 && param.asInt() == 1)
  {
    // execute this code if the switch widget is now ON
    //Fan_on();
    digitalWrite(8, 1);
  }
  if (manual_mode == 1 && param.asInt() == 0)
  {
    // execute this code if the switch widget is now OFF
    //Fan_off();
    digitalWrite(8, 0);
  }
}
void auto_mode()
{
  while (manual_mode == 0)
  {
    if (dht.readTemperature() >= 24)
    {
      digitalWrite(8, 1);
    }
    else
    {
      digitalWrite(8, 0);
    }
  }
}
// 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 t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Serial.println(dht.readHumidity());
  Serial.println(t);
  Blynk.virtualWrite(V5, dht.readHumidity());
  Blynk.virtualWrite(V6, t);
}

void setup()
{

  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);

  pinMode(8, OUTPUT);

  dht.begin();

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

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

however, when i used this code, there is a strange problem.
it works fine on the manual mode.
however, when i changed to auto mode, the reading of temperature wont affect the relay even it is > 24. and if i switch back to manual and back to auto again, the device will go offline.

I’m not going to comment any further until you explain more about what this project is for, what is controlled by the relay, and why manual control is better than automatic control by default.

You’re still getting far to bogged down in the coding than providing high level background information.

Pete.

the relay is controlling a fan. and manual control is better because i always think that manual control is somehow more reasonable than auto programming for some reason.

A fan for what? What is the overall purpose of the project?
Is it a single speed fan, or multi-speed?
If manual control is better than automatic then why aren’t you simply using a physical on/off switch, or a physical switch plus the widget switch in the app?

Pete.