BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

BLYNK_WRITE(V1) error message

I’m not sure why my project on the Blynk App intermittently doesn’t work or goes offline. I don’t have any error messages from Arduino IDE when I compile my sketch but the project does not always work. For example, when I click on the pushbuttons in my Blynk Project via the app, to turn the respective relays in my project ON/OFF, it doesn’t always work and sometimes when it does it takes a long time to activate the relay, like 10 to 15 seconds delay. I have 2 relays, both are controlled manually only via the Blynk App via 2 button widgets.

My project says “online” even though it initially said it had “disconnected”. Hence i am sometimes able to push the pushbuttons ON/OFF but nothing happens to the relays.

Can the Blynk community please look at my code and my attached picture and tell me what’s wrong.

Details :
• Arduino UNO with USB Communication (No Shields)
• Android + version 9
• Blynk server
• Blynk Library version 0.61


Hi Blynk Community! My program has 2 main functions. The first is called “void sensorDataSendTempControl()” which simply peforms temperature control by activating my Heating Element which is a" Heating Fan" (Heating Element + Fan on the back) which is 1 component made of 2 components in 1. This Heating Fan has 4 wires in total (2 for the Heating Element and 2 for the Fan of the Heater). This is manually via the Blynk app using 2 Button Widges (see attached screenshot) and is controlled by 2 Relays at Digital Pin 8 and 7 for the Heating Element and the Fan of the Heater respectively. I then have an LM35 Temperature Sensor which reads the Temperature and an Extractor Fan to remove the heat and maintain the setpoint temperature using Adaptive Cooling via PID Control.

The setpoint temperature needs to be inputted by the user via the Blynk App. Which I have created a numeric input. However, a problem only arose when I wrote the code for connecting the setpoint temperature (numeric input) to my Arduino IDE Code using the 2 fundamental commands for Virtual Pins:

“BLYNK_WRITE(V1)” and Blynk.virtualWrite(V1) <<<<< Most of my problems started when I incorporated these commands

I haven’t used the second command “Blynk.virtualWrite(V1)” as yet because I was getting the problems I mentioned above which mainly started after the incorporation of this code, using the first command.

The second main function of my code is called “void sensorDataSendBrightnessControl()” which simply peforms brigntess control using PWM. I’ve mapped the PWM Range (0 to 255) to the Operating Lumen Values Range (6 lumens to 74 lumens).

Please help me community, it’s urgent :cry:

This is my code:

#define BLYNK_PRINT DebugSerial
#define BLYNK_WRITE(pin)

// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "MUMRNK5o0oiBIJHNsIcjM6IaV8EyeUbu";

BlynkTimer timer1; // Announcing the timer1
BlynkTimer timer2; // Announcing the timer2

//Untuned PID Control for Fan Speed Control Circuit + Relay Module incorporated

int PwmPin = 3;                                  //initialising the pin number of PWM signal

int analogPin = A0;                              //analogue pin that will be used (Pin A0)
int raw = 0;                                     //store the raw value coming from ADC (0-1023)
int sensorValue = 0;                             //real ADC Value (Value from the 3 Point Averaging Filter)
float voltage = 0;                               //need to first calculate the voltage from this
float roomTemp = 0;                              //then can calculate the temperature
double roomTemperature;                          //To store Converted roomTemp to double to be compatibe type for PID Controller
double temp_PWM;                                 //PWM Value of roomTemperature

int V1variable;
double setpointTemp;                             //initialising Setpoint Temperature

BLYNK_WRITE(V1){

 V1variable = param.asInt(); 
 setpointTemp = V1variable;
}

double setpoint_PWM;                             //PWM Value of Setpoint Temperature

double maxTemp = 30;                             //Max Temp room is goining to be heated to by Heating Fan

float DutyCyclePercent;                          //variable to store Duty Cycle. Type float used for calculations

// Relay pin for Heater (Heating Element) is controlled with D8. The active wire is connected to Normally Open and COM
int relayHeater = 8;

// Relay pin for Fan of Fan Heater is controlled with D7. The active wire is connected to Normally Open and COM
int relayFanH = 7;


#include <PID_v1.h>

//Define Variables we'll be connecting to

double Setpoint, Input, Output;
 
//Specify the links and initial tuning parameters

PID myPID(&Input, &Output, &Setpoint,2,5,0, REVERSE);    //Gain values of Kp, Ki, Kd

//initilisation for second program "BrightnessControl"

int PwmPin2 = 6;             //initialising the pin number of PWM signal
int PwmValue = 255;           //variable to store the PWM value
float DutyCyclePercent2;     //variable to store Duty Cycle. Type float for  calculations


int V2variable;
double setpointLumens;

BLYNK_WRITE(V2){

 V2variable = param.asInt(); 
 setpointLumens = V2variable; 
 Serial.println(setpointLumens);
}

double setpointLumens_PWM;
double SetpointLumensPWM;

void setup() {

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);                             //setup of Serial Monitor
  Blynk.begin(Serial, auth);

  Blynk.syncVirtual(V1);
  Blynk.syncVirtual(V2);

  timer1.setInterval(2000L, sensorDataSendTempControl); //timer will run every 2 seconds
  timer2.setInterval(1000L, sensorDataSendBrightnessControl); //timer will run every 2 seconds

  //turn the PID ON

  myPID.SetMode(AUTOMATIC);

}


  void sensorDataSendTempControl() {

  
  
  pinMode(PwmPin,OUTPUT);                         //initialising the PWM pin as an output
  pinMode(analogPin,INPUT);                       //initialisin the Temperaure sensor pin as an input
 
  //initialize the variables we're linked to

  setpoint_PWM = map(setpointTemp, 26,30, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
  Setpoint = setpoint_PWM;
 
  raw = analogRead(analogPin);                      //1st time to read analogue Input
  raw = 0;                                          //and discard it
  for(int x = 0; x <= 2; x++){                      //3 Point Averaging Filter
      raw = raw + analogRead(analogPin);            //reading analog input 3 times
  }                                                 //and adding it to raw

  sensorValue = raw / 3;                            //real ADC value

  voltage = (5*float(sensorValue)) / 1023;          //Conversion to Temperature in Degrees Celcius
  roomTemp = voltage * 100;

  roomTemperature = double(roomTemp);                //Convert roomTemp to double to be compatibe type for PID Controller
  temp_PWM = map(roomTemperature, 26, 30, 0, 255);   //Added for troubleshooting
  Input = temp_PWM;                                  //Convert roomTemperature from operating Temperature Range to PWM Range to be compatibe type for PID Controller

  myPID.Compute();                                   //Use PID Controller library to compute Output PWM Signal
  analogWrite(3,Output);                             //Write this Ouput PWM Signal to Analog Pin 3

  Serial.print("Temperature = ");                     //Print text in quotation marks
  Serial.print(roomTemp);                             //Print measured temperature
  Serial.println(" Degrees Celcius");                 //Print text in quotation marks
  Serial.println("");

  Serial.print("PWM Value = ");                       //print the text in quotation marks
  Serial.println(Output);                             //print the PWM value next to this
  
  }
  

 void sensorDataSendBrightnessControl() {

  pinMode(PwmPin2,OUTPUT);     //initialising the PWM pin as an output

  analogWrite(PwmPin2,PwmValue);     //sending the PWM value to the PWM pin
  DutyCyclePercent2 = ((PwmValue * 100) / 255);  //duty cycle calculation
  
  Serial.print("PWM Value = ");        //to display the text in quotation marks
  Serial.println(PwmValue);          //print the PWM value next to this
   
  Serial.print("Duty Cycle = ");         //to display the text in quotation marks
  Serial.print(DutyCyclePercent2);      //print the DC value next to this
  Serial.println("%");         //print the symbol in quotation marks
  Serial.println("");

  setpointLumens_PWM = map(setpointLumens, 6,74, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
  SetpointLumensPWM = setpointLumens_PWM;

  analogWrite(PwmPin2,SetpointLumensPWM);     //sending the PWM value to the PWM pin
  DutyCyclePercent2 = ((SetpointLumensPWM * 100) / 255);  //duty cycle calculation
  
  Serial.print("PWM Value = ");        //to display the text in quotation marks
  Serial.println(SetpointLumensPWM);          //print the PWM value next to this
   
  Serial.print("Duty Cycle = ");         //to display the text in quotation marks
  Serial.print(DutyCyclePercent2);      //print the DC value next to this
  Serial.println("%");         //print the symbol in quotation marks
  Serial.println("");
  
 }

  void loop() {

  Blynk.run();        // run Blynk magic
  timer1.run();        // run timer every 2 seconds
  timer2.run();        // run timer every 2 seconds
  
}
  
}

I wasn’t having issues with my code before incorporating the BLYNK_WRITE(V1) command.

This was my code before. Although I have to have communication with my Blynk App project GUI and my Arduino IDE Code. I have to have the user input a setpoint via the Blynk App GUI for both the Temperature Control Function and the Brightness Control Function so that i can extract those values and use them for calculation purposes in my Arduino IDE.

Here’s my slightly more working code:

#define BLYNK_PRINT DebugSerial


// You could use a spare Hardware Serial on boards that have it (like Mega)
#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(2, 3); // RX, TX

#include <BlynkSimpleStream.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "g4ICf7w1JnxlzXHz0DXulKASwGq6vtUr";

BlynkTimer timer1; // Announcing the timer1
BlynkTimer timer2; // Announcing the timer2

//Untuned PID Control for Fan Speed Control Circuit + Relay Module incorporated

int PwmPin = 3;                                  //initialising the pin number of PWM signal

int analogPin = A0;                              //analogue pin that will be used (Pin A0)
int raw = 0;                                     //store the raw value coming from ADC (0-1023)
int sensorValue = 0;                             //real ADC Value (Value from the 3 Point Averaging Filter)
float voltage = 0;                               //need to first calculate the voltage from this
float roomTemp = 0;                              //then can calculate the temperature
double roomTemperature;                          //To store Converted roomTemp to double to be compatibe type for PID Controller
double temp_PWM;                                 //PWM Value of roomTemperature

double setpointTemp = 28;                        //initialising Setpoint Temperature
double setpoint_PWM;                             //PWM Value of Setpoint Temperature

double maxTemp = 30;                             //Max Temp room is goining to be heated to by Heating Fan

float DutyCyclePercent;                          //variable to store Duty Cycle. Type float used for calculations

// Relay pin for Heater (Heating Element) is controlled with D8. The active wire is connected to Normally Open and COM
int relayHeater = 8;

// Relay pin for Fan of Fan Heater is controlled with D7. The active wire is connected to Normally Open and COM
int relayFanH = 7;


#include <PID_v1.h>

//Define Variables we'll be connecting to

double Setpoint, Input, Output;
 
//Specify the links and initial tuning parameters

PID myPID(&Input, &Output, &Setpoint,2,5,0, REVERSE);    //Gain values of Kp, Ki, Kd

//initilisation for second program "BrightnessControl"

int PwmPin2 = 6;             //initialising the pin number of PWM signal
int PwmValue = 0;           //variable to store the PWM value
float DutyCyclePercent2;     //variable to store Duty Cycle. Type float for  calculations

double setpointLumens = 58;
double setpointLumens_PWM;
double SetpointLumensPWM;

void setup() {

  // Debug console
  DebugSerial.begin(9600);

  // Blynk will work through Serial
  // Do not read or write this serial manually in your sketch
  Serial.begin(9600);                             //setup of Serial Monitor
  Blynk.begin(Serial, auth);

  timer1.setInterval(2000L, sensorDataSendTempControl); //timer will run every 2 seconds
  timer2.setInterval(1000L, sensorDataSendBrightnessControl); //timer will run every 2 seconds

  //turn the PID ON

  myPID.SetMode(AUTOMATIC);

}


  void sensorDataSendTempControl() {

  
  pinMode(PwmPin,OUTPUT);                         //initialising the PWM pin as an output
  pinMode(analogPin,INPUT);                       //initialisin the Temperaure sensor pin as an input
 
  //initialize the variables we're linked to

  setpoint_PWM = map(setpointTemp, 26,30, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
  Setpoint = setpoint_PWM;
 
  raw = analogRead(analogPin);                      //1st time to read analogue Input
  raw = 0;                                          //and discard it
  for(int x = 0; x <= 2; x++){                      //3 Point Averaging Filter
      raw = raw + analogRead(analogPin);            //reading analog input 3 times
  }                                                 //and adding it to raw

  sensorValue = raw / 3;                            //real ADC value

  voltage = (5*float(sensorValue)) / 1023;          //Conversion to Temperature in Degrees Celcius
  roomTemp = voltage * 100;

  Blynk.virtualWrite(V1, sensorValue);              // sending sensor value to Blynk app

  roomTemperature = double(roomTemp);                //Convert roomTemp to double to be compatibe type for PID Controller
  temp_PWM = map(roomTemperature, 26, 30, 0, 255);   //Added for troubleshooting
  Input = temp_PWM;                                  //Convert roomTemperature from operating Temperature Range to PWM Range to be compatibe type for PID Controller

  myPID.Compute();                                   //Use PID Controller library to compute Output PWM Signal
  analogWrite(3,Output);                             //Write this Ouput PWM Signal to Analog Pin 3

  Serial.print("Temperature = ");                     //Print text in quotation marks
  Serial.print(roomTemp);                             //Print measured temperature
  Serial.println(" Degrees Celcius");                 //Print text in quotation marks
  Serial.println("");

  Serial.print("PWM Value = ");                       //print the text in quotation marks
  Serial.println(Output);                             //print the PWM value next to this
  
  }
  

 void sensorDataSendBrightnessControl() {

  pinMode(PwmPin2,OUTPUT);     //initialising the PWM pin as an output

  analogWrite(PwmPin2,PwmValue);     //sending the PWM value to the PWM pin
  DutyCyclePercent2 = ((PwmValue * 100) / 255);  //duty cycle calculation
  
  Serial.print("PWM Value = ");        //to display the text in quotation marks
  Serial.println(PwmValue);          //print the PWM value next to this
   
  Serial.print("Duty Cycle = ");         //to display the text in quotation marks
  Serial.print(DutyCyclePercent2);      //print the DC value next to this
  Serial.println("%");         //print the symbol in quotation marks
  Serial.println("");

  setpointLumens_PWM = map(setpointLumens, 9,80, 0, 255); //Convert setpointTemp from operating Temperature Range to PWM Range to be compatibe type for PID Controller
  SetpointLumensPWM = setpointLumens_PWM +40;

  analogWrite(PwmPin2,SetpointLumensPWM);     //sending the PWM value to the PWM pin
  DutyCyclePercent2 = ((SetpointLumensPWM * 100) / 255);  //duty cycle calculation
  
  Serial.print("PWM Value = ");        //to display the text in quotation marks
  Serial.println(SetpointLumensPWM);          //print the PWM value next to this
   
  Serial.print("Duty Cycle = ");         //to display the text in quotation marks
  Serial.print(DutyCyclePercent2);      //print the DC value next to this
  Serial.println("%");         //print the symbol in quotation marks
  Serial.println("");
  
 
  Blynk.virtualWrite(V2, SetpointLumensPWM);              // sending sensor value to Blynk app


 }

  void loop() {

  Blynk.run();        // run Blynk magic
  timer1.run();        // run timer every 2 seconds
  timer2.run();        // run timer every 2 seconds
  
}

@David123 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:
```

Pete.

Thanks for the heads up Pete. Done.

Your first post was okay, but you’ve now messed that up so that the text of the message is surrounded by triple backticks and your code is not, and your screenshots no longer display.
The second post also has triple backticks around the message body text, but not around the code.

Please re-edit these two posts.

Pete.

Okay, a few observations…

  1. I don’t see the SimpleStream connection type, using a script running on a computer to provide a serial to internet communication interface, as a permanent connection solution. It’s fine for getting started with Blynk, but you’ll not get reliable and consistent results that way, as the CMD window on the computer needs to be open all of the time and the computer cannot be allowed to sleep.

  2. You’ve set-up a “DebugSerial” port using SoftwareSerial. This will serve no purpose unless you have an FTDI adapter connected to pins 2 & 3 of your board top view these debug messages.

  3. the hardware serial port (Serial) needs to be used exclusively for the SimpleStream communication. You can’t send debug serial data to this port like this, It will interfere with the SimpleStream communication (as you’ve discovered):

  1. You don’t need multiple BlynkTimer objects. One object, preferably called ‘timer’ can handle up to 16 separate timers.

Pete.

Hi Pete,

Thank you for your response. I understand your points and they do make sense. Could you please show me where and how I’d need to edit my code exactly, because I don’t know how to do any of the corrections you’ve mentioned to be honest. I’m new to Blynk but I really am trying.

Can you please show me by editing on my code. I appreciate your time in advance.

My advice would be to start with some decent hardware, rather than an Uno which requires you to use a serial connection and the serial script running on a computer.

If you’re stuck with the Uno and USB then buy an FTDI adapter before you go any further, but don’t expect to have a functioning home heating control system with this setup.

Pete.